FFmpeg问题:more samples than frame size

1、问题描述:

写文件的时候,编码器的 frame_size 比 输入帧的 nb_samples 小,导致如下图所示问题

img

2、尝试解决

  1. (失败)显示修改 编码器的 frame_size 属性,失败原因:打开编码器(即调用avcodec_open2())时,会自动重置编码器frame_size属性。
  2. 根据第一步的尝试,去查看源码,发现 frame_size 属性注释如下,简单说明:即编码器的 frame_size 属性应该由 avcodec_open2() 自己设置, 用户提交给编码器的 帧的帧长必须是 frame_size 的采样点数。 但当 AV_CODEC_CAP_VARIABLE_FRAME_SIZE 设置时, frame_size 可以不受限制(该字段表示编码器输入样本数量可变)。
    /* The following data should not be initialized. */
    /**
     * Number of samples per channel in an audio frame.
     *
     * - encoding: set by libavcodec in avcodec_open2(). Each submitted frame
     *   except the last must contain exactly frame_size samples per channel.
     *   May be 0 when the codec has AV_CODEC_CAP_VARIABLE_FRAME_SIZE set, then the
     *   frame size is not restricted.
     * - decoding: may be set by some decoders to indicate constant frame size
     */
    int frame_size;
  1. 根据2,尝试设置 AV_CODEC_CAP_VARIABLE_FRAME_SIZE ,设置方式如下:
audioCodec->capabilities = AV_CODEC_CAP_VARIABLE_FRAME_SIZE;
  1. 问题解决

3、解决方法

方法一: 设置 编码器 capabilities 属性 为 AV_CODEC_CAP_VARIABLE_FRAME_SIZE,如下
audioCodec->capabilities = AV_CODEC_CAP_VARIABLE_FRAME_SIZE;
方法二(未尝试):申请一个缓冲区,将输入帧送入缓冲区,然后根据 编码器的 frame_size 属性从缓冲区拿数据即可。
posted @ 2022-10-26 10:56  小超不挑食  阅读(498)  评论(0编辑  收藏  举报