问题描述:音频进行aac编码后生成aac文件,使用命令 ffplay ./audio.aac
报如下的错误:
录制过程中的输出:
[aac @ 0x153047000] Trying to remove 512 more samples than there are in the queue
[aac @ 0x153047000] Trying to remove 512 more samples than there are in the queue
[aac @ 0x153047000] Trying to remove 512 more samples than there are in the queue
[aac @ 0x153047000] Trying to remove 512 more samples than there are in the queue
[aac @ 0x153047000] Trying to remove 512 more samples than there are in the queue
[aac @ 0x153047000] Trying to remove 512 more samples than there are in the queue
[aac @ 0x153047000] Qavg: 120.549
finish!
Process finished with exit code 0
播放过程中的输出
[aac @ 0x121104080] Format aac detected only with low score of 1, misdetection possible!
[aac @ 0x14180f7d0] Error decoding AAC frame header.
[aac @ 0x14180f7d0] More than one AAC RDB per ADTS frame is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[aac @ 0x14180f7d0] Assuming an incorrectly encoded 7.1 channel layout instead of a spec-compliant 7.1(wide) layout, use -strict 1 to decode according to the specification instead.
[aac @ 0x14180f7d0] Number of bands (7) exceeds limit (5).
[aac @ 0x14180f7d0] Multiple frames in a packet.
[aac @ 0x14180f7d0] channel element 3.0 is not allocated
[aac @ 0x14180f7d0] channel element 3.12 is not allocated
[aac @ 0x14180f7d0] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x14180f7d0] Inconsistent channel configuration.
[aac @ 0x14180f7d0] get_buffer() failed
[aac @ 0x14180f7d0] channel element 3.5 is not allocated
[aac @ 0x14180f7d0] channel element 1.1 is not allocated
[aac @ 0x14180f7d0] Reserved bit set.
[aac @ 0x14180f7d0] Number of bands (39) exceeds limit (32).
[aac @ 0x14180f7d0] Reserved bit set.
[aac @ 0x14180f7d0] Pulse data corrupt or invalid.
[aac @ 0x14180f7d0] channel element 2.8 is not allocated
[aac @ 0x14180f7d0] channel element 2.0 is not allocated
[aac @ 0x14180f7d0] Reserved bit set.
[aac @ 0x14180f7d0] Pulse tool not allowed in eight short sequence.
[aac @ 0x14180f7d0] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x14180f7d0] Inconsistent channel configuration.
[aac @ 0x14180f7d0] get_buffer() failed
[aac @ 0x14180f7d0] Number of scalefactor bands in group (15) exceeds limit (12).
[aac @ 0x14180f7d0] channel element 3.9 is not allocated
[aac @ 0x14180f7d0] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x14180f7d0] Inconsistent channel configuration.
[aac @ 0x14180f7d0] get_buffer() failed
[aac @ 0x14180f7d0] Gain control is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[aac @ 0x14180f7d0] channel element 3.14 is not allocated
[aac @ 0x14180f7d0] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x14180f7d0] Inconsistent channel configuration.
[aac @ 0x14180f7d0] get_buffer() failed
[aac @ 0x14180f7d0] channel element 3.3 is not allocated
[aac @ 0x14180f7d0] channel element 2.9 is not allocated
[aac @ 0x14180f7d0] Reserved bit set.
[aac @ 0x14180f7d0] Number of bands (55) exceeds limit (41).
[aac @ 0x14180f7d0] channel element 2.4 is not allocated
[aac @ 0x14180f7d0] channel element 3.9 is not allocated
[aac @ 0x121104080] Packet corrupt (stream = 0, dts = NOPTS).
[aac @ 0x14180f7d0] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x121104080] Estimating duration from bitrate, this may be inaccurate
[aac @ 0x121104080] Could not find codec parameters for stream 0 (Audio: aac (LTP), 7.1, fltp, 441 kb/s): unspecified sample rate
Consider increasing the value for the 'analyzeduration' (0) and 'probesize' (5000000) options
Input #0, aac, from './audio.aac':
Duration: 00:00:01.33, bitrate: 450 kb/s
Stream #0:0: Audio: aac (LTP), 7.1, fltp, 441 kb/s
Failed to open file './audio.aac' or configure filtergraph
我的尝试:
1. 不编码,仅仅采集声音数据,将声音数据进行重采样,参数设置为AV_CH_LAYOUT_MONO
, AV_SAMPLE_FMT_FLT
,48000
, 保存成pcm数据,使用命令ffplay -ar 48000 -ac 1 -f f32le ./audio.pcm
可以正常播放。
2. 我的ffmpeg的版本为4.0.6版本,我看老师的课程录制是在两年前,我就选择两年前的版本
3. 将编码器:codec_ctx->profile = FF_PROFILE_AAC_HE_V2
改为codec_ctx->profile = FF_PROFILE_AAC_LD;
还是无法打开aac。
4. ret = avcodec_receive_packet(ctx, pkt);
第一次 ret为-35, 我加了:
if(ret == -35){
av_usleep(10000);
continue;
}
还是无法打开aac
最后是我的代码:里面的设置的参数,是我在保证,重采样后的音频数据能正常播放的参数
static void encode(AVCodecContext *ctx,
AVFrame *frame,
AVPacket *pkt,
FILE *output){
int ret = 0;
//将数据送编码器
ret = avcodec_send_frame(ctx, frame);
//如果ret>=0说明数据设置成功
while(ret >= 0){
//获取编码后的音频数据,如果成功,需要重复获取,直到失败为止
ret = avcodec_receive_packet(ctx, pkt);
if(ret == -35){
av_usleep(10000);
continue;
}
if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){
return;
}else if( ret < 0){
printf("Error, encoding audio frame\n");
exit(-1);
}
//write file
fwrite(pkt->data, 1, pkt->size, output);
fflush(output);
}
return;
}
static AVCodecContext* open_coder(){
//打开编码器
//avcodec_find_encoder(AV_CODEC_ID_AAC);
// AVCodec *codec = avcodec_find_encoder_by_name("libfdk_aac");
AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
//创建 codec 上下文
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
codec_ctx->sample_fmt =AV_SAMPLE_FMT_FLT; //输入音频的采样大小
codec_ctx->channel_layout = AV_CH_LAYOUT_MONO; //输入音频的channel layout
codec_ctx->channels = 1; //输入音频 channel 个数
codec_ctx->sample_rate = 48000; //输入音频的采样率
codec_ctx->bit_rate = 0; //AAC_LC: 128K, AAC HE: 64K, AAC HE V2: 32K
codec_ctx->profile = FF_PROFILE_AAC_LD; //阅读 ffmpeg 代码
//打开编码器
if(avcodec_open2(codec_ctx, codec, NULL)<0){
//
return NULL;
}
return codec_ctx;
}
static
SwrContext* init_swr(){
SwrContext *swr_ctx = NULL;
//channel, number/
swr_ctx = swr_alloc_set_opts(NULL, //ctx
AV_CH_LAYOUT_MONO, //输出channel布局
AV_SAMPLE_FMT_FLT, //输出的采样格式
48000, //采样率
AV_CH_LAYOUT_MONO, //输入channel布局
AV_SAMPLE_FMT_FLT, //输入的采样格式
48000, //输入的采样率
0, NULL);
if(!swr_ctx){
}
if(swr_init(swr_ctx) < 0){
}
return swr_ctx;
}