views:

806

answers:

2

I run ffmpeg like this:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo(ffmpegPath, myParams);
p.Start();
p.WaitForExit();

... but the problem is that the console with ffmpeg pops up and disappears right away, so I can't get any feedback. I don't even know if the process ran correctly.

So how can I either:

  • Tell the console to stay opened

  • Retrieve in the C# what the console displayed

+6  A: 

What you need to do is capture the Standard Output stream:

p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
// instead of p.WaitForExit(), do
string q = "";
while ( ! p.HasExited ) {
    q += p.StandardOutput.ReadToEnd();
}

You may also need to do something similar with StandardError. You can then do what you wish with q.

It is a bit finicky, as I discovered in one of my questions

As Jon Skeet has pointed out, it is not smart performance-wise to use string concatenation like this; you should instead use a StringBuilder:

p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
// instead of p.WaitForExit(), do
StringBuilder q = new StringBuilder();
while ( ! p.HasExited ) {
    q.Append(p.StandardOutput.ReadToEnd());
}
string r = q.ToString();
Lucas Jones
Right basic stuff, but I wouldn't recommend building up the string like that. Use a StringBuilder :)
Jon Skeet
Thanks. :)
Lucas Jones
thanks, it looks good I'm going to try that right now
marcgg
actually there's an error: StandardOut has not been redirected or the process hasn't started yet. I'm trying to figure out what's going on
marcgg
Hmmm... I'm not sure about that.
Lucas Jones
If you still have a problem post your code to http://pastebin.com and I'll have a look.
Lucas Jones
I tried changing some stuff but it's still not working. Here's the full code: http://pastebin.com/m50f26695
marcgg
I'm supposed to use ffmpegPath to call ffmpeg, but "ffmpeg.exe" is also working for debug.
marcgg
apparently when I run the debuger the program ends up in the loop while(! p.HasExited ) with p.HasExited == true :\
marcgg
Ah, sorry for not making that clear - `p.StartInfo.RedirectStandardOutput = true` needs to go before the `p.Start()`.
Lucas Jones
Thanks. It fixed it... but now I have "The Process object must have the UseShellExecute property set to false in order to redirect IO streams." Is this something I have control over?
marcgg
ok, found it: p.StartInfo.UseShellExecute = false;
marcgg
I've edited and accepted your answer. If you could incorporate jon's answer that would be perfect :) But if you don't that was already helpful! thanks a lot
marcgg
I've added an example of using a StringBuilder at the bottom. :)
Lucas Jones
A: 

I know this question is old, but I'll add to it anyway.

If all you wish to do is display the output of a command line process, and you're spawning the process from a console window, you need only redirect the standard input (yes, I know it sounds wrong, but it works).

So:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo(ffmpegPath, myParams);
p.UseShellExecute = false;
p.RedirectStandardInput = true;
p.Start();
p.WaitForExit();

Would do just fine.

Michael Vasquez
You didn't add much to it, because that was already mentioned in the accepted answer.
Josh Stodola
No it wasn't.‮‮
Michael Vasquez