【问题标题】:How do I set ffmpeg pipe output?如何设置 ffmpeg 管道输出?
【发布时间】:2019-02-17 14:35:30
【问题描述】:

我需要将 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 时长:00:01:36.22,开始:0.023021,比特率:128 kb/s 流 #0:0:音频:mp3,48000 Hz,立体声,fltp,128 kb/s 元数据: 编码器:Lavc58.27

然后管道:

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

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

无法为 'pipe:1' pipe:1 找到合适的输出格式:无效 论据

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

【问题讨论】:

  • ffmpeg 是一个转换程序。你给它一个输入,但没有告诉它如何处理输入。你想要什么作为 ffmpeg 的输出?

标签: c# audio ffmpeg .net-core pipe


【解决方案1】:

当你在 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 命令来做你想做的事,然后在它从命令行运行后在你的程序中使用它吗?
    • "-i input.mp3 pipe:1" 似乎有点帮助,现在日志无法为 'pipe:1' pipe:1 找到合适的输出格式:参数无效
    • 我用它来转换带有“$ ffmpeg -i input.mp3 output.wav”之类的命令的文件。我想将文件从 mp3 转换为 wav 并将输出发送到管道中。但你是对的,我不知道如何告诉 ffmpeg 我要转换它。
    • 你能测试一下命令行上的ffmepg -i input.mp3 -f wav pipe:1 | cat > output.wav 是否会生成一个工作的wav 文件吗?如果是这样,那么 -f wav 就是你想要的。
    猜你喜欢
    • 2019-07-26
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2016-02-13
    • 1970-01-01
    • 2014-04-05
    相关资源
    最近更新 更多