首页 > 解决方案 > 从 OpenCV-Python 流式传输 RTP/RTSP 流时遇到问题

问题描述

我正在尝试使用我在这里找到的这个示例脚本来获取 OpenCV 图像并将它们转换为 rtp/rtsp 流:
https ://github.com/madams1337/python-opencv-gstreamer-examples/blob/master/gst_device_to_rtp .py

这是脚本的描述:

“gst_device_to_rtp 抓取 VideoCapture(0),编码帧并将其流式传输到 rtp://localhost:5000”

这是我尝试使用的代码

# Cam properties
fps = 30.
frame_width = 1920
frame_height = 1080
# Create capture

#cap = cv2.VideoCapture(0)

# Set camera properties
cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)
cap.set(cv2.CAP_PROP_FPS, fps)

# Define the gstreamer sink
gst_str_rtp = "appsrc ! videoconvert ! x264enc noise-reduction=10000 tune=zerolatency byte-stream=true threads=4 " \
              " ! h264parse ! mpegtsmux ! rtpmp2tpay ! udpsink host=127.0.0.1 port=5000"

# Create videowriter as a SHM sink
out = cv2.VideoWriter(gst_str_rtp, 0, fps, (frame_width, frame_height), True)

# Loop it
while True:
    # Get the frame
    ret, frame = cap.read()

    # Check
    if ret is True:
        # Flip frame
        frame = cv2.flip(frame, 1)
        # Write to SHM
        out.write(frame)
    else:
        print "Camera error."
        time.sleep(10)

cap.release()

主要是这段代码,它指定了 gstreamer 管道配置:

# Define the gstreamer sink
gst_str_rtp = "appsrc ! videoconvert ! x264enc noise-reduction=10000 tune=zerolatency byte-stream=true threads=4 " \
              " ! h264parse ! mpegtsmux ! rtpmp2tpay ! udpsink host=127.0.0.1 port=5000"

# Create videowriter as a SHM sink
out = cv2.VideoWriter(gst_str_rtp, 0, fps, (frame_width, frame_height), True)

据我了解,它将 OpenCV 视频图像发送到“rtp://localhost:5000”

但是,每当我在让脚本运行时尝试在终端中执行此终端命令时:

ffplay 'rtp://localhost:5000'

它就像这样永远挂起: 在此处输入图像描述 我无法确定这到底意味着什么。这是否意味着它可以在该端口连接到本地主机,但在那里什么也没找到?我真的不知道。如果有另一个具有其他 rtsp url 的命令似乎有效,但不是这个。

如果我尝试“ffplay 'rtsp://localhost:5000'”,那么我只会收到 Connection Refused 错误(可能在该流中没有生成任何内容)

该脚本是否真的将 rtp 流输出到 localhost:5000?或者我的机器上的ffplay有问题吗?还是我应该执行一个特殊的 ffplay 命令?我应该怎么办?

标签: pythonopencvstreaminggstreamerrtp

解决方案


推荐阅读