簡體   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