import pygst pygst.require('0.10') import gst import gobject gobject.threads_init() class VideoResampler(gst.BaseTransform): __gsttemplates__ = ( gst.PadTemplate ("sink", gst.PAD_SINK, gst.PAD_ALWAYS, gst.caps_new_any()), gst.PadTemplate ("src", gst.PAD_SRC, gst.PAD_ALWAYS, gst.caps_new_any()) ) def __init__(self, rate=1.0): gst.BaseTransform.__init__(self) self.set_in_place(True) self.rate = rate self.start = gst.CLOCK_TIME_NONE def do_event(self, event): if event.type == gst.EVENT_NEWSEGMENT: # we need to modify the duration ! update, rate, format, start, stop, position = event.parse_new_segment() # store start position self.start = start if stop == gst.CLOCK_TIME_NONE or stop == -1: return True # update the new stop position stop = long(start + float(stop - start) / self.rate) event = gst.event_new_new_segment(update, rate, format, start, stop, position) self.get_pad("src").push_event(event) return False return True def do_transform_ip(self, buffer): print gst.TIME_ARGS(buffer.timestamp), gst.TIME_ARGS(buffer.duration) if buffer.timestamp == gst.CLOCK_TIME_NONE: return gst.FLOW_OK if not buffer.timestamp == self.start: # modify the timestamp buffer.timestamp = long((buffer.timestamp - self.start) / self.rate) # modify the duration if not buffer.duration == gst.CLOCK_TIME_NONE: buffer.duration = long(buffer.duration / self.rate) print gst.TIME_ARGS(buffer.timestamp), gst.TIME_ARGS(buffer.duration) return gst.FLOW_OK gobject.type_register(VideoResampler) if __name__ == "__main__": # test pipeline p = gst.Pipeline() src = gst.element_factory_make("videotestsrc") overl = gst.element_factory_make("timeoverlay") # SET THE RATE HERE !!! # 1.0 == normal play # > 1.0 == faster # < 1.0 == slower rs = VideoResampler(2.0) vrate = gst.element_factory_make("videorate") sink = gst.element_factory_make("xvimagesink") p.add(src, overl, rs, vrate, sink) gst.element_link_many(src, overl, rs, vrate, sink) ml = gobject.MainLoop() p.set_state(gst.STATE_PLAYING) ml.run()