开发者

How do i pipe stdout/stderr in .NET?

开发者 https://www.devze.com 2022-12-24 02:27 出处:网络
I want to do something like this ffmpeg -i audio.mp3 -f flac - | oggenc2.exe - -o audio.ogg i know how to do ffmpeg -i audio.mp3 -f flac using the process class i开发者_StackOverflown .NET but how

I want to do something like this

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

i know how to do ffmpeg -i audio.mp3 -f flac using the process class i开发者_StackOverflown .NET but how do i pipe it to oggenc2?

Any example of how to do this (it doesnt need to be ffmpeg or oggenc2) would be fine.


OK, I wrote the following code to do this:

    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();

Hope, this helps.

0

精彩评论

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