gst-looping-video-{1,2}.py: port to GStreamer 1.0
authorAntonio Ospite <ao2@ao2.it>
Wed, 17 Sep 2014 20:17:05 +0000 (22:17 +0200)
committerAntonio Ospite <ao2@ao2.it>
Thu, 18 Sep 2014 14:14:55 +0000 (16:14 +0200)
python/gst-looping-video-1.py
python/gst-looping-video-2.py

index 9886abe..7d1cc3f 100755 (executable)
@@ -1,50 +1,62 @@
 #!/usr/bin/env python
 #!/usr/bin/env python
-
+#
+# A simple looping player.
+# Version 1, based on EOS handling.
+#
 # Get a test sample with:
 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
 
 import sys
 
 # Get a test sample with:
 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
 
 import sys
 
-import gobject
-gobject.threads_init()
+import gi
+gi.require_version('Gst', '1.0')
+from gi.repository import Gst
+Gst.init(None)
 
 
-import gst
+from gi.repository import GObject
+GObject.threads_init()
 
 
 class Player:
 
 
 class Player:
-    def __init__(self, filename):
-        self._filename = filename
-
-        self._player = gst.element_factory_make("playbin2", "player")
-        self._player.set_property("uri", filename)
+    def __init__(self, uri):
+        self._player = Gst.ElementFactory.make("playbin", "player")
+        self._player.set_property("uri", uri)
 
         bus = self._player.get_bus()
         bus.add_signal_watch()
         bus.connect('message::eos', self.on_eos)
 
         bus = self._player.get_bus()
         bus.add_signal_watch()
         bus.connect('message::eos', self.on_eos)
+        bus.connect('message::error', self.on_error)
 
     def run(self):
 
     def run(self):
-        self._player.set_state(gst.STATE_PLAYING)
-        loop = gobject.MainLoop()
-        loop.run()
+        self._player.set_state(Gst.State.PLAYING)
+        self.loop = GObject.MainLoop()
+        self.loop.run()
+
+    def quit(self):
+        self._player.set_state(Gst.State.NULL)
+        self.loop.quit()
 
     def on_eos(self, bus, msg):
         sys.stderr.write(".")
 
     def on_eos(self, bus, msg):
         sys.stderr.write(".")
-        self._player.seek_simple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, 0)
+        self._player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, 0)
+
+    def on_error(self, bus, msg):
+        (err, debug) = msg.parse_error()
+        print "Error: %s" % err, debug
+        self.quit()
 
 
 def main(args):
     def usage():
 
 
 def main(args):
     def usage():
-        sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
+        sys.stdout.write("usage: %s <filename>\n" % args[0])
 
     if len(args) != 2:
         usage()
         sys.exit(1)
 
 
     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)
+    uri = Gst.filename_to_uri(args[1])
 
 
-    player = Player(args[1])
+    player = Player(uri)
     player.run()
 
 if __name__ == '__main__':
     player.run()
 
 if __name__ == '__main__':
index eabb7a4..074dbda 100755 (executable)
@@ -1,47 +1,63 @@
 #!/usr/bin/env python
 #!/usr/bin/env python
-
+#
+# A simple looping player.
+# Version 2, based on "about-to-finish" handling.
+#
 # Get a test sample with:
 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
 
 import sys
 
 # Get a test sample with:
 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
 
 import sys
 
-import gobject
-gobject.threads_init()
+import gi
+gi.require_version('Gst', '1.0')
+from gi.repository import Gst
+Gst.init(None)
 
 
-import gst
+from gi.repository import GObject
+GObject.threads_init()
 
 
 class Player:
 
 
 class Player:
-    def __init__(self, filename):
-        self._filename = filename
-
-        self._player = gst.element_factory_make("playbin2", "player")
-        self._player.set_property("uri", filename)
+    def __init__(self, uri):
+        self._uri = uri
+        self._player = Gst.ElementFactory.make("playbin", "player")
+        self._player.set_property("uri", uri)
         self._player.connect("about-to-finish", self.on_about_to_finish)
 
         self._player.connect("about-to-finish", self.on_about_to_finish)
 
+        bus = self._player.get_bus()
+        bus.add_signal_watch()
+        bus.connect('message::error', self.on_error)
+
     def run(self):
     def run(self):
-        self._player.set_state(gst.STATE_PLAYING)
-        loop = gobject.MainLoop()
-        loop.run()
+        self._player.set_state(Gst.State.PLAYING)
+        self.loop = GObject.MainLoop()
+        self.loop.run()
+
+    def quit(self):
+        self._player.set_state(Gst.State.NULL)
+        self.loop.quit()
 
     def on_about_to_finish(self, player):
         sys.stderr.write(".")
 
     def on_about_to_finish(self, player):
         sys.stderr.write(".")
-        player.set_property("uri", self._filename)
+        player.set_property("uri", self._uri)
+
+    def on_error(self, bus, msg):
+        (err, debug) = msg.parse_error()
+        print "Error: %s" % err, debug
+        self.quit()
 
 
 def main(args):
     def usage():
 
 
 def main(args):
     def usage():
-        sys.stdout.write("usage: %s <URI-OF-MEDIA-FILE>\n" % args[0])
+        sys.stdout.write("usage: %s <filename>\n" % args[0])
 
     if len(args) != 2:
         usage()
         sys.exit(1)
 
 
     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)
+    uri = Gst.filename_to_uri(args[1])
 
 
-    player = Player(args[1])
+    player = Player(uri)
     player.run()
 
 if __name__ == '__main__':
     player.run()
 
 if __name__ == '__main__':