gst-test-perspective.py: fix a typo
[experiments/gstreamer.git] / python / gst-looping-video-2.py
1 #!/usr/bin/env python
2 #
3 # A simple looping player.
4 # Version 2, based on "about-to-finish" 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):
22         self._uri = uri
23         self._player = Gst.ElementFactory.make("playbin", "player")
24         self._player.set_property("uri", uri)
25         self._player.connect("about-to-finish", self.on_about_to_finish)
26
27         bus = self._player.get_bus()
28         bus.add_signal_watch()
29         bus.connect('message::error', self.on_error)
30
31     def run(self):
32         self._player.set_state(Gst.State.PLAYING)
33         self.loop = GObject.MainLoop()
34         self.loop.run()
35
36     def quit(self):
37         self._player.set_state(Gst.State.NULL)
38         self.loop.quit()
39
40     def on_about_to_finish(self, player):
41         sys.stderr.write(".")
42         player.set_property("uri", self._uri)
43
44     def on_error(self, bus, msg):
45         (err, debug) = msg.parse_error()
46         print "Error: %s" % err, debug
47         self.quit()
48
49
50 def main(args):
51     def usage():
52         sys.stdout.write("usage: %s <filename>\n" % args[0])
53
54     if len(args) != 2:
55         usage()
56         sys.exit(1)
57
58     uri = Gst.filename_to_uri(args[1])
59
60     player = Player(uri)
61     player.run()
62
63 if __name__ == '__main__':
64     sys.exit(main(sys.argv))