Search code examples
pythongstreamerglibpygobject

Python Gstreamer bindings with PyGObject Only has Core modules, no Plugins


I have gstreamer installed on OSX 12.0.1 Monterey. I just installed the python bindings inside of a virtual environment running python 3.9 with:

pip3 install pycairo PyGObject

I can import gi and gi.repository.Gst without an issue. However it seems that almost all gstreamer plugins are missing. This is my test script:

import gi
gi.require_versions({'Gst': '1.0'})

from gi.repository import Gst, GLib

Gst.init(None)
Gst.debug_set_active(True)
Gst.debug_set_default_threshold(5)

if not Gst.init_check()[0]:
    print("gstreamer initialization failed")


class Main:
    def __init__(self):
        self.pipeline = Gst.parse_launch('playbin uri=https://gstreamer.freedesktop.org/data/media/small/sintel.mkv')
        self.pipeline.set_state(Gst.State.PLAYING)
        self.main_loop = GLib.MainLoop.new(None, False)
        GLib.MainLoop.run(self.main_loop)
        self.bus = self.pipeline.get_bus()
        self.msg = self.bus.timed_pop_filtered(
            Gst.CLOCK_TIME_NONE,
            Gst.MessageType.ERROR | Gst.MessageType.EOS
        )

        if self.msg is not None:
            self.msg.unref()
        self.bus.unref()
        self.pipeline.set_state(Gst.State.NULL)
        self.pipeline.unref()


Main()

It fails with:

0:00:00.006178000 92472 0x7fbd7d049210 INFO            GST_PIPELINE gstparse.c:345:gst_parse_launch_full: parsing pipeline description 'playbin uri=https://gstreamer.freedesktop.org/data/media/small/sintel.mkv'
0:00:00.006205000 92472 0x7fbd7d049210 DEBUG           GST_PIPELINE parse.l:135:priv_gst_parse_yylex: flex: IDENTIFIER: playbin
0:00:00.006217000 92472 0x7fbd7d049210 WARN     GST_ELEMENT_FACTORY gstelementfactory.c:701:gst_element_factory_make_with_properties: no such element factory "playbin"!
0:00:00.006229000 92472 0x7fbd7d049210 ERROR           GST_PIPELINE gst/parse/grammar.y:851:priv_gst_parse_yyparse: no element "playbin"
0:00:00.006237000 92472 0x7fbd7d049210 DEBUG           GST_PIPELINE parse.l:181:priv_gst_parse_yylex: flex: SPACE: [ ]
0:00:00.006243000 92472 0x7fbd7d049210 DEBUG           GST_PIPELINE parse.l:93:priv_gst_parse_yylex: flex: ASSIGNMENT: uri=https://gstreamer.freedesktop.org/data/media/small/sintel.mkv
0:00:00.006261000 92472 0x7fbd7d049210 DEBUG           GST_PIPELINE gst/parse/grammar.y:1228:priv_gst_parse_launch: got 0 elements and 0 links
Traceback (most recent call last):
  File "/python_experiments/playbin-example-audio.py", line 32, in <module>
    Main()
  File "/python_experiments/playbin-example-audio.py", line 16, in __init__
    self.pipeline = Gst.parse_launch('playbin uri=https://gstreamer.freedesktop.org/data/media/small/sintel.mkv')

Here is the output of gst-inspect-1.0 | grep playbin:

(gst-plugin-scanner:92783): GLib-GObject-WARNING **: 15:29:32.244: type name '-a-png-encoder-pred' contains invalid characters

(gst-plugin-scanner:92783): GLib-GObject-CRITICAL **: 15:29:32.245: g_type_set_qdata: assertion 'node != NULL' failed

(gst-plugin-scanner:92783): GLib-GObject-CRITICAL **: 15:29:32.245: g_type_set_qdata: assertion 'node != NULL' failed

(gst-plugin-scanner:92783): GLib-GObject-WARNING **: 15:29:32.293: type name '-a-png-encoder-pred' contains invalid characters

(gst-plugin-scanner:92783): GLib-GObject-CRITICAL **: 15:29:32.293: g_type_set_qdata: assertion 'node != NULL' failed

(gst-plugin-scanner:92783): GLib-GObject-CRITICAL **: 15:29:32.293: g_type_set_qdata: assertion 'node != NULL' failed
playback:  playbin: Player Bin 2
playback:  playbin3: Player Bin 3

Do the GLib errors thrown have something to do with this? gst-launch-1.0 playbin uri=https://gstreamer.freedesktop.org/data/media/small/sintel.mkv has no problem with video playback it just appears to be the python bindings. Are there any further debugging steps I should take before attempting to purge and reinstall gstreamer entirely?

Edit: I reinstalled gstreamer using the command: brew reinstall gstreamer gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly gst-libav I then used pip to uninstall cairo and PyGObject from my venv and my system install. I then used brew install pygobject3 and tried to run the script again, this time from my python system install. Still failed

Edit: Revisiting this as my bounty expires soon. I do have access to the gstreamer core. I can make filesrc with ElementFactory.make but nothing useful.

Edit: REPL using Gst.ElementFactory.make()

>>> import gi
>>> gi.require_versions({'Gst': '1.0'})
>>> from gi.repository import Gst, GLib
>>> Gst.init(None)
[]
>>> Gst.debug_set_active(True)
>>> Gst.debug_set_default_threshold(5)
>>> Gst.ElementFactory.make('playbin', 'playbin')
0:00:12.767487000 49323 0x7fc9a2321c10 WARN     GST_ELEMENT_FACTORY gstelementfactory.c:754:gst_element_factory_make_valist: no such element factory "playbin"!
>>>

Solution

  • I found the registry paths used from python are different to those used by the gst command line tools. You can check this as follows.

    Run gst-inspect-1.0 playbin and check the path in the "Filename" line - this was /usr/local/lib/gstreamer-1.0/ on the Mac I used. But this didn't match the path from Python. Run this snippet and it will print all the plugins and the paths they are loaded from.

    import gi
    gi.require_version('Gst', '1.0')
    from gi.repository import Gst
    Gst.init(None)
    
    reg = Gst.Registry.get()
    for x in reg.get_plugin_list():
        print (x.get_name(), x.get_filename())
    

    If there is a difference here, you have to force the registry to look in the same location that gst-inspect is reporting. You can do this after import with:

    Gst.Registry.get().scan_path("/usr/local/lib/gstreamer-1.0/")
    

    I think you can also set the GST_PLUGIN_PATH environment variable - but I didn't test this.