gst-trick-mode.py: indent to be more PEP8 compliant
[experiments/gstreamer.git] / python / gst-trick-mode.py
1 #!/usr/bin/env python
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 GObject
16 GObject.threads_init()
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 = GObject.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         self.quit()
43
44     def on_error(self, bus, msg):
45         (err, debug) = msg.parse_error()
46         print "Error: %s" % err, debug
47         self.quit()
48
49     def on_state_changed(self, bus, msg):
50         if msg.src != self._player:
51             return
52
53         print 'on_state_changed'
54         old_state, new_state, pending = msg.parse_state_changed()
55         print "%s -> %s" % (old_state, new_state)
56         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
57             self.set_rate(self._rate)
58
59     def set_rate(self, rate):
60         self._rate = rate
61         try:
62             position, fmt = self._player.query_position(Gst.Format.TIME)
63         except:
64             position = 0
65
66         # Create the seek event
67         if rate > 0:
68             seek_event = Gst.Event.new_seek(rate,
69                                             Gst.Format.TIME,
70                                             Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
71                                             Gst.SeekType.SET, position,
72                                             Gst.SeekType.NONE, 0)
73         else:
74             seek_event = Gst.Event.new_seek(rate,
75                                             Gst.Format.TIME,
76                                             Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
77                                             Gst.SeekType.SET, 0,
78                                             Gst.SeekType.SET, position)
79
80         if seek_event:
81             self._player.send_event(seek_event)
82             Gst.info("rate set to %s" % rate)
83         else:
84             Gst.warining("change rate failed")
85
86
87 def main(args):
88     def usage():
89         sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
90
91     if len(args) != 3:
92         usage()
93         sys.exit(1)
94
95     uri = Gst.filename_to_uri(args[1])
96     rate = float(args[2])
97
98     player = Player(uri, rate)
99     player.run()
100
101 if __name__ == '__main__':
102     sys.exit(main(sys.argv))