简体   繁体   English

OpenCV,C ++中的简单视频播放

[英]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++. 我想问一个问题,如何使用C ++中的OpenCV库解码和显示视频。 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. 我有一个每秒25帧的.mp4视频。

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. 其中capture是指向VideoCapture对象的指针, fMat对象。

I am having problems in setting the value of the delay variable. 我在设置delay变量的值时遇到问题。

Ideally, delay should be set to 1000/fps. 理想情况下, 延迟应设置为1000 / fps。

In my case 1000/25 = 40. However, if I set delay to 40ms the video playback is slower than it should. 在我的情况下,1000/25 =40。但是,如果我将延迟设置为40ms,则视频播放会比应有的慢。

I can obviously modify the delay value. 我显然可以修改延迟值。 If I use 30ms the playback is still too slow. 如果使用30毫秒,则播放速度仍然太慢。 If I use 20ms it is faster than it should. 如果我使用20毫秒,它将比应该的更快。

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. 显然,read和imshow命令需要一些时间才能执行,因此播放速度较慢。 I'd measure the elapsed time from the previous imshow, say 5ms, and wait only 40-5=35 ms. 我会测量上一次显示的经过时间,例如5ms,然后仅等待40-5 = 35 ms。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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