我正在尝试从文件中读取视频(.mp4),并将其显示在窗体上的Imagebox上。当我对播放的总时间进行计时时,它实际上超过了实际的视频时长。例如,如果视频实际上是10秒,则播放12-13秒。这是为什么?我的视频的帧速率为31fps
我试图确保fps变量已经具有正确的帧率。
private void Play_Video(string filePath)
{
VideoCapture cameraCapture = new VideoCapture(filePath);
var stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
Mat m = new Mat();
cameraCapture.Read(m);
if (!m.IsEmpty)
{
imageBox1.Image = m;
double fps = cameraCapture.GetCaptureProperty(CapProp.Fps);
Thread.Sleep(1000 / Convert.ToInt32(fps));
}
else
{
break;
}
}
stopwatch.Stop();
double elapsed_time = stopwatch.ElapsedMilliseconds * 0.001;
// My elapsed_time here shows about 13 seconds, when the actual length of video is 10 seconds. Why?
}
新编辑: 我在单独的函数中更新了对帧的检索,以免在渲染过程中引起延迟,但仍有几秒钟的延迟。例如,30秒的视频播放32-33秒。我更新的代码:
private void Play_Video(List<Mat> frames, double fps, Emgu.CV.UI.ImageBox imageBox)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < frames.Count; i++)
{
imageBox.Image = frames[i];
Thread.Sleep(1000 / Convert.ToInt32(fps));
}
stopwatch.Stop();
double elapsed_time = stopwatch.ElapsedMilliseconds * 0.001;
}