python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git] / python / gst-trick-mode-looping-1.py
1 #!/usr/bin/env python3
2 #
3 # A simple "trick-mode" looping player.
4 # Version 1, based on EOS handling.
5 #
6 # Get a test sample with:
7 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
8
9 import sys
10
11 import gi
12 gi.require_version('Gst', '1.0')
13 from gi.repository import Gst
14 Gst.init(None)
15
16 from gi.repository import GLib
17
18
19 class Player:
20     def __init__(self, uri, rate):
21         self._rate = rate
22
23         self._player = Gst.ElementFactory.make("playbin", "player")
24         self._player.set_property("uri", uri)
25
26         bus = self._player.get_bus()
27         bus.add_signal_watch()
28         bus.connect('message::eos', self.on_eos)
29         bus.connect('message::error', self.on_error)
30         bus.connect('message::state-changed', self.on_state_changed)
31
32     def run(self):
33         self._player.set_state(Gst.State.PLAYING)
34         self.loop = GLib.MainLoop()
35         self.loop.run()
36
37     def quit(self):
38         self._player.set_state(Gst.State.NULL)
39         self.loop.quit()
40
41     def on_eos(self, bus, msg):
42         sys.stderr.write(".")
43         self.set_seek(0)
44
45     def on_error(self, bus, msg):
46         (err, debug) = msg.parse_error()
47         print("Error: %s" % err, debug)
48         self.quit()
49
50     def on_state_changed(self, bus, msg):
51         if msg.src != self._player:
52             return
53
54         print('on_state_changed')
55         old_state, new_state, pending = msg.parse_state_changed()
56         print("%s -> %s" % (old_state, new_state))
57         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
58             self.set_rate(self._rate)
59
60     def set_rate(self, rate):
61         self._rate = rate
62         position = self._player.query_position(Gst.Format.TIME)[1]
63         self.set_seek(position)
64
65     def set_seek(self, position):
66         flags = Gst.SeekFlags.SKIP | Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE
67
68         if self._rate >= 0:
69             seek_event = Gst.Event.new_seek(self._rate,
70                                             Gst.Format.TIME,
71                                             flags,
72                                             Gst.SeekType.SET, position,
73                                             Gst.SeekType.NONE, 0)
74         else:
75             seek_event = Gst.Event.new_seek(self._rate,
76                                             Gst.Format.TIME,
77                                             flags,
78                                             Gst.SeekType.NONE, 0,
79                                             Gst.SeekType.END, position)
80
81         if seek_event:
82             self._player.send_event(seek_event)
83             Gst.info("rate set to %s" % self._rate)
84         else:
85             Gst.warining("change rate failed")
86
87
88 def main(args):
89     def usage():
90         sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
91
92     if len(args) != 3:
93         usage()
94         sys.exit(1)
95
96     uri = Gst.filename_to_uri(args[1])
97     rate = float(args[2])
98
99     player = Player(uri, rate)
100     player.run()
101
102 if __name__ == '__main__':
103     sys.exit(main(sys.argv))