3 # Get a test sample with:
4 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
9 gi.require_version('Gst', '1.0')
10 from gi.repository import Gst
13 from gi.repository import GObject
14 GObject.threads_init()
18 def __init__(self, filename, rate):
19 self._filename = filename
22 self._player = Gst.ElementFactory.make("playbin", "player")
23 self._player.set_property("uri", filename)
25 bus = self._player.get_bus()
26 bus.add_signal_watch()
27 bus.connect('message::state-changed', self.on_state_changed)
30 self._player.set_state(Gst.State.PLAYING)
31 loop = GObject.MainLoop()
34 def on_state_changed(self, bus, msg):
35 if msg.src != self._player:
38 print 'on_state_changed'
39 old_state, new_state, pending = msg.parse_state_changed()
40 print "%s -> %s" % (old_state, new_state)
41 if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
42 self.set_rate(self._rate)
44 def set_rate(self, rate):
47 position, fmt = self._player.query_position(Gst.Format.TIME)
51 # Create the seek event
53 seek_event = Gst.Event.new_seek(rate,
55 Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
56 Gst.SeekType.SET, position,
59 seek_event = Gst.Event.new_seek(rate,
61 Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
63 Gst.SeekType.SET, position)
66 self._player.send_event(seek_event)
67 Gst.info("rate set to %s" % rate)
69 Gst.warining("change rate failed")
74 sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
80 if not Gst.uri_is_valid(args[1]):
81 sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
84 player = Player(args[1], 3.0)
87 if __name__ == '__main__':
88 sys.exit(main(sys.argv))