OpenCV VideoWriter won't write anything, although cvWriteToAVI does

心不动则不痛 提交于 2019-11-29 04:46:05
G453

The syntax in Opencv C++ reference is bit different, and here is a working code in C++. I Just added imshow and waitkey, for checking you can remove them if you want.

int main()
{
    VideoCapture* capture2 = new VideoCapture(CV_CAP_DSHOW);
    Size size2 = Size(640, 480);
    int codec = CV_FOURCC('M', 'J', 'P', 'G');
    // Unlike in C, here we use an object of the class VideoWriter//
    VideoWriter writer2("video_.avi", codec, 15.0, size2, true);

    writer2.open("video_.avi", codec, 15.0, size2, true);
    if (writer2.isOpened())
    {
        int a = 100;
        Mat frame2;
        while (a > 0)
        {
            capture2->read(frame2);
            imshow("live", frame2);
            waitKey(100);
            writer2.write(frame2);
            a--;
        }
    }
    else
    {
        cout << "ERROR while opening" << endl;
    }

    // No Need to release the Writer as the distructor will called automatically
    capture2->release();

    return 0;
}

I had the same problem over and over again, and none of the solutions I found online helped.

Strange enough, the problem (identified purely with a trial and error method) was with the write permission. Everything worked after I sudo chmod u+rwx the python script.

I have the same problem and after a few time i realize that the input video isn't the same size with the output. Resize the input video may help u.

 capture2->read(frame2);
 cv::resize(frame2,frame2,cv::Size(640,480);
 writer2->write(frame2);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!