Ask Your Question

Codecs list

asked 2016-08-24 07:45:14 -0600

Mikl gravatar image

updated 2016-08-30 08:32:56 -0600

Hello all

I am trying to run OpenCV for the first time. Sorry for stupid question. I need to grab images from camera and wright them in AVI file. I need to have an option: compression or not compressed.

When i am using (int)1 as a paremeter for i have an exception, but after pressing "continue" video is recorded. the same with ('D', 'I', 'B', ' ')

Can you point me to list of codecs and\or how to get it from system.

All i found for the moment is this, but not all seems to work for me:

Basically, I need all allowed parameters for fourcc() function. dynamically or static.


If i am passing (-1), i have this codecs in drop down. image description

But even not all of them are not working. By the way, what is the codes of this codecs?

By "not working" i mean file is created, but frames are not recorded by write() function. File size is 0 byte.

UPDATE after pklab answer

First of all thank you for such detailed answer. Read my answers here.

OpenCV doesn't have a function to get the list of available codecs.

But how window with list of codecs (if you use -1 ) is working in this case?

But, it's really useful to programmatically get this list or you just need to know if a codec is available or to know its fourcc ?

Both. i just need to know "codec is available" "programmatically" to chose one. For example, app will have a list of preferences and automatically select codec to use.

This codec is native on Windows.

Yes, i am developing in Windows, but app CAN (have to) work in Linux. Can i "bring" codec with me in the file(dll)?


I am developing commercial app. I found some articles, what i can not use it.

not compressed... did you mean

Yes. The idea is to gain speed. It is real time fail proof application: no frames should be missed.

I would suggest to install some 3rd....

I prefer to be portable. No installations is expected.

using codec=0

I have exception in debug doing this. It is catched somewhere and i don't see it when executing app outside visual studio. And recording is going well. It is normal?

Update with a bit of my code

here you can see simplification of my code. it is extraction of 3 functions

QString name = "File full path";
width = 800;
height = 400;
unsigned char* frame = raw RGB data, coming directly from IP camera;

//initialization : StartVideoFile(QString name, int width, int height)
Size frameSize(width, height);
VideoWriter* _recorder = new VideoWriter(name.toStdString(), -1, 20, frameSize, true);
Mat* _frame = new Mat(frameSize, CV_8UC3); //3 chanels, 8 bit per chanel

//record frames : AttachVideoFrame( unsigned char* frame)
_frame->data = frame;

//finalization : CloseVideoFile()
edit retag flag offensive close merge delete


If your files are not recorded, it could be that the dimensions are incorrect and not the codecs. The codecs of the dropdown menu are the ones supported for your system!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-08-24 08:03:56 -0600 )edit

@StevenPuttemans Thank you for comment. But how comes? it is the same data stream. And how i can programmatically get this list myself. Not via VideoWriter class interface?

Mikl gravatar imageMikl ( 2016-08-24 08:46:33 -0600 )edit

you will need to dive into the source code!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-08-24 09:00:06 -0600 )edit

@StevenPuttemans Source code of OpenCV?

Mikl gravatar imageMikl ( 2016-08-24 09:02:53 -0600 )edit

Yep! Take a look at the video modules!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-08-24 09:07:24 -0600 )edit

:) thanks

Mikl gravatar imageMikl ( 2016-08-24 09:37:08 -0600 )edit

wow ! how many points here.

  • Check my update answer about the list of available codecs
  • Real-time depends from many factor, 1st your app should ensure to get/read all frames if source is real-time, maybe using worker threads and priority. After this you have to know if codec discard some frame for compression. BTW VideoCapture::write works quite fast (more than 100fps is quite normal)
  • On Linux you need .so files on Win you have DLLs and cross platform and codecs is a challenge.
  • For FFMPEG see
pklab gravatar imagepklab ( 2016-08-26 10:02:51 -0600 )edit

About exception...are you using debug DLLs? try to catch the exception and report it here. You might enclose your code with:

   your code here
catch (cv::Exception& ex) { //OpenCV Exception
    cerr << "OpenCV Error:\n" << ex.what();
    ret = ex.code;
catch (runtime_error& ex) {
    cerr << "Runtime Error:\n" << ex.what();
catch (...) {
    cerr << "Generic Error";
pklab gravatar imagepklab ( 2016-08-26 10:03:37 -0600 )edit

@pklab Thank you for update.

  • As you can see on bottom answer of, You might have a problem here.
  • And i can not catch it. I see it only when visual studio is stopping app.
Mikl gravatar imageMikl ( 2016-08-30 02:33:47 -0600 )edit

why you are using new ? If you are using C++ just declare objects like below

Mat _frame(frameSize,CV_8UC3,(void*)frame); //build a Mat over user allocated data
int codec=CV_FOURCC('M','J','P','G'); //select your codec or use -1
VideoWriter _recorder(name.toStdString(), codec, 20, frameSize, true);

Than grab from your camera into the frame memory buffer and write the _frame object (which is mapped over 'frame')

pklab gravatar imagepklab ( 2016-08-30 11:06:24 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-08-25 13:21:17 -0600

pklab gravatar image

updated 2016-08-26 10:50:09 -0600

1st of all, codecs must be available on your system at runtime and doesn't depends on OpenCV but depends on codec you have installed on your system. This means that the codec must be available on the machine that runs the OpenCV application.


OpenCV doesn't have a function to get the list of available codecs. To a get list of available codecs you need to call some system function. On Windows, OpenCV just calls the function that pup up the OS codec form.

The CV_FOURCC('M','J','P','G') codec should be supported natively by OpenCV (no need of external library)

BTW each codec follows some specs (frame size or ratio, fps, compression ...) thus if your app provides codec selection you have to manage codec specs too.

You might define a vector of supported codecs than test if they are available at start-up with multiple calls to VideoWriter::open and write. Something like this:

Size myFrameSize(320,240); //some codecs have limitations about frame size
Mat testImg(myFrameSize,CV_8UC3); //and frame depth

vector<int> availableCodecs;
vector<int> supportedCodec({
    others you can support ...

VideoWriter vw;
for(int i=0; i<supportedCodec.size(); i++) {"test.avi", supportedCodec[i], ...);
        if (vw.write(testImg))


after this, some points:

  • Use int codec=CV_FOURCC('M','R','L','E')for Microsoft RLE (see this This codec is native on Windows.
  • If FFMPEG is enabled in your OpenCV bin, using codec=0; fps=0; you can create an uncompressed (raw) video file. In this case you have to deploy the ffmpeg's DLL with your exe.
  • You can save image sequence using a proper filename (eg. img_%02d.jpg) and fourcc=0 OR fps=0. Use uncompressed image format (eg. img_%02d.BMP) to save raw frames.
  • not compressed... did you mean lossless ? In this case you might install and use some lossless codec like Huffman (HFYU), Lagarith (LAGS). You will have about 30% of compression without quality loss. Even in this case the codec must be available on target system.

I would suggest to install some 3rd party codec pack, (take a look also to ffdshow). This will install on your system a lot of codecs (e.g. XVID, X264, HFYU,...) that you can use with many applications including OpenCV or VirtualDub.

Remember that on Windows, cv::VideoWriter uses MJPG or VFW API (Video For Windows) thus MJPG codec or any available codec that is VFW compliant can be used with VideoWriter.

To check for installed Video Codecs on your system:

  • just run Windows Media Player, enable the menu bar than About > Technical Info
  • OR run MSINFO32.exe than components > Multimedia > Codec video
  • OR use OpenCV with codec = -1. VideoWriter pops up codec selection dialog from the system, the list include all available and compatible codecs.

Finally, get the 4 char code(fourcc) for your selected codec from http ... (more)

edit flag offensive delete link more


when i am using 'M','J','P','G' and streaming RGB, video file is empty. What can be a problem?

Mikl gravatar imageMikl ( 2016-08-30 04:51:38 -0600 )edit

Update your question with a bit of your code

pklab gravatar imagepklab ( 2016-08-30 06:39:25 -0600 )edit

Question Tools



Asked: 2016-08-24 07:45:14 -0600

Seen: 41,380 times

Last updated: Aug 30 '16