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 GObject
20 GObject.threads_init()
24 def __init__(self, uri, rate):
27 self._player = Gst.ElementFactory.make("playbin", "player")
28 self._player.set_property("uri", uri)
30 bus = self._player.get_bus()
31 bus.add_signal_watch()
32 bus.connect('message::error', self.on_error)
33 bus.connect('message::segment-done', self.on_segment_done)
34 bus.connect('message::state-changed', self.on_state_changed)
37 self._player.set_state(Gst.State.PLAYING)
38 self.loop = GObject.MainLoop()
42 self._player.set_state(Gst.State.NULL)
45 def on_segment_done(self, bus, msg):
46 self._player.seek(self._rate, Gst.Format.TIME,
47 Gst.SeekFlags.SEGMENT | Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE,
51 def on_error(self, bus, msg):
52 (err, debug) = msg.parse_error()
53 print "Error: %s" % err, debug
56 def on_state_changed(self, bus, msg):
57 if msg.src != self._player:
60 print 'on_state_changed'
61 old_state, new_state, pending = msg.parse_state_changed()
62 print "%s -> %s" % (old_state, new_state)
63 if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
64 self.set_rate(self._rate)
66 def set_rate(self, rate):
70 self._player.set_state(Gst.State.PAUSED)
72 self._player.set_state(Gst.State.PLAYING)
73 self._player.seek(rate,
75 Gst.SeekFlags.SEGMENT | Gst.SeekFlags.SKIP | Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
82 sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
88 uri = Gst.filename_to_uri(args[1])
91 player = Player(uri, rate)
94 if __name__ == '__main__':
95 sys.exit(main(sys.argv))