开发者

Write data in cmd to memory instead of file, catch it with C#

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 redirec开发者_如何学Ct 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜