Ask Your Question

VideoWriter fourcc=-1 works but CV_FOURCC('C','R','A','M') does not

asked 2013-05-06 10:02:04 -0500

nklatt gravatar image

updated 2013-05-06 11:29:42 -0500


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?




EDIT: Images showing binary differences between now-working and working generated video files:

image description

(Here there be zeros.)

image description

edit retag flag offensive close merge delete


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. 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?

HD_Mouse gravatar imageHD_Mouse ( 2013-05-06 13:26:06 -0500 )edit

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?

nklatt gravatar imagenklatt ( 2013-05-07 08:36:47 -0500 )edit

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?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-07 10:21:58 -0500 )edit

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.

nklatt gravatar imagenklatt ( 2013-05-07 12:16:49 -0500 )edit
  • Options I get in the dropdown when I pass in -1 for the codec:
  • Microsoft RLE which doesn't work at all
  • Microsoft Video 1 generates a CRAM file
  • Intel IYUV codec generates an IYUV file
  • Intel IYUV codec generates an IYUV file (yes, it's in the list twice)
  • Cinepak Codec by Radius generates a CVID file but everything slows to 6 FPS
  • Full Frames (Uncompressed) generates a file that works but I can't tell the type
nklatt gravatar imagenklatt ( 2013-05-07 12:19:32 -0500 )edit

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.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-07 13:20:51 -0500 )edit

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.

nklatt gravatar imagenklatt ( 2013-05-08 10:42:27 -0500 )edit

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

nklatt gravatar imagenklatt ( 2013-05-08 10:43:09 -0500 )edit

The error is encoder specific. I do not have experience with that.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-11 12:46:13 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-05-07 03:30:16 -0500

Looking at the video codecs that are supported by fourcc, I found the following explanation with the CRAM option:

Allegedly identical to MSVC.

So I would suggest to replace the CV_FOURCC('C','R','A','M') by CV_FOURCC('M','S','V','C') and see if this solves the problem. More info on this codec can be found here.

edit flag offensive delete link more


(Please see my comment, above - thanks.)

nklatt gravatar imagenklatt ( 2013-05-07 08:37:50 -0500 )edit

(This isn't actually the answer - that can be found in the comments to the original post - but I want to be sure Mr Puttemans receives the points for helping solve my problem.)

nklatt gravatar imagenklatt ( 2013-05-08 10:45:33 -0500 )edit

Question Tools


Asked: 2013-05-06 10:02:04 -0500

Seen: 22,485 times

Last updated: May 07 '13