To convert the provided gst-launch-1.0 pipeline into a Python script using GStreamer's Python bindings (gi.repository.Gst), follow these steps:
-
Initialize GStreamer and Create a Pipeline: Set up the GStreamer environment and create a new pipeline.
-
Create and Configure Elements: For each element in the pipeline, create it using Gst.ElementFactory.make() and set its properties accordingly.
-
Link Elements: Connect the elements in the correct sequence.
-
Handle Dynamic Pads: For elements like tee and nvstreammux that require dynamic pad linking, request the necessary pads and link them appropriately.
-
Set Up a Main Loop and Bus Message Handling: Implement a main loop to keep the pipeline running and handle messages from the bus to manage end-of-stream (EOS) and errors.
Here's the complete Python script:
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib
# Initialize GStreamer
Gst.init(None)
# Create the pipeline
pipeline = Gst.Pipeline.new("video-processing-pipeline")
# Create elements
aravissrc = Gst.ElementFactory.make("aravissrc", "source")
capsfilter1 = Gst.ElementFactory.make("capsfilter", "capsfilter1")
tcamconvert = Gst.ElementFactory.make("tcamconvert", "tcamconvert")
nvvidconv1 = Gst.ElementFactory.make("nvvidconv", "nvvidconv1")
capsfilter2 = Gst.ElementFactory.make("capsfilter", "capsfilter2")
tee = Gst.ElementFactory.make("tee", "tee")
queue1 = Gst.ElementFactory.make("queue", "queue1")
nvstreammux = Gst.ElementFactory.make("nvstreammux", "nvstreammux")
nvinfer = Gst.ElementFactory.make("nvinfer", "nvinfer")
nvdsosd = Gst.ElementFactory.make("nvdsosd", "nvdsosd")
nvegltransform = Gst.ElementFactory.make("nvegltransform", "nvegltransform")
fpsdisplaysink = Gst.ElementFactory.make("fpsdisplaysink", "fpsdisplaysink")
queue2 = Gst.ElementFactory.make("queue", "queue2")
nvvidconv2 = Gst.ElementFactory.make("nvvidconv", "nvvidconv2")
x264enc = Gst.ElementFactory.make("x264enc", "x264enc")
capsfilter3 = Gst.ElementFactory.make("capsfilter", "capsfilter3")
splitmuxsink = Gst.ElementFactory.make("splitmuxsink", "splitmuxsink")
# Check if all elements are created
if not all([aravissrc, capsfilter1, tcamconvert, nvvidconv1, capsfilter2, tee, queue1, nvstreammux, nvinfer, nvdsosd, nvegltransform, fpsdisplaysink, queue2, nvvidconv2, x264enc, capsfilter3, splitmuxsink]):
print("Failed to create one or more elements")
exit(-1)
# Set properties for elements
aravissrc.set_property("features", "OverlapMode=On Width=1024 Height=1024 ExposureAuto=Continuous AcquisitionFrameRate=88.5 AcquisitionFrameRateEnable=1 GevSCPSPacketSize=8164 Gain=0 GainAuto=Continuous GammaEnable=1 GammaSelector=sRGB")
capsfilter1.set_property("caps", Gst.Caps.from_string("video/x-bayer,width=1024,height=1024"))
capsfilter2.set_property("caps", Gst.Caps.from_string("video/x-raw(memory:NVMM),format=NV12,width=640,height=640"))
queue1.set_property("max-size-buffers", 1)
queue1.set_property("leaky", 2) # leaky=downstream
nvstreammux.set_property("batch-size", 1)
nvstreammux.set_property("width", 640)
nvstreammux.set_property("height", 640)
nvstreammux.set_property("live-source", True)
nvinfer.set_property("config-file-path", "config_infer_primary_yoloV8.txt")
fpsdisplaysink.set_property("text-overlay", False)
fpsdisplaysink.set_property("video-sink", "nveglglessink")
queue2.set_property("max-size-buffers", 1)
queue2.set_property("leaky", 2) # leaky=downstream
nvvidconv2.set_property("compute-hw", 1)
x264enc.set_property("bitrate", 10000)
x264enc.set_property("speed-preset", 1) # ultrafast
x264enc.set_property("tune", 4) # zerolatency
capsfilter3.set_property("caps", Gst.Caps.from_string("video/x-h264,stream-format=avc,alignment=au"))
splitmuxsink.set_property("location", "output_%05d.mp4")
splitmuxsink.set_property("max-size-time", 30000000000)
splitmuxsink.set_property("muxer", Gst.ElementFactory.make("mp4mux", "mp4mux"))
# Add elements to the pipeline
elements = [
aravissrc, capsfilter1, tcamconvert, nvvidconv1, capsfilter2, tee,
queue1, nvstreammux, nvinfer, nvdsosd, nvegltransform, fpsdisplaysink,
queue2, nvvidconv2, x264enc, capsfilter3, splitmuxsink
]
for element in elements:
pipeline.add(element)
# Link elements
if not Gst.Element.link_many(aravissrc, capsfilter1, tcamconvert, nvvidconv1, capsfilter2, tee):
print("Failed to link source elements")
exit(-1)
# Link first branch: tee -> queue1 -> nvstreammux -> nvinfer -> nvdsosd -> nvegltransform -> fpsdisplaysink
tee_src_pad1 = tee.get_request_pad("src_%u")
queue1_sink_pad = queue1.get_static_pad("sink")
if tee_src_pad1.link(queue1_sink_pad) != Gst.PadLinkReturn.OK:
print("Failed to link tee to queue1")
exit(-1)
if not Gst.Element.link_many(queue1, nvstreammux):
print("Failed to link queue1 to nvstreammux")
exit(-1)
# Link nvstreammux to nvinfer
nvstreammux_src_pad = nvstreammux.get_static_pad("src")
nvinfer_sink_pad = nvinfer.get_static_pad("sink")
if nvstreammux_src_pad.link(nvinfer_sink_pad) != Gst.PadLinkReturn.OK:
print("Failed to link nvstreammux to nvinfer")
exit(-1)
if not Gst.Element.link_many(nvinfer, nvdsosd, nvegltransform, fpsdisplaysink):
print("Failed to link nvinfer to fpsdisplaysink