在實際的項目中,從音頻設備采集到的音頻的類型和編碼器類型(aac ,amr)通常是不一致的。
那么我們首先需要做重采樣的過程。利用swr_convert 重新采樣。
這時候我們可能會遇到另外一個問題。就是在encode_audio的時候遇到
more samples than frame size (avcodec_encode_audio2) 的問題。
問題的原因在於 我們編碼器的frame_size 比采集到的fram->nb_samples小
- /* check for valid frame size */
- if (frame) {
- if (avctx->codec->capabilities & 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");
- return AVERROR(EINVAL);
- }
- } else if (!(avctx->codec->capabilities & 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)
- return ret;
- 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;
- }
- }
- }
解決的方案是:每次采集到的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即可