C#从视频文件的一部分中提取帧 [英] C# extract frames from part of a video file

查看:561
本文介绍了C#从视频文件的一部分中提取帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AForge ffmpeg包装器,您可以使用VideoFileReader类从视频中提取帧并将其另存为位图.

Using AForge ffmpeg wrapper you can extract frames from a video using the VideoFileReader class and save it as a bitmap.

参见以下示例: 提取.avi文件的帧

我的问题是,您无法指定从何处开始读取框架.它总是从视频文件的开头开始.

My problem with that is that you cannot specified where to start reading the frames. It always starts from the beginning of the video file.

但是,如果我想提取两个小时长的视频文件中间的帧,该怎么办?使用该类,您必须分析整个第一个小时的正当性,以获取这些帧.

But what if i wanted to extract frames that are in the middle of a two hours long video file. Using that class you'd have to parse the whole first hour juste to get to those frames.

有人知道实现这一目标的方法吗?

Does anyone know a way to achieve that?

推荐答案

如果您知道要在视频中开始阅读的位置,只需跳过适当的帧数即可;不需要处理任何一个.

If you know where in the video you want to start reading, just skip the appropriate number of frames; there's no need to process any of them.

当然,这假定您知道要开始读取的确切帧号,可以通过将帧率乘以执行提取的时间来计算.在您的示例中,如果视频长度为两个小时,并且您想从中间提取帧...

This assumes, of course, that you know the exact frame number you want to start reading from, which you can calculate by multiplying the framerate by the time at which you want to perform the extraction. In your example, if the video is two hours long and you want to extract frames from the middle...

VideoFileReader reader = new VideoFileReader();
reader.Open("file.avi");

// Jump to 1 hour into the video
int framesToSkip = reader.FrameRate * 3600;    // 1 hour = 3600 seconds
for (int i = 0; i < framesToSkip; i++)
    reader.ReadVideoFrame();

// Now the next time ReadVideoFrame() is called, we will get the frame at the 1 hour mark

这假定.FrameRate属性以每秒帧数为单位返回该值.不幸的是文档没有说,所以我不确定它如何处理带有非整数帧率(即29.97是常见帧率).

This assumes that the .FrameRate property returns the value in frames per second. Unfortunately the documentation doesn't say, so I'm not sure how it handles video files with non-integral framerates (i.e. 29.97 is a common framerate.)

这篇关于C#从视频文件的一部分中提取帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆