How to launch Gstreamer pipeline in Python

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


      All code is available here.

      Go through next simple steps and learn how to launch Gstreamer pipeline. We are going to do it in two common ways. But first let go through basics:

#1. Import Gstreamer

      Import libraries (GstGObject) and check required versions:

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

#2. Init Gstreamer library

      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:

#3. Create 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 get next window:

videotestsrc Gstreamer plugin

      Now we have two options to initialize Gst.Pipeline.

# 1 – With Gst.parse_launch (code)

      This method is useful when you don’t want to handle connections between plugins manually and just want to launch some existing pipeline.

# 2 – Using Gst.ElementFactory (code):

      Next approach is for those who wants to handle all elements, properties, connections. This is more flexible way, but it requires more advanced Gstreamer knowledges

#4. Get Pipeline’s Bus

       Gstreamer 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):

#5. Start Pipeline

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

      The stop pipeline call similar, but with NULL state:

Note: To handle messages from Gst.Bus start GObject loop

      Now, try to launch this code (ready to use code for this tutorial). You should get something like this:

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:


Add a Comment

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