FFmpeg - 如何获取打开 ALAC 编解码器所需的额外数据

FFmpeg - How do i get the extradata required to open the ALAC Codec?

本文关键字:编解码器 ALAC 数据 何获取 获取 FFmpeg      更新时间:2023-10-16

我想知道如何使用 FFmpeg 库从 ALAC 媒体文件中获取额外数据,而无需手动解析文件?

我最初设置:

avformat_open_input(&formatContext, pszFileName, 0, 0);
avformat_find_stream_info(formatContext, NULL);
av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
codecContext = avcodec_alloc_context3(codec);

目前,我能够检测并找到 ALAC 编解码器,但它无法打开返回AVERROR_INVALIDDATA的编解码器,该编解码器来自额外的数据并且extradata_size未设置。

avcodec_open2(codecContext, codec, NULL);

FFmpeg 文档指出,某些编解码器需要将额外的数据和extradata_size设置为编解码器的规范。但是这些数据不应该由avformat_find_stream_info设置吗?

是的,extradataavformat_open_input()avformat_find_stream_info()期间填充。但是,它会将其填充到您未使用的字段中。您需要以下代码:

avformat_open_input(&formatContext, pszFileName, 0, 0);
avformat_find_stream_info(formatContext, NULL);
int audioStreamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
codecContext = avcodec_alloc_context3(codec);
avcodec_copy_context(codecContext, formatContext->streams[audioStreamIndex]->codec);
avcodec_open2(codecContext, codec, NULL);

相关的额外行是 avcodec_copy_context() ,它将数据从libavformat解复用器(formatContext->streams[])复制到该上下文的副本(codecContext),您将使用该上下文的副本在 libavcodec 中使用解码器进行解码。