3 # A simple "trick-mode" looping player.
4 # Version 2, based on "about-to-finish" handling.
6 # Not working with speed rates other that 1.
8 # Get a test sample with:
9 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
14 gi.require_version('Gst', '1.0')
15 from gi.repository import Gst
18 from gi.repository import GObject
19 GObject.threads_init()
23 def __init__(self, uri, rate):
27 self._player = Gst.ElementFactory.make("playbin", "player")
28 self._player.set_property("uri", uri)
29 self._player.connect("about-to-finish", self.on_about_to_finish)
31 bus = self._player.get_bus()
32 bus.add_signal_watch()
33 bus.connect('message::error', self.on_error)
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_about_to_finish(self, player):
47 self._player.set_property("uri", self._uri)
49 # XXX After the first iteration of the loop the playback rate is
50 # reset to 1. Do I need to re-set the rate here?
52 # If I uncomment the following line, nothing changes:
55 def on_error(self, bus, msg):
56 (err, debug) = msg.parse_error()
57 print "Error: %s" % err, debug
60 def on_state_changed(self, bus, msg):
61 if msg.src != self._player:
64 print 'on_state_changed'
65 old_state, new_state, pending = msg.parse_state_changed()
66 print "%s -> %s" % (old_state, new_state)
67 if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
68 self.set_rate(self._rate)
70 def set_rate(self, rate):
72 position = self._player.query_position(Gst.Format.TIME)[1]
73 self.set_seek(position, True)
75 def set_seek(self, position, flush=False):
76 flags = Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE
79 flags |= Gst.SeekFlags.FLUSH
82 seek_event = Gst.Event.new_seek(self._rate,
85 Gst.SeekType.SET, position,
88 seek_event = Gst.Event.new_seek(self._rate,
92 Gst.SeekType.END, position)
95 self._player.send_event(seek_event)
96 Gst.info("rate set to %s" % self._rate)
98 Gst.warining("change rate failed")
103 sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
109 uri = Gst.filename_to_uri(args[1])
110 rate = float(args[2])
112 player = Player(uri, rate)
115 if __name__ == '__main__':
116 sys.exit(main(sys.argv))