我如何在.NET中管道stdout / stderr?

时间:2010-03-23 17:01:09

标签: .net ipc stdout stderr

我想做这样的事情

ffmpeg -i audio.mp3 -f flac - | oggenc2.exe - -o audio.ogg

我知道如何在.NET中使用进程类ffmpeg -i audio.mp3 -f flac但是如何将它传递给oggenc2?

如何做到这一点的任何例子(它不需要是ffmpeg或oggenc2)都可以。

1 个答案:

答案 0 :(得分:4)

好的,我编写了以下代码来执行此操作:

    Process ffmpeg = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "ffmpeg.exe",
            Arguments = "-i audio.mp3 -f flac -",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            WindowStyle = ProcessWindowStyle.Hidden
        }
    };
    Process oggenc2 = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "oggenc2.exe",
            Arguments = "- -o audio.ogg",
            UseShellExecute = false,
            RedirectStandardInput = true,
            WindowStyle = ProcessWindowStyle.Hidden
        }
    };

    ffmpeg.Start();
    oggenc2.Start();

    int bufferLength = 4096;
    var input = oggenc2.StandardInput.BaseStream;
    var output = ffmpeg.StandardOutput.BaseStream;

    using (BinaryWriter writer = new BinaryWriter(input))
    {
        using (BinaryReader r = new BinaryReader(output))
        {
            byte[] buffer = new byte[bufferLength];
            int bytesRead;
            do
            {
                bytesRead = r.Read(buffer, 0, bufferLength);
                writer.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);
        }
    }

    ffmpeg.WaitForExit();

希望,这有帮助。