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?