使用 Python OpenCV 和 GStreamer 从 UDP 接收器读取 RTSP 流

Ral*_*sto 6 opencv rtsp video-streaming gstreamer python-3.x

我们正在开发一种软​​件,可以使用 GStreamer 使用 RTSP 从两个不同的摄像机流式传输视频。为了简化获取过程,我们将 OpenCV 与 Python 3 结合使用。

问题是:我们想将流推送到 UDP 接收器,将其作为 RTSP 流在我们的 LAN 上重新发布,然后在另一台 PC 中读取它。但是我们不能让它发挥作用。

这是获取相机图像并使用udpsink. 在这种情况下,我们正在访问我们的本地网络摄像头,以便任何人都可以直接测试代码。

import cv2
import time
from multiprocessing import Process

def send():
    video_writer = cv2.VideoWriter(
        'appsrc ! '
        'videoconvert ! '
        'x264enc tune=zerolatency speed-preset=superfast ! '
        'rtph264pay ! '
        'udpsink host=127.0.0.1 port=5000',
        cv2.CAP_GSTREAMER, 0, 1, (640, 480), True)

    video_getter = cv2.VideoCapture(0)

    while True:

        if video_getter.isOpened():
            ret, frame = video_getter.read()
            video_writer.write(frame)
            # cv2.imshow('send', data_to_stream)
            time.sleep(0.1)   

if __name__ == '__main__':
    s = Process(target=send)
    s.start()
    s.join()
    cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

运行此程序时,我们只会收到一个警告:

[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Run Code Online (Sandbox Code Playgroud)

然后我们尝试使用examples/test-launchfrom GStreamer将流通过 LAN 重新发布为 RTSP

./test-launch " udpsrc port=5000 ! h264parse ! rtph264pay name=pay0 pt=96"
Run Code Online (Sandbox Code Playgroud)

这给我们没有错误,但默认消息

stream ready at rtsp://127.0.0.1:8554/test
Run Code Online (Sandbox Code Playgroud)

然后 VLC 无法在此地址打开流。

                     ~$ vlc -v rtsp://127.0.0.1:8554/test
VLC media player 3.0.11 Vetinari (revision 3.0.11-0-gdc0c5ced72)
[000055f5dacf3b10] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
Qt: Session management error: None of the authentication protocols specified are supported
[00007f5ea40010f0] live555 demux error: Failed to connect with rtsp://127.0.0.1:8554/test
[00007f5ea4003120] satip stream error: Failed to setup RTSP session
Run Code Online (Sandbox Code Playgroud)

我想这与我们的管道有关,但我真的不明白它可能是什么。任何帮助将不胜感激。提前致谢。

小智 1

您可以删除VideoWriter中的rtph264pay,然后python脚本发送h264数据,并测试启动接收h264数据,并进行rtp packetize,然后进行rtsp。

所以 VideoWriter 应该是:

video_writer = cv2.VideoWriter(
    'appsrc ! '
    'videoconvert ! '
    'x264enc tune=zerolatency speed-preset=superfast ! '
    'udpsink host=127.0.0.1 port=5000',
    cv2.CAP_GSTREAMER, 0, 1, (640, 480), True)
Run Code Online (Sandbox Code Playgroud)