How to launch Gstreamer pipeline in Python

5 min. read |

Common use of Gstreamer is through command line. In the next post we are going to launch Gstreamer pipeline from Python. This could be helpful for those who have already working pipeline and want to debug/extend it with own code.

Requirements

Code

What you’ll learn?

Go through next simple steps and learn how to launch Gstreamer pipeline within Python script in two common ways:

  • with Gst.parse_launch
  • with Gst.ElementFactory

Guide

Setup gst-python-tutorials

Basics

Import

At first import Gstreamer into Python script. Common libraries to import are GstGObject, GstApp, GstVideo, etc. To explore all of them use Python GObject API reference.

Note: If you do not specify gi.require_version you’ll get warning message:

Init Gstreamer

This call initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.

Note: if you do not call Gst.init you’ll get next error:

Define Pipeline

Gstreamer Pipeline is simple container of ordered elements through which data flows in specific direction, from Src to Sink

For simplicity we’ll use “hello world” pipeline. Check it in your terminal.

Following pipeline generates 50 buffers of next video pattern. Hope you got similar window.

videotestsrc Gstreamer plugin

Let’s init this pipeline in python.

Init Pipeline in Python

Note: Gstreamer Pipeline in Python: Gst.Pipeline

With Gst.parse_launch

This method is fast and useful when you don’t want to handle connections between plugins manually and just want to launch some existing pipeline. Creating pipeline with Gst.parse_launch is very simple and flexible solution.

With Gst.ElementFactory

Next approach is for those who wants to handle all elements, properties, connections. This approach requires more advanced Gstreamer knowledge.

Gst.ElementFactory is suitable when you exactly know the pipeline and don’t change it to often.

Start Pipeline

To start pipeline just set pipeline’s state to Gst.State.PLAYING (list of pipeline’s states)

To reset pipeline set NULL state:

Run GObject.MainLoop

Start GObject.MainLoop for pipeline to enable: receive signals, events, states or handle messages from Gst.Bus.

Handle messages with Gst.Bus

Pipeline’s Bus allows handle messages in main thread. All errors, tags, meta info could be handled by Gst.Bus

Note: Every object in Gst module is GObject itself, so a lot of capabilities from GObject are accessible. For example, in Gst.Bus with GObject’s connect you can add message handler, like this (those are basic message types that you should handle in any Gstreamer Pipeline):

Run Examples

How to launch Gstreamer Pipeline in Python

Note: Look at videotestsrc patterns and try them. In previous example I changed pattern with next line of code:

With pipeline_with_factory.py you can pass any pipeline

Conclusion

Hope everything works as expected 🙂 . In case of any troubles – leave comments.

Add a Comment

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