b1fedb31cafc7df042546b2c028b2e90d5628864
[experiments/gstreamer.git] / python / gst-trick-mode-looping-1.py
1 #!/usr/bin/env python
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 GObject
17 GObject.threads_init()
18
19
20 class Player:
21     def __init__(self, uri, rate):
22         self._rate = rate
23
24         self._player = Gst.ElementFactory.make("playbin", "player")
25         self._player.set_property("uri", uri)
26
27         bus = self._player.get_bus()
28         bus.add_signal_watch()
29         bus.connect('message::eos', self.on_eos)
30         bus.connect('message::error', self.on_error)
31         bus.connect('message::state-changed', self.on_state_changed)
32
33     def run(self):
34         self._player.set_state(Gst.State.PLAYING)
35         self.loop = GObject.MainLoop()
36         self.loop.run()
37
38     def quit(self):
39         self._player.set_state(Gst.State.NULL)
40         self.loop.quit()
41
42     def on_eos(self, bus, msg):
43         sys.stderr.write(".")
44         seek_event = Gst.Event.new_seek(self._rate,
45                                         Gst.Format.TIME,
46                                         Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
47                                         Gst.SeekType.SET, 0,
48                                         Gst.SeekType.NONE, 0)
49         self._player.send_event(seek_event)
50
51     def on_error(self, bus, msg):
52         (err, debug) = msg.parse_error()
53         print "Error: %s" % err, debug
54         self.quit()
55
56     def on_state_changed(self, bus, msg):
57         if msg.src != self._player:
58             return
59
60         print 'on_state_changed'
61         old_state, new_state, pending = msg.parse_state_changed()
62         print "%s -> %s" % (old_state, new_state)
63         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
64             self.set_rate(self._rate)
65
66     def set_rate(self, rate):
67         self._rate = rate
68         position = self._player.query_position(Gst.Format.TIME)[1]
69
70         # Create the seek event
71         seek_event = Gst.Event.new_seek(rate,
72                                         Gst.Format.TIME,
73                                         Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
74                                         Gst.SeekType.SET, position,
75                                         Gst.SeekType.SET, -1)
76
77         if seek_event:
78             self._player.send_event(seek_event)
79             Gst.info("rate set to %s" % rate)
80         else:
81             Gst.warining("change rate failed")
82
83
84 def main(args):
85     def usage():
86         sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
87
88     if len(args) != 3:
89         usage()
90         sys.exit(1)
91
92     uri = Gst.filename_to_uri(args[1])
93     rate = float(args[2])
94
95     player = Player(uri, rate)
96     player.run()
97
98 if __name__ == '__main__':
99     sys.exit(main(sys.argv))