VideoWriter fourcc=-1 works but CV_FOURCC('C','R','A','M') does not
Howdy,
I am using the VideoWriter class to capture video from a camera to a file. I am doing CV in the code as well, of course, but am adding video capture to help troubleshoot and optimize.
I am able to capture the video if I pass the magic number "-1" as the fourcc parameter, like so:
outputVideo = new cv::VideoWriter(outputVideo_name, -1, 20, img_rgb.size(), true);
I select "Microsoft Video 1" from the dropdown and it generates a video file using the CRAM codec.
However, if I replace that "-1" with "CV_FOURCC('C','R','A','M')", it says it's capturing video but the file is always 6kB and does not play. Using MSVC or WHAM produces similar results.
I am running on Windows 7 with a PS3 Eye camera using the CL-Eye driver and OpenCV 2.4.1, compiled with Eclipse and MinGW's g++ (GCC) 4.5.2.
Any ideas on what I might be doing wrong?
Thanks,
Nathan
=====
EDIT: Images showing binary differences between now-working and working generated video files:
(Here there be zeros.)
FOURCC supports a very large amount of codecs, but in my experience, aside from the major codecs, whether the codec actually works varies. In my experience, I just try the different codecs until one works. http://www.fourcc.org/codecs.php lists 'CRAM' as one of their supported codecs, but it also says that it is allegedly simialr to 'MSVC'. Another codec listed is 'WHAM'. Maybe give these a try?
Yeah, I did try those for that same reason but no joy. I used CRAM first since that is what is embedded in the working video file, as you can see in the first image - easier to see if you view it full-size.
I also have the same result with IYUV which is a better-regarded codec but it's the same deal - it works if I select it from the dropdown but not if I direct VideoWriter to use that codec from the code.
I feel like I must be doing something more basic wrong like improperly mixing C and C++ interfaces or something, maybe?
I think you can reduce this to your operating system not having the necessary codecs. Could you install a software package with the most random codecs inside, like VLC media player and see if it gets solved?
Okay, I've installed VLC and ran through some codecs (DIVX, IYUV, CRAM, MSVC, WHAM, CVID, PIM1, MJPG, MP42, DIV3, U263, I263, FLV1) but nothing seems to have changed. One thing I was surprised by is I still have the same small list of options* when I pass -1 in as the codec; is there something I need to do in my build or OS environment to tell the program where to find the codecs for encoding?
One other thing I realized is I am able to generate a playable video file by passing in CVID. Unfortunately, that particular codec, whether chosen from the dropdown or specified by the code, causes my throughput to drop from 20 FPS to 6. But, it implies the code works, I just need to figure out how to get it to work with other codecs.
Thanks a lot for the help.
Hmm seems like VLC keeps his codecs purely for the software. Check this link which contains the K-lite codec pack. This should add most general codecs to your system.
Well, I first tried just downloading the DivX codec and it, like the others, worked from the -1 select but not when specified directly in the code. So, I grabbed the K-lite Basic pack but it only has decoders so I grabbed the 22MB Mega pack and it has some encoders. And... one of them (X264) even works! It doesn't impact my throughput at all and it generates small files. The only issues are relatively minor: it pops up a warning about frames being lost* and a message is logged to the terminal, "Could not find encoder for codec id 28: Encoder not found". Still, much better than the -1 select menu.
I would love to know why it was such a struggle but I am happy to have a workable solution for now.
Thanks again.
This is the warning message I mentioned above:
x264vfw [warning]: Few frames probably would be lost. Ways to fix this:
x264vfw [warning]: - if you use VirtualDub or its fork than you can enable 'VirtualDub Hack' option
x264vfw [warning]: - you can enable 'File' output mode
x264vfw [warning]: - you can enable 'Zero Latency' option
The error is encoder specific. I do not have experience with that.