开源项目地址:https://github.com/vladjerca/FFMpegSharp
首先需要在 web.config 或 app.config 中配置
<appSettings> <!-- FFMpegSharp 需要的参数 --> <add key="ffmpegRoot" value="D:\参考资料\C#\FFmpeg_Binary\ffmpeg-20190325-6e42021-win32and64-shared\" /> </appSettings>
代码:
using FFMpegSharp; using FFMpegSharp.FFMPEG; using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms; namespace UIForms { /// <summary> /// https://github.com/vladjerca/FFMpegSharp /// </summary> public partial class ffmpegTest01 : Form { public ffmpegTest01() { InitializeComponent(); } string _videoFileFullPath = @"D:\Workspace\TestVideo.mp4"; FFMpeg fpeg = new FFMpeg(); private void btnStart_Click(object sender, EventArgs e) { string inputFile = _videoFileFullPath; string outputFileFullPath = string.Format(@"D:\Workspace\TestVideo-mp4-thumb-{0}.png", DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")); FileInfo output = new FileInfo(outputFileFullPath); var video = VideoInfo.FromPath(_videoFileFullPath); Bitmap bitImg = fpeg.Snapshot( video, output, //size : new Size(400, 400), captureTime : TimeSpan.FromMinutes(1) ); if (bitImg != null) { bitImg.Save(outputFileFullPath, ImageFormat.Png); MessageBox.Show("save ok"); } MessageBox.Show("finished"); } private void btnStop_Click(object sender, EventArgs e) { } private void btnGetVideoInfo_Click(object sender, EventArgs e) { var video = new VideoInfo(_videoFileFullPath); string output = video.ToString(); MessageBox.Show(output); } } }
运行截图:
小结:这个工具抓图时没有想象中的那么快,据说 FFmpeg 参数的配置顺序也可能导致慢,详细的请看 https://www.cnblogs.com/wanghetao/p/3420788.html
谢谢浏览!