views:

102

answers:

2

Hi,

For a C# project I'm experimenting with ffmpeg to extract a .wav file from a video file (in Windows). You could do this by running it on the command line like this: "ffmpeg -i inputvid.avi + 'extra parameters' + extracted.wav". This obviously extracts the audio from the input .avi file to a specified .wav file.

Now, I can easily run this command in C# so it creates the desired .wav file. However, I don't need this wav file to stay on the harddisk. For performance reasons it would be much better if ffmpeg could save this file temporarily to the memory, which can than be used in my C# program. After execution of my program the .wav file is no longer needed.

So the actual question is: can I redirect the outputfile from a program to the memory? And if this is possible, how can I read this in C#?

I know it's a long shot and I doubt it very much if it's possible, but I can always ask...

Thanks in advance.

+6  A: 

Instead of specifying the output filename on the ffmpeg command line, use '-'. The '-' tells ffmpeg to send the output to stdout. Note that you might then have to manually specify your output format in the command line because ffmpeg can no longer derive it from the filename (the '-f' switch might be what you need for this).

Once you have that command line, refer to any number of places for help in reading stdout into your C# program.

ladenedge
A: 

Awesome, sounds so easy!

I still have a problem though, I use the following code now:

    System.Diagnostics.ProcessStartInfo psi =
    new System.Diagnostics.ProcessStartInfo(@"ffmpeg.exe");
    psi.Arguments = @"-i movie.avi -vn -acodec pcm_s16le -ar 44100 -ac 1 -f wav -";
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;
    Process proc = Process.Start(psi);
    System.IO.StreamReader myOutput = proc.StandardOutput;
    proc.WaitForExit();
    string output;
    if (proc.HasExited)
    {
        output = myOutput.ReadToEnd();
    }
    MessageBox.Show("Done!");

When I execute this now, the black cmd box just pops up and keeps blinking at me. When I open task manager it shows that it isn't doing anything.

If, however, I set the RedirectStandardOutput property to false, it shows the proper output in the cmd screen.

To cut it short: ffmpeg does redirect it's output now, but I can't read it with my C# code.

Mee
You have a deadlock. The [MSDN docs on RedirectStandardOutput](http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx) explain the interdependency in detail, but the upshot is that you need to clear the stream (using ReadToEnd) *before* waiting for the process to complete (using WaitForExit).
ladenedge
Thanks, it works now!
Mee