支持aac和mp3转码,主要是dst_nb_sample的计算,aac大致为1024,MP3为1152,
static int dst_nb_samples = 0;
#define SWR
int encoding(const char *in_file, const char *out_file)
{
//注册所有的工具
av_register_all();
AVFormatContext *fmt_ctx = NULL;
AVCodecContext *cod_ctx = NULL;
AVCodec *cod = NULL;
int nRet;
//分配一个avformat
fmt_ctx = avformat_alloc_context();
if (fmt_ctx == NULL)
printf("alloc fail");
//打开文件,解封装
if (avformat_open_input(&fmt_ctx, in_file, NULL, NULL) != 0)
printf("open fail");
//查找文件的相关流信息
if (avformat_find_stream_info(fmt_ctx, NULL) < 0)
printf("find stream fail");
//输出格式信息
av_dump_format(fmt_ctx, 0, in_file, 0);
//查找解码信息
int stream_index = -1;
for (int i = 0; i < fmt_ctx->nb_streams; i++)
if (fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
stream_index = i;
break;
}
if (st