利用管道技术可以获取其他进程在命令行上的输出,即管道改向。有关内容请参考:
添加链接描述
对于一般的应用,上面的描述没有任何问题,也是正确的。但是对于FFmpeg 这个大咖而言上面的参考代码竟然无效!
直接进入主题:FFmpeg在错误时使用的是 StandardOutput 而正确时,使用竟然是StandardError!不知道自己是否理解有问题。
获取ffmpeg 正确输出的代码如下:
private void btnTest_Click(object sender, EventArgs e)
{
string mergeCmd = "ffmpeg.exe";
string mergeParm = "-y -i \"concat:K:\\temp\\39349807290087364_0.flv|K:\\temp\\39349807290087364_1.flv\" -i K:\\temp\\39349807290087364_2.mp4 -c:v copy -c:a aac -threads 10 -preset ultrafast -strict experimental MergedVideo.mp4";
ProcessStartInfo psi;
psi = new ProcessStartInfo(mergeCmd, mergeParm);
psi.UseShellExecute = false;
//psi.RedirectStandardOutput = true; //允许重定向标准输出
psi.RedirectStandardError = true;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
var p = Process.Start(psi);
//StreamReader myStreamReader = p.StandardError;
Read the standard output of the spawned process.
//string myString = myStreamReader.ReadLine();
//Console.WriteLine(myString);
//the following does not work well if the there is no error about the process
//p.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);
p.BeginErrorReadLine();
while (!p.HasExited)
{
Application.DoEvents();
txtInfo.Text = m_showInfo;// member variable ..
}
//if (p.ExitCode != 0)
//{
// txtInfo.Text += p.StandardError.ReadToEnd();
//}
p.Close();
}
其中:OnDataReceived 代码如下:
private void OnDataReceived(object Sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
//txtInfo.Text += e.Data;// will ocur an not the same instance error
m_showInfo = e.Data;//use a variable to store the infor ,
}
}
上面代码工作正常。
马拉孙 2021-03-17 于 泛五地区