Antonio Sesto
Antonio Sesto

Reputation: 3154

Simple video playback in OpenCV, C++

I would like to ask you a question on how to decode and display a video with the OpenCV library in C++. More in details, I need to understand how to set up correctly the delay between two frames.

I have a .mp4 video with 25 frames per second.

I grab and display the frames using the usual cycle:

while (something) {
  ...
  capture->read(f);
  imshow("video_title", f);
  waitKey(delay)
}

where capture is a pointer to a VideoCapture object, f is a Mat object.

I am having problems in setting the value of the delay variable.

Ideally, delay should be set to 1000/fps.

In my case 1000/25 = 40. However, if I set delay to 40ms the video playback is slower than it should.

I can obviously modify the delay value. If I use 30ms the playback is still too slow. If I use 20ms it is faster than it should.

Since there are no intermediate processing steps, this case should be very easy to manage.

So:

With 'correct speed' I mean the frame rate that keeps synchronized the audio and the video.

Upvotes: 0

Views: 1912

Answers (1)

b_m
b_m

Reputation: 1533

Obviously the read and imshow commands take some time to execute, hence the playback is slower. I'd measure the elapsed time from the previous imshow, say 5ms, and wait only 40-5=35 ms.

Upvotes: 1

Related Questions