fe00556876ee4105933d72fab9e2f3cee4430977
[experiments/gstreamer.git] / gst-trick-mode-looping-1.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::eos', self.on_eos)
25         bus.connect('message::state-changed', self.on_state_changed)
26
27     def run(self):
28         self._player.set_state(gst.STATE_PLAYING)
29         loop = gobject.MainLoop()
30         loop.run()
31
32     def on_eos(self, bus, msg):
33         sys.stderr.write(".")
34         self._player.set_state(gst.STATE_PLAYING)
35         seek_event = gst.event_new_seek(self._rate,
36                 gst.FORMAT_TIME,
37                 gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
38                 gst.SEEK_TYPE_SET, 0,
39                 gst.SEEK_TYPE_NONE, 0)
40         self._player.send_event(seek_event)
41
42     def on_state_changed(self, bus, msg):
43         if msg.src != self._player:
44             return
45
46         print 'on_state_changed'
47         old_state, new_state, pending = msg.parse_state_changed()
48         print "%s -> %s" % (old_state, new_state)
49         if old_state == gst.STATE_READY and new_state == gst.STATE_PAUSED:
50             self.set_rate(self._rate)
51
52     def set_rate(self, rate):
53         self._rate = rate
54         try:
55             position, format = self._player.query_position(gst.FORMAT_TIME)
56         except:
57             position = 0
58
59         # Create the seek event
60         if rate > 0:
61             seek_event = gst.event_new_seek(rate,
62                     gst.FORMAT_TIME,
63                     gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
64                     gst.SEEK_TYPE_SET, position,
65                     gst.SEEK_TYPE_NONE, 0)
66         else:
67             seek_event = gst.event_new_seek(rate,
68                     gst.FORMAT_TIME,
69                     gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE,
70                     gst.SEEK_TYPE_SET, 0,
71                     gst.SEEK_TYPE_SET, position)
72
73         if seek_event:
74             self._player.send_event(seek_event)
75             gst.info("rate set to %s" % rate)
76         else:
77             gst.warining("change rate failed")
78
79
80 def main(args):
81     def usage():
82         sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
83
84     if len(args) != 2:
85         usage()
86         sys.exit(1)
87
88     if not gst.uri_is_valid(args[1]):
89         sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
90         sys.exit(1)
91
92     player = Player(args[1], 3.0)
93     player.run()
94
95 if __name__ == '__main__':
96     sys.exit(main(sys.argv))