All pastes #895875 Raw Edit

Unnamed

public python v1 · immutable
·published 2008-02-08 03:56 UTC
rendered paste body
import gstimport sysimport gobjectdef 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)    # <excerpt 2> #    # connect a message handler to the pipeline    def messageHandler(bus, message):        duration = 0        if not duration:            try:                duration = pipeline.query_duration(gst.FORMAT_TIME)[0]            except gst.QueryError:                pass        else:            callback(file, duration)            bus.remove_signal_watch()            bus = None            pipeline.set_state(gst.STATE_NULL)            pipeline = None    bus = pipeline.get_bus()    bus.add_signal_watch()    bus.connect('message', messageHandler)    # pull the trigger    pipeline.set_state(gst.STATE_PAUSED)    # </excerpt>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()