c++ - 带有 FFMPEG 的 OpenCV RTP-Stream

标签 c++ opencv ffmpeg

我使用带有 ffmpeg(api-preference CAP_FFMPEG)的 OpenCV 来接收 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

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

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

问题

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

最佳答案

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

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

关于c++ - 带有 FFMPEG 的 OpenCV RTP-Stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60060584/

相关文章:

c++ - 错误 : ‘asm’ operand has impossible constraints when using gcc 32bit

c++ - 如何在 opencv 中将 QPointf 转换为 Point?

java - 当新图像传入时,保持 ffmpeg 将图像转换为视频

c++ - 打印字符串数组长度而不是 C++ 中的数组项数

c++ - C++ 有自由函数 `size(object)` 吗?

c++ - 递归矩阵乘法

c++ - OpenCV 中心单应

c++ - 我用 Gstreamer MSVC 1.16.1 构建 opencv 3.4,现在 imread 和 VideoCapture 不起作用

bash - 创建一个 convertAll() 函数来转换工作目录中的所有 .filetype

audio - 如何使用 Silk 编解码器生成 rtp pcap 文件?