网站首页 博客 C#调用ffmpeg获取视频,音频的时长
成员属性定义
private string ffmpeg = "D:/tools/ffmpeg.exe"; //ffmpeg路径 private string logtext = ""; //日志文字 private string curr_duration = ""; //视频时长,格式00:08:50 private string transit_fullpath = "E:/video/test.mp4"; //原始文件
获取视频、音频的时长
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = this.ffmpeg;
p.StartInfo.Arguments = " -i " + this.transit_fullpath;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardError = true;
p.Start();
StreamReader errorreader = p.StandardError;
p.WaitForExit(1000);
string result = errorreader.ReadToEnd();
try
{
this.curr_duration = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length);
this.logtext = "成功。" + this.curr_duration + "\r\n";
//do something
}
catch (Exception ex)
{
this.curr_duration = "";
this.logtext = "失败。"+ ex.Message +"\r\n";
//do something
}