ffmpeg获取文件的总时长(mp3/mp4/flv等)

使用ffmpeg.exe获取文件属性信息,C#中可以在进程外异步调用这个工具,如下:

using (System.Diagnostics.Process pro = new System.Diagnostics.Process())
{
    pro.StartInfo.UseShellExecute = false;
    pro.StartInfo.ErrorDialog = false;
    pro.StartInfo.CreateNoWindow = true;
    pro.StartInfo.RedirectStandardError = true;
    pro.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "ffmpeg.exe";
    pro.StartInfo.Arguments = " -i " + fileName;
    pro.Start();
    System.IO.StreamReader errorreader = pro.StandardError;
    pro.WaitForExit(1000);
    string result = errorreader.ReadToEnd();
    if (!string.IsNullOrEmpty(result))
    {
        result = result.Substring(result.IndexOf("Duration: ") + ("Duration: ").Length, ("00:00:00").Length);
        duration = result;
    }
}

关于运行程序隐藏窗口的问题:

1、首先CreateNoWindow只对那些命令行程序有效。如果:cmd.exe。 (NoWindow理解成非消息循环程序可能更恰当)

2、如果要将CreateNoWindow设为true,那UseShellExecute必须为:false才有效。

3、想要使WindowStyle有效,UseShellExecute必须为:true。WindowStyle只对有UI界面的程序有效。(UseShellExecue默认为:true)

4、上述结论不是对所有程序有效,比如:calc.exe(你怎么就选中它来测试呢...),把calc.exe换成notepad.exe或Iexplore.exe或自己写个WinForm程序什么的就可以看到效果了。

5、瞎猜的。 UseShellExecute为true时可能是调用了WndMain,而那些不听话的程序都是因为没对第四个参数:ShowCmd做处理?

6、对于计算器这类不听话的家伙,好象只能System.Threading.Thread.Sleep(xxx)后ShowWindow了。但延迟多少时间是个问题,少了没用,多了界面会弹出来一会才隐藏。

原文地址:https://www.cnblogs.com/EasonJim/p/6255737.html