美文网首页
opencv 视频录制保存本地

opencv 视频录制保存本地

作者: Caiaolun | 来源:发表于2019-11-07 10:29 被阅读0次
    #include <opencv2/opencv.hpp>
    #include <iostream>
     
    using namespace cv;
    using namespace std;
     
    int main(int argc, char** argv) {
        VideoCapture cam(0);
        if (!cam.isOpened())
        {
            cout << "cam open failed!" << endl;
            getchar();
            return -1;
        }
        cout << "cam open success!" << endl;
        namedWindow("cam");
        Mat img;
        VideoWriter vw;
        int fps = cam.get(CAP_PROP_FPS);  //获取摄像机帧率
        if (fps <= 0)fps = 25;
        //创建视频文件
        vw.open("out.avi", //路径
            VideoWriter::fourcc('X', '2', '6', '4'), //编码格式
            fps, //帧率
            Size(cam.get(CAP_PROP_FRAME_WIDTH),
                cam.get(CAP_PROP_FRAME_HEIGHT))  //尺寸
            );
        if (!vw.isOpened())
        {
            cout << "VideoWriter open failed!" << endl;
            getchar();
            return -1;
        }
        cout << "VideoWriter open success!" << endl;
     
        for (;;)
        {
            cam.read(img);
            if (img.empty())break;
            imshow("cam", img);
            //写入视频文件
            vw.write(img);
            if (waitKey(5) == 'q') break;
        }
     
        waitKey(0);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:opencv 视频录制保存本地

          本文链接:https://www.haomeiwen.com/subject/mdmmbctx.html