opencv 视频录制保存本地

2019-11-07  本文已影响0人  Caiaolun
#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;
}
上一篇下一篇

猜你喜欢

热点阅读