如何设置ffmpeg的管道输出?

10

我需要将ffmpeg的输出作为管道读取。

这是一个代码示例:

    public static void PipeTest()
    {
        Process proc = new Process();
        proc.StartInfo.FileName = Path.Combine(WorkingFolder, "ffmpeg");
        proc.StartInfo.Arguments = String.Format("$ ffmpeg -i input.mp3 pipe:1");
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardInput = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();

        FileStream baseStream = proc.StandardOutput.BaseStream as FileStream;
        byte[] audioData;
        int lastRead = 0;

        using (MemoryStream ms = new MemoryStream())
        {
            byte[] buffer = new byte[5000];
            do
            {
                lastRead = baseStream.Read(buffer, 0, buffer.Length);
                ms.Write(buffer, 0, lastRead);
            } while (lastRead > 0);

            audioData = ms.ToArray();
        }

        using(FileStream s = new FileStream(Path.Combine(WorkingFolder, "pipe_output_01.mp3"), FileMode.Create))
        {
            s.Write(audioData, 0, audioData.Length);
        }
    }

这是来自ffmpeg的日志,第一个文件已被读取:

输入 #0,mp3,来自 'norm.mp3': 元数据: 编码器:Lavf58.17.103 Duration: 00:01:36.22, start: 0.023021, bitrate: 128 kb/s Stream #0:0: Audio: mp3, 48000 Hz, stereo, fltp, 128 kb/s Metadata: encoder : Lavc58.27

然后进行管道操作:

[NULL @ 0x7fd58a001e00] 无法找到适合的输出格式'$' $:无效参数

如果我运行“-i input.mp3 pipe:1”,则日志为:

无法找到适合的输出格式'pipe:1' pipe:1:无效参数

如何设置正确的输出?ffmpeg怎么知道输出格式是什么?


1
ffmpeg是一个转换程序。你正在给它输入,但没有告诉它如何处理输入。你想从ffmpeg得到什么输出? - omajid
2个回答

12

每当您在ffmpeg中使用管道时,需要使用-f fmt参数以避免出现您看到的错误。

您可以通过键入ffmpeg -formats来获取可能格式的列表。

例如,如果要获取wav文件,请添加-f wav

在您的示例中,参数应为:

-i input.mp3 -f wav pipe:1

您可以将wav替换为flac或任何其他喜欢的音频格式。


2

我认为在"$ ffmpeg -i input.mp3 pipe:1"中有一个打字错误。如果你只想使用-i等选项调用ffmpeg,请省略$字符。 只需"ffmpeg -i input.mp3 pipe:1" 你已经在StartInfo.FileName中传递了主程序名称,因此你应该将其省略。尝试使用"-i input.mp3 pipe:1"作为你的Arguments


你以前用过ffmpeg吗? 你知道要怎么使用它吗?在命令行上运行 ffmpeg -i input.mp3 pipe:1也不起作用,所以在程序中这样调用它也不会起作用。你能找出实现你想要的ffmepg命令,并在命令行上运行成功后,在程序中调用它吗? - omajid
“-i input.mp3 pipe:1” 看起来现在有所帮助,但日志显示无法找到适合 'pipe:1' 的输出格式,pipe:1:无效参数。 - mr_blond
我使用它来转换文件,例如“$ ffmpeg -i input.mp3 output.wav”这样的命令。我想将文件从mp3转换为wav并将输出发送到管道中。但你说得对,我不知道如何告诉ffmpeg我想要转换它。 - mr_blond
你能否测试一下在命令行中运行 ffmepg -i input.mp3 -f wav pipe:1 | cat > output.wav 是否会生成一个可用的wav文件?如果可以,那么 -f wav 就是你需要的。 - omajid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接