Back to development
[platform/upstream/gstreamer.git] / subprojects / gst-python / old_examples / helloworld.py
1 #!/usr/bin/env python
2
3 import sys
4
5 import gobject
6 gobject.threads_init()
7
8 import pygst
9 pygst.require('0.10')
10 import gst
11
12 def bus_call(bus, message, loop):
13     t = message.type
14     if t == gst.MESSAGE_EOS:
15         sys.stout.write("End-of-stream\n")
16         loop.quit()
17     elif t == gst.MESSAGE_ERROR:
18         err, debug = message.parse_error()
19         sys.stderr.write("Error: %s: %s\n" % err, debug)
20         loop.quit()
21     return True
22
23 def main(args):
24     if len(args) != 2:
25         sys.stderr.write("usage: %s <media file or uri>\n" % args[0])
26         sys.exit(1)
27         
28     playbin = gst.element_factory_make("playbin2", None)
29     if not playbin:
30         sys.stderr.write("'playbin2' 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_watch(bus_call, loop)
45     
46     # start play back and listed to events
47     playbin.set_state(gst.STATE_PLAYING)
48     loop.run()
49     
50     # cleanup
51     playbin.set_state(gst.STATE_NULL)
52
53 if __name__ == '__main__':
54     sys.exit(main(sys.argv))