ffmpeg 解码mp3 保存成PCM数据

ykmao10 2013-03-22 03:08:08
保存下来的pCM数据转换成WAV播放声音很奇怪,不知代码那里出了问题,大牛帮忙解决下吧 分不够在加
int main(int argc,char **argv)
{
if(argc < 3)
{
printf("argc number is error\n");
return 0;
}

avcodec_register_all();
av_register_all();
AVFormatContext *pFormatCtx;
AVCodec *codec;
AVCodecContext *c= NULL;
int len;
FILE *f, *outfile;
uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 2)];
AVPacket avpkt;
AVFrame *decoded_frame = NULL;

if(!(pFormatCtx = avformat_alloc_context()))
{
return -1;
}

if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
return -1;
if(avformat_find_stream_info(pFormatCtx, NULL)<0)
return -1;
//av_dump_format(pFormatCtx, 0, inputfilename, 0);
// int i,audio_index = -1;
// for(i=0; i<pFormatCtx->nb_streams; i++)
// {
// if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO && audio_index < 0)
// audio_index=i;
// }

//codec = avcodec_find_decoder(pFormatCtx->streams[audio_index]->codec->codec_id);

codec = avcodec_find_decoder(AV_CODEC_ID_MP3);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
av_init_packet(&avpkt);

c = avcodec_alloc_context3(codec);

if (!c) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}

/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}

f = fopen(argv[1], "rb");
if (!f) {
fprintf(stderr, "Could not open %s\n", argv[1]);
exit(1);
}
outfile = fopen(argv[2], "a+");
if (!outfile) {
av_free(c);
exit(1);
}

if (!(decoded_frame = avcodec_alloc_frame())) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
avcodec_get_frame_defaults(decoded_frame);

while(av_read_frame(pFormatCtx, &avpkt)>=0)
{
int got_frame = 0, len = 0 , data_size = 0;

len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);

if(len < 0)
{
continue;
}
if(got_frame <= 0) /* No data yet, get more frames */
continue;
data_size = av_samples_get_buffer_size(NULL, c->channels, decoded_frame->nb_samples, c->sample_fmt, 1);
//memcpy(audio_buf, decoded_frame->data[0], decoded_frame->linesize[0]);
if(data_size < 0)
{
continue;
}
fwrite(decoded_frame->data[0],1,data_size,outfile);
}


fclose(outfile);
fclose(f);

avcodec_close(c);
av_free(c);
avcodec_free_frame(&decoded_frame);
}
...全文
1035 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
neuredfox 2013-10-25
  • 打赏
  • 举报
回复
怎么都是错误呢,难道没有一个是调试通过了?关注中,调试好了的,出来结贴吧。 我也来参考下呢。
Safin 2013-06-04
  • 打赏
  • 举报
回复
楼主你好 请问你用的ffmpeg是哪个版本 我用的是ffmpeg1.0.1 但是照抄你的一部分代码,打开MP3文件就会出错,但是打开视频文件比如AVI没有出错 出错点在avformat_find_stream_info(pFormatCtx, NULL) avcodec_register_all(); av_register_all(); AVFormatContext *pFormatCtx; AVCodec *codec; AVCodecContext *c= NULL; int len; FILE *f, *outfile; uint8_t audio_buf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 2)]; AVPacket avpkt; AVFrame *decoded_frame = NULL; if(!(pFormatCtx = avformat_alloc_context())) { return -1; } if(avformat_open_input(&pFormatCtx, "1.mp3", NULL, NULL)!=0) return -1; if(avformat_find_stream_info(pFormatCtx, NULL)<0) return -1;
msdn2100 2013-05-08
  • 打赏
  • 举报
回复
用elecard分析一下,试试转换其它格式
china_jeffery 2013-05-07
  • 打赏
  • 举报
回复
我也是,关注中

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧