From 8040464e24174de2fe5051fd9bd7912b95b4cd5c Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Thu, 28 Feb 2013 16:54:44 +0100 Subject: [PATCH] Add some more examples --- gst-looping-video-1.py | 51 ++++++++++++++++++++++ gst-looping-video-2.py | 48 +++++++++++++++++++++ gst-trick-mode-looping-1.py | 96 +++++++++++++++++++++++++++++++++++++++++ gst-trick-mode-looping-2.py | 102 ++++++++++++++++++++++++++++++++++++++++++++ gst-trick-mode.py | 85 ++++++++++++++++++++++++++++++++++++ 5 files changed, 382 insertions(+) create mode 100755 gst-looping-video-1.py create mode 100755 gst-looping-video-2.py create mode 100755 gst-trick-mode-looping-1.py create mode 100755 gst-trick-mode-looping-2.py create mode 100755 gst-trick-mode.py diff --git a/gst-looping-video-1.py b/gst-looping-video-1.py new file mode 100755 index 0000000..9886abe --- /dev/null +++ b/gst-looping-video-1.py @@ -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 \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)) diff --git a/gst-looping-video-2.py b/gst-looping-video-2.py new file mode 100755 index 0000000..eabb7a4 --- /dev/null +++ b/gst-looping-video-2.py @@ -0,0 +1,48 @@ +#!/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) + self._player.connect("about-to-finish", self.on_about_to_finish) + + def run(self): + self._player.set_state(gst.STATE_PLAYING) + loop = gobject.MainLoop() + loop.run() + + def on_about_to_finish(self, player): + sys.stderr.write(".") + player.set_property("uri", self._filename) + + +def main(args): + def usage(): + sys.stdout.write("usage: %s \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)) diff --git a/gst-trick-mode-looping-1.py b/gst-trick-mode-looping-1.py new file mode 100755 index 0000000..e0e496c --- /dev/null +++ b/gst-trick-mode-looping-1.py @@ -0,0 +1,96 @@ +#!/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, rate): + self._filename = filename + self._rate = rate + + 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) + bus.connect('message::state-changed', self.on_state_changed) + + 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.set_state(gst.STATE_PLAYING) + 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) + self._player.send_event(seek_event) + + def on_state_changed(self, bus, msg): + if msg.src != self._player: + return + + print 'on_state_changed' + old_state, new_state, pending = msg.parse_state_changed() + print "%s -> %s" % (old_state, new_state) + if 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 + + # 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) + + if seek_event: + self._player.send_event(seek_event) + gst.info("rate set to %s" % rate) + else: + gst.warining("change rate failed") + + +def main(args): + def usage(): + sys.stdout.write("usage: %s \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], 3.0) + player.run() + +if __name__ == '__main__': + sys.exit(main(sys.argv)) diff --git a/gst-trick-mode-looping-2.py b/gst-trick-mode-looping-2.py new file mode 100755 index 0000000..03843c4 --- /dev/null +++ b/gst-trick-mode-looping-2.py @@ -0,0 +1,102 @@ +#!/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, rate): + self._filename = filename + self._rate = rate + + self._player = gst.element_factory_make("playbin2", "player") + self._player.set_property("uri", filename) + self._player.connect("about-to-finish", self.on_about_to_finish) + + bus = self._player.get_bus() + bus.add_signal_watch() + bus.connect('message::state-changed', self.on_state_changed) + + def run(self): + self._player.set_state(gst.STATE_PLAYING) + loop = gobject.MainLoop() + loop.run() + + def on_about_to_finish(self, player): + sys.stderr.write(".") + player.set_property("uri", self._filename) + + # XXX do I need to re-set the rate here? + # If I uncomment the following line, the player hangs: + #self.set_rate(self._date) + + # And it even hangs 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) + #self._player.send_event(seek_event) + + def on_state_changed(self, bus, msg): + if msg.src != self._player: + return + + print 'on_state_changed' + old_state, new_state, pending = msg.parse_state_changed() + print "%s -> %s" % (old_state, new_state) + if 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 + + # 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) + + if seek_event: + self._player.send_event(seek_event) + gst.info("rate set to %s" % rate) + else: + gst.warining("change rate failed") + + +def main(args): + def usage(): + sys.stdout.write("usage: %s \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], 3.0) + player.run() + +if __name__ == '__main__': + sys.exit(main(sys.argv)) diff --git a/gst-trick-mode.py b/gst-trick-mode.py new file mode 100755 index 0000000..d741b59 --- /dev/null +++ b/gst-trick-mode.py @@ -0,0 +1,85 @@ +#!/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, rate): + self._filename = filename + self._rate = rate + + 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::state-changed', self.on_state_changed) + + def run(self): + self._player.set_state(gst.STATE_PLAYING) + loop = gobject.MainLoop() + loop.run() + + def on_state_changed(self, bus, msg): + if msg.src != self._player: + return + + print 'on_state_changed' + old_state, new_state, pending = msg.parse_state_changed() + print "%s -> %s" % (old_state, new_state) + if 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 + + # 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) + + if seek_event: + self._player.send_event(seek_event) + gst.info("rate set to %s" % rate) + else: + gst.warining("change rate failed") + + +def main(args): + def usage(): + sys.stdout.write("usage: %s \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], 3.0) + player.run() + +if __name__ == '__main__': + sys.exit(main(sys.argv)) -- 2.1.4