在ubuntu下利用opencv打开usb相机(其他相机类似),并保存视频流

#include 
#include 
#include 
#include 
#include 
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
    cv::VideoCapture iv;
    iv.open(0);
    if(!iv.isOpened())
    {
        cout<<"Could not open reference "<> src;
    bool isColor = (src.type() == CV_8UC3);
    VideoWriter writer;
    int codec = VideoWriter::fourcc('M', 'J', 'P', 'G');  // 选择视频的格式
    double fps = 10.0;//保存的帧率,如果想要保存后的视频时间和录的时间一样,此处fps需要和相机帧率保持一致
    string filename = "/home/robooster/camera.mp4";             // 视频保存的路径
    writer.open(filename, codec, fps, src.size(), isColor);
    // 检查视频是否读取出来
    if (!writer.isOpened()) 
    {
        cerr << "Could not open the video file\n";
        return -1;
    }
    while (1)
    {
        iv >> src;
        writer.write(src);
    }
}

你可能感兴趣的:(c++,opencv)