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