Muxing audio and video inside a container

• Hardware Platform : DGPU
• DeepStream Version : 6.2.0 **
• TensorRT Version : 8.5.2.2

• NVIDIA GPU Driver Version : 530.30.02
• Issue Type
Not able to include Nvstreamdemux into the python pipeline

•Requirement :
write the python implementation of the command line

Reference command line in gstreamer:

gst-launch-1.0 -e filesrc location=<.mp4 file> ! qtdemux name=demux demux.video_0 ! h264parse ! nvv4l2decoder num-extra-surfaces=2 ! m.sink_0 nvstreammux batch-size=1 width=1280 height=720 name=m ! nvstreamdemux name=d d.src_0 ! nvv4l2h264enc ! h264parse ! mux.video_0 qtmux name=mux ! filesink location=new.mp4 enable-last-sample=0 demux.audio_0 ! queue ! mux.audio_0

Python implementation

def cb_new_pad_1(qtmux, pad, qtdemux):
    
    name = pad.get_name()
    print("qtmux",name)
    if "audio_0" in  name and qtdemux.get_static_pad("audio_0").link(pad):
        print(f"Could not link {name} pad of qtmux to audio_0 pad of qtdemux")
        
        
def cb_new_pad(qtdemux, pad, data,qtmux):
    
    h264parser = data
    name = pad.get_name()

    if "video_0" in  name and pad.link(h264parser.get_static_pad("sink")):
        print(f"Could not link {name} pad of qtdemux to sink pad of h264parser")
        
    if "audio_0" in name and pad.link(qtmux.get_request_pad("audio_0")):
        print(f"Could not link {name} pad of qtdemux to audio_0 pad of qtmux")

        
def main(source_path,dest_path):
    
    Gst.init(None)
    pipeline = Gst.Pipeline()

    source = Gst.ElementFactory.make("filesrc", "file-source")
    demux = Gst.ElementFactory.make("qtdemux", "demux")
    h264parser = Gst.ElementFactory.make("h264parse", "h264-parser")
    decoder = Gst.ElementFactory.make("nvv4l2decoder", "nvv4l2-decoder")
    streammux = Gst.ElementFactory.make("nvstreammux", "Stream-muxer")
    encoder = Gst.ElementFactory.make("nvv4l2h264enc", "nvv4l2-h264-encoder")
    h264parser2 = Gst.ElementFactory.make("h264parse", "h264-parser2")
    qtmux = Gst.ElementFactory.make("qtmux", "qtmux")
    sink = Gst.ElementFactory.make("filesink", "filesink")
    nvstreamdemux = Gst.ElementFactory.make("nvstreamdemux", "nvstream-demux")



    decoder.set_property("num-extra-surfaces",2)
    
    sink.set_property("location", dest_path)
    source.set_property('location', source_path) # replace it with your mp4 file
    streammux.set_property('batch-size', 1)
    if os.environ.get('USE_NEW_NVSTREAMMUX') != 'yes': 
        streammux.set_property('width', 1280)
        streammux.set_property('height', 720)

    pipeline.add(source)
    pipeline.add(demux)
    pipeline.add(h264parser)
    pipeline.add(decoder)

    pipeline.add(streammux)
    pipeline.add(nvstreamdemux)

    pipeline.add(encoder)
    pipeline.add(h264parser2)
    pipeline.add(qtmux)
    pipeline.add(sink)



    print("Linking elements in the Pipeline \n")
    
    source.link(demux)        
    demux.connect("pad-added", cb_new_pad, h264parser,qtmux)
    h264parser.link(decoder)
    
    sinkpad = streammux.get_request_pad("sink_0")
    if not sinkpad:
        sys.stderr.write(" Unable to get the sink pad of streammux \n")
        
    srcpad = decoder.get_static_pad("src")
    if not srcpad:
        sys.stderr.write(" Unable to get source pad of decoder \n")
    srcpad.link(sinkpad)
    
    streammux.link(nvstreamdemux)
    
    sinkpad_encoder = encoder.get_static_pad("sink")
    if not sinkpad_encoder:
        sys.stderr.write(" Unable to get the sink pad of encoder \n")
        
    srcpad_nvdemux = nvstreamdemux.get_request_pad("src_0")
    if not srcpad_nvdemux:
        sys.stderr.write(" Unable to get source pad of nvdemux \n")
        
    srcpad_nvdemux.link(sinkpad_encoder)
      
    encoder.link(h264parser2)
    
    qtmux_audio = streammux.get_request_pad("audio_0")
    if not qtmux_audio:
        sys.stderr.write(" Unable to get qtmux_audio \n")
    
#     qtmux.connect("pad-added", cb_new_pad_1,demux)
    
    h264parser2.link(qtmux)
    
    qtmux.link(sink)

    
    # create an event loop and feed gstreamer bus mesages to it
    loop = GLib.MainLoop()
    bus = pipeline.get_bus()
    bus.add_signal_watch()
    bus.connect ("message", bus_call, loop)



    # start play back and listen to events
    print("Starting pipeline (First Time)\n")
    time_run_pipeline_start = time.time()
    pipeline.set_state(Gst.State.PLAYING)
    try:
        loop.run()
    except:
        pass
    
    pipeline.set_state(Gst.State.NULL)

The above pipeline is supposed to decode->encode video and final output will be muxed with source audio

The pipeline freezes and does not terminate

The last step of the Pipeline : demux.audio_0 ! queue ! mux.audio_0 where in linking the Demux audio to Mux Audio has some issue I guess.

Tried accessing the pad using :

qtmux_audio = streammux.get_request_pad(“audio_0”)
if not qtmux_audio:
sys.stderr.write(" Unable to get qtmux_audio \n")
Not able to get the audio_0 from qtmux.

I am passing a file with audio and video, Can anyone help me resolve this issue?

Adding Queue Between the Qtdemux audio and the Qtmux audio pads resolved the issue

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.