Fixes for python3 + cleanups
[python-gstreamer-examples] / example1.py
1 #!/usr/bin/env python
2 # This is a reworked version of the example from Jono Bacon's Python+Gstreamer
3 # primer: http://www.jonobacon.org/2006/08/28/getting-started-with-gstreamer-with-python/
4 #
5 # It uses Gstreamer 1.0, and replaces alsa with pulse for audio output, and
6 # drops the use of the GTK main loop, since we're not bothered with a GUI
7 # Info on porting python scripts to Gstreamer 1.0 can be found here:
8 # https://wiki.ubuntu.com/Novacut/GStreamer1.0
9
10 import gi
11 gi.require_version('Gst', '1.0')
12 from gi.repository import Gst, GLib
13
14 class Main:
15 def __init__(self):
16 # Initiate the pipeline
17 Gst.init(None)
18 self.pipeline = Gst.Pipeline()
19
20 # Add an audiotestsrc element to the pipeline
21 self.audiotestsrc = Gst.ElementFactory.make("audiotestsrc", "audio")
22 self.pipeline.add(self.audiotestsrc)
23
24 # Add a pulsesink element to the pipeline
25 self.pulsesink = Gst.ElementFactory.make("pulsesink", "sink")
26 self.pipeline.add(self.pulsesink)
27
28 # Link the two elements together
29 self.audiotestsrc.link(self.pulsesink)
30
31 # Set the pipeline to the playing state
32 self.pipeline.set_state(Gst.State.PLAYING)
33
34 # Create the pipelie and enter main loop, quit with ctrl+c
35 start = Main()
36 mainloop = GLib.MainLoop()
37
38 try:
39 mainloop.run()
40 except KeyboardInterrupt:
41 exit(0)