開源項目地址: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
謝謝瀏覽!