python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git] / python / gst-trick-mode-looping-2.py
1 #!/usr/bin/env python3
2 #
3 # A simple "trick-mode" looping player.
4 # Version 2, based on "about-to-finish" handling.
5 #
6 # Not working with speed rates other that 1.
7 #
8 # Get a test sample with:
9 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
10
11 import sys
12
13 import gi
14 gi.require_version('Gst', '1.0')
15 from gi.repository import Gst
16 Gst.init(None)
17
18 from gi.repository import GLib
19
20
21 class Player:
22     def __init__(self, uri, rate):
23         self._uri = uri
24         self._rate = rate
25
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)
29
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)
34
35     def run(self):
36         self._player.set_state(Gst.State.PLAYING)
37         self.loop = GLib.MainLoop()
38         self.loop.run()
39
40     def quit(self):
41         self._player.set_state(Gst.State.NULL)
42         self.loop.quit()
43
44     def on_about_to_finish(self, player):
45         sys.stderr.write(".")
46         self._player.set_property("uri", self._uri)
47
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?
50
51         # If I uncomment the following line, nothing changes:
52         #self.set_seek(0)
53
54     def on_error(self, bus, msg):
55         (err, debug) = msg.parse_error()
56         print("Error: %s" % err, debug)
57         self.quit()
58
59     def on_state_changed(self, bus, msg):
60         if msg.src != self._player:
61             return
62
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)
68
69     def set_rate(self, rate):
70         self._rate = rate
71         position = self._player.query_position(Gst.Format.TIME)[1]
72         self.set_seek(position, True)
73
74     def set_seek(self, position, flush=False):
75         flags = Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE
76
77         if flush:
78             flags |= Gst.SeekFlags.FLUSH
79
80         if self._rate >= 0:
81             seek_event = Gst.Event.new_seek(self._rate,
82                                             Gst.Format.TIME,
83                                             flags,
84                                             Gst.SeekType.SET, position,
85                                             Gst.SeekType.NONE, 0)
86         else:
87             seek_event = Gst.Event.new_seek(self._rate,
88                                             Gst.Format.TIME,
89                                             flags,
90                                             Gst.SeekType.NONE, 0,
91                                             Gst.SeekType.END, position)
92
93         if seek_event:
94             self._player.send_event(seek_event)
95             Gst.info("rate set to %s" % self._rate)
96         else:
97             Gst.warining("change rate failed")
98
99
100 def main(args):
101     def usage():
102         sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
103
104     if len(args) != 3:
105         usage()
106         sys.exit(1)
107
108     uri = Gst.filename_to_uri(args[1])
109     rate = float(args[2])
110
111     player = Player(uri, rate)
112     player.run()
113
114 if __name__ == '__main__':
115     sys.exit(main(sys.argv))