Ask Your Question
0

VideoWriter: recorded video play too fast

asked 2019-11-04 09:42:46 -0600

bui3 gravatar image

updated 2019-11-04 10:12:55 -0600

Hello all, I'm new to OpenCv, computer vision in general and to this forum too (so...nice to meet you :-) ). I'm working on the most simple example of reading video from the cam of my laptop and store it in a file (well, to be honest, I apply a log polar transformation before). The problem is that the saved video plays too fast. Here's my basic code:

VideoCapture capture;
capture.open(0);
double fps = capture.get( CAP_PROP_FPS ); // get the fps of the camera, which is 30
VideoWriter writer;
Size size((int)capture.get(CAP_PROP_FRAME_WIDTH),(int)capture.get(CAP_PROP_FRAME_HEIGHT));
writer.open( "sample.avi", VideoWriter::fourcc('M','J','P','G'), fps, size);
Mat logpolar_frame, frame;
for(;;)
{
  capture >> frame;
  imshow("window1", frame);
  logPolar(frame, logpolar_frame,...);
  imshow("window2", logpolar_frame);
  writer.write(logpolar_frame);
  char c = waitKey(1000/fps); // here I tried lot of values, also fps and 1
  if ( c == 27 )
    break;
}
capture.release();
writer.release();

Do you have any idea? Thank you.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2019-11-04 12:28:24 -0600

mvuori gravatar image

Since you do the heavy transformation and also wait for keypresses (why?) you will get frames from the camera much slower than 30 fps. Perhaps you could stream to a file and then as second phase convert the file frame by frame. Or else, measure what the system can really do and use that as output fps.

edit flag offensive delete link more

Comments

Thank you. I wait for keypress to have a way to stop recording. I will try to skip transformation and make a simple recording. Well, this example is taken from a book (Learning OpenCV 3) and maybe the correct synchronization between read and write is out of the scope. Moreover, I will try also skipping the read frames to a window.

bui3 gravatar imagebui3 ( 2019-11-05 06:28:41 -0600 )edit
1

answered 2019-11-04 10:59:48 -0600

txstrong gravatar image

Try a lower value for fps on VideoWriter open.

edit flag offensive delete link more

Comments

I tried fps/2 and it works fine, but I can't explain why this happens.

bui3 gravatar imagebui3 ( 2019-11-05 06:40:22 -0600 )edit

You have a read fps, such as 30.0. Then you have a waitKey value. Unless you want jumping frames in your written video, lower the value to 1. You can still escape out of the program any time. It looks like you are waiting 1000/fps milliseconds between frames - too long. This means you are probably missing frames and so your written video looks like it is running very fast. You can tell be adding a simple counter for the number of frames. Then opening the written video to see how many frames are included. BTW, imshow takes time too. If read fps was 30.0, subtract a bit to find the write fps for smooth playing. Try 25, 20. I am not sure how long longPolar takes.

txstrong gravatar imagetxstrong ( 2019-11-06 04:04:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-11-04 09:42:46 -0600

Seen: 12,176 times

Last updated: Nov 04 '19