Add an example program for the matrix plugin
[experiments/gstreamer.git] / python / gst-trick-mode-looping-3.py
1 #!/usr/bin/env python
2
3 # Get a test sample with:
4 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
5
6 import sys
7
8 import gobject
9 gobject.threads_init()
10
11 import gst
12
13
14 class Player:
15     def __init__(self, filename, rate):
16         self._filename = filename
17         self._rate = rate
18
19         self._player = gst.element_factory_make("playbin2", "player")
20         self._player.set_property("uri", filename)
21
22         bus = self._player.get_bus()
23         bus.add_signal_watch()
24         bus.connect('message::state-changed', self.on_state_changed)
25         bus.connect('message::segment-done', self.on_segment_done)
26
27     def run(self):
28         self._player.set_state(gst.STATE_PLAYING)
29         loop = gobject.MainLoop()
30         loop.run()
31
32     def on_segment_done(self, bus, msg):
33         self._player.seek(self._rate, gst.FORMAT_TIME,
34                 gst.SEEK_FLAG_SEGMENT | gst.SEEK_FLAG_SKIP | gst.SEEK_FLAG_ACCURATE,
35                 gst.SEEK_TYPE_SET, 0,
36                 gst.SEEK_TYPE_END, 0)
37
38     def on_state_changed(self, bus, msg):
39         if msg.src != self._player:
40             return
41
42         print 'on_state_changed'
43         old_state, new_state, pending = msg.parse_state_changed()
44         print "%s -> %s" % (old_state, new_state)
45         if old_state == gst.STATE_READY and new_state == gst.STATE_PAUSED:
46             self.set_rate(self._rate)
47
48     def set_rate(self, rate):
49         self._rate = rate
50
51         if rate == 0:
52             self._player.set_state(gst.STATE_PAUSED)
53         else:
54             self._player.set_state(gst.STATE_PLAYING)
55             self._player.seek(self._rate, gst.FORMAT_TIME,
56                     gst.SEEK_FLAG_SEGMENT | gst.SEEK_FLAG_SKIP | gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
57                     gst.SEEK_TYPE_SET, 0,
58                     gst.SEEK_TYPE_END, 0)
59
60
61 def main(args):
62     def usage():
63         sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
64
65     if len(args) != 2:
66         usage()
67         sys.exit(1)
68
69     if not gst.uri_is_valid(args[1]):
70         sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
71         sys.exit(1)
72
73     player = Player(args[1], 3.0)
74     player.run()
75
76 if __name__ == '__main__':
77     sys.exit(main(sys.argv))