【问题标题】:OpenCV RTP-Stream with FFMPEG带有 FFMPEG 的 OpenCV RTP-Stream
【发布时间】:2020-05-20 11:16:26
【问题描述】:

我使用 OpenCV 和 ffmpeg(api-preference CAP_FFMPEG)来接收 RTP-Stream 并显示视频。

当我发送一个短视频时,视频会按预期播放。但是当我尝试发送第二个视频时,第二个根本没有显示。

我的代码:

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdlib.h>

using namespace std;
using namespace cv;
int main(int argc, char** argv) {

    cout << "--- OpenCV Verson: " << CV_VERSION << " ---" << endl;

    char env[] = "OPENCV_FFMPEG_CAPTURE_OPTIONS=protocol_whitelist;file,rtp,udp";
    int rc = putenv(env);
    if (rc != 0){
        cout << "Could not set environment variables\n" << endl;
    }

    VideoCapture cap;
    char input[] = "path/to/saved_sdp_file.sdp";
    cap.open(input, CAP_FFMPEG);
    if(!cap.isOpened()) {
        cout << "Could not open Stream" << endl;
        return 1;
    }

    string windowName = "Test";
    namedWindow(windowName, cv::WindowFlags::WINDOW_NORMAL);
    Mat frame;

    while(1){

        cap >> frame;
        if (frame.empty()){
            cout << "Application closed due to empty frame" << endl;
            return 1;
        }

        imshow(windowName, frame);
        int c = waitKey(1);
        if (c == 27){
            break;
        }
    }

    cap.release();
    destroyAllWindows();
    return 0;
}

我的 .sdp 文件:

SDP:
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
m=video 6005 RTP/AVP 96
b=AS:132
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z2QADazZQUH7ARAAAAMAEAAAAwPA8UKZYA==,aOvjyyLA; profile-level-id=64000D

启动流的命令:

ffmpeg -re -thread_queue_size 4 -i video_file.mp4 -strict -2 -vcodec copy -an -f rtp rtp://192.168.20.98:6005

我用 ffplay 测试了流媒体。我使用以下命令:

ffplay -protocol_whitelist "file,rtp,udp" -i saved_sdp_test.sdp -reorder_queue_size 0

似乎一切正常,我可以发送和接收多个视频。 但我不知道如何在 OpenCV 中传递 -reorder_queue_size 选项。

我知道使用 gstreamer 而不是 ffmpeg 也可以做到这一点。但是 OpenCV (https://github.com/opencv/opencv/issues/5715) 中的 gstreamer-api 存在内存泄漏问题,我无法解决。

问题

如何使用 OpenCV 和 ffmpeg 通过 rtp(连续)接收多个视频?

【问题讨论】:

    标签: c++ opencv ffmpeg


    【解决方案1】:

    我可以像添加 protocol_whitelist 选项一样添加 -reorder_queue_size 选项。我只需要在选项之间使用|。比一切都按预期工作。

    char env[] = "OPENCV_FFMPEG_CAPTURE_OPTIONS=protocol_whitelist;file,rtp,udp | reorder_queue_size;0";
    putenv(env);
    

    【讨论】:

      猜你喜欢
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      相关资源
      最近更新 更多