我想处理并显示从树莓派相机创建的网络 rtsp 流。
我有这个代码:
#include <iostream>
#include <functional>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main(int argc, char** argv) {
cv::VideoCapture * stream = new cv::VideoCapture("rtsp://192.168.55.151:8554/");
if (!stream->isOpened()) return -1;
cv::namedWindow("rtsp_stream", CV_WINDOW_AUTOSIZE);
cv::Mat frame;
while (true) {
if (!stream->read(frame)) return -1;
cv::imshow("rtsp_stream", frame);
cv::waitKey(15);
}
return 1;
}
当流不实时时,执行此操作会导致:
[tcp @ 0xa12480] Connection to tcp://192.168.55.151:8554?timeout=0 failed: Connection refused
这意味着流尝试与 tcp 连接。当流处于直播状态时,执行结果为:
[rtsp @ 0xb07960] method SETUP failed: 461 Client error
通过互联网研究,我发现问题可能是流使用 udp。如果我将网址更改为:
"udp://192.168.55.151:8554/"
然后执行冻结在cv::VideoCapture("udp://192.168.55.151:8554/");
VLC能够打开rtsp流。据我了解,ffmpeg 用于使用 opencv 解码流。当我跑步时:
ffmpeg -rtsp_transport udp -i rtsp://192.168.55.151:8554/ -t 5 test.mp4
码流解码保存成功。
那么如何在opencv代码中指定底层协议为udp呢?还有其他方法可以用 opencv 实现吗?
编辑:如果我更改 ffmpeg 命令以使用 tcp,即:
ffmpeg -rtsp_transport tcp -i rtsp://192.168.55.151:8554/ -t 5 test.mp4
然后我得到与 C++ 代码相同的错误,461 客户端错误
编辑:当代码使用 udp:// 链接时,冻结大约 15 秒后,执行返回错误