import gst
import sys
import gobject
def linkDynamic(element, target):
def onPad(obj, pad, target):
sinkpad = target.get_compatible_pad(pad, pad.get_caps())
if sinkpad:
pad.link(sinkpad)
element.connect('pad-added', onPad, target)
def queryDuration(fname, callback):
# create a simple pipeline: source -> decode -> fakesink
pipeline = gst.Pipeline()
source = gst.element_factory_make('filesrc')
source.set_property('location', fname)
decode = gst.element_factory_make('decodebin')
sink = gst.element_factory_make('fakesink')
pipeline.add(source, decode, sink)
source.link(decode)
linkDynamic(decode, sink)
# #
# connect a message handler to the pipeline
def messageHandler(bus, message):
try:
callback(fname, pipeline.query_duration(gst.FORMAT_TIME)[0])
except gst.QueryError:
pass
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message', messageHandler)
# pull the trigger
pipeline.set_state(gst.STATE_PAUSED)
#
if __name__ == '__main__':
# #
# example usage
import gtk
def printDuration(fname, duration):
print "%s: length='%s' seconds" % (fname, float(duration) / gst.SECOND)
gtk.main_quit()
gobject.threads_init();
queryDuration(sys.argv[1], printDuration)
gtk.main()
#