日期:2014-05-17 浏览次数:20416 次
/**/
/// <summary>
/// 将视频文件转换
/// </summary>
/// <param name="sourceFile">要转换的文件</param>
/// <returns></returns>
public bool Convert(string sourceFile, string qscale, string suffix, VideoType type)
{
try
{
System.Diagnostics.Process p = new System.Diagnostics.Process();//建立外部调用线程
p.StartInfo.FileName = "C:\\ffmpeg.exe";//要调用外部程序的绝对路径
p.StartInfo.Arguments = "";//参数(这里就是FFMPEG的参数了)
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
p.StartInfo.CreateNoWindow = false;//不创建进程窗口
p.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
//string allErrorMessage = ?? 我在这里如何得到Output 里面的所有 ouput.Data 的值呢
//也就相当于在这里取得那个ErrorDataReceived的所有信息
//谢谢
return true;
}
catch (Exception exp)
{
throw exp;
}
}
private void Output(object sendProcess, System.Diagnostics.DataReceivedEventArgs output)
{
if (!String.IsNullOrEmpty(output.Data))
{
//处理方法...
string message = output.Data;
Console.WriteLine(message);
}
}