3 # A simple "trick-mode" player to play a file at a given speed rate.
5 # Get a test sample with:
6 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
11 gi.require_version('Gst', '1.0')
12 from gi.repository import Gst
15 from gi.repository import GLib
19 def __init__(self, uri, rate):
22 self._player = Gst.ElementFactory.make("playbin", "player")
23 self._player.set_property("uri", uri)
25 bus = self._player.get_bus()
26 bus.add_signal_watch()
27 bus.connect('message::eos', self.on_eos)
28 bus.connect('message::error', self.on_error)
29 bus.connect('message::state-changed', self.on_state_changed)
32 self._player.set_state(Gst.State.PLAYING)
33 self.loop = GLib.MainLoop()
37 self._player.set_state(Gst.State.NULL)
40 def on_eos(self, bus, msg):
43 def on_error(self, bus, msg):
44 (err, debug) = msg.parse_error()
45 print("Error: %s" % err, debug)
48 def on_state_changed(self, bus, msg):
49 if msg.src != self._player:
52 print('on_state_changed')
53 old_state, new_state, pending = msg.parse_state_changed()
54 print("%s -> %s" % (old_state, new_state))
55 if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
56 self.set_rate(self._rate)
58 def set_rate(self, rate):
60 position = self._player.query_position(Gst.Format.TIME)[1]
62 # Create the seek event
63 seek_event = Gst.Event.new_seek(rate,
65 Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
66 Gst.SeekType.SET, position,
70 self._player.send_event(seek_event)
71 Gst.info("rate set to %s" % rate)
73 Gst.warining("change rate failed")
78 sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
84 uri = Gst.filename_to_uri(args[1])
87 player = Player(uri, rate)
90 if __name__ == '__main__':
91 sys.exit(main(sys.argv))