3 # Get a test sample with:
4 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
15 def __init__(self, filename, rate):
16 self._filename = filename
19 self._player = gst.element_factory_make("playbin2", "player")
20 self._player.set_property("uri", filename)
22 bus = self._player.get_bus()
23 bus.add_signal_watch()
24 bus.connect('message::state-changed', self.on_state_changed)
27 self._player.set_state(gst.STATE_PLAYING)
28 loop = gobject.MainLoop()
31 def on_state_changed(self, bus, msg):
32 if msg.src != self._player:
35 print 'on_state_changed'
36 old_state, new_state, pending = msg.parse_state_changed()
37 print "%s -> %s" % (old_state, new_state)
38 if new_state == gst.STATE_PAUSED:
39 self.set_rate(self._rate)
41 def set_rate(self, rate):
44 position, format = self._player.query_position(gst.FORMAT_TIME)
48 # Create the seek event
50 seek_event = gst.event_new_seek(rate,
52 gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
53 gst.SEEK_TYPE_SET, position,
54 gst.SEEK_TYPE_NONE, 0)
56 seek_event = gst.event_new_seek(rate,
58 gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
60 gst.SEEK_TYPE_SET, position)
63 self._player.send_event(seek_event)
64 gst.info("rate set to %s" % rate)
66 gst.warining("change rate failed")
71 sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
77 if not gst.uri_is_valid(args[1]):
78 sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
81 player = Player(args[1], 3.0)
84 if __name__ == '__main__':
85 sys.exit(main(sys.argv))