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