首页 > 解决方案 > 如何使用 ffmpeg 中的 libavcodec 将 opus 文件解码为 pcm 文件?

问题描述

我正在尝试使用 libavcodec 将 opus 解码为 pcm 文件。所以,我使用https://ffmpeg.org/doxygen/trunk/decode_audio_8c-example.html中的 ffmpeg 示例。我将 AV_CODEC_ID_MP2 更改为 AV_CODEC_ID_OPUS。但我收到错误消息。

codec = avcodec_find_decoder((AV_CODEC_ID_MP2);
codec = avcodec_find_decoder(AV_CODEC_ID_OPUS);

错误:

    codec ./decode_audio ./out.opus ./out.pcm                  
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.
    [opus @ 0x7ff361800000] Error parsing Opus packet header.

所以我尝试将作品 AV_CODEC_ID_OPUS 更改为 AV_CODEC_ID_MP3 并重试。

    codec ./decode_audio ./out.mp3 ./out.pcm            
    [mp3float @ 0x7fe564002000] Header missing
    Error submitting the packet to the decoder

为什么 ffmpeg 中的示例会出错?我该怎么办?

标签: audioffmpegopus

解决方案


你不能用这种方式解码作品。Mp3 数据包是自定界的,opus 不是。这意味着 opus 需要一个容器(通常是 ogg)。必须解析该容器以确定您可以解码的 opus 数据包的开始和结束。libavformat可用于从文件中读取 AVPackets。


推荐阅读