How to install Gstreamer Python Bindings

3 min. read |

Development for Gstreamer could be quite challenging at the beginning. But there is a lifeboard, – Gstreamer Python Bindings. With Python it is much easier to understand basic Gstreamer concepts and at the same time to speed up applications development.

Requirements

  • Ubuntu 18
  • Python 3.6

Guide

Personal experience

Gstreamer is written on pure C. I worked with C/C++ for almost 5 years. But at first it was still quite difficult to use it and prototype video analytics applications within short deadlines. And almost all logic for such applications often is written in Python using OpenCV, Tensorflow. But with Gstreamer Bindings for Python it was much easier to build applications using advantages of Gstreamer’s flexibility and Python’s simplicity. Now I could hide any image processing in Gstreamer pipeline and for analytics pipeline just use decoded RGB buffer.

Dockerfile

Checkout Dockerfile to build Docker Image for Ubuntu 18 and Gstreamer Python Binding.

docker build -t gstreamer:python /path/to/Dockerfile

xhost +local:  # To enable UI

sudo docker run --name gstreamer-python -it -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix gstreamer:python

Manual

First, Install Gstreamer on Ubuntu

In addition we need to install dev package for gstreamer

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

Then, install Python specific packages.

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

Also install additional packages required to build Gstreamer from sources

sudo apt-get install git autoconf automake libtool

Let’s install packages for Python GObject, GObject Introspection required to make Gstreamer friends with Python.

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

Now system is ready to build gst-python package from repository.

Building gst-python from sources

First, create a test virtual environment to check installation

python3 -m venv venv
source venv/bin/activate

pip install --upgrade wheel pip setuptools

After all, install pip-requirements. Detailed requirements for Python GObject installation

pip install pycairo
pip install PyGObject

Checkout Python GObject API References. This resource is going to be main source of Gstreamer API for Python.

Gst-Python API Check

Now check that essential imports are working in python

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')"

Gst-Python Plugin Check

Download gst-plugin-check.py and execute next command

python gstreamer_empty_plugin_test_case.py

If while running previous commands you got any mistakes, then proceed with next steps.

Let’s build gst-python from sources. This allows to use the latest gstreamer python bindings and align it with pre-installed gstreamer version.

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

export PYTHON=/usr/bin/python3
git checkout 1.14.5

./autogen.sh --disable-gtk-doc --noconfigure
./configure --with-libpython-dir=/usr/lib/x86_64-linux-gnu --prefix venv

make
make install

Hacks

  • Set PYTHON variable to change python executable location
export PYTHON=/usr/bin/python3
  • Gstreamer Version Check
GSTREAMER_VERSION=$(gst-launch-1.0 --version | grep version | tr -s ' ' '\n' | tail -1)

git checkout $GSTREAMER_VERSION
  • Get libpython-dir (location of libpython*m.so)
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 /))
  • Get Prefix for current environment (Python Library Path)
PREFIX=$(dirname $(dirname $(which python)))

Common Issues

Element factory metadata for plugin has no valid long-name field

For example, during execution gst-plugin-check.py :

python gstreamer_empty_plugin_test_case.py

You can get the following error message

GStreamer-WARNING **: 11:02:19.587: Element factory metadata for 'plugin_name' has no valid long-name field
ImportError: Plugin plugin_name not registered

Solution. While gst-python installation the following files should be copied to PYTHON_PACKAGES_DIR/gi/overrides. Because existing files installed by pygobject could be outdated.

Gst.py
GstPbutils.py
_gi_gst.la
_gi_gst.cpython-35m-x86_64-linux-gnu.so

Note: PYTHON_PACKAGES_DIR is usually path to site-packages or dist-packages

Usually, all the files are being copied during make install execution.

But in case if you want to move files into another location you can use the following procedure.

1. Define GI_OVERRIDES_PATH:

GI_OVERRIDES_PATH=$($PYTHON -c 'import gi; import os; print(os.path.join(os.path.dirname(gi.__file__), "overrides"))')

# echo $GI_OVERRIDES_PATH
# > /usr/lib/python3/dist-packages/gi/overrides

2. Define GST_GI_OVERRIDES_PATH. Path to files generated by make inside gst-python folder

GST_GI_OVERRIDES_PATH=/path/to/gst-python/gi/overrides

# echo $GST_GI_OVERRIDES_PATH
# > /home/gst-python/gi/overrides

3. Now, copy required files from GST_GI_OVERRIDES_PATH to GI_OVERRIDES_PATH

cp $GST_GI_OVERRIDES_PATH/Gst*.py $GI_OVERRIDES_PATH
cp $GST_GI_OVERRIDES_PATH/_gi_gst.* $GI_OVERRIDES_PATH

This should fix previous error.

Note: The only time I needed this is when I was installing gst-python with prefix /usr. Because in that case all files are being install into ../site-packages folder, but python uses ../dist-packages. In that case manual files transfer helps.

But in general, I suggest you to use venv. In such a case python uses ../site-packages folder

Notes

pip install git+https://github.com/jackersson/gstreamer-python.git@{tag_name}#egg=gstreamer-python

Repeat GstPython API Check.

Hope everything works as expected. Finally, in case of any errors, mistakes or problems leave comments down here. I’d be happy to find out solution with you 😉

24 Comments

Add a Comment

Your email address will not be published. Required fields are marked *