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)
25 bus.connect('message::segment-done', self.on_segment_done)
28 self._player.set_state(gst.STATE_PLAYING)
29 loop = gobject.MainLoop()
32 def on_segment_done(self, bus, msg):
33 self._player.seek(self._rate, gst.FORMAT_TIME,
34 gst.SEEK_FLAG_SEGMENT | gst.SEEK_FLAG_SKIP | gst.SEEK_FLAG_ACCURATE,
38 def on_state_changed(self, bus, msg):
39 if msg.src != self._player:
42 print 'on_state_changed'
43 old_state, new_state, pending = msg.parse_state_changed()
44 print "%s -> %s" % (old_state, new_state)
45 if old_state == gst.STATE_READY and new_state == gst.STATE_PAUSED:
46 self.set_rate(self._rate)
48 def set_rate(self, rate):
52 self._player.set_state(gst.STATE_PAUSED)
54 self._player.set_state(gst.STATE_PLAYING)
55 self._player.seek(self._rate, gst.FORMAT_TIME,
56 gst.SEEK_FLAG_SEGMENT | gst.SEEK_FLAG_SKIP | gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
63 sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
69 if not gst.uri_is_valid(args[1]):
70 sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
73 player = Player(args[1], 3.0)
76 if __name__ == '__main__':
77 sys.exit(main(sys.argv))