gst-raiplay-download.sh: rename tsdemux from "ts" to "demux"
[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.set_seek(0)
47
48     def on_error(self, bus, msg):
49         (err, debug) = msg.parse_error()
50         print "Error: %s" % err, debug
51         self.quit()
52
53     def on_state_changed(self, bus, msg):
54         if msg.src != self._player:
55             return
56
57         print 'on_state_changed'
58         old_state, new_state, pending = msg.parse_state_changed()
59         print "%s -> %s" % (old_state, new_state)
60         if old_state == Gst.State.READY and new_state == Gst.State.PAUSED:
61             self.set_rate(self._rate)
62
63     def set_rate(self, rate):
64         self._rate = rate
65         self.set_seek(0, True)
66
67     def set_seek(self, position, flush=False):
68         flags = Gst.SeekFlags.SEGMENT | Gst.SeekFlags.SKIP | Gst.SeekFlags.ACCURATE
69
70         if flush:
71             flags |= Gst.SeekFlags.FLUSH
72
73         if self._rate >= 0:
74             seek_event = Gst.Event.new_seek(self._rate,
75                                             Gst.Format.TIME,
76                                             flags,
77                                             Gst.SeekType.SET, position,
78                                             Gst.SeekType.NONE, 0)
79         else:
80             seek_event = Gst.Event.new_seek(self._rate,
81                                             Gst.Format.TIME,
82                                             flags,
83                                             Gst.SeekType.NONE, 0,
84                                             Gst.SeekType.END, position)
85
86         if seek_event:
87             self._player.send_event(seek_event)
88             Gst.info("rate set to %s" % self._rate)
89         else:
90             Gst.warining("change rate failed")
91
92
93 def main(args):
94     def usage():
95         sys.stdout.write("usage: %s <filename> <speedrate>\n" % args[0])
96
97     if len(args) != 3:
98         usage()
99         sys.exit(1)
100
101     uri = Gst.filename_to_uri(args[1])
102     rate = float(args[2])
103
104     player = Player(uri, rate)
105     player.run()
106
107 if __name__ == '__main__':
108     sys.exit(main(sys.argv))