3 # A simple "trick-mode" looping player.
4 # Version 3, based on segment seeking.
6 # TODO: the trickmodes work way better but the looping functionality
9 # Get a test sample with:
10 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
15 gi.require_version('Gst', '1.0')
16 from gi.repository import Gst
19 from gi.repository import GLib
23 def __init__(self, uri, rate):
26 self._player = Gst.ElementFactory.make("playbin", "player")
27 self._player.set_property("uri", uri)
29 bus = self._player.get_bus()
30 bus.add_signal_watch()
31 bus.connect('message::error', self.on_error)
32 bus.connect('message::segment-done', self.on_segment_done)
33 bus.connect('message::state-changed', self.on_state_changed)
36 self._player.set_state(Gst.State.PLAYING)
37 self.loop = GLib.MainLoop()
41 self._player.set_state(Gst.State.NULL)
44 def on_segment_done(self, bus, msg):
47 def on_error(self, bus, msg):
48 (err, debug) = msg.parse_error()
49 print("Error: %s" % err, debug)
52 def on_state_changed(self, bus, msg):
53 if msg.src != self._player:
56 print('on_state_changed')
57 old_state, new_state, pending = msg.parse_state_changed()
58 print("%s -> %s" % (old_state, new_state))
59 if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
60 self.set_rate(self._rate)
62 def set_rate(self, rate):
64 self.set_seek(0, True)
66 def set_seek(self, position, flush=False):
67 flags = Gst.SeekFlags.SEGMENT | Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE
70 flags |= Gst.SeekFlags.FLUSH
73 seek_event = Gst.Event.new_seek(self._rate,
76 Gst.SeekType.SET, position,
79 seek_event = Gst.Event.new_seek(self._rate,
83 Gst.SeekType.END, position)
86 self._player.send_event(seek_event)
87 Gst.info("rate set to %s" % self._rate)
89 Gst.warining("change rate failed")
94 sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
100 uri = Gst.filename_to_uri(args[1])
101 rate = float(args[2])
103 player = Player(uri, rate)
106 if __name__ == '__main__':
107 sys.exit(main(sys.argv))