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):
48 def on_error(self, bus, msg):
49 (err, debug) = msg.parse_error()
50 print "Error: %s" % err, debug
53 def on_state_changed(self, bus, msg):
54 if msg.src != self._player:
57 print 'on_state_changed'
58 old_state, new_state, pending = msg.parse_state_changed()
59 print "%s -> %s" % (old_state, new_state)
60 if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
61 self.set_rate(self._rate)
63 def set_rate(self, rate):
65 self.set_seek(0, True)
67 def set_seek(self, position, flush=False):
68 flags = Gst.SeekFlags.SEGMENT | Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE
71 flags |= Gst.SeekFlags.FLUSH
74 seek_event = Gst.Event.new_seek(self._rate,
77 Gst.SeekType.SET, position,
80 seek_event = Gst.Event.new_seek(self._rate,
84 Gst.SeekType.END, position)
87 self._player.send_event(seek_event)
88 Gst.info("rate set to %s" % self._rate)
90 Gst.warining("change rate failed")
95 sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
101 uri = Gst.filename_to_uri(args[1])
102 rate = float(args[2])
104 player = Player(uri, rate)
107 if __name__ == '__main__':
108 sys.exit(main(sys.argv))