Skip to content

Instantly share code, notes, and snippets.

@sampsyo
Created July 7, 2015 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sampsyo/7121923bb6acc8298600 to your computer and use it in GitHub Desktop.
Save sampsyo/7121923bb6acc8298600 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import sys
import os
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst
def on_message(bus, message, udata):
pipeline, loop = udata
if message.type == Gst.MessageType.EOS:
pipeline.set_state(Gst.State.NULL)
loop.quit()
elif message.type == Gst.MessageType.ERROR:
print(message.parse_error())
pipeline.set_state(Gst.State.NULL)
loop.quit()
return True
def on_pad_added(element, pad, udata):
pipeline, sink = udata
pad.link(sink.get_static_pad('sink'))
# Get the duration of the source file.
duration, success = pad.query_duration(Gst.Format.TIME)
print(duration, success)
def decode(input_file):
loop = GObject.MainLoop()
pipeline = Gst.Pipeline()
uridecodebin = Gst.ElementFactory.make('uridecodebin')
sink = Gst.ElementFactory.make('fakesink')
pipeline.add(uridecodebin)
pipeline.add(sink)
uridecodebin.link(sink)
uridecodebin.set_property('uri', input_file)
uridecodebin.connect('pad-added', on_pad_added, (pipeline, sink))
bus = pipeline.get_bus()
bus.add_watch(0, on_message, (pipeline, loop))
pipeline.set_state(Gst.State.PLAYING)
print(pipeline.get_state(100000000))
duration, success = uridecodebin.query_duration(Gst.Format.TIME)
print(duration, success)
loop.run()
if __name__ == '__main__':
Gst.init()
input_file = 'file://' + os.path.abspath(sys.argv[1])
decode(input_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment