通过opencv和c ++从ip camera流式传输视频

时间:2018-05-09 06:52:03

标签: c++ opencv video-streaming rtsp ip-camera

我正在尝试使用openCV代码从c ++访问网络摄像头流但是它失败并显示无法打开流的错误。将URL替换为0时,下面提到的代码访问网络摄像头。 可以从VLC和python代码访问相同的摄像头。

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

int main(int, char**) {

    VideoCapture cap("rtsp://admin:admin@10.11.18.185:554/cam/realmonitor?channel=1&subtype=0"); // open the video camera using http protocol with the URL specified 
    while (!cap.isOpened())  // if not success, exit program
    {
        cout << "cap not open" << endl;
        continue;
        //return -1;
    }

    Mat frame;
    namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); 
    while (1) {
        cap.read(frame);

        imshow("MyVideo", frame);
        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            exit(0);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

VideoCapture库需要一组特定的格式来接收视频流。

从已保存的文件中读取内容时,我们会在文件扩展名中指定相同内容 - .mp4.avi等。 如果未指定此扩展名,则VideoCapture将无法捕获帧。

尝试使用:

VideoCapture cap("rtsp://admin:admin@10.11.18.185:554/cam/realmonitor?channel=1&subtype=0/video?x.mjpeg");