使用fdk_aac生成一帧AAC静音数据

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;
}

4,结果打印

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值