Pushing the stream in code is an error, but it works on the command line

I tried to push RTMP from the command line, and the program worked as follows

gst-launch-1.0 rtspsrc location=rtsp://admin:wzu123456@192.168.0.64:554/h264/ch1/main/av_stream !rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw,width=1920,height=1080,format=BGRx ! videoconvert !  nvvidconv ! omxh264enc bitrate=2000000  ! flvmux streamable=true ! rtmpsink location='rtmp://192.168.0.2:1935/live'

But when I pushed the stream in python code, I was reminded that I couldn’t find the “omxh264” component

import cv2
import gi
import numpy as np
import os

os.environ['GST_DEBUG'] = '3'
os.environ['GST_DEBUG_FILE'] = 'gstreamer.log'
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib


def on_need_data(src, length, data):
    if data['frame'] is not None:
        frame = data['frame']
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        gst_buffer = Gst.Buffer.new_allocate(None, frame.nbytes, None)
        gst_buffer.fill(0, frame.tobytes())
        src.emit('push-buffer', gst_buffer)


def start_gstreamer_pipeline(rtsp_url, rtmp_url):
    Gst.init(None)

    pipeline_str = ("appsrc name=source is-live=True format=time !  video/x-raw,width=1920,height=1080,format=BGRx,framerate=25/1 ! nvvidconv ! omxh264enc bitrate=2000 !  flvmux streamable=true ! rtmpsink location='rtmp://192.168.0.2:1935/live'")
    pipeline = Gst.parse_launch(pipeline_str)

    appsrc = pipeline.get_by_name('source')
    if not appsrc:
        print("Failed to get appsrc from pipeline")
        return

    pipeline.set_state(Gst.State.PLAYING)
    cap = cv2.VideoCapture(rtsp_url)
    pipeline.set_state(Gst.State.PLAYING)
    if not cap.isOpened():
        print("Failed to open video stream.")
        return

    try:
        while True:
            pipeline.set_state(Gst.State.PLAYING)
            ret, frame = cap.read()
            if not ret:
                print("Failed to get frame from video stream.")
                break
                
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            if not frame.any():
                print("Frame is None")
            gst_buffer = Gst.Buffer.new_allocate(None, frame.nbytes, None)
            gst_buffer.fill(0, frame.tobytes())
            appsrc.emit('push-buffer', gst_buffer)
    finally:
        cap.release()
        pipeline.set_state(Gst.State.NULL)

    

rtsp_url = "rtspsrc location=rtsp://admin:wzu123456@192.168.0.64:554/h264/ch1/main/av_stream latency=0  ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw,width=1920,height=1080,format=BGRx ! videoconvert !  appsink "
rtmp_url = "rtmp://your_rtmp_destination/live/stream"
start_gstreamer_pipeline(rtsp_url, rtmp_url)

It is worth mentioning that errors also occur when I use the call command line run in python,like this:

import gi
import numpy as np
import os
import subprocess
import sys

os.environ['GST_DEBUG'] = '4'
os.environ['GST_DEBUG_FILE'] = 'gstreamer_1.log'
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib


os.system("sudo gst-launch-1.0 rtspsrc location=rtsp://admin:wzu123456@192.168.0.64:554/h264/ch1/main/av_stream !rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw,width=1920,height=1080,format=BGRx ! videoconvert !  nvvidconv ! omxh264enc bitrate=2000000  ! flvmux streamable=true ! rtmpsink location='rtmp://192.168.0.2:1935/live'")

Here are some of the data that might be used

 gst-inspect-1.0 | grep omx
libav:  avenc_h264_omx: libav OpenMAX IL H.264 video encoder encoder
omx:  nvoverlaysink: OpenMax Video Sink
omx:  omxvp9enc: OpenMAX VP9 Video Encoder
omx:  omxvp8enc: OpenMAX VP8 Video Encoder
omx:  omxh265enc: OpenMAX H.265 Video Encoder
omx:  omxh264enc: OpenMAX H.264 Video Encoder
omx:  omxwmvdec: OpenMAX WMV Video Decoder
omx:  omxmpeg2videodec: OpenMAX MPEG2 Video Decoder
omx:  omxvp9dec: OpenMAX VP9 Video Decoder
omx:  omxvp8dec: OpenMAX VP8 Video Decoder
omx:  omxh265dec: OpenMAX H.265 Video Decoder
omx:  omxh264dec: OpenMAX H.264 Video Decoder
omx:  omxmpeg4videodec: OpenMAX MPEG4 Video Decoder

How can I solve these problems?

Hi,
We have deprecated omx plugins. Please use v4l2 plugins such as nvv4l2h264enc and nvv4l2decoder.

When I replace omxh264enc with nvv4l2h264enc, it says “could not link nvv4l2h264enc0 to flvmux0 (3)”.Here’s the new code:

gst-launch-1.0 rtspsrc location=rtsp://admin:wzu123456@192.168.0.64:554/h264/ch1/main/av_stream !rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw,width=1920,height=1080,format=BGRx ! videoconvert !  nvvidconv ! nvv4l2h264enc bitrate=2000000  ! flvmux streamable=true ! rtmpsink location='rtmp://192.168.0.2:1935/live'

Hi,
Please try

... ! nvv4l2h264enc ! h264parse ! flvmux ! ...

Thank you for your help. I tried this, but it only worked on the command line. I rewrote the code to work in python, but it still didn’t work. I checked that the process of pulling streams from the RTSP worked:

import cv2
import gi
import numpy as np
import os
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib

os.environ['GST_DEBUG'] = '3'
os.environ['GST_DEBUG_FILE'] = 'gstreamer3.log'
def main():
    Gst.init(None)

    rtsp_url = "rtsp://your_rtsp_stream_address"
    rtmp_url = "rtmp://your_rtmp_server/live/stream"

    gst_in_pipeline = ("rtspsrc location=rtsp://admin:wzu123456@192.168.0.64:554/h264/ch1/main/av_stream latency=0  ! rtph264depay ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw,width=1920,height=1080,format=BGRx ! videoconvert !  appsink")

    cap = cv2.VideoCapture(gst_in_pipeline, cv2.CAP_GSTREAMER)
    if not cap.isOpened():
        print("Cannot open RTSP stream")
        return

    gst_out_pipeline = ("appsrc name=source is-live=True format=time !  video/x-raw,width=1920,height=1080,format=BGRx,framerate=25/1 ! nvvidconv  ! nvv4l2h264enc maxperf-enable=1 profile=4 preset-level=1 iframeinterval=500 control-rate=1  bitrate=2000000 ! h264parse! flvmux  ! rtmpsink location='rtmp://192.168.0.2:1935/live'")
    out = Gst.parse_launch(gst_out_pipeline)
    out.set_state(Gst.State.PLAYING)

    appsrc = out.get_by_name("source")

    try:
        while True:
            ret, frame = cap.read()
            if not ret:
                print("Can't receive frame (stream end?). Exiting ...")
                break

            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = np.array(frame, dtype=np.uint8)
            data = frame.tobytes()
            buffer = Gst.Buffer.new_allocate(None, len(data), None)
            buffer.fill(0, data)
            appsrc.emit("push-buffer", buffer)

            cv2.imshow('Video', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

    except KeyboardInterrupt:
        print("Interrupted")

    finally:
        cap.release()
        out.set_state(Gst.State.NULL)
        cv2.destroyAllWindows()


if __name__ == "__main__":
    main()

The command-line output and logging are as follows:

$ sudo python pull_test_2.py
Opening in BLOCKING MODE 
NvMMLiteOpen : Block : BlockType = 261 
NVMEDIA: Reading vendor.tegra.display-size : status: 6 
NvMMLiteBlockCreate : Block : BlockType = 261 
NvMMLiteOpen : Block : BlockType = 261 
NVMEDIA: Reading vendor.tegra.display-size : status: 6 
NvMMLiteBlockCreate : Block : BlockType = 261 
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (896) open OpenCV | GStreamer warning: unable to query duration of stream
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=0, duration=-1
Opening in BLOCKING MODE 
NvMMLiteOpen : Block : BlockType = 4 
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 4 
NVMAP_IOC_WRITE:1075858950 failed [4]
H264: Profile = 100, Level = 0 
NVMEDIA_ENC: bBlitMode is set to TRUE 


0:00:00.142853374 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:4476:gst_v4l2_object_probe_caps:<nvv4l2decoder0:src>e[00m Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: Unknown error -1
0:00:00.142971190 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24ea6d0e[00m Failed to determine interlace mode
0:00:00.143031243 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24ea6d0e[00m Failed to determine interlace mode
0:00:00.143104422 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24ea6d0e[00m Failed to determine interlace mode
0:00:00.207942417 e[333m11270e[00m   0x7f98040140 e[32;01mFIXME  e[00m e[00;04m             default gstutils.c:3981:gst_pad_create_stream_id_internal:<fakesrc0:src>e[00m Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:00.208092264 e[333m11270e[00m   0x7f98040230 e[32;01mFIXME  e[00m e[00;04m             default gstutils.c:3981:gst_pad_create_stream_id_internal:<fakesrc1:src>e[00m Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:00.272002841 e[333m11270e[00m   0x7f6c0044f0 e[32;01mFIXME  e[00m e[00m            basesink gstbasesink.c:3145:gst_base_sink_default_event:<appsink0>e[00m stream-start event without group-id. Consider implementing group-id handling in the upstream elements
0:00:00.376278458 e[333m11270e[00m   0x7f6c004d40 e[33;01mWARN   e[00m e[00m             basesrc gstbasesrc.c:3055:gst_base_src_loop:<udpsrc3>e[00m error: Internal data stream error.
0:00:00.376362418 e[333m11270e[00m   0x7f6c004d40 e[33;01mWARN   e[00m e[00m             basesrc gstbasesrc.c:3055:gst_base_src_loop:<udpsrc3>e[00m error: streaming stopped, reason not-linked (-1)
0:00:00.381927910 e[333m11270e[00m   0x7f6c0044f0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:4476:gst_v4l2_object_probe_caps:<nvv4l2decoder0:src>e[00m Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: Unknown error -1
0:00:00.381985307 e[333m11270e[00m   0x7f6c0044f0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24ea6d0e[00m Failed to determine interlace mode
0:00:00.382049475 e[333m11270e[00m   0x7f6c0044f0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24ea6d0e[00m Failed to determine interlace mode
0:00:00.382092341 e[333m11270e[00m   0x7f6c0044f0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24ea6d0e[00m Failed to determine interlace mode
0:00:00.384657816 e[333m11270e[00m   0x7f6c0044f0 e[33;01mWARN   e[00m e[00m        v4l2videodec gstv4l2videodec.c:1755:gst_v4l2_video_dec_decide_allocation:<nvv4l2decoder0>e[00m Duration invalid, not setting latency
0:00:00.385216110 e[333m11270e[00m   0x7f6c0044f0 e[33;01mWARN   e[00m e[00m      v4l2bufferpool gstv4l2bufferpool.c:1087:gst_v4l2_buffer_pool_start:<nvv4l2decoder0:pool:src>e[00m Uncertain or not enough buffers, enabling copy threshold
0:00:00.402792398 e[333m11270e[00m   0x7f540c2850 e[33;01mWARN   e[00m e[00m      v4l2bufferpool gstv4l2bufferpool.c:1536:gst_v4l2_buffer_pool_dqbuf:<nvv4l2decoder0:pool:src>e[00m Driver should never set v4l2_buffer.field to ANY
0:00:00.445783122 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                rtmp :0::e[00m Unknown protocol!

0:00:00.449649773 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                rtmp :0::e[00m Unknown protocol!

0:00:00.496338393 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24f60b0e[00m Failed to determine interlace mode
0:00:00.496425895 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24f60b0e[00m Failed to determine interlace mode
0:00:00.496501366 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24f60b0e[00m Failed to determine interlace mode
0:00:00.496549127 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:2388:gst_v4l2_object_add_interlace_mode:0x55b24f60b0e[00m Failed to determine interlace mode
0:00:00.496646630 e[333m11270e[00m   0x55b24f72c0 e[33;01mWARN   e[00m e[00m                v4l2 gstv4l2object.c:4476:gst_v4l2_object_probe_caps:<nvv4l2h264enc0:src>e[00m Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: Unknown error -1
0:00:00.497722904 e[333m11270e[00m   0x55b254e0a0 e[32;01mFIXME  e[00m e[00;04m             default gstutils.c:3981:gst_pad_create_stream_id_internal:<source:src>e[00m Creating random stream-id, consider implementing a deterministic way of creating a stream-id
0:00:00.534278845 e[333m11270e[00m   0x55b254e0a0 e[33;01mWARN   e[00m e[00m      v4l2bufferpool gstv4l2bufferpool.c:1087:gst_v4l2_buffer_pool_start:<nvv4l2h264enc0:pool:src>e[00m Uncertain or not enough buffers, enabling copy threshold
0:00:00.576610231 e[333m11270e[00m   0x7f30003c00 e[33;01mWARN   e[00m e[00m      v4l2bufferpool gstv4l2bufferpool.c:1536:gst_v4l2_buffer_pool_dqbuf:<nvv4l2h264enc0:pool:src>e[00m Driver should never set v4l2_buffer.field to ANY
0:00:00.578137766 e[333m11270e[00m   0x55b254e0f0 e[32;01mFIXME  e[00m e[00m            basesink gstbasesink.c:3145:gst_base_sink_default_event:<rtmpsink0>e[00m stream-start event without group-id. Consider implementing group-id handling in the upstream elements
0:00:00.578245477 e[333m11270e[00m   0x55b254e0f0 e[33;01mWARN   e[00m e[00m              flvmux gstflvmux.c:1282:gst_flv_mux_write_header:<flvmux0>e[00m downstream did not handle seeking query
0:00:00.578277925 e[333m11270e[00m   0x55b254e0f0 e[33;01mWARN   e[00m e[00m              flvmux gstflvmux.c:1290:gst_flv_mux_write_header:<flvmux0>e[00m downstream is not seekable, but streamable=false. Will ignore that and create streamable output instead
0:00:00.582238120 e[333m11270e[00m   0x55b254e0f0 e[31;01mERROR  e[00m e[00m                rtmp :0::e[00m RTMP_ReadPacket, failed to read RTMP packet header
0:00:00.582299996 e[333m11270e[00m   0x55b254e0f0 e[33;01mWARN   e[00m e[00m            rtmpsink gstrtmpsink.c:245:gst_rtmp_sink_render:<rtmpsink0>e[00m error: Could not connect to RTMP stream "'rtmp://192.168.0.2:1935/live'" for writing
0:00:00.662114583 e[333m11270e[00m   0x55b254e0a0 e[33;01mWARN   e[00m e[00m             basesrc gstbasesrc.c:3055:gst_base_src_loop:<source>e[00m error: Internal data stream error.
0:00:00.662203960 e[333m11270e[00m   0x55b254e0a0 e[33;01mWARN   e[00m e[00m             basesrc gstbasesrc.c:3055:gst_base_src_loop:<source>e[00m error: streaming stopped, reason error (-5)
0:00:02.559359815 e[333m11270e[00m   0x55b24ff9e0 e[33;01mWARN   e[00m e[00m             rtspsrc gstrtspsrc.c:6161:gst_rtspsrc_send:<rtspsrc0>e[00m error: Unhandled error
0:00:02.559431848 e[333m11270e[00m   0x55b24ff9e0 e[33;01mWARN   e[00m e[00m             rtspsrc gstrtspsrc.c:6161:gst_rtspsrc_send:<rtspsrc0>e[00m error: Option not supported (551)
0:00:02.559516694 e[333m11270e[00m   0x55b24ff9e0 e[33;01mWARN   e[00m e[00m             rtspsrc gstrtspsrc.c:8244:gst_rtspsrc_pause:<rtspsrc0>e[00m error: Could not send message. (Generic error)
0:00:02.566107314 e[333m11270e[00m   0x55b24ff9e0 e[33;01mWARN   e[00m e[00m             rtspsrc gstrtspsrc.c:5919:gst_rtsp_src_receive_response:<rtspsrc0>e[00m receive interrupted
0:00:02.566193201 e[333m11270e[00m   0x55b24ff9e0 e[33;01mWARN   e[00m e[00m             rtspsrc gstrtspsrc.c:7673:gst_rtspsrc_close:<rtspsrc0>e[00m TEARDOWN interrupted