gstreamer/subprojects/gst-python/examples/helloworld.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.4 KiB
Python
Raw Normal View History

2017-02-21 11:02:14 +00:00
#!/usr/bin/env python
import sys
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GLib', '2.0')
from gi.repository import GLib, Gst
2017-02-21 11:02:14 +00:00
def bus_call(bus, message, loop):
t = message.type
if t == Gst.MessageType.EOS:
2018-07-20 13:58:35 +00:00
sys.stdout.write("End-of-stream\n")
2017-02-21 11:02:14 +00:00
loop.quit()
elif t == Gst.MessageType.ERROR:
err, debug = message.parse_error()
sys.stderr.write("Error: %s: %s\n" % (err, debug))
loop.quit()
return True
2017-02-21 11:02:14 +00:00
def main(args):
if len(args) != 2:
sys.stderr.write("usage: %s <media file or uri>\n" % args[0])
sys.exit(1)
Gst.init(None)
2017-02-21 11:02:14 +00:00
playbin = Gst.ElementFactory.make("playbin", None)
if not playbin:
sys.stderr.write("'playbin' gstreamer plugin missing\n")
sys.exit(1)
# take the commandline argument and ensure that it is a uri
if Gst.uri_is_valid(args[1]):
uri = args[1]
2017-02-21 11:02:14 +00:00
else:
uri = Gst.filename_to_uri(args[1])
2017-02-21 11:02:14 +00:00
playbin.set_property('uri', uri)
# create and event loop and feed gstreamer bus mesages to it
loop = GLib.MainLoop()
2017-02-21 11:02:14 +00:00
bus = playbin.get_bus()
bus.add_signal_watch()
bus.connect("message", bus_call, loop)
2017-02-21 11:02:14 +00:00
# start play back and listed to events
playbin.set_state(Gst.State.PLAYING)
try:
loop.run()
except e:
pass
2017-02-21 11:02:14 +00:00
# cleanup
playbin.set_state(Gst.State.NULL)
2017-02-21 11:02:14 +00:00
if __name__ == '__main__':
sys.exit(main(sys.argv))