//初始化、注册编解码器
avcodec_init();
av_register_all();
avformat_network_init(); //选取测试文件
char* FileName="test.mp4";
string strFileName(FileName);
WavMaker test(AudioName.c_str());//初始化wav对象 //尝试打开测试文件
AVFormatContext *pFormatCtx;
if(av_open_input_file(&pFormatCtx, FileName, NULL, 0, NULL) !=0 )
{
printf("打开文件失败\n");
return -1;
}
dump_format(pFormatCtx, 0, NULL, NULL);//打印相关参数 //寻找流信息,获取格式上下文信息
int err = av_find_stream_info(pFormatCtx);
if(err < 0)
{
printf("查看流信息失败");
return 0;
} //找到所有的音频流和视频流
vector<int> v_video_index;
vector<int> v_audio_index;
for(int i = 0;i<pFormatCtx->nb_streams;i++)
{
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
{
v_audio_index.push_back(i);
}
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
v_video_index.push_back(i);
}
} //取第一条音频流,作为测试
AVCodecContext *pAVCodecCtx = pFormatCtx->streams[v_audio_index[0]]->codec;
AVCodec *pAVCodec = avcodec_find_decoder(pAVCodecCtx->codec_id);//找到相应的解码器
if(!pAVCodec)
{
printf("Unsupported codec!\n");
return -1;
}
avcodec_open(pAVCodecCtx,pAVCodec); //获取编码器上下文信息 //初始化包
AVPacket packet;
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
int16_t * outbuf = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE * 2]; //解码完一帧pcm缓冲区 //输出wav文件名
int index1 = strFileName.rfind('.');
string AudioName = strFileName.substr( 0, index1) + ".wav";
WavMaker test(AudioName.c_str()); int count = 0;
while(av_read_frame(pFormatCtx,&packet)>=0)//读取包
{ if(packet.stream_index==v_audio_index[0])//只解码第一条音频流
{
uint8_t *pktdata = packet.data;
int pktsize = packet.size;
while (pktsize > 0)
{
int times=0;
int out_size =AVCODEC_MAX_AUDIO_FRAME_SIZE*2;
int len = avcodec_decode_audio3(pAVCodecCtx, (short *)outbuf, &out_size, &packet);
if (len < 0)
{
fprintf(stderr, "Error while decoding\n");
break;
}
if (out_size > 0)
{
test.writebody((uint8_t*)outbuf,out_size); //解码后的数据写入文件
break;
}
pktsize -= len;
pktdata += len;
}
}
av_free_packet(&packet);//释放packet
} test.writeheader(pAVCodecCtx->channels,pAVCodecCtx->sample_rate);//写wav头
test.closeFile();//关闭文件

ffmpeg解码音频流的更多相关文章

  1. FFmpeg解码H264及swscale缩放详解

    本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...

  2. 【图像处理】FFmpeg解码H264及swscale缩放详解

      http://blog.csdn.net/gubenpeiyuan/article/details/19548019 主题 FFmpeg 本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何 ...

  3. FFmpeg开发笔记(四):ffmpeg解码的基本流程详解

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  4. FFmpeg开发笔记(五):ffmpeg解码的基本流程详解(ffmpeg3新解码api)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  5. FFmpeg开发笔记(九):ffmpeg解码rtsp流并使用SDL同步播放

    前言   ffmpeg播放rtsp网络流和摄像头流.   Demo   使用ffmpeg播放局域网rtsp1080p海康摄像头:延迟0.2s,存在马赛克     使用ffmpeg播放网络rtsp文件流 ...

  6. WebRTC VideoEngine超详细教程(三)——集成X264编码和ffmpeg解码

    转自:http://blog.csdn.net/nonmarking/article/details/47958395 本系列目前共三篇文章,后续还会更新 WebRTC VideoEngine超详细教 ...

  7. FFMPEG解码流程

    FFMPEG解码流程:  1. 注册所有容器格式和CODEC: av_register_all()  2. 打开文件: av_open_input_file()  3. 从文件中提取流信息: av_f ...

  8. 使用FFmpeg解码H264-2016.01.14

    使用jni方式调用FFmepg项目中接口,对H264裸码进行解码. 该Demo主要实现从文件中读取H264编码的视频流,然后使用FFmpeg解码,将解码后的码流保存到文件. 工程目录结构如图所示: A ...

  9. 新版本ffmpeg解码非完整H264帧失败

    按照ffmpeg/doc/examples/decoding_encoding.c中video_decode_example解码H264,新版本ffmpeg解码非完整H264帧,定量读取数据直接给av ...

随机推荐

  1. chain.doFilter(request,response)含义

    过滤器的生命周期一般都要经过下面三个阶段: 初始化 当容器第一次加载该过滤器时,init() 方法将被调用.该类在这个方法中包含了一个指向 Filter Config 对象的引用.我们的过滤器实际上并 ...

  2. yum -y install与yum install有什么不同

    yum -y install 包名(支持*) :自动选择y,全自动 yum install 包名(支持*) :手动选择y or n yum remove 包名(不支持*) rpm -ivh 包名(支持 ...

  3. centos 7.0 ln命令 和chkconfig 命令介绍 开机自动启 服务

    有时候centos需要 程序开机启动的时候  自启动 首先在 /etc/init.d/ cd /etc/init.d 文件夹下建立开机启动项 使用ln命令 使用方式 : ln [options] so ...

  4. oracle 11g express 修改oem端口

    begin dbms_xdb.sethttpport('8081'); dbms_xdb.setftpport('0'); end; / 这样就把端口设置为8081了.

  5. JavaScript 学习笔记 -- Function

    JS 中 函数.继承.闭包.作用域链... 一直都是硬伤,一碰到这样的问题头就大了.但是如果我继续着说:我不会,就真的无药可救了.要勇敢地说出:我的字典里就没有不会这个词,吼吼..正好昨天在书城里看了 ...

  6. Linux中服务器软件为什么需要编译安装

    为什么服务器软件需要编译安装?一个流传很广的说法是编译安装性能更好,其实这是个谣言. 服务器CPU事实已经被Intel垄断了,就那么几种型号,编来编去生成的机器码是一样的.Intel宣传自己的编译工具 ...

  7. python简明手册学习

    1.行末单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行. >>> "This is the first sentence.\ ... This is the s ...

  8. 快速切换IP的批处理!

    内容如下: @echo off color 1A Title [SMART专用 IP设置V1.0] cls echo. echo SMART专用 IP设置V1.0 %date%%time% echo. ...

  9. Net上传附件大小控控值(转)

    Server Error 404 – File or directory not found. The resource you are looking for might have been rem ...

  10. eclipse无法自动识别出svn项目

    因为重新安装了svn插件,重启后发现原来的svn项目无法自动识别出来,连Team->Share Project都没有,而本地用tortoiseSvn是可以正常操作的. 后来我把项目删除然后重新导 ...