繁体   English   中英

OpenCV-在3.4版中使用FFMPEG在RTSP上流式传输H264

[英]OpenCV - Streaming H264 over RTSP using FFMPEG in version 3.4

我正在尝试将VIRB 360摄像机的RTSP流捕获到OpenCV 该视频为H264 ,根据此处的评论之一, OpenCV 3.4应该可以处理它。 这是代码:

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>

int main()
{
    cv::VideoCapture cap("rtsp://192.168.0.1/livePreviewStream?maxResolutionVertical=720&liveStreamActive=1", cv::CAP_FFMPEG);

    if(!cap.isOpened())
    {   
        std::cout << "Input error\n";
        return -1;
    }

    cv::namedWindow("Video Feed", cv::WINDOW_AUTOSIZE);

    cv::Mat frame;
    for(;;)
    {
        //std::cout << "Format: " << cap.get(CV_CAP_PROP_FORMAT) << "\n";
        cap >> frame;
        cv::imshow("Video Feed", frame);    
        if (cv::waitKey(10) == 27)
        {
            break;
        }
    }   
    cv::destroyAllWindows();
    return 0;
}

我已经用ffmpeggstreamer功能编译了OpenCV 当我运行以下Gstreamer命令时,我可以流式传输它,但要延迟3秒(不可接受):

 gst-launch-1.0 playbin uri=rtsp://192.168.0.1/livePreviewStream?maxResolutionVertical=720\&liveStreamActive=1

另一方面,我使用ffplay/ffmpeg命令得到了0.5秒的延迟(可以接受):

ffplay -fflags nobuffer -rtsp_transport udp rtsp://192.168.0.1/livePreviewStream?maxResolutionVertical=720\&liveStreamActive=1

要么

ffplay -probesize 32 -sync ext rtsp://192.168.0.1/livePreviewStream?maxResolutionVertical=720\&liveStreamActive=1

在上面编写的OpenCV代码中,在以下行中使用cv::CAP_FFMPEG标志:

cv::VideoCapture cap("rtsp://192.168.0.1/livePreviewStream?maxResolutionVertical=720&liveStreamActive=1", cv::CAP_FFMPEG);

给出错误:

[rtsp @ 0x2312040] The profile-level-id field size is invalid (65)
[rtsp @ 0x2312040] method SETUP failed: 461 Unsupported transport
Input error

如果我使用cv::CAP_GSTREAMER ,它不会引发任何错误,但是什么也不会发生。 我认为问题在于OpenCV无法处理UDP传输层。 有什么可能的解决方案? 请提供建议。

编辑1:通过执行此操作,我能够捕获流。 我进行了以下更改:而不是cv::VideoCapture cap("rtsp://192.168.0.1/livePreviewStream?maxResolutionVertical=720&liveStreamActive=1", cv::CAP_FFMPEG); 该代码现在具有:

#if WIN32
    _putenv_s("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp");
#else
    setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp", 1);
#endif
auto cap = cv::VideoCapture("rtsp://192.168.0.1/livePreviewStream?maxResolutionVertical=720&liveStreamActive=1", cv::CAP_FFMPEG);

#if WIN32
    _putenv_s("OPENCV_FFMPEG_CAPTURE_OPTIONS", "");
#else
    unsetenv("OPENCV_FFMPEG_CAPTURE_OPTIONS");
#endif

但是,它引发以下错误:

[rtsp @ 0x2090580] The profile-level-id field size is invalid (65)
[rtsp @ 0x2090580] Error parsing AU headers
[h264 @ 0x208d240] error while decoding MB 69 40, bytestream -7
[rtsp @ 0x2090580] Error parsing AU headers
[rtsp @ 0x2090580] Error parsing AU headers
[h264 @ 0x2316700] left block unavailable for requested intra4x4 mode -1
[h264 @ 0x2316700] error while decoding MB 0 16, bytestream 112500
[rtsp @ 0x2090580] Error parsing AU headers

这意味着视频有时会出现故障,看起来像: 在此处输入图片说明

我认为这与以下方面有关:

setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp", 1);

我将不胜感激任何建议或改进。 谢谢。

设置此:

cv::VideoCapture cap;
cap.set(CV_CAP_PROP_BUFFERSIZE, 3); /

我认为这里已经回答了。 由于捕获缓冲区导致OpenCV VideoCapture滞后

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM