From e8d3c4cfe934b23010a8a266e380e56a978aa1bb Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Wed, 17 Sep 2014 16:29:10 +0200 Subject: [PATCH] gst-trick-mode-looping-2.py: port to GStreamer 1.0 Integrate also the improvements made to gst-trick-mode.py --- python/gst-trick-mode-looping-2.py | 100 ++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/python/gst-trick-mode-looping-2.py b/python/gst-trick-mode-looping-2.py index 920677d..7b6b07c 100755 --- a/python/gst-trick-mode-looping-2.py +++ b/python/gst-trick-mode-looping-2.py @@ -1,52 +1,71 @@ #!/usr/bin/env python - +# +# A simple "trick-mode" looping player. +# Version 2, based on "about-to-finish" handling. +# +# Not working with speed rates other that 1. +# # 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: - def __init__(self, filename, rate): - self._filename = filename + def __init__(self, uri, rate): + self._uri = uri self._rate = rate - self._player = gst.element_factory_make("playbin2", "player") - self._player.set_property("uri", filename) + self._player = Gst.ElementFactory.make("playbin", "player") + self._player.set_property("uri", uri) 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) 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._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(".") - player.set_property("uri", self._filename) + self._player.set_property("uri", self._uri) - # XXX Ater the first iteration of the loop the playback rate is + # XXX After the first iteration of the loop the playback rate is # reset to normal. Do I need to re-set the rate here? - # If I uncomment the following line, the player hangs: - #self.set_rate(self._date) + # If I uncomment the following line, nothing changes: + #self.set_rate(self._rate) - # And it hangs also if I set the seek_event manually using position=0: - #seek_event = gst.event_new_seek(self._rate, - # gst.FORMAT_TIME, - # gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, - # gst.SEEK_TYPE_SET, 0, - # gst.SEEK_TYPE_NONE, 0) + # And nothing changes either if I set the seek_event manually using + # position=0: + #seek_event = Gst.Event.new_seek(self._rate, + # Gst.Format.TIME, + # Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE, + # Gst.SeekType.SET, 0, + # Gst.SeekType.NONE, 0) #self._player.send_event(seek_event) + 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: return @@ -54,50 +73,39 @@ class Player: print 'on_state_changed' old_state, new_state, pending = msg.parse_state_changed() print "%s -> %s" % (old_state, new_state) - if old_state == gst.STATE_READY and new_state == gst.STATE_PAUSED: + if old_state == Gst.State.READY and new_state == Gst.State.PAUSED: self.set_rate(self._rate) def set_rate(self, rate): self._rate = rate - try: - position, format = self._player.query_position(gst.FORMAT_TIME) - except: - position = 0 + position = self._player.query_position(Gst.Format.TIME)[1] # Create the seek event - if rate > 0: - seek_event = gst.event_new_seek(rate, - gst.FORMAT_TIME, - gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, - gst.SEEK_TYPE_SET, position, - gst.SEEK_TYPE_NONE, 0) - else: - seek_event = gst.event_new_seek(rate, - gst.FORMAT_TIME, - gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, - gst.SEEK_TYPE_SET, 0, - gst.SEEK_TYPE_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) - gst.info("rate set to %s" % rate) + Gst.info("rate set to %s" % rate) else: - gst.warining("change rate failed") + Gst.warining("change rate failed") def main(args): def usage(): - sys.stdout.write("usage: %s \n" % args[0]) + sys.stdout.write("usage: %s \n" % args[0]) - if len(args) != 2: + if len(args) != 3: 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__': -- 2.1.4