tried your sample pipeline… Its working
I tried to add this pipeline in my code, but its not working. Below is my main code: Can you check it.
def main(args):
# Check input arguments
if len(args) < 2:
sys.stderr.write("usage: %s <uri1> [uri2] ... [uriN] <output_folder>\n" % args[0])
sys.exit(1)
global perf_data
perf_data = PERF_DATA(len(args) - 2)
number_sources = len(args) - 2
global folder_name
folder_name = args[-1]
if os.path.exists(folder_name):
sys.stderr.write("The output folder %s already exists. Please remove it first.\n" % folder_name)
sys.exit(1)
os.mkdir(folder_name)
print("Frames will be saved in ", folder_name)
# Standard GStreamer initialization
Gst.init(None)
# Create GStreamer elements
pipeline = Gst.Pipeline()
is_live = False
if not pipeline:
sys.stderr.write("Unable to create Pipeline\n")
# Create nvstreammux instance to form batches from one or more sources.
streammux = Gst.ElementFactory.make("nvstreammux", "Stream-muxer")
if not streammux:
sys.stderr.write("Unable to create NvStreamMux\n")
pipeline.add(streammux)
audioconvert = Gst.ElementFactory.make("audioconvert", "audioconvert")
if not audioconvert:
sys.stderr.write("Unable to create audioconvert element\n")
pipeline.add(audioconvert)
for i in range(number_sources):
frame_count["stream_" + str(i)] = 0
saved_count["stream_" + str(i)] = 0
print("Creating source_bin ", i, " \n ")
uri_name = args[i + 1]
if uri_name.find("rtsp://") == 0:
is_live = True
source_bin = create_source_bin(i, uri_name)
source_audio_bin = create_source_audio_bin(i, uri_name)
if not source_bin or not source_audio_bin:
sys.stderr.write("Unable to create source bin or source audio bin \n")
pipeline.add(source_bin)
pipeline.add(source_audio_bin)
padname_audio = "sink_audio_%u" % i
sinkpad_audio = audioconvert.get_request_pad(padname_audio)
print("Sinkpad audio:", sinkpad_audio) # Add this line
if not sinkpad_audio:
sys.stderr.write("Unable to create audio src pad bin \n")
srcpad_audio = audioconvert.get_static_pad("src")
print("Srcpad audio:", srcpad_audio) # Add this line
if not srcpad_audio:
sys.stderr.write("Unable to create audio sink pad bin \n")
if srcpad_audio and sinkpad_audio:
srcpad_audio.link(sinkpad_audio)
else:
sys.stderr.write("Error linking audio pads: srcpad_audio or sinkpad_audio is None\n")
# Creating audio processing elements
audio_queue = Gst.ElementFactory.make("queue", "audio-queue")
if not audio_queue:
sys.stderr.write("Unable to create audio queue\n")
pipeline.add(audio_queue)
avenc_aac = Gst.ElementFactory.make("avenc_aac", "avenc_aac")
if not avenc_aac:
sys.stderr.write("Unable to create avenc_aac\n")
pipeline.add(avenc_aac)
mp4mux_audio = Gst.ElementFactory.make("mp4mux", "mp4mux_audio")
if not mp4mux_audio:
sys.stderr.write("Unable to create mp4mux_audio\n")
pipeline.add(mp4mux_audio)
audio_sink = Gst.ElementFactory.make("filesink", "audio-sink")
if not audio_sink:
sys.stderr.write("Unable to create audio sink\n")
audio_sink.set_property("location", os.path.join(folder_name, "Annotated_Audio.mp4"))
pipeline.add(audio_sink)
print("Creating Pgie \n ")
pgie = Gst.ElementFactory.make("nvinfer", "primary-inference")
if not pgie:
sys.stderr.write("Unable to create pgie\n")
# Add nvvidconv1 and filter1 to convert the frames to RGBA
# which is easier to work with in Python.
print("Creating nvvidconv1 \n ")
nvvidconv1 = Gst.ElementFactory.make("nvvideoconvert", "convertor1")
if not nvvidconv1:
sys.stderr.write("Unable to create nvvidconv1\n")
print("Creating filter1 \n ")
caps1 = Gst.Caps.from_string("video/x-raw(memory:NVMM), format=RGBA")
filter1 = Gst.ElementFactory.make("capsfilter", "filter1")
if not filter1:
sys.stderr.write("Unable to get the caps filter1\n")
filter1.set_property("caps", caps1)
print("Creating tiler \n ")
tiler = Gst.ElementFactory.make("nvmultistreamtiler", "nvtiler")
if not tiler:
sys.stderr.write("Unable to create tiler\n")
print("Creating nvvidconv \n ")
nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "convertor")
if not nvvidconv:
sys.stderr.write("Unable to create nvvidconv\n")
print("Creating video encoding and output elements \n ")
nvvidconv_out = Gst.ElementFactory.make("nvvideoconvert", "convertor_out")
if not nvvidconv_out:
sys.stderr.write("Unable to create nvvideoconvert\n")
mp4mux = Gst.ElementFactory.make("mp4mux", "mp4mux")
if not mp4mux:
sys.stderr.write("Unable to create mp4mux\n")
sys.exit(1) # Exit the program if mp4mux creation fails
ret = pipeline.add(mp4mux)
if ret != Gst.PadProbeReturn.OK:
sys.stderr.write("Failed to add mp4mux to the pipeline\n")
sys.exit(1) # Exit the program if adding mp4mux to the pipeline fails
filesink = Gst.ElementFactory.make("filesink", "filesink")
if not filesink:
sys.stderr.write("Unable to create filesink\n")
sys.exit(1) # Exit the program if filesink creation fails
filesink.set_property("location", os.path.join(folder_name, "Annotated_Video.mp4"))
pipeline.add(filesink)
if is_live:
print("At least one of the sources is live")
streammux.set_property('live-source', 1)
streammux.set_property('width', 1920)
streammux.set_property('height', 1080)
streammux.set_property('batch-size', number_sources)
streammux.set_property('batched-push-timeout', 4000000)
pgie.set_property('config-file-path', "config_infer_primary_yoloV8_face.txt")
pgie_batch_size = pgie.get_property("batch-size")
if pgie_batch_size != number_sources:
print("WARNING: Overriding infer-config batch-size", pgie_batch_size, " with the number of sources ",
number_sources, " \n")
pgie.set_property("batch-size", number_sources)
tiler_rows = int(math.sqrt(number_sources))
tiler_columns = int(math.ceil((1.0 * number_sources) / tiler_rows))
tiler.set_property("rows", tiler_rows)
tiler.set_property("columns", tiler_columns)
tiler.set_property("width", TILED_OUTPUT_WIDTH)
tiler.set_property("height", TILED_OUTPUT_HEIGHT)
if not is_aarch64():
# Use CUDA unified memory in the pipeline so frames
# can be easily accessed on CPU in Python.
mem_type = int(pyds.NVBUF_MEM_CUDA_UNIFIED)
streammux.set_property("nvbuf-memory-type", mem_type)
nvvidconv.set_property("nvbuf-memory-type", mem_type)
nvvidconv1.set_property("nvbuf-memory-type", mem_type)
tiler.set_property("nvbuf-memory-type", mem_type)
print("Adding elements to Pipeline \n")
pipeline.add(pgie)
pipeline.add(tiler)
pipeline.add(nvvidconv)
pipeline.add(filter1)
pipeline.add(nvvidconv1)
pipeline.add(nvvidconv_out)
pipeline.add(mp4mux)
pipeline.add(filesink)
print("Linking elements in the Pipeline \n")
streammux.link(pgie)
pgie.link(nvvidconv1)
nvvidconv1.link(filter1)
filter1.link(tiler)
tiler.link(nvvidconv)
nvvidconv.link(nvvidconv_out)
nvvidconv_out.link(mp4mux)
mp4mux.link(filesink)
# create an event loop and feed GStreamer bus messages to it
loop = GLib.MainLoop()
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message", bus_call, loop)
tiler_sink_pad = tiler.get_static_pad("sink")
if not tiler_sink_pad:
sys.stderr.write("Unable to get src pad \n")
else:
tiler_sink_pad.add_probe(Gst.PadProbeType.BUFFER, tiler_sink_pad_buffer_probe, 0)
# perf callback function to print fps every 5 sec
GLib.timeout_add(5000, perf_data.perf_print_callback)
# List the sources
print("Now playing...")
for i, source in enumerate(args[:-1]):
if i != 0:
print(i, ": ", source)
print("Starting pipeline \n")
# start play back and listen to events
pipeline.set_state(Gst.State.PLAYING)
try:
loop.run()
except:
pass
# cleanup
print("Exiting app\n")
pipeline.set_state(Gst.State.NULL)