在用FFmpeg对音频进行编码的时候报如下错误:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
[aac @ 000001cfc2717200] more samples than frame size (avcodec_encode_audio2)

 

 

原因:我们编码器的 frame_size 比采集到的 frame->nb_samples 小:

官方源代码链接:http://ffmpeg.org/doxygen/trunk/encode_8c_source.html

int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
    AVPacket *avpkt,
    const AVFrame *frame,
    int *got_packet_ptr)
{

    // ...

     /* check for valid frame size */
    if (frame) {
        if (avctx->codec->capabilities & AV_CODEC_CAP_SMALL_LAST_FRAME) {
            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;
            }
        }
        else if (!(avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) {
            if (frame->nb_samples < avctx->frame_size &&
                !avctx->internal->last_audio_frame) {
                ret = pad_last_frame(avctx, &padded_frame, frame);
                if (ret < 0)
                    goto end;

                frame = padded_frame;
                avctx->internal->last_audio_frame = 1;
            }

            if (frame->nb_samples != avctx->frame_size) {
                av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);
                ret = AVERROR(EINVAL);
                goto end;
            }
        }
    }

    // ...
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄