int pcm2aac()
{
AVFormatContext* pFormatCtx;
AVOutputFormat* fmt;
AVStream* audio_st;//音頻流
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
uint8_t* frame_buf;
AVFrame* frame;
int size;
FILE *in_file = fopen("tdjm.pcm", "rb"); //音頻PCM采樣數(shù)據(jù)
int framenum=1000; //音頻幀數(shù)
const char* out_file = "tdjm.aac"; //輸出文件路徑
av_register_all();
//方法1.組合使用幾個函數(shù)
pFormatCtx = avformat_alloc_context();
//猜格式
fmt = av_guess_format(NULL, out_file, NULL);
pFormatCtx->oformat = fmt;
//方法2.更加自動化一些
//avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
//fmt = pFormatCtx->oformat;
//注意輸出路徑
if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0)
{
printf("輸出文件打開失斍狻椒舵!\n");
return -1;
}
//創(chuàng)建音頻流
audio_st = avformat_new_stream(pFormatCtx,NULL);
//audio_st = av_new_stream(pFormatCtx, 0);
if (audio_st==NULL){
return -1;
}
pCodecCtx = audio_st->codec;
pCodecCtx->codec_id = fmt->audio_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_AUDIO;
pCodecCtx->sample_fmt = AV_SAMPLE_FMT_S16;
pCodecCtx->sample_rate= 44100;
pCodecCtx->channel_layout=AV_CH_LAYOUT_STEREO;
pCodecCtx->channels = av_get_channel_layout_nb_channels(pCodecCtx->channel_layout);
pCodecCtx->bit_rate = 64000;
//輸出格式信息
av_dump_format(pFormatCtx, 0, out_file, 1);
pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec)
{
printf("沒有找到合適的編碼器!\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0)
{
printf("編碼器打開失敹匾觥!\n");
return -1;
}
frame = av_frame_alloc();
//frame = avcodec_alloc_frame();
frame->nb_samples= pCodecCtx->frame_size;
frame->format= pCodecCtx->sample_fmt;
size = av_samples_get_buffer_size(NULL, pCodecCtx->channels,pCodecCtx->frame_size,pCodecCtx->sample_fmt, 1);
frame_buf = (uint8_t *)av_malloc(size);
avcodec_fill_audio_frame(frame, pCodecCtx->channels, pCodecCtx->sample_fmt,(const uint8_t*)frame_buf, size, 1);
//寫文件頭
avformat_write_header(pFormatCtx,NULL);
AVPacket pkt;
av_new_packet(&pkt,size);
for (int i=0; i<framenum; i++){
//讀入PCM
if (fread(frame_buf, 1, size, in_file) < 0)
{
printf("文件讀取錯誤耙旦!\n");
return -1;
}else if(feof(in_file)){
break;
}
frame->data[0] = frame_buf; //采樣信號
frame->pts=i*100;
int got_frame=0;
//編碼
int ret = avcodec_encode_audio2(pCodecCtx, &pkt,frame, &got_frame);
if(ret < 0)
{
printf("編碼錯誤栖榨!\n");
return -1;
}
if (got_frame==1)
{
printf("編碼成功第%d幀!\n",i);
pkt.stream_index = audio_st->index;
ret = av_write_frame(pFormatCtx, &pkt);
av_free_packet(&pkt);
}
}
//寫文件尾
av_write_trailer(pFormatCtx);
//清理
if (audio_st)
{
avcodec_close(audio_st->codec);
av_free(frame);
av_free(frame_buf);
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
fclose(in_file);
return 0;
}
//https://blog.csdn.net/leixiaohua1020/article/details/84598124
FFmpeg4.2.9 PCM編碼AAC
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門令蛉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來聚霜,“玉大人,你說我怎么就攤上這事珠叔⌒睿” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵祷安,是天一觀的道長姥芥。 經(jīng)常有香客問我,道長汇鞭,這世上最難降的妖魔是什么凉唐? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮霍骄,結(jié)果婚禮上台囱,老公的妹妹穿的比我還像新娘。我一直安慰自己读整,他們只是感情好簿训,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著米间,像睡著了一般强品。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上屈糊,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鳄哭!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起纲熏,我...
- 正文 年R本政府宣布扇单,位于F島的核電站,受9級特大地震影響奠旺,放射性物質(zhì)發(fā)生泄漏蜘澜。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一响疚、第九天 我趴在偏房一處隱蔽的房頂上張望鄙信。 院中可真熱鬧,春花似錦忿晕、人聲如沸装诡。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽慎王。三九已至,卻和暖如春宏侍,著一層夾襖步出監(jiān)牢的瞬間赖淤,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 提取測試文件: s16格式 flt格式 ffmpeg只能提取packed格式的PCM數(shù)據(jù)诸典,在編碼時候如果輸入要為f...
- 使用FFmpeg把PCM裸數(shù)據(jù)編碼成AAC音頻流,具體步驟跟YUV編碼成H264差不多崎苗。 一狐粱、命令行 -f PC...
- 前言 pcm音頻是未壓縮的數(shù)據(jù)舀寓,占空間,在存儲或者傳輸時一般都會選擇先進行有損壓縮(比如aac肌蜻,MP3等等)互墓。pc...
- vx 搜索『gjzkeyframe』 關(guān)注『關(guān)鍵幀Keyframe』來及時獲得最新的音視頻技術(shù)文章篡撵。 這個公眾號會...
- vx 搜索『gjzkeyframe』 關(guān)注『關(guān)鍵幀Keyframe』來及時獲得最新的音視頻技術(shù)文章育谬。 這個公眾號會...