openCV cvCreateVideoWriter always returns null

2019-06-08 06:14发布

I am trying to save a video file with openCV under Mandriva Linux 2009.1. Unfortunately cvCreateVideoWriter always returns null although I could see and modify both and mpeg fiels, in other words cvCaptureFromCam, cvCaptureFromAvi, cvRetrieveFrame is working.

I tried all the possible codecs from this tutorial and this codec page. I also tried 0 for uncompressed avi and -1 for a selection box in the fourcc parameter, but no choice has popped up and nothing happened. I also modified isColor to 0.

I also have write permissions in the working directory.

Here is the relevant code snippet:

    CvVideoWriter *writer = 0;
    int isColor = 1;
    int fps = 25;
    int frameW = 640;
    int frameH = 480;


    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
    else if( argc == 2 ) {
        printf("argc == 2 :%s:\n", argv[1] );
        capture = cvCaptureFromFile( argv[1] );
    }

    cvInitSystem(argc,argv);

 writer = cvCreateVideoWriter("out.avi",CV_FOURCC('I','Y','U','V'),fps,cvSize(frameW,frameH),isColor);

What could be the problem?

标签: video opencv avi
2条回答
Deceive 欺骗
2楼-- · 2019-06-08 06:28

This will depend on how your OpenCV library was configured. In particular I believe HAVE_FFMPEG must be defined in order for the 'IYUV' FOURCC value to be handled. Was your library built with HAVE_FFMPEG defined?

The default writer object (which is created when fourcc is zero, or the fourcc value is not handled by one of the available handlers) creates an image file per frame. In this case the filename needs to be something like "file%04d.foo" or "file0001.foo", otherwise writer creation fails. You might try passing something like "test0001.png" for the filename parameter as an experiment to verify that the default image writer is created successfully (this won't help directly, but might be a way to confirm that your fourcc value is not being recognized by an available writer.)

According to documentation, the option to open a selection dialog when the fourcc equal -1 is Windows-specific.

查看更多
The star\"
3楼-- · 2019-06-08 06:52

I read your post, Lance's reply and your comments to it, then finally found workaround for myself and decided to add some notes for everyone struggling with that issue.

First thing to check is whether CMake has built OpenCV with FFmpeg. My fast and horrible way to do this is just to run cmake with flags and parameters, as you wrote them during installation, again. In the end of output there will be logs about which components will be installed. If your parameters copy those used during the actual installation, you'll get info about what is already installed.

Next you may want to check available encoders and decoders for codecs on your linux system. I do this by using ffmpeg -codecs.

Third note is reliable and primitive arguments set for cvCreateVideoWriter:

"a0000.png", 0, fps, size, 0

fps and size here are variables containing your values for fps and frame_size fields respectively. You'll get bags of images -- what Lance Richardson and rics already noted.

Finally, for my system, on which OpenCV is built with FFmpeg and XVID encoder is installed, I plug these arguments into cvCreateVideoWriter:

"/home/me/openCVOut/a.avi", CV_FOURCC('X','V','I','D'), fps, size

and finally get what I want.

Hope my answer is of some use!

查看更多
登录 后发表回答