I'm making a program to work with some video files.
I'm using the ffmpeg executable to merge several files in a single file. This command takes several minutes to finish, so, I need a way to "monitor" the output, and show a progress bar on GUI.
Looking at the following stackoverflow topics:
- 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
I made this code:
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();
When I run this code, the ffmpeg start to merge files, I can see the ffmpeg process on Windows Task Manager, and if I wait long enough, the ffmpeg finish the job without any error. But, the Debug.WriteLine(e.Data)
is never called (no output on Debug window). Tried to change to Console.WriteLine
too (again, no output).
So, after this, I tried this another version:
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();
Again, the ffmpeg is started without any error, but the C# "hangs" on While (!ffmpeg.StandardOutput.EndOfStream)
until ffmpeg is finished.
If I execute the exact command on Windows prompt, a lot of output text is showed with progress of ffmpeg.