我正在开发一个调用多个命令行应用程序以对某些视频文件进行后期处理的应用程序。
现在我正在尝试使用 Comskip从我的有线卡调谐器中识别视频录制中的商业广告。这运行得很好,但我在获取我需要的屏幕输出时遇到了问题。
String stdout = null;
using (var process = new Process())
{
var start = new ProcessStartInfo(comskip, cmdLine);
start.WindowStyle = ProcessWindowStyle.Normal;
start.CreateNoWindow = true;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
process.StartInfo = start;
process.Start();
process.WaitForExit();
stdout = process.StandardOutput.ReadToEnd();
}
我希望 stdout 能够像手动启动应用程序(下面的屏幕截图)一样抓取屏幕上显示的内容,这是应用程序正在做的事情的连续反馈,并且混合在输出中的是给出进度百分比的行,我想用它来更新进度条
但是运行上面的代码只会给我:
The commandline used was: "C:\Users\Chris\Google Drive\Tools\ComSkip\comskip.exe" "C:\Users\Chris\Desktop\ComSkip Tuning Files\Modern Family.wtv" "--ini=C:\Users\Chris\Desktop\ComSkip Tuning Files\comskip_ModernFamily.ini"
Setting ini file to C:\Users\Chris\Desktop\ComSkip Tuning Files\comskip_ModernFamily.ini as per commandline Using C:\Users\Chris\Desktop\ComSkip Tuning Files\comskip_ModernFamily.ini for initiation values.
我还尝试重定向 StandardError 流并获取 process.StandardError.ReadToEnd(); 但如果我使用这些选项运行,进程似乎挂起。
我是否遗漏了一些东西来捕捉我所希望的,或者这个应用程序的输出流是否有可能去到其他无法访问的地方?
请您参考如下方法:
您必须设置以下内容:
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.OutputDataReceived += new DataReceivedEventHandler(ReadOutput);
process.ErrorDataReceived += new DataReceivedEventHandler(ErrorOutput);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
并在 ReadOutput 和 ErrorOutput 中捕获输出
private static void ErrorOutput(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
stdout = "Error: " + e.Data;
}
}
private static void ReadOutput(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
stdout = e.Data;
}
}
