mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-01-23 07:38:16 +00:00
Port old helloworld.py example to GI
This commit is contained in:
parent
1acc0ec664
commit
4731dbc118
1 changed files with 58 additions and 0 deletions
58
examples/helloworld.py
Normal file
58
examples/helloworld.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
|
||||
import gi
|
||||
gi.require_version('Gst', '1.0')
|
||||
from gi.repository import GObject, Gst
|
||||
|
||||
def bus_call(bus, message, loop):
|
||||
t = message.type
|
||||
if t == Gst.MessageType.EOS:
|
||||
sys.stout.write("End-of-stream\n")
|
||||
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
|
||||
|
||||
def main(args):
|
||||
if len(args) != 2:
|
||||
sys.stderr.write("usage: %s <media file or uri>\n" % args[0])
|
||||
sys.exit(1)
|
||||
|
||||
GObject.threads_init()
|
||||
Gst.init(None)
|
||||
|
||||
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]
|
||||
else:
|
||||
uri = Gst.filename_to_uri(args[1])
|
||||
playbin.set_property('uri', uri)
|
||||
|
||||
# create and event loop and feed gstreamer bus mesages to it
|
||||
loop = GObject.MainLoop()
|
||||
|
||||
bus = playbin.get_bus()
|
||||
bus.add_signal_watch()
|
||||
bus.connect ("message", bus_call, loop)
|
||||
|
||||
# start play back and listed to events
|
||||
playbin.set_state(Gst.State.PLAYING)
|
||||
try:
|
||||
loop.run()
|
||||
except:
|
||||
pass
|
||||
|
||||
# cleanup
|
||||
playbin.set_state(Gst.State.NULL)
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
Loading…
Reference in a new issue