gst-trick-mode-looping-3.py: port to GStreamer 1.0
[experiments/gstreamer.git] / python / gst-trick-mode-looping-3.py
1 #!/usr/bin/env python
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 GObject
20 GObject.threads_init()
21
22
23 class Player:
24     def __init__(self, uri, rate):
25         self._rate = rate
26
27         self._player = Gst.ElementFactory.make("playbin", "player")
28         self._player.set_property("uri", uri)
29
30         bus = self._player.get_bus()
31         bus.add_signal_watch()
32         bus.connect('message::error', self.on_error)
33         bus.connect('message::segment-done', self.on_segment_done)
34         bus.connect('message::state-changed', self.on_state_changed)
35
36     def run(self):
37         self._player.set_state(Gst.State.PLAYING)
38         self.loop = GObject.MainLoop()
39         self.loop.run()
40
41     def quit(self):
42         self._player.set_state(Gst.State.NULL)
43         self.loop.quit()
44
45     def on_segment_done(self, bus, msg):
46         self._player.seek(self._rate, Gst.Format.TIME,
47                           Gst.SeekFlags.SEGMENT | Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE,
48                           Gst.SeekType.SET, 0,
49                           Gst.SeekType.END, 0)
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
69         if rate == 0:
70             self._player.set_state(Gst.State.PAUSED)
71         else:
72             self._player.set_state(Gst.State.PLAYING)
73             self._player.seek(rate,
74                               Gst.Format.TIME,
75                               Gst.SeekFlags.SEGMENT | Gst.SeekFlags.SKIP | Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
76                               Gst.SeekType.SET, 0,
77                               Gst.SeekType.END, 0)
78
79
80 def main(args):
81     def usage():
82         sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
83
84     if len(args) != 3:
85         usage()
86         sys.exit(1)
87
88     uri = Gst.filename_to_uri(args[1])
89     rate = float(args[2])
90
91     player = Player(uri, rate)
92     player.run()
93
94 if __name__ == '__main__':
95     sys.exit(main(sys.argv))