Merging gst-python
[platform/upstream/gstreamer.git] / subprojects / gst-python / examples / helloworld.py
1 #!/usr/bin/env python
2
3 import sys
4
5 import gi
6 gi.require_version('Gst', '1.0')
7 from gi.repository import GObject, Gst
8
9 def bus_call(bus, message, loop):
10     t = message.type
11     if t == Gst.MessageType.EOS:
12         sys.stdout.write("End-of-stream\n")
13         loop.quit()
14     elif t == Gst.MessageType.ERROR:
15         err, debug = message.parse_error()
16         sys.stderr.write("Error: %s: %s\n" % (err, debug))
17         loop.quit()
18     return True
19
20 def main(args):
21     if len(args) != 2:
22         sys.stderr.write("usage: %s <media file or uri>\n" % args[0])
23         sys.exit(1)
24
25     GObject.threads_init()
26     Gst.init(None)
27         
28     playbin = Gst.ElementFactory.make("playbin", None)
29     if not playbin:
30         sys.stderr.write("'playbin' gstreamer plugin missing\n")
31         sys.exit(1)
32
33     # take the commandline argument and ensure that it is a uri
34     if Gst.uri_is_valid(args[1]):
35       uri = args[1]
36     else:
37       uri = Gst.filename_to_uri(args[1])
38     playbin.set_property('uri', uri)
39
40     # create and event loop and feed gstreamer bus mesages to it
41     loop = GObject.MainLoop()
42
43     bus = playbin.get_bus()
44     bus.add_signal_watch()
45     bus.connect ("message", bus_call, loop)
46     
47     # start play back and listed to events
48     playbin.set_state(Gst.State.PLAYING)
49     try:
50       loop.run()
51     except:
52       pass
53     
54     # cleanup
55     playbin.set_state(Gst.State.NULL)
56
57 if __name__ == '__main__':
58     sys.exit(main(sys.argv))