Home | Blog | GitHub

How to read FFmpeg output in CSharp

There seems to be significant confusion in the C# community regarding spawning a FFmpeg process within a GUI app (e.g., developed with the Avalonia framework) and capturing its standard output. The following articles are top-ranked on Google but do not provide a straightforward solution:

The trick is to read from stderr (!!!!) after setting the redirection flags and do this in a separate process to not block the GUI:

Process process = new Process();
process.StartInfo.FileName = "ffmpeg";             // or "ffmpeg.exe" if on MS Windows
process.StartInfo.Arguments = args;                // ffmpeg args go here
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();

Task.Run(() => {
	// read all text at once and write it to a file
	string output = process.StandardError.ReadToEnd();
	File.WriteAllText("ffmpeg-output.txt", output);
});

If needed, it is simple to modify the above snippet so that the output is read in a line-by-line fashion.