前提:
需要使用到执行程序(需要自己下载,哪个版本都行):ffmpeg.exe,可以点我获取下载地址,或者点我直接下载
/// <summary>
/// 获取视频时长
/// </summary>
/// <param name="sourceFile">视频地址</param>
/// <param name="ffmpegfile">ffmpeg存放文件夹地址</param>
/// <returns></returns>
public static string GetVideoDuration(string sourceFile,string ffmpegfile)
{
using (System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process())
{
String duration;
String result;
StreamReader errorreader;
ffmpeg.StartInfo.UseShellExecute = false;
//ffmpeg.StartInfo.ErrorDialog = false;
ffmpeg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.StartInfo.FileName = ffmpegfile;
ffmpeg.StartInfo.Arguments = "-i " + sourceFile;
ffmpeg.StartInfo.CreateNoWindow = true;// 不显示程序窗口
ffmpeg.Start();
errorreader = ffmpeg.StandardError;
ffmpeg.WaitForExit();
result = errorreader.ReadToEnd();
duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length);
return duration;
}
}