如何使用 libavcodec 对 AAC-LC、AAC-HE-V1、AAC-HE-V2 中的音频进行编码?

Har*_*rma 2 audio ffmpeg transcoding libavcodec libav

我正在尝试使用 libavcodec/ffmpeg API 对 AAC-LC、AAC-HE-V1、AAC-HE-V2 中的音频进行编码。

但是当我使用以下配置和 API 调用时。它显示“无效的 AAC 配置文件”。

AVCodecContext *encoder_ctx;
encoder_ctx->codec_id           =   AV_CODEC_ID_AAC;
encoder_ctx->sample_fmt         =   AV_SAMPLE_FMT_S16; 
encoder_ctx->profile            =   FF_PROFILE_AAC_HE;

encoder = avcodec_find_encoder(encoder_ctx->codec_id);
avcodec_open2(encoder_ctx, encoder, NULL);
Run Code Online (Sandbox Code Playgroud)

您能解释一下这有什么问题吗?

Har*_*rma 6

首先看一下这个 文档:

杜比数字:ac3

杜比数字+:eac3

MP2: libtwolame, mp2

Windows Media 音频 1:wmav1

Windows Media 音频 2:wmav2

LC-AAC:libfdk_aac、libfaac、aac、libvo_aacenc

HE-AAC:libfdk_aac、libaacplus

Vorbis:libvorbis、vorbis

MP3:libmp3lame、libshine

作品:libopus

从上面的阅读中您会清楚地知道,为了以 HE-AAC/HE-AAC-V2 编码音频,您必须使用 libfdk_aac 或 libaacplus。

我将解释如何使用 libfdk_aac 来做到这一点:

首先确保配置 ffmpeg 以及以下选项:

--enable-libfdk_aac --enable-nonfree
Run Code Online (Sandbox Code Playgroud)

现在构建 ffmpeg 并尝试运行以下命令并查看它是否有效:

ffmpeg -i <input file> -vcodec copy -acodec libfdk_aac -profile:a aac_he <output file>
Run Code Online (Sandbox Code Playgroud)

如果有效,则意味着 libav 已与 libfdk_aac 链接。

现在为了在代码中使用它:

使用以下说明打开编码器:

AVCodecContext *encoder_ctx;
encoder_ctx->codec_id           =   AV_CODEC_ID_AAC;
encoder_ctx->sample_fmt         =   AV_SAMPLE_FMT_S16; 
encoder_ctx->profile            =   FF_PROFILE_AAC_HE;

encoder = avcodec_find_encoder_by_name("libfdk_aac");
// if you still try to open it using avcodec_find_encoder it will open libfaac only.
avcodec_open2(encoder_ctx, encoder, NULL);
Run Code Online (Sandbox Code Playgroud)

开始吧,您已经打开了 libfdk_aac 编码器!您可以使用的配置文件如本源中 所示