如何将PCM格式的原始音频采样数据编码为MP3格式或AAC格式的音频文件?
一.打开和关闭输入文件和输出文件以及判断输入文件是否读取完毕
//io_data.cpp
static FILE* input_file= nullptr;
static FILE* output_file= nullptr;
int32_t open_input_output_files(const char* input_name,const char* output_name){
if(strlen(input_name)==0||strlen(output_name)==0){
cout<<"Error:empty input or output file name."<<endl;
return -1;
}
close_input_output_files();
input_file=fopen(input_name,"rb");//rb:读取一个二进制文件,该文件必须存在
if(input_file==nullptr){
cerr<<"Error:failed to open input file."<<endl;
return -1;
}
output_file=fopen(output_name,"wb");//wb:打开或新建一个二进制文件,只允许写
if(output_file== nullptr){
cout<<"Error:failed to open output file."<<endl;
return -1;
}
return 0;
}
void close_input_output_files(){
if(input_file!= nullptr){
fclose(input_file);
input_file= nullptr;
}
if(output_file!= nullptr){
fclose(output_file);
output_file= nullptr;
}
}
int32_t end_of_input_file(){
return feof(input_file);
}
二.音频编码器的初始化
//audio_encoder_core.cpp
static const AVCodec* codec= nullptr;
static AVCodecContext* codec_ctx= nullptr;
static AVFrame* frame= nullptr;
static AVPacket* pkt= nullptr;
static enum AVCodecID audio_codec_id;
int32_t init_audio_encoder(const char* codec_name){
if(strcasecmp(codec_name,"MP3")==0){
audio_codec_id=AV_CODEC_ID_MP3;
cout<<"Select codec id:MP3"<<endl;
}
else if(strcasecmp(codec_name,"AAC")==0){
audio_codec_id=AV_CODEC_ID_AAC;
cout<<"Select codec id:AAC"<<endl;
}
else{
cerr<<"Error:invalid audio format."<<endl;
return -1;
}
codec=avcodec_find_encoder(audio_codec_id);
if(!codec){
cerr<<"Error:could not find codec."<<endl;
return -1;
}
codec_ctx=avcodec_alloc_context3(codec);
if(!codec_ctx){
cerr<<"Error:could not alloc codec_ctx."<<endl;
return -1;
}
//设置音频编码器的参数
codec_ctx->bit_rate=128000;
codec_ctx->sample_fmt=AV_SAMPLE_FMT_FLTP;
codec_ctx->sample_rate=44100;
codec_ctx->channel_layout=AV_CH_LAYOUT_STEREO;
codec_ctx->channels=2; int32_t result=avcodec_open2(codec_ctx,codec, nullptr);
if(result<0){
cerr<<"Error:could not open codec."<<endl;
return -1;
}
frame=av_frame_alloc();
if(!frame){
cerr<<"Error:could not alloc frame."<<endl;
return -1;
}
frame->nb_samples=codec_ctx->frame_size;//采样点数量
frame->format=codec_ctx->sample_fmt;
frame->channel_layout=codec_ctx->channel_layout;
result= av_frame_get_buffer(frame,0);
if(result<0){
cerr<<"Error:AVFrame could not get buffer."<<endl;
return -1;
}
pkt=av_packet_alloc();
if(!pkt){
cerr<<"Error:could not alloc packet."<<endl;
return -1;
}
return 0;
}
三.编码循环体
1.PCM文件的存储结构
音频采样格式可以分为packed和planar两类。以packed格式保存的采样数据,各声道间按照采样值交替存储;以planar格式保存的采样数据,各个采样值按照不同声道连续存储
下面以8bit为例展示planar和packed格式是如何保存音频采样数据的:
packed:
| 左声道0 | 右声道0 | 左声道1 | 右声道1 | 左声道2 | 右声道2 | 左声道3 | 右声道3 |
planar:
| 左声道0 | 左声道1 | 左声道2 | 左声道3 | 右声道0 | 右声道1 | 右声道2 | 右声道3 |
2.读取PCM音频采样数据
由于我们代码里设置了采样格式为fltp,即planar格式,而输入的PCM音频采样数据是packed格式的,因此我们需要将packed格式转化为planar格式进行保存:
//io_data.cpp
int32_t read_pcm_to_frame(AVFrame* frame,AVCodecContext* codec_ctx){
int data_size= av_get_bytes_per_sample(codec_ctx->sample_fmt);
if(data_size<0){
cerr<<"Error:Failed to calculate data size."<<endl;
return -1;
}
for(int i=0;i<frame->nb_samples;i++){
for(int ch=0;ch<codec_ctx->channels;ch++){
fread(frame->data[ch]+i*data_size,1,data_size,input_file);
}
}
return 0;
}
3.编码音频采样数据
//audio_encoder_core.cpp
static int32_t encode_frame(bool flushing){
int32_t result=0;
if(!flushing){
cout<<"Send frame to encoder with pts:"<<frame->pts<<endl;
}
result=avcodec_send_frame(codec_ctx,flushing? nullptr:frame);
if(result<0){
cerr<<"Error:avcodec_send_frame failed."<<endl;
return result;
}
while(result>=0){
result= avcodec_receive_packet(codec_ctx,pkt);
if(result==AVERROR(EAGAIN)||result==AVERROR_EOF){//尚未完成对新一帧的编码,要传入后续帧或编码器已完全输出内部缓存的码流
return 1;
}
else if(result<0){
cerr<<"Error:avcodec_receive_packet failed."<<endl;
return result;
}
if(flushing){
cout<<"Flushing:";
}
cout<<"Got encoded package with dts:"<<pkt->dts<<",pts:"<<pkt->pts<<", "<<endl;
write_pkt_to_file(pkt);
}
return 0;
}
4.写出码流数据
//io_data.cpp
void write_pkt_to_file(AVPacket* pkt){
fwrite(pkt->data,1,pkt->size,output_file);
}
5.实现编码循环
//audio_encoder_core.cpp
int32_t audio_encoding(){
int32_t result=0;
while(!end_of_input_file()){
result= read_pcm_to_frame(frame,codec_ctx);
if(result<0){
cerr<<"Error:read_pcm_to_frame failed."<<endl;
return -1;
}
result=encode_frame(false);
if(result<0){
cerr<<"Error:encode_frame failed."<<endl;
return result;
}
}
result=encode_frame(true);//刷新缓存区
if(result<0){
cerr<<"Error:flushing failed."<<endl;
return result;
}
return 0;
}
6.关闭编码器
//audio_encoder_core.cpp
void destroy_audio_encoder(){
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&codec_ctx);
}
7.最终main函数的实现如下:
int main(){
const char* input_file_name="../input.pcm";
const char* output_file_name="../output.mp3";
const char* codec_name="MP3";
int32_t result= open_input_output_files(input_file_name,output_file_name);
if(result<0){
return result;
}
result=init_audio_encoder(codec_name);
if(result<0){
return result;
}
result=audio_encoding();
if(result<0){
return result;
}
destroy_audio_encoder();
close_input_output_files();
return 0;
}
与视频文件类似,可以使用ffplay播放输出的.mp3文件来测试效果。
如何将PCM格式的原始音频采样数据编码为MP3格式或AAC格式的音频文件?的更多相关文章
- 视音频数据处理入门:PCM音频采样数据处理
===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...
- 视音频编解码学习工程:AAC格式分析器
=====================================================视音频编解码学习工程系列文章列表: 视音频编解码学习工程:H.264分析器 视音频编解码学习工 ...
- 视音频编解码学习工程:FLV封装格式分析器
===================================================== 视音频编解码学习工程系列文章列表: 视音频编解码学习工程:H.264分析器 视音频编解码学习 ...
- 音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系
音频采样中left-or right-justified(左对齐,右对齐), I2S时钟关系 原创 2014年02月11日 13:56:51 4951 0 0 刚刚过完春节,受假期综合症影响脑袋有点发 ...
- WebRTC 音频采样算法 附完整C++示例代码
之前有大概介绍了音频采样相关的思路,详情见<简洁明了的插值音频重采样算法例子 (附完整C代码)>. 音频方面的开源项目很多很多. 最知名的莫过于谷歌开源的WebRTC, 其中的音频模块就包 ...
- 视音频编解码学习工程:TS封装格式分析器
=====================================================视音频编解码学习工程系列文章列表: 视音频编解码学习工程:H.264分析器 视音频编解码学习工 ...
- ffmpeg 合并aac格式音频文件
1:连接到一起 'ffmpeg - i "concat:D:\learn\audio\1.aac|D:\learn\audio\2.aac" - acodec copy D:\le ...
- aac格式介绍
AAC编码后数据打包到FLV很简单. 1. FLV音频Tag格式 字节位置 意义0x08, ...
- aac格式解析
AAC格式有以下两种: ADIF:Audio Data Interchange Format 音频数据交换格式.这种格式的特征是可以确定的找到这个音频数据的开始,不需进行在音频数据流中间开始的解码,即 ...
- C#文字转换语音朗读或保存MP3、WAV等格式
最近遇到一个需求,需要把文字转换语音,参考很多大佬写的方法,最后经过自己改造实现文字在线朗读.保存MP3.WAV等格式. //需要引用System.Speech程序集 //引用using System ...
随机推荐
- Windows注册表中修改UAC(用户账号控制)及批处理脚
我在windows电脑上安装软件时,或者设置开机启动软件时,会先弹出提示框: 用户账户控制 你要允许此应用对你的设备进行更改吗? 很烦人,我不想每次都提醒.需要关闭"用户账户控制" ...
- [云计算&大数据]概念辨析:数据仓库 | 数据湖 | 数据中心 | 数据中台 | 数据平台 【待续】
今日客户对这些个概念不清楚,让我解释解释. 说实在的,虽然对各概念都有印象和理解,但我也不能完完全全地辨析得很清晰. 作为从业者,还是有必要拎清一点. 让一切业务数据化,一切数据业务化. 业务数据化 ...
- 扎实打牢数据结构算法根基,从此不怕算法面试系列之004 week01 02-04 使用泛型实现线性查找法
1.算法描述 在数组中逐个查找元素,即遍历. 2.上一篇文的实现结果 在 扎实打牢数据结构算法根基,从此不怕算法面试系列之003 week01 02-03 代码实现线性查找法中,我们实现了如下代码: ...
- Consistency Models终结扩散模型
最近看到一篇论文,觉得特别有意思,并且在学术界引起了不小的动静,他就是一致性模型,据说图像生成效果快.质量高,并且还可以实现零样本图像编辑,即不进行一些视觉任务训练,可以实现图像超分.修复.上色等功能 ...
- 当 SQL Server(mssql-jdbc) 遇上 BigDecimal → 精度丢失,真坑!
开心一刻 中午和哥们一起喝茶 哥们说道:晚上喝酒去啊 我:不去,我女朋友过生日 哥们瞪大眼睛看着我:你有病吧,充气的过什么生日 我生气到:有特么生产日期的好吧 需求背景 系统对接了外部系统,调用外部系 ...
- 自定义alert、confirm、prompt的vue组件
Prompt.vue组件 说明: 通过props定制定制的Prompt,可选值 mode 默认值:prompt, 其他模式:confirm.message(简单的提示,可设置提示显示时间,类似aler ...
- 带你简单了解Chatgpt背后的秘密:大语言模型所需要条件(数据算法算力)以及其当前阶段的缺点局限性
带你简单了解Chatgpt背后的秘密:大语言模型所需要条件(数据算法算力)以及其当前阶段的缺点局限性 1.什么是语言模型? 大家或多或少都听过 ChatGPT 是一个 LLMs,那 LLMs 是什么? ...
- 文心一言 VS chatgpt (11)-- 算法导论2.3 7题 3.1 4~5题
四.2 ^ (n+1)=O(2 ^ n)成立吗? 2^(2n)=O(2 ^ n)成立吗? 文心一言: chatgpt: 第一个式子不成立,第二个式子成立. 2 ^ (n+1) = O(2 ^ n) 该 ...
- 2023-03-29:如何高效计算三条线路选择方案?小A的旅行线路规划问题
2023-03-29:第一行有一个正整数n(3<=n<=100000),代表小A拟定的路线数量 第二行有n个正整数,第i个代表第i条路线的起始日期 第三行有n个正整数,第i个代表第i条路线 ...
- 2022-10-20:以下go语言代码输出什么?A:7;B:7.0;C:0;D:编译错误。 package main import ( “fmt“ ) func main() { const
2022-10-20:以下go语言代码输出什么?A:7:B:7.0:C:0:D:编译错误. package main import ( "fmt" ) func main() { ...