200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > ffmpeg pcm编码aac

ffmpeg pcm编码aac

时间:2023-03-14 10:16:07

相关推荐

ffmpeg pcm编码aac

本博文参考自雷神博客 /leixiaohua1020/article/details/25430449

这是ffmpeg加上libfdk-aac 将pcm转aac的一个例子

代码如下

#include <string.h>#define __STDC_CONSTANT_MACROS//Linux...#ifdef __cplusplusextern "C"{#endif#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#ifdef __cplusplus};#endif/// unsigned char *buf;int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index);int write_buffer(void * opaque,uint8_t *buf_t, int buf_size)//回调函数{fwrite(buf_t,buf_size,1,(FILE *)opaque);fflush((FILE *)opaque);return buf_size;}int main(void){AVFormatContext* pFormatCtx;AVOutputFormat* fmt;AVStream* audio_st;AVCodecContext* pCodecCtx;AVCodec* pCodec;uint8_t* frame_buf;AVFrame* pFrame;AVPacket pkt;int got_frame=0;int ret=0;int size=0;const char* out_file = "./hefang_cjx_pcm.aac";const char *in_file="./hefang.pcm";FILE *out_fb=fopen(out_file,"wb+");FILE *in_fb=fopen(in_file,"r");int i;av_register_all();pFormatCtx = avformat_alloc_context();fmt = av_guess_format(NULL, out_file, NULL);pFormatCtx->oformat = fmt;unsigned char* outbuffer=(unsigned char*)av_malloc(32768);AVIOContext *avio_out =avio_alloc_context(outbuffer, 32768,1,out_fb,NULL,write_buffer,NULL);pFormatCtx->pb=avio_out;pFormatCtx->flags=AVFMT_FLAG_CUSTOM_IO;avformat_init_output(pFormatCtx,NULL);audio_st = avformat_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->channels =1;pCodecCtx->profile= FF_PROFILE_AAC_LOW ;pCodecCtx->bit_rate =64000;pCodec = avcodec_find_encoder_by_name("libfdk_aac");if (!pCodec){printf("Can not find encoder!\n");return -1;}if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){printf("Failed to open encoder!\n");return -1;}pFrame = av_frame_alloc();pFrame->nb_samples= pCodecCtx->frame_size;pFrame->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(pFrame, pCodecCtx->channels, pCodecCtx->sample_fmt,(const uint8_t*)frame_buf, size, 1);//Write Headeravformat_write_header(pFormatCtx,NULL);av_new_packet(&pkt,size);while(!feof(in_fb)){//Read PCMif (fread(frame_buf, size, 1, in_fb) < 0){printf("Failed to read raw data! \n");}pFrame->data[0] = frame_buf; //PCM DatapFrame->pts=i;got_frame=0;//Encoderet = avcodec_encode_audio2(pCodecCtx, &pkt,pFrame, &got_frame);if(ret < 0){printf("Failed to encode!\n");return -1;}if (got_frame==1){printf("Succeed to encode 1 frame! \tsize:%5d\n",pkt.size);pkt.stream_index = audio_st->index;ret = av_write_frame(pFormatCtx, &pkt);av_free_packet(&pkt);}i++;}//Flush Encoderret = flush_encoder(pFormatCtx,0);if (ret < 0) {printf("Flushing encoder failed\n");return -1;}//Write Trailerav_write_trailer(pFormatCtx);//Cleanif (audio_st){avcodec_close(audio_st->codec);av_free(pFrame);av_free(frame_buf);}// avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);fclose(out_fb);fclose(in_fb);return 0;}int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index){int ret;int got_frame;AVPacket enc_pkt;if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &CODEC_CAP_DELAY))return 0;while (1) {enc_pkt.data = NULL;enc_pkt.size = 0;av_init_packet(&enc_pkt);ret = avcodec_encode_audio2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,NULL, &got_frame);av_frame_free(NULL);if (ret < 0)break;if (!got_frame){ret=0;break;}/* mux encoded frame */ret = av_write_frame(fmt_ctx, &enc_pkt);if (ret < 0)break;}return ret;}

这里write_buffer是回调函数,在调用av_write_frame会触发write_buffer,其中必须得调用avformat_init_output设置,否则执行不了回调。这样就可以将编码后的aac数据输入到内存中,再从内存中保存到文件。

下载链接:/download/yinsui1839/10433940

没有积分的朋友可以在评论区发邮箱给我

linux下编译:g++ main.cpp -lavformat -lavcodec -lavdevice -lavutil -lz -lm -lswresample -lfdk-aac

windows下:不知道

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。