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: Set up the necessary imports and initialize GStreamer.
-
Create the Pipeline and Elements: Instantiate each element using Gst.ElementFactory.make().
-
Set Element Properties: Configure each element's properties as specified.
-
Link Elements: Connect the elements in the correct sequence.
-
Handle Dynamic Pads: Manage elements like tee and nvstreammux that require dynamic pad handling.
-
Set Up the Main Loop and Bus: Implement a main loop to run the pipeline and handle messages on the bus.
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-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")
queue2 = Gst.ElementFactory.make("queue", "queue2")
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")
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, queue2,
nvstreammux, nvinfer, nvdsosd, nvegltransform, fpsdisplaysink, nvvidconv2, x264enc,
capsfilter3, splitmuxsink]):
print("Not all elements could be created.")
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"))
nvvidconv1.set_property("compute-hw", 1)
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
queue2.set_property("max-size-buffers", 1)
queue2.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", 1)
nvinfer.set_property("config-file-path", "config_infer_primary_yoloV8.txt")
fpsdisplaysink.set_property("text-overlay", False)
fpsdisplaysink.set_property("video-sink", "nveglglessink")
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("Elements could not be linked.")
exit(-1)
# Link tee to queue1 and queue2
tee_src_pad1 = tee.get_request_pad("src_%u")
queue1_sink_pad = queue1.get_static_pad("sink")
tee_src_pad1.link(queue1_sink_pad)
tee_src_pad2 = tee.get_request_pad("src_%u")
queue2_sink_pad = queue2.get_static_pad("sink")
tee_src_pad2.link(queue2_sink_pad)
# Link queue1 to nvstreammux
pipeline.add(queue1)
queue1.link(nvstreammux)
# Link nvstreammux to the rest of the elements
if not Gst.Element.link_many(nvstreammux, nvinfer, nvdsosd, nvegltransform, fpsdisplaysink):
print("Elements could not be linked.")
exit(-1)
# Link queue2 to the rest of the elements
if not Gst.Element.link_many(queue2, nvvidconv2, x264enc, capsfilter3, splitmuxsink):
print("Elements could not be linked.")
exit(-1)
# Create a GLib MainLoop
loop = GLib.MainLoop()
# Define a function to handle messages on the bus
def on_message(bus,