FFMPEG more samples than frame size (avcodec_encode_audio2) 的解决方案

在实际的项目中,从音频设备采集到的音频的类型和编码器类型(aac ,amr)通常是不一致的。

那么我们首先需要做重采样的过程。利用swr_convert 重新采样。

 

这时候我们可能会遇到另外一个问题。就是在encode_audio的时候遇到

more samples than frame size (avcodec_encode_audio2)  的问题。

 

问题的原因在于 我们编码器的frame_size 比采集到的fram->nb_samples小

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. /* check for valid frame size */  
  2.     if (frame) {  
  3.         if (avctx->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {  
  4.             if (frame->nb_samples > avctx->frame_size) {  
  5.                 av_log(avctx, AV_LOG_ERROR, "more samples than frame size (avcodec_encode_audio2)\n");  
  6.                 return AVERROR(EINVAL);  
  7.             }  
  8.         } else if (!(avctx->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)) {  
  9.             if (frame->nb_samples < avctx->frame_size &&  
  10.                 !avctx->internal->last_audio_frame) {  
  11.                 ret = pad_last_frame(avctx, &padded_frame, frame);  
  12.                 if (ret < 0)  
  13.                     return ret;  
  14.   
  15.                 frame = padded_frame;  
  16.                 avctx->internal->last_audio_frame = 1;  
  17.             }  
  18.   
  19.             if (frame->nb_samples != avctx->frame_size) {  
  20.                 av_log(avctx, AV_LOG_ERROR, "nb_samples (%d) != frame_size (%d) (avcodec_encode_audio2)\n", frame->nb_samples, avctx->frame_size);  
  21.                 ret = AVERROR(EINVAL);  
  22.                 goto end;  
  23.             }  
  24.         }  
  25.     }  


解决的方案是:每次采集到的frame数据保存到一个AUDIO FIFO中,每次等凑齐刚好frame_size大小的数据(请注意这里是刚好,网上有人说拆分AVframe,本人在ffmpeg2.1用这个办法没办法解决),接着把这些数据一口气encode。

 

如果有剩余的数据就存储到下一次encode。可以使用audio_fifo_来操作数据

http://www.ffmpeg.org/doxygen/1.0/audio__fifo_8c-source.html

 

http://blog.csdn.net/zsc09_leaf/article/details/16858193

参考transcode_aac.c即可

转载于:https://www.cnblogs.com/youngt/p/3771977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值