import sys, os, time, thread import glib, gobject import pygst pygst.require("0.10") import gst class CLI_Main: def __init__(self): self.frame_count = 0 self.keyframe_count = 0 self.player = gst.Pipeline("player") self.filesrc = gst.element_factory_make("filesrc","filesource") self.decoder = gst.element_factory_make("decodebin2", "decoder") self.decoder.connect("new-decoded-pad",self.decoder_callback) self.fakesink = gst.element_factory_make("fakesink","fakesink") self.fakesink.set_property("signal-handoffs","1") self.fakesink.connect("handoff",self.handoff_callback) self.player.add(self.filesrc, self.decoder,self.fakesink) gst.element_link_many(self.filesrc, self.decoder) bus = self.player.get_bus() bus.add_signal_watch() bus.connect("message", self.on_message) def on_message(self, bus, message): t = message.type print t if t == gst.MESSAGE_EOS: print "End of Stream!" print "Frame count: %s" % self.frame_count print "Key Frame count: %s" % self.keyframe_count self.player.set_state(gst.STATE_NULL) self.playmode = False elif t == gst.MESSAGE_ERROR: self.player.set_state(gst.STATE_NULL) err, debug = message.parse_error() print "Error: %s" % err, debug self.playmode = False def start(self): for filepath in sys.argv[1:]: if os.path.isfile(filepath): print filepath self.filesrc.set_property("location",filepath) self.playmode=True self.player.set_state(gst.STATE_PLAYING) while self.playmode: time.sleep(1) time.sleep(1) loop.quit() def decoder_callback(self,decoder,pad,data): print "New Decoded Pad" name= pad.get_caps()[0].get_name() print "Got pad!: %s" % name if name.startswith("video"): print "Linking to fakesink" fpad= self.fakesink.get_pad("sink") pad.link(fpad) def handoff_callback(self,sink,buffer,pad): print "Handoff" bf=gst.BufferFlag(gst.BUFFER_FLAG_DELTA_UNIT) if not buffer.flag_is_set(bf) : print "Is a key frame" self.keyframe_count+=1 print buffer.offset # this gives me a constant value? self.frame_count+=1 mainclass = CLI_Main() thread.start_new_thread(mainclass.start, ()) gobject.threads_init() loop = glib.MainLoop() loop.run()