Aakash Patel Aakash Patel - 1 year ago 254
C++ Question

OpenCV VideoWriter Framerate Issue

I'm attempting to record video from a 1080p webcam into a file. I held a timer up in the video and in every trial the timestamp reported by a video player (VLC is what I used) doesn't sync up with the time in the video. It's always off a few seconds (usually in-video timer is faster than player-reported time).

As seen below, I set up the C++ program to capture video in one thread, and record in another thread. This is working fine as my CPU usage is ~200% (possible max out?). I'm on a Macbook Air w/ OS X 10.8 @ 1.8 GHz Intel Core i7.

I've tried changing the framerate to 15fps and that results in very choppy/slow video. I've also tried setting

to a lower resolution and it results in slow video. It appears that 1080p @ 30fps results in good steady video, but it is still always plays faster than it's supposed to. I've also tried putting in
record << frame;
but it did not affect anything.

Any recommendations on how to make the video match up in time?


#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <boost/thread.hpp>

using namespace cv;

void captureFunc(Mat *frame, VideoCapture *capture){
// get a new frame from camera
(*capture) >> (*frame);

int main(int, char**)
VideoCapture capture(0); // open the default camera
if( !capture.isOpened() ) {
printf("Camera failed to open!\n");
return -1;

capture.set(CV_CAP_PROP_FPS,30); //set capture rate to 30fps
Mat frame;
capture >> frame; // get first frame for size

// initialize recording of video
VideoWriter record("test.avi", CV_FOURCC('D','I','V','X'), 30, frame.size(), true);
if( !record.isOpened() ) {
printf("VideoWriter failed to open!\n");
return -1;

boost::thread captureThread(captureFunc, &frame, &capture); //start capture thread

sleep(1); //just to make sure capture thread is ready

// add frame to recorded video
record << frame;

return 0;

Answer Source

I resolved my issue after a bit of debugging; it was an issue with VideoWriter being picky on the rate at which frames were fed to it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download