Opencv VideoWriter只保存一帧

时间:2015-11-22 18:11:35

标签: c++ opencv

我正在尝试从两张图片创建1fps webm, 代码:

    // path to output
    string outputVideoPath = "/home/gio/Desktop/giffer/def.webm";
    // write dimensions in size struct
    Size dims = Size(640,480);

    // create and open VideoWriter object
    VideoWriter outputVideo;
    outputVideo.open(outputVideoPath, CV_FOURCC('V','P','8', '0'), 1, dims, true);

    // check if opened
    if(!outputVideo.isOpened()){
        cout << "Creating outputVideo fail" << endl;
        return -1;
    }

    // I hate strings
    string paths[2];
    paths[0] = "/home/gio/Desktop/giffer/images_temp/g0.jpg";
    paths[1] = "/home/gio/Desktop/giffer/images_temp/g1.jpg";

    for (int i = 0; i < 2; i++) {
        cout << paths[i] << endl;
        Mat temp = imread(paths[i]);
        outputVideo.write(temp);
        usleep(10000);
    }

结果是唯一的帧(最后一张图像),1秒长。 我做错了什么?

修改 我尝试使用.avi扩展名DIVX编解码器,但它确实有效。有关如何使其在.webm上运行的任何想法?或.gif

1 个答案:

答案 0 :(得分:1)

应该是

usleep(1000000);

由于usleep参数以微秒为单位,因为can read here