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 GLib
22 def __init__(self, uri, rate):
26 self._player = Gst.ElementFactory.make("playbin3", "player")
27 self._player.set_property("uri", uri)
28 self._player.connect("about-to-finish", self.on_about_to_finish)
30 bus = self._player.get_bus()
31 bus.add_signal_watch()
32 bus.connect('message::error', self.on_error)
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_about_to_finish(self, player):
46 self._player.set_property("uri", self._uri)
48 # XXX After the first iteration of the loop the playback rate is
49 # reset to 1. Do I need to re-set the rate here?
51 # If I uncomment the following line, nothing changes:
54 def on_error(self, bus, msg):
55 (err, debug) = msg.parse_error()
56 print("Error: %s" % err, debug)
59 def on_state_changed(self, bus, msg):
60 if msg.src != self._player:
63 print('on_state_changed')
64 old_state, new_state, pending = msg.parse_state_changed()
65 print("%s -> %s" % (old_state, new_state))
66 if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
67 self.set_rate(self._rate)
69 def set_rate(self, rate):
71 position = self._player.query_position(Gst.Format.TIME)[1]
72 self.set_seek(position, True)
74 def set_seek(self, position, flush=False):
75 flags = Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE
78 flags |= Gst.SeekFlags.FLUSH
81 seek_event = Gst.Event.new_seek(self._rate,
84 Gst.SeekType.SET, position,
87 seek_event = Gst.Event.new_seek(self._rate,
91 Gst.SeekType.END, position)
94 self._player.send_event(seek_event)
95 Gst.info("rate set to %s" % self._rate)
97 Gst.warining("change rate failed")
102 sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
108 uri = Gst.filename_to_uri(args[1])
109 rate = float(args[2])
111 player = Player(uri, rate)
114 if __name__ == '__main__':
115 sys.exit(main(sys.argv))