Move python experiments to a python/ subdir
[experiments/gstreamer.git] / python / gst-looping-video-1.py
diff --git a/python/gst-looping-video-1.py b/python/gst-looping-video-1.py
new file mode 100755 (executable)
index 0000000..9886abe
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+# Get a test sample with:
+# youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
+
+import sys
+
+import gobject
+gobject.threads_init()
+
+import gst
+
+
+class Player:
+    def __init__(self, filename):
+        self._filename = filename
+
+        self._player = gst.element_factory_make("playbin2", "player")
+        self._player.set_property("uri", filename)
+
+        bus = self._player.get_bus()
+        bus.add_signal_watch()
+        bus.connect('message::eos', self.on_eos)
+
+    def run(self):
+        self._player.set_state(gst.STATE_PLAYING)
+        loop = gobject.MainLoop()
+        loop.run()
+
+    def on_eos(self, bus, msg):
+        sys.stderr.write(".")
+        self._player.seek_simple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, 0)
+
+
+def main(args):
+    def usage():
+        sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
+
+    if len(args) != 2:
+        usage()
+        sys.exit(1)
+
+    if not gst.uri_is_valid(args[1]):
+        sys.stderr.write("Error: Invalid URI: %s\n" % args[1])
+        sys.exit(1)
+
+    player = Player(args[1])
+    player.run()
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))