Skip to content

Python & GStreamer

Alex Lennon edited this page Dec 26, 2020 · 2 revisions

Basic example from http://lifestyletransfer.com/how-to-launch-gstreamer-pipeline-in-python/

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib

def on_message(bus: Gst.Bus, message: Gst.Message, loop: GLib.MainLoop):
    mtype = message.type
    if mtype == Gst.MessageType.EOS:
        # Handle End of Stream
        print("End of stream")
        loop.quit()
    elif mtype == Gst.MessageType.ERROR:
        # Handle Errors
        err, debug = message.parse_error()
        print(err, debug)
        loop.quit()
    elif mtype == Gst.MessageType.WARNING:
        # Handle warnings
        err, debug = message.parse_warning()
        print(err, debug)
    else:
      print("Message")

    return True

# Main

Gst.init(sys.argv)

# The easy way

pipeline = Gst.parse_launch("videotestsrc ! autovideosink")

# The hard way

#pipeline = Gst.Pipeline()
#src = Gst.ElementFactory.make("videotestsrc")
#src.set_property("num-buffers", 50)
#sink = Gst.ElementFactory.make("gtksink")
#pipeline.add(src, sink)
#src.link(sink)

bus = pipeline.get_bus()
bus.connect("message", on_message, None)

pipeline.set_state(Gst.State.PLAYING)

loop = GLib.MainLoop()
try:
    loop.run()
except:
    loop.quit()

pipeline.set_state(Gst.State.NULL)

Clone this wiki locally