Skip to content

Instantly share code, notes, and snippets.

@liviaerxin
Last active July 8, 2024 13:55
Show Gist options
  • Save liviaerxin/9934a5780f5d3fe5402d5986fc32d070 to your computer and use it in GitHub Desktop.
Save liviaerxin/9934a5780f5d3fe5402d5986fc32d070 to your computer and use it in GitHub Desktop.
Install Gst Python Binding
#!/usr/bin/python3
#-*- coding: utf-8 -*-
import logging
import timeit
import traceback
import time
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, GLib
GObject.threads_init()
Gst.init(None)
class GstPluginPy(Gst.Element):
__gstmeta__ = ("gstplugin_py",
"Gst Plugin Python Implementation",
"gst.Element wraps processing model written in Python",
"DataAI")
__gstmetadata__ = __gstmeta__
_srctemplate = Gst.PadTemplate.new('src', Gst.PadDirection.SRC,
Gst.PadPresence.ALWAYS,
Gst.Caps.from_string("video/x-raw,format=RGB"))
_sinktemplate = Gst.PadTemplate.new('sink', Gst.PadDirection.SINK,
Gst.PadPresence.ALWAYS,
Gst.Caps.from_string("video/x-raw,format=RGB"))
__gsttemplates__ = (_srctemplate, _sinktemplate)
__gproperties__ = {
"model": (GObject.TYPE_PYOBJECT,
"model",
"Contains model that implements IDataTransform",
GObject.ParamFlags.READWRITE)
}
def __init__(self):
Gst.Element.__init__(self)
self.sinkpad = Gst.Pad.new_from_template(self._sinktemplate, 'sink')
self.sinkpad.set_chain_function_full(self.chainfunc, None)
self.sinkpad.set_event_function_full(self.eventfunc, None)
self.add_pad(self.sinkpad)
self.srcpad = Gst.Pad.new_from_template(self._srctemplate, 'src')
self.srcpad.set_event_function_full(self.srceventfunc, None)
self.srcpad.set_query_function_full(self.srcqueryfunc, None)
self.add_pad(self.srcpad)
self.model = None
def chainfunc(self, pad, parent, buffer):
try:
if self.model is not None:
item = {
"pad": pad,
"buffer": buffer,
"timeout": 0.01
}
self.model.process(**item)
except Exception as e:
logging.error(e)
traceback.print_exc()
return self.srcpad.push(buffer)
def do_get_property(self, prop):
if prop.name == 'model':
return self.model
else:
raise AttributeError('unknown property %s' % prop.name)
def do_set_property(self, prop, value):
if prop.name == 'model':
self.model = value
else:
raise AttributeError('unknown property %s' % prop.name)
def eventfunc(self, pad, parent, event):
return self.srcpad.push_event(event)
def srcqueryfunc(self, pad, object, query):
return self.sinkpad.query(query)
def srceventfunc(self, pad, parent, event):
return self.sinkpad.push_event(event)
def register(class_info):
def init(plugin, plugin_impl, plugin_name):
type_to_register = GObject.type_register(plugin_impl)
return Gst.Element.register(plugin, plugin_name, 0, type_to_register)
# Parameters explanation
# https://lazka.github.io/pgi-docs/Gst-1.0/classes/Plugin.html#Gst.Plugin.register_static
version = '14.1'
gstlicense = 'LGPL'
origin = ''
source = class_info.__gstmeta__[1]
package = class_info.__gstmeta__[0]
name = class_info.__gstmeta__[0]
description = class_info.__gstmeta__[2]
init_function = lambda plugin : init(plugin, class_info, name)
if not Gst.Plugin.register_static(Gst.VERSION_MAJOR, Gst.VERSION_MINOR,
name, description,
init_function, version, gstlicense,
source, package, origin):
raise ImportError("Plugin {} not registered".format(name))
return True
register(GstPluginPy)

What Is Gst-Python

Install Dependencies

sudo apt-get install gstreamer-1.0
sudo apt-get install gstreamer1.0-dev

sudo apt-get install python3.6 python3.6-dev python-dev python3-dev
sudo apt-get install python3-pip python-dev 
sudo apt-get install python3.6-venv

sudo apt-get install git autoconf automake libtool

sudo apt-get install python3-gi python-gst-1.0 
sudo apt-get install libgirepository1.0-dev
sudo apt-get install libcairo2-dev gir1.2-gstreamer-1.0

Install Gst-Python

git clone https://github.com/GStreamer/gst-python.git
cd gst-python

GSTREAMER_VERSION=$(gst-launch-1.0 --version | grep version | tr -s ' ' '\n' | tail -1)
git checkout $GSTREAMER_VERSION

PYTHON=/usr/bin/python3
LIBPYTHON=$($PYTHON -c 'from distutils import sysconfig; print(sysconfig.get_config_var("LDLIBRARY"))')
LIBPYTHONPATH=$(dirname $(ldconfig -p | grep -w $LIBPYTHON | head -1 | tr ' ' '\n' | grep /))
PREFIX=$(dirname $(dirname $(which python))) # in jetson nano, `PREFIX=~/.local` to use local site-packages,

./autogen.sh --disable-gtk-doc --noconfigure
./configure --with-libpython-dir=$LIBPYTHONPATH --prefix $PREFIX

make
make install

Check Gst-Python API

python -c "import gi; gi.require_version('Gst', '1.0'); \
gi.require_version('GstApp', '1.0'); \
gi.require_version('GstVideo', '1.0'); \
gi.require_version('GstBase', '1.0')"

Check Gst-Python Plugin

python3 gstreamer_empty_plugin_test_case.py

Install gstreamer-python package(Optional)

git clone [email protected]:jackersson/gstreamer-python.git
cd gstreamer-python
### to skip ./build-gst-python.sh
pip install . -v --install-option "build_py" --install-option "--skip-gst-python"

How to install Gstreamer Python Bindings

@liviaerxin
Copy link
Author

python -c "import gi; gi.require_version('Gst', '1.0'); \
gi.require_version('GstApp', '1.0'); \
gi.require_version('GstVideo', '1.0'); \
gi.require_version('GstBase', '1.0')"

Found that the above should say python3 instead of just python, otherwise it cannot find gi

Sure, it should be python3 that I do it in a venv.

@DemonRx
Copy link

DemonRx commented Apr 28, 2023

The script needs to get updated since some changes in python.

/home/demon/dev/gstreamer_empty_plugin_test_case.py:13: PyGIDeprecationWarning: Since version 3.11, calling threads_init is no longer needed. See: https://wiki.gnome.org/PyGObject/Threading
  GObject.threads_init()

(python3:227473): GStreamer-WARNING **: 01:42:13.793: Element factory metadata for 'gstplugin_py' has no valid long-name field
Traceback (most recent call last):
  File "/home/demon/dev/gstreamer_empty_plugin_test_case.py", line 121, in <module>
    register(GstPluginPy)
  File "/home/demon/dev/gstreamer_empty_plugin_test_case.py", line 117, in register
    raise ImportError("Plugin {} not registered".format(name)) 
ImportError: Plugin gstplugin_py not registered

Also most of the dependencies listed here don't exist in Ubuntu 22.04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment