基本流程
1、获取编码器
2、设置编码前的结构
3、转码准备
4、编码处理
5、清理
获取编码器
//初始化
av_register_all();
//根据输出文件后缀,获取流的基本信息
avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_filename);
//打开输出文件
if (avio_open(&pFormatCtx->pb, out_filename, AVIO_FLAG_READ_WRITE) < 0)
{
printf("打开输出文件失败\n");
return;
}
//创建新的输出流到文件
audio_st = avformat_new_stream(pFormatCtx, 0);
if (audio_st == NULL)
{
printf("创建输出流失败\n");
return;
}
//找到AAC编码器
pCodec = avcodec_find_encoder(pFormatCtx->oformat->audio_codec);
if (!pCodec)
{
printf("没有找到编码器\n");
return;
}
//创建编码器上下文
pCodeCtx = avcodec_alloc_context3(pCodec);
pCodeCtx->codec_id = pFormatCtx->oformat->audio_codec;
pCodeCtx->codec_type = AVMEDIA_TYPE_AUDIO;
pCodeCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
pCodeCtx->sample_rate = nSampleRate; //采样率
pCodeCtx->channel_layout = channel_layout; //音频通道布局
pCodeCtx->channels = audio_channels; //声道数
//pCodeCtx->bit_rate = 64000;
//根据编码器上下文打开编码器
if (avcodec_open2(pCodeCtx, pCodec, NULL) < 0)
{
printf("无法打开编码器\n");
return;
}
//将AVCodecContext的成员复制到AVCodecParameters结构体,防止警告
avcodec_parameters_from_context

该博客详细介绍了使用ffmpeg将PCM音频编码为AAC的过程,包括获取编码器、设置编码结构、转码准备、编码处理和资源释放等步骤。特别强调了在编码时要根据PCM格式正确计算数据量,避免音频播放速度异常的问题。此外,还提及在语音聊天程序中如何通过UDP发送和接收编码后的音频数据。
最低0.47元/天 解锁文章
3595

被折叠的 条评论
为什么被折叠?



