ffmpeg,MP3转换到aac如何转呢? 多谢,用代码如何实现呢
ffmpeg,MP3转换到aac怎么转呢? 谢谢,用代码怎么实现呢?
或者说基于我的代码来看,因为啊,这段代码是我自己写的,可是老是出错呢。怎么办呢? 谢谢,非常感谢。
------解决思路----------------------
怎么简短的代码,又没有自己的中间处理,直接命令行轻松搞定了~~
另外,你说的出错出在哪里呢?你都没判断哪里有错;
打开文件?获取流信息?,
1,
input_context->streams[0] 没用你find出的best stream
2,
打开解码器
源码里好像用的
avcodec_find_decoder(codec_id) 和 ffdec_open_codec(stream, codec);
而你是用的findbest里的编码器,流却用的索引0的~~~不一定对的上哦;
3,
av_read_frame 出来的pkt你没判断他的stream_index是否是你所需要的流
4,
avcodec_decode_audio4的使用;你没用它的返回值,去判断读出来的pkt是否完全解码。。。。
大概看一下,就发现这些,你肯定要单步或者日志一下看哪些具体步骤出错再说,另外可以先多看看ffmpeg.c那份源码
或者说基于我的代码来看,因为啊,这段代码是我自己写的,可是老是出错呢。怎么办呢? 谢谢,非常感谢。
void ecc()
{
const char* infile = "H:/near_work/music.mp3";
const char* out_file = "music.aac";
AVFormatContext *input_context = 0;
AVFormatContext *output_context = 0;
AVCodec *dec, *out_encode;
AVPacket inpkt;
AVFrame inframe;
int got_inframe = 0;
AVStream* in_stream = 0;
AVStream* out_stream = 0;
av_register_all();
avformat_open_input(&input_context, infile, 0, 0);
avformat_find_stream_info(input_context, 0);
av_find_best_stream(input_context, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
in_stream = input_context->streams[0];
in_stream->time_base = in_stream->codec->time_base;
avcodec_open2(in_stream->codec, dec, NULL);
avformat_alloc_output_context2(&output_context, NULL, NULL, out_file);
avio_open(&output_context->pb, out_file, AVIO_FLAG_READ_WRITE);
out_encode = avcodec_find_encoder(AV_CODEC_ID_AAC);
out_stream = avformat_new_stream(output_context, out_encode);
avcodec_open2(out_stream->codec, out_encode, NULL);
out_stream->time_base = out_stream->codec->time_base;
avformat_write_header(output_context, NULL);
out_stream->codec->frame_size = 1152;
out_stream->codec->codec_id = output_context->oformat->audio_codec;
out_stream->codec->codec_type = AVMEDIA_TYPE_AUDIO;
out_stream->codec->sample_fmt = AV_SAMPLE_FMT_S16;
out_stream->codec->sample_rate= 44100;
out_stream->codec->channel_layout=AV_CH_LAYOUT_STEREO;
out_stream->codec->channels = av_get_channel_layout_nb_channels(out_stream->codec->channel_layout);
out_stream->codec->bit_rate = 64000;
int size = av_samples_get_buffer_size(NULL, out_stream->codec->channels,out_stream->codec->frame_size,out_stream->codec->sample_fmt, 1);
AVPacket out_pkt;
av_new_packet(&out_pkt, size);
while(1)
{
if (av_read_frame(input_context, &inpkt) < 0) break;
avcodec_get_frame_defaults(&inframe);
got_inframe = 0;
avcodec_decode_audio4(in_stream->codec, &inframe, &got_inframe, &inpkt);
if(got_inframe)
{
int gf = 0;
avcodec_encode_audio2(out_stream->codec, &out_pkt, &inframe, &gf);
inpkt.stream_index = out_stream->index;
av_write_frame(output_context, &inpkt);
av_free_packet(&inpkt);
}
}
}
------解决思路----------------------
怎么简短的代码,又没有自己的中间处理,直接命令行轻松搞定了~~
另外,你说的出错出在哪里呢?你都没判断哪里有错;
打开文件?获取流信息?,
1,
input_context->streams[0] 没用你find出的best stream
2,
打开解码器
源码里好像用的
avcodec_find_decoder(codec_id) 和 ffdec_open_codec(stream, codec);
而你是用的findbest里的编码器,流却用的索引0的~~~不一定对的上哦;
3,
av_read_frame 出来的pkt你没判断他的stream_index是否是你所需要的流
4,
avcodec_decode_audio4的使用;你没用它的返回值,去判断读出来的pkt是否完全解码。。。。
大概看一下,就发现这些,你肯定要单步或者日志一下看哪些具体步骤出错再说,另外可以先多看看ffmpeg.c那份源码