这里写自定义目录标题
1,FFmpeg使用fdk_aac库
windows下集成方式请看下面的文章:
ffmpeg集成fdk_aac (windows)
2,核心函数
(1) avcodec_find_encoder /*获取编码器对象*/
(2) //配置参数
context->sample_fmt = AV_SAMPLE_FMT_S16; //采样格式位深
context->sample_rate = 44100; //采样率
context->channel_layout = AV_CH_LAYOUT_STEREO; //通道数
context->channels = av_get_channel_layout_nb_channels(context->channel_layout);
context->profile = FF_PROFILE_AAC_LOW; //低复杂度规格
context->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
(3) av_samples_set_silence /*填充静音数据*/
3,源码
void gen_aac_silence()
{
AVPacket *enc_pkt;
AVFrame *frame;
int got_frame = 0;
int ret;
int frame_size_out_encode = 1024; //AAC sample
AVCodec *codec = NULL;
AVCodecContext *context = NULL;
codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (codec == NULL)
{
printf("Failed to find encoder!\n");
return;
}
context = avcodec_alloc_context3(codec);
context->sample_fmt = AV_SAMPLE_FMT_S16;
context->sample_rate = 44100;
context->channel_layout = AV_CH_LAYOUT_STEREO;
context->channels = av_get_channel_layout_nb_channels(context->channel_layout);
context->bit_rate = 64000;
context->profile = FF_PROFILE_AAC_LOW;
context->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
if ((ret = avcodec_open2(context, codec, NULL)) < 0) {
printf("Failed to open encoder!\n");
goto end;
}
frame = av_frame_alloc();
frame->nb_samples = frame_size_out_encode;
frame->channel_layout = context->channel_layout;
frame->channels = av_get_channel_layout_nb_channels(frame->channel_layout);
frame->format = context->sample_fmt;
frame->sample_rate = context->sample_rate;
av_frame_get_buffer(frame, 0);
av_samples_set_silence(frame->data, 0, frame->nb_samples, frame->channels, frame->format);
enc_pkt = av_packet_alloc();
av_init_packet(enc_pkt);
/* send the frame for encoding */
ret = avcodec_send_frame(context, frame);
if (ret < 0) {
goto end;
}
while (ret >= 0) {
ret = avcodec_receive_packet(context, enc_pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
return 0;
}
else if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n");
return -1;
}
int i = 0;
printf("packet[%d]", enc_pkt->size);
for (i; i < enc_pkt->size; i++) {
printf(" %02x", enc_pkt->data[i]);
}
printf("\n");
}
end:
av_packet_free(&enc_pkt);
av_frame_free(&frame);
avcodec_free_context(&context);
return;
}