Ask Your Question

How to make a good long video (capture) with VideoWriter ?

asked 2015-08-10 03:45:58 -0500

NikoM gravatar image

updated 2017-08-22 08:20:29 -0500


I want to capture my software window by using OpenCV. I found the VideoWriter class and use it to make video with screenshots taken each X msec. My software use Qt so I take a QPixmap each time, convert it to cv::Mat and add it into VideoWriter.

My problem: the video become big after only one minute and when the video is bigger than 2Go it cannot be read by video media player (Windows 7). Moreover, the video cannot be bigger than 4Go.

There is my initialize method:

cv::Size videoSize = cv::Size(_mainWindow->width(),_mainWindow->height());

int codec = cv::VideoWriter::fourcc('M', 'S', 'V', 'C'); // This codex works only with short video., 6, FPS-5, videoSize, true);

Can you help me to identify the problem? Is it the used codec?

Thanks in advance


Now I use DivX correctly I think but the video look like the release fail. I cannot open it with my player. There is my code: // Select codec int codec = cv::VideoWriter::fourcc('D', 'I', 'V', 'X');

// Create and open video file, codec, FPS, videoSize, true);

// Test main loop for(int j=0; j<100; j++) { takeScreenshot(writer); }

// Release video writer.release();

Have you an idea? Is it a codec problem?

Thank you for your help

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2015-08-10 05:47:16 -0500

pklab gravatar image

updated 2015-08-12 05:08:20 -0500

Short answer: better codec is the way.

You could use codec like DivX/mpeg

// High compression rate lossy image quality
int codec = cv::VideoWriter::fourcc('D', 'I', 'V', 'X');

or lossless codec like Lagarith or FFV1

// Lossless in quality with good processing performance
int codec = cv::VideoWriter::fourcc('L', 'A', 'G', 'S');

Finally you could try some codec designed for screen capture like TechSmith or iSCC or something else.

You could try to detect changes on the screen and save only modified frames but this is already done by codecs.

Some codecs has some limitations about frame size (odd,even, 4x,..) and/or frame format (RGB,YV2/12,...)

The codec need to be installed in your computer and in the computer where the resulting video will played. Finally you need to link the opencv_ffmpeg library and deploy the related opencv_ffmpeg DLL


Look at doc and have to use the selected codec at opening time:, cv::VideoWriter::fourcc('D', 'I', 'V', 'X'), FPS-5, videoSize, true);

Or, if you pass CV_FOURCC_PROMPT for the codec argument "...than a window will pop up at runtime that contains all the codec installed on your system and ask you to select the one to use...", CV_FOURCC_PROMPT, FPS-5, videoSize, true);
edit flag offensive delete link more


Hello, Thank you for your answer and your help. Unfortunately with theses codec, videos are always big (about 1Go for 35/40 sec). I try with DivX/mpeg, Lagarith and TechSmith for now. With this problem, videos cannot be long because the video cannot be bigger than 4Go. I really don’t understand why videos are the same size by using DivX or another codex without compression? Thanks in advance.

NikoM gravatar imageNikoM ( 2015-08-11 02:30:07 -0500 )edit

You are using always codec=6 while you have to use the selected codec...see updates in the answer if it solves your issue please vote and/or accept it

pklab gravatar imagepklab ( 2015-08-12 05:11:13 -0500 )edit

Hello, yes a see my mistake, yesterday, sorry. Now video look like to be less big than before but now the release seem to not work. When the video is done (and released) I cannot read it in Windows Media Player.(error C00D11B1). I will update my first message. Again, thank you a lot for your help!

NikoM gravatar imageNikoM ( 2015-08-14 02:29:22 -0500 )edit

That's more likely to be caused by not having installed the right codecs in your PC. Windows Media Player tries to decode the video, but if it can't find the right codec, it will crash. Download the K-Lite Codec Pack

LorenaGdL gravatar imageLorenaGdL ( 2015-08-14 02:41:08 -0500 )edit

I try to use CV_FOURCC_PROMPT, but the list isn't change after I installed K-Lite Codec Pack. Do you know why?

NikoM gravatar imageNikoM ( 2015-08-14 02:49:38 -0500 )edit

I don't know if that list should be updated, but probably you can now open your video con WMP. Anyway, I'd recommend you to use other player such as VLC or Media Player Classic (that's aside OpenCV)

LorenaGdL gravatar imageLorenaGdL ( 2015-08-14 02:53:20 -0500 )edit

Thank you for your answer but the video cannot be open by another player. But I get an error message: pin failed: File Source (Async.)::Output.. Moreover, the media type is unknow. For a DivX video, the extention is "avi" too, right ?

NikoM gravatar imageNikoM ( 2015-08-14 07:44:10 -0500 )edit

I download the DivX codec pack and the problem is always here. But I see that all my video do the same size (5.54 KB). So there is a problem during writing the video and not during the release.

NikoM gravatar imageNikoM ( 2015-08-14 08:21:16 -0500 )edit
  • Is your video file well done ? I mean if you can read it your video via cv::VideoCapture or some other player.
  • You could check info with mediainfo which is distributed with K-Lite Codec Pack Full
  • Check Video For Windows (FVW) vs DirectShow codecs using ffdshow
pklab gravatar imagepklab ( 2015-08-14 11:34:32 -0500 )edit

Hello, and thank you for your help.

  • I try to read this video with OpenCV and I get this error: "Context scratch buffers could not be allocated due to unknown size.".
  • With mediainfo, I don't see any error: this codec is DivX; the size is good...
  • I don't know how to use ffdshow. I will search that on the web.

For me, the video file isn't well done but I don't know why.

NikoM gravatar imageNikoM ( 2015-08-17 02:34:34 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-08-10 03:45:58 -0500

Seen: 10,727 times

Last updated: Aug 14 '15