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 normal. Do I need to re-set the rate here?
52 # If I uncomment the following line, nothing changes:
53 #self.set_rate(self._rate)
55 # And nothing changes either if I set the seek_event manually using
57 #seek_event = Gst.Event.new_seek(self._rate,
59 # Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
60 # Gst.SeekType.SET, 0,
61 # Gst.SeekType.NONE, 0)
62 #self._player.send_event(seek_event)
64 def on_error(self, bus, msg):
65 (err, debug) = msg.parse_error()
66 print "Error: %s" % err, debug
69 def on_state_changed(self, bus, msg):
70 if msg.src != self._player:
73 print 'on_state_changed'
74 old_state, new_state, pending = msg.parse_state_changed()
75 print "%s -> %s" % (old_state, new_state)
76 if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
77 self.set_rate(self._rate)
79 def set_rate(self, rate):
81 position = self._player.query_position(Gst.Format.TIME)[1]
83 # Create the seek event
84 seek_event = Gst.Event.new_seek(rate,
86 Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
87 Gst.SeekType.SET, position,
91 self._player.send_event(seek_event)
92 Gst.info("rate set to %s" % rate)
94 Gst.warining("change rate failed")
99 sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
105 uri = Gst.filename_to_uri(args[1])
106 rate = float(args[2])
108 player = Player(uri, rate)
111 if __name__ == '__main__':
112 sys.exit(main(sys.argv))