OpenCV RTP-Stream with FFMPEG

OpenCV RTP-Stream with FFMPEG

本文关键字:FFMPEG with RTP-Stream OpenCV      更新时间:2023-10-16

我使用带有ffmpeg(api-ppreferences CAP_FFMPEG(的OpenCV来接收RTP流并显示视频。

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

我的代码:

#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 variablesn" << 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 存在内存泄漏问题,我无法解决。

问题

如何使用带有 ffmpeg 的 OpenCV 通过 rtp 接收多个视频(连续(?

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

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