网站首页 博客 C#调用ffmpeg截取视频画面
需要用到的命名空间
using System.IO; using System.Diagnostics;
成员属性定义
private string ffmpeg = "D:/tools/ffmpeg.exe"; //ffmpeg的文件路径 private int captureclip = 3; //截取第几秒的画面作为缩略图,考虑到开头可能会有黑屏,所以默认为3 private string capturesize = "300*200"; //缩略图尺寸 private string sourcefile = "E:/video/test.mp4"; //视频文件路径 private string thumbnail = "E:/video/test_thumb.png"; //截图保存路径 private string logtext = ""; //日志文字内容
截取视频中的画面
System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = this.ffmpeg; p.StartInfo.Arguments = " -i " + this.sourcefile + " -y -f image2 -ss " + this.captureclip + " -t 0.001 -s " + this.capturesize + " " + this.thumbnail; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.Start(); p.WaitForExit(); p.Dispose(); if (File.Exists(this.thumbnail)) { this.logtext = "生成缩略图成功。"; //do something... } else { this.logtext = "生成缩略图失败。"; //do something... }