我正在尝试编写一个简短的 C++ 脚本,它可以向我显示来自本地网络上摄像机的 RTSP 流。目前我的脚本如下:
#include <iostream>
#include <opencv2/opencv.hpp>
int main()
{
std::string window_name = "RGB Camera";
setenv("OPENCV_FFMPEG_CAPTURE_OPTIONS", "rtsp_transport;udp", 1);
const std::string rtsp_url = "rtsp://192.168.2.100:5010/video.sdp";
cv::VideoCapture cap; // Declare capture stream
cap.open(rtsp_url, cv::CAP_FFMPEG);
if (!cap.isOpened()) // Prompt an error message if no video stream is found
{
perror("Video Stream Error");
exit(EXIT_FAILURE);
}
cv::namedWindow(window_name, cv::WindowFlags::WINDOW_KEEPRATIO); // Declaring the video to show the video
cv::Mat frame; // Declaring a matrix to load the frames
while (1)
{
const bool read_success = cap.read(frame);
if (!read_success)
{
printf("End of stream\n.");
break;
}
cv::imshow(window_name, frame);
char c = (char)cv::waitKey(25); // 25 milliseconds per frame
if (c == 27) // If 'Esc' key is pressed, break the loop
{
printf("Esc key pressed, terminating loop...\n");
break;
}
}
cap.release(); // Release memory buffer
cv::destroyAllWindows(); // Close all windows
return 0;
}
但是在运行时我收到以下错误消息:
Video Stream Error: Operation now in progress
我知道我的脚本在
if (!cap.isOpened())
检查中失败,但是我可以在 VLC 中使用 ffplay
和 rtsp url 打开此流。我不知道为什么 OpenCV 不起作用。
此外,当我删除
cv::CAP_FFMPEG
函数中的 cap.open()
时,我收到错误:
Video Stream Error: No such file or directory
如有任何建议,我们将不胜感激。
您必须在 rtsp_url 上添加用户名和密码
const std::string rtsp_url = "rtsp://<username>:<password>@192.168.2.100:5010/video.sdp";