Rename gst-test-rotate.sh to gst-test-videoconvert-background.sh
[experiments/gstreamer.git] / python / gst-trick-mode.py
index e50e252..cc0e7a6 100755 (executable)
@@ -1,5 +1,7 @@
 #!/usr/bin/env python
 #!/usr/bin/env python
-
+#
+# A simple "trick-mode" player to play a file at a given speed rate.
+#
 # Get a test sample with:
 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
 
 # Get a test sample with:
 # youtube-dl -t http://www.youtube.com/watch?v=yWa-YXiSk2Y
 
@@ -15,21 +17,34 @@ GObject.threads_init()
 
 
 class Player:
 
 
 class Player:
-    def __init__(self, filename, rate):
-        self._filename = filename
+    def __init__(self, uri, rate):
         self._rate = rate
 
         self._player = Gst.ElementFactory.make("playbin", "player")
         self._rate = rate
 
         self._player = Gst.ElementFactory.make("playbin", "player")
-        self._player.set_property("uri", filename)
+        self._player.set_property("uri", uri)
 
         bus = self._player.get_bus()
         bus.add_signal_watch()
 
         bus = self._player.get_bus()
         bus.add_signal_watch()
+        bus.connect('message::eos', self.on_eos)
+        bus.connect('message::error', self.on_error)
         bus.connect('message::state-changed', self.on_state_changed)
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
         bus.connect('message::state-changed', self.on_state_changed)
 
     def run(self):
         self._player.set_state(Gst.State.PLAYING)
-        loop = GObject.MainLoop()
-        loop.run()
+        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):
+        self.quit()
+
+    def on_error(self, bus, msg):
+        (err, debug) = msg.parse_error()
+        print "Error: %s" % err, debug
+        self.quit()
 
     def on_state_changed(self, bus, msg):
         if msg.src != self._player:
 
     def on_state_changed(self, bus, msg):
         if msg.src != self._player:
@@ -43,24 +58,14 @@ class Player:
 
     def set_rate(self, rate):
         self._rate = rate
 
     def set_rate(self, rate):
         self._rate = rate
-        try:
-            position, fmt = self._player.query_position(Gst.Format.TIME)
-        except:
-            position = 0
+        position = self._player.query_position(Gst.Format.TIME)[1]
 
         # Create the seek event
 
         # Create the seek event
-        if rate > 0:
-            seek_event = Gst.Event.new_seek(rate,
-                    Gst.Format.TIME,
-                    Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
-                    Gst.SeekType.SET, position,
-                    Gst.SeekType.NONE, 0)
-        else:
-            seek_event = Gst.Event.new_seek(rate,
-                    Gst.Format.TIME,
-                    Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
-                    Gst.SeekType.SET, 0,
-                    Gst.SeekType.SET, position)
+        seek_event = Gst.Event.new_seek(rate,
+                                        Gst.Format.TIME,
+                                        Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
+                                        Gst.SeekType.SET, position,
+                                        Gst.SeekType.SET, -1)
 
         if seek_event:
             self._player.send_event(seek_event)
 
         if seek_event:
             self._player.send_event(seek_event)
@@ -71,17 +76,16 @@ class Player:
 
 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> <speedrate>\n" % args[0])
 
 
-    if len(args) != 2:
+    if len(args) != 3:
         usage()
         sys.exit(1)
 
         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])
+    rate = float(args[2])
 
 
-    player = Player(args[1], 3.0)
+    player = Player(uri, rate)
     player.run()
 
 if __name__ == '__main__':
     player.run()
 
 if __name__ == '__main__':