简体   繁体   中英

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:

  • what am I doing wrong?
  • can you suggest a method to set up correctly the delay in order to have a video played at the correct speed?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM