1.背景
音频aac编码的时候,调用函数接口avcodec_encode_audio2)提示错误信息:[aac @ 0x7f7bb40184a0] more samples than frame size (avcodec_encode_audio2)
2.原因
代码块如下:
if (frame->nb_samples > avctx->frame_size) {
av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");
ret = AVERROR(EINVAL);
goto end;
}
解析:
frame->nb_samples:实际编码时音频采样数的大小;
avctx->frame_size:预设定的音频编码采样率;
3.解决的方案
根据原因可知道,是喂给编码器的每帧采样数和预设定的音频编码采样率不一致导致的,解决方法就是让他们达到一致即可。
方法:将每次采集到的音频frame数据保存到一个AUDIO FIFO中,每次凑齐到frame_size大小的数据时,把这些数据喂给音频编码器即可。
2021.05.25记:FFMPEG官方已提transcode_acc.c进行参考