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