From: Antonio Ospite Date: Tue, 28 Sep 2021 21:27:43 +0000 (+0200) Subject: python: update python examples to modern python, Gst, and Glib versions X-Git-Url: https://git.ao2.it/experiments/gstreamer.git/commitdiff_plain/refs/heads/master python: update python examples to modern python, Gst, and Glib versions --- diff --git a/python/gst-custom-player.py b/python/gst-custom-player.py index c95434d..93cb548 100755 --- a/python/gst-custom-player.py +++ b/python/gst-custom-player.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Simple media player with GStreamer # @@ -29,8 +29,8 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) +from gi.repository import GLib from gi.repository import GObject -GObject.threads_init() # The player window will have a fixed width and height. # This is just to demonstrate the use of capabilities. @@ -49,7 +49,7 @@ class CustomVideoBin(Gst.Bin): self.add(rescale) queue.link(rescale) - caps = Gst.Caps("video/x-raw-yuv,format=(fourcc)AYUV,width=%d,height=%d,pixel-aspect-ratio=1/1" % (WIDTH, HEIGHT)) + caps = Gst.Caps("video/x-raw,format=(fourcc)AYUV,width=%d,height=%d,pixel-aspect-ratio=1/1" % (WIDTH, HEIGHT)) capsfilter = Gst.ElementFactory.make("capsfilter", "filter") capsfilter.set_property("caps", caps) self.add(capsfilter) @@ -88,21 +88,21 @@ class CustomAudioBin(Gst.Bin): class CustomPlayBin(Gst.Pipeline): __gproperties__ = { - 'source': (Gst.Element, "source", "Source element", GObject.PARAM_READABLE) + 'source': (Gst.Element, "source", "Source element", GObject.ParamFlags.READABLE) } def __init__(self, uri=None): Gst.Pipeline.__init__(self, 'CustomPlayBin') - self._uri = uri - self._playbin = Gst.ElementFactory.make("playbin", "playbin") self.add(self._playbin) - self._playbin.set_property("uri", self._uri) self._playbin.set_property("video-sink", CustomVideoBin()) self._playbin.set_property("audio-sink", CustomAudioBin()) + if uri: + self.set_uri(uri) + def set_uri(self, uri): self._uri = uri self._playbin.set_property("uri", self._uri) @@ -124,24 +124,24 @@ class GstPlayer: bus.connect('message::state-changed', self.on_state_changed) def on_eos(self, bus, msg): - print 'on_eos' + print('on_eos') self.stop() if self.eos_cb: self.eos_cb() def on_tag(self, bus, msg): - print 'on_tag:' + print('on_tag:') taglist = msg.parse_tag() - print '\t', taglist.to_string() + print('\t', taglist.to_string()) def on_error(self, bus, msg): - print 'on_error' + print('on_error') error, debug = msg.parse_error() - print "Error: %s" % error, debug + print("Error: %s" % error, debug) self.stop() def on_state_changed(self, bus, msg): - print 'on_state_changed' + print('on_state_changed') if msg.src != self.pipeline: return @@ -166,12 +166,12 @@ class PlayerTUI(): self.player = GstPlayer() self.player.eos_cb = self.quit - self.mainloop = GObject.MainLoop() + self.mainloop = GLib.MainLoop() self.player.set_location(location) self.player.play() - GObject.io_add_watch(sys.stdin, GObject.IO_IN, self.on_stdin) + GLib.io_add_watch(sys.stdin, GLib.IO_IN, self.on_stdin) try: self.mainloop.run() diff --git a/python/gst-decoupled-pipelines.py b/python/gst-decoupled-pipelines.py index 25a95b7..50ff31c 100755 --- a/python/gst-decoupled-pipelines.py +++ b/python/gst-decoupled-pipelines.py @@ -9,8 +9,7 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib PIPELINE_SRC = """ @@ -28,7 +27,7 @@ intervideosrc timeout=-1 ! videoconvert ! rotate name=rotate ! videoconvert ! au class Player: def __init__(self): - self.loop = GObject.MainLoop() + self.loop = GLib.MainLoop() self.pipeline_src = Gst.parse_launch(PIPELINE_SRC) self.pipeline_sink = Gst.parse_launch(PIPELINE_SINK) @@ -94,8 +93,8 @@ def main(): player.on_rotate() return True - GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb) - GObject.timeout_add(100, timeout_cb) + GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb) + GLib.timeout_add(100, timeout_cb) print("\nPress Enter to freeze the video\n") player.run() diff --git a/python/gst-input-selector-switch.py b/python/gst-input-selector-switch.py index 4f622c6..db4754e 100755 --- a/python/gst-input-selector-switch.py +++ b/python/gst-input-selector-switch.py @@ -7,8 +7,7 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib # The following pipeline works @@ -21,7 +20,7 @@ input-selector name=selector ! autovideosink class Player: def __init__(self): - self.loop = GObject.MainLoop() + self.loop = GLib.MainLoop() self.pipeline = Gst.parse_launch(PIPELINE) self.selector = self.pipeline.get_by_name('selector') @@ -79,7 +78,7 @@ def main(): player.on_switch() return True - GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb) + GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb) print("\nPress Enter to switch the source\n") player.run() diff --git a/python/gst-looping-video-1.py b/python/gst-looping-video-1.py index 7d1cc3f..7d8bd01 100755 --- a/python/gst-looping-video-1.py +++ b/python/gst-looping-video-1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # A simple looping player. # Version 1, based on EOS handling. @@ -13,8 +13,7 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib class Player: @@ -29,7 +28,7 @@ class Player: def run(self): self._player.set_state(Gst.State.PLAYING) - self.loop = GObject.MainLoop() + self.loop = GLib.MainLoop() self.loop.run() def quit(self): @@ -42,7 +41,7 @@ class Player: def on_error(self, bus, msg): (err, debug) = msg.parse_error() - print "Error: %s" % err, debug + print("Error: %s" % err, debug) self.quit() diff --git a/python/gst-looping-video-2.py b/python/gst-looping-video-2.py index 074dbda..2976906 100755 --- a/python/gst-looping-video-2.py +++ b/python/gst-looping-video-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # A simple looping player. # Version 2, based on "about-to-finish" handling. @@ -13,14 +13,13 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib class Player: def __init__(self, uri): self._uri = uri - self._player = Gst.ElementFactory.make("playbin", "player") + self._player = Gst.ElementFactory.make("playbin3", "player") self._player.set_property("uri", uri) self._player.connect("about-to-finish", self.on_about_to_finish) @@ -30,7 +29,7 @@ class Player: def run(self): self._player.set_state(Gst.State.PLAYING) - self.loop = GObject.MainLoop() + self.loop = GLib.MainLoop() self.loop.run() def quit(self): @@ -43,7 +42,7 @@ class Player: def on_error(self, bus, msg): (err, debug) = msg.parse_error() - print "Error: %s" % err, debug + print("Error: %s" % err, debug) self.quit() diff --git a/python/gst-playbin-switch.py b/python/gst-playbin-switch.py index 85be7f2..0128a94 100755 --- a/python/gst-playbin-switch.py +++ b/python/gst-playbin-switch.py @@ -24,14 +24,13 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib class Player: def __init__(self): - self.loop = GObject.MainLoop() - self.pipeline = Gst.ElementFactory.make("playbin", "player") + self.loop = GLib.MainLoop() + self.pipeline = Gst.ElementFactory.make("playbin3", "player") files = ["sample_440hz.webm", "sample_880hz.webm"] self.uris = [Gst.filename_to_uri(f) for f in files] @@ -97,7 +96,7 @@ def main(): player.on_switch() return True - GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb) + GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb) print("\nPress Enter to switch the source\n") player.run() diff --git a/python/gst-player-example.py b/python/gst-player-example.py index b7b838e..0ef074f 100755 --- a/python/gst-player-example.py +++ b/python/gst-player-example.py @@ -27,8 +27,7 @@ Gst.init(None) gi.require_version('GstPlayer', '1.0') from gi.repository import GstPlayer -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib class Player(object): @@ -38,7 +37,7 @@ class Player(object): self.player.connect("end-of-stream", self.end_of_stream_cb) self.player.connect("state-changed", self.state_changed_cb) - self.mainloop = GObject.MainLoop() + self.mainloop = GLib.MainLoop() def end_of_stream_cb(self, player): self.mainloop.quit() diff --git a/python/gst-test-perspective.py b/python/gst-test-perspective.py index 1b094c2..e40217d 100755 --- a/python/gst-test-perspective.py +++ b/python/gst-test-perspective.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # test program for the "perspective" geometric transform element @@ -11,8 +11,8 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) +from gi.repository import GLib from gi.repository import GObject -GObject.threads_init() def calc_matrix(): @@ -57,15 +57,18 @@ def main(): perspective = Gst.ElementFactory.make("perspective", None) pipeline.add(perspective) - print perspective.get_property("matrix") + print(perspective.get_property("matrix")) M = calc_rotation_matrix() - perspective.set_property("matrix", M) - print perspective.get_property("matrix") + M2 = GObject.ValueArray() + for v in M: + M2.append(float(v)) + perspective.set_property("matrix", M2) + print(perspective.get_property("matrix")) # This should fail! perspective.set_property("matrix", [0]) - print perspective.get_property("matrix") + print(perspective.get_property("matrix")) videoconvert = Gst.ElementFactory.make("autovideoconvert", None) pipeline.add(videoconvert) @@ -82,7 +85,7 @@ def main(): pipeline.set_state(Gst.State.PLAYING) - loop = GObject.MainLoop() + loop = GLib.MainLoop() loop.run() diff --git a/python/gst-trick-mode-looping-1.py b/python/gst-trick-mode-looping-1.py index 5a14848..9d4ecbd 100755 --- a/python/gst-trick-mode-looping-1.py +++ b/python/gst-trick-mode-looping-1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # A simple "trick-mode" looping player. # Version 1, based on EOS handling. @@ -13,8 +13,7 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib class Player: @@ -32,7 +31,7 @@ class Player: def run(self): self._player.set_state(Gst.State.PLAYING) - self.loop = GObject.MainLoop() + self.loop = GLib.MainLoop() self.loop.run() def quit(self): @@ -45,16 +44,16 @@ class Player: def on_error(self, bus, msg): (err, debug) = msg.parse_error() - print "Error: %s" % err, debug + print("Error: %s" % err, debug) self.quit() def on_state_changed(self, bus, msg): if msg.src != self._player: return - print 'on_state_changed' + print('on_state_changed') old_state, new_state, pending = msg.parse_state_changed() - print "%s -> %s" % (old_state, new_state) + print("%s -> %s" % (old_state, new_state)) if old_state == Gst.State.READY and new_state == Gst.State.PAUSED: self.set_rate(self._rate) diff --git a/python/gst-trick-mode-looping-2.py b/python/gst-trick-mode-looping-2.py index b3487dc..a398331 100755 --- a/python/gst-trick-mode-looping-2.py +++ b/python/gst-trick-mode-looping-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # A simple "trick-mode" looping player. # Version 2, based on "about-to-finish" handling. @@ -15,8 +15,7 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib class Player: @@ -24,7 +23,7 @@ class Player: self._uri = uri self._rate = rate - self._player = Gst.ElementFactory.make("playbin", "player") + self._player = Gst.ElementFactory.make("playbin3", "player") self._player.set_property("uri", uri) self._player.connect("about-to-finish", self.on_about_to_finish) @@ -35,7 +34,7 @@ class Player: def run(self): self._player.set_state(Gst.State.PLAYING) - self.loop = GObject.MainLoop() + self.loop = GLib.MainLoop() self.loop.run() def quit(self): @@ -54,16 +53,16 @@ class Player: def on_error(self, bus, msg): (err, debug) = msg.parse_error() - print "Error: %s" % err, debug + print("Error: %s" % err, debug) self.quit() def on_state_changed(self, bus, msg): if msg.src != self._player: return - print 'on_state_changed' + print('on_state_changed') old_state, new_state, pending = msg.parse_state_changed() - print "%s -> %s" % (old_state, new_state) + print("%s -> %s" % (old_state, new_state)) if old_state == Gst.State.READY and new_state == Gst.State.PAUSED: self.set_rate(self._rate) diff --git a/python/gst-trick-mode-looping-3.py b/python/gst-trick-mode-looping-3.py index 1fef9d3..cb9d8da 100755 --- a/python/gst-trick-mode-looping-3.py +++ b/python/gst-trick-mode-looping-3.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # A simple "trick-mode" looping player. # Version 3, based on segment seeking. @@ -16,8 +16,7 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib class Player: @@ -35,7 +34,7 @@ class Player: def run(self): self._player.set_state(Gst.State.PLAYING) - self.loop = GObject.MainLoop() + self.loop = GLib.MainLoop() self.loop.run() def quit(self): @@ -47,16 +46,16 @@ class Player: def on_error(self, bus, msg): (err, debug) = msg.parse_error() - print "Error: %s" % err, debug + print("Error: %s" % err, debug) self.quit() def on_state_changed(self, bus, msg): if msg.src != self._player: return - print 'on_state_changed' + print('on_state_changed') old_state, new_state, pending = msg.parse_state_changed() - print "%s -> %s" % (old_state, new_state) + print("%s -> %s" % (old_state, new_state)) if old_state == Gst.State.READY and new_state == Gst.State.PAUSED: self.set_rate(self._rate) diff --git a/python/gst-trick-mode.py b/python/gst-trick-mode.py index cc0e7a6..5ae62b6 100755 --- a/python/gst-trick-mode.py +++ b/python/gst-trick-mode.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # A simple "trick-mode" player to play a file at a given speed rate. # @@ -12,8 +12,7 @@ gi.require_version('Gst', '1.0') from gi.repository import Gst Gst.init(None) -from gi.repository import GObject -GObject.threads_init() +from gi.repository import GLib class Player: @@ -31,7 +30,7 @@ class Player: def run(self): self._player.set_state(Gst.State.PLAYING) - self.loop = GObject.MainLoop() + self.loop = GLib.MainLoop() self.loop.run() def quit(self): @@ -43,16 +42,16 @@ class Player: def on_error(self, bus, msg): (err, debug) = msg.parse_error() - print "Error: %s" % err, debug + print("Error: %s" % err, debug) self.quit() def on_state_changed(self, bus, msg): if msg.src != self._player: return - print 'on_state_changed' + print('on_state_changed') old_state, new_state, pending = msg.parse_state_changed() - print "%s -> %s" % (old_state, new_state) + print("%s -> %s" % (old_state, new_state)) if old_state == Gst.State.READY and new_state == Gst.State.PAUSED: self.set_rate(self._rate)