【发布时间】:2017-08-15 02:17:22
【问题描述】:
我正在制作一个程序来处理一些视频文件。
我正在使用 ffmpeg 可执行文件将多个文件合并到一个文件中。 这个命令需要几分钟才能完成,所以,我需要一种方法来“监控”输出,并在 GUI 上显示一个进度条。
查看以下 stackoverflow 主题:
- How to parse command line output from c#?
- Process.start: how to get the output?
- How To: Execute command line in C#, get STD OUT results
我编写了这段代码:
Process ffmpeg = new Process
{
StartInfo =
{
FileName = @"d:\tmp\ffmpeg.exe",
Arguments = "-f concat -safe 0 -i __sync.txt -c copy output.mp4",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
WorkingDirectory = @"d:\tmp"
}
}
ffmpeg.EnableRaisingEvents = true;
ffmpeg.OutputDataReceived += (s, e) => Debug.WriteLine(e.Data);
ffmpeg.ErrorDataReceived += (s, e) => Debug.WriteLine($@"Error: {e.Data}");
ffmpeg.Start();
ffmpeg.BeginOutputReadLine();
ffmpeg.WaitForExit();
当我运行此代码时,ffmpeg 开始合并文件,我可以在 Windows 任务管理器中看到 ffmpeg 进程,如果我等待足够长的时间,ffmpeg 会完成工作而不会出现任何错误。但是,Debug.WriteLine(e.Data) 永远不会被调用(调试窗口上没有输出)。也尝试更改为Console.WriteLine(同样,没有输出)。
所以,在此之后,我尝试了另一个版本:
Process ffmpeg = new Process
{
StartInfo =
{
FileName = @"d:\tmp\ffmpeg.exe",
Arguments = "-f concat -safe 0 -i __sync.txt -c copy output.mp4",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
WorkingDirectory = @"d:\tmp"
}
}
ffmpeg.Start();
while (!ffmpeg.StandardOutput.EndOfStream)
{
var line = ffmpeg.StandardOutput.ReadLine();
System.Diagnostics.Debug.WriteLine(line);
Console.WriteLine(line);
}
ffmpeg.WaitForExit();
同样,ffmpeg 启动时没有任何错误,但 C# 在 While (!ffmpeg.StandardOutput.EndOfStream) 上“挂起”,直到 ffmpeg 完成。
如果我在 Windows 提示符下执行确切的命令,会显示很多输出文本以及 ffmpeg 的进度。
【问题讨论】: