//初始化、注册编解码器
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. nginx重定向配置

    # /etc/nginx/nginx.conf #写在server,location核心模块中,if也可以写.$http_host客户端设法要到达主机的主机名 if ($http_host !~ “^ ...

  2. yourphp的edit,updata,dele

    参考文件Yourphp\Lib\Action\User\PostAction.class.php public function add() { $form=new Form(); $form-> ...

  3. Spring中ApplicationContext对事件的支持

    Spring中ApplicationContext对事件的支持   ApplicationContext具有发布事件的能力.这是因为该接口继承了ApplicationEventPublisher接口. ...

  4. nginx使用ssl模块配置支持HTTPS访问

    默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译nginx时指定–with-http_ssl_module参数. 需求: 做一个网站域名为 www.localhost.cn 要求通过htt ...

  5. MySQL 使用SELECT ... FOR UPDATE 做事务写入前的确认(转)

    Select…For Update语句的语法与select语句相同,只是在select语句的后面加FOR UPDATE [NOWAIT]子句. 该语句用来锁定特定的行(如果有where子句,就是满足w ...

  6. iOS- storyboard this class is not key value coding-compliant for the key xxx

    如图: 在使用storyboard的时候出现此问题,主要是因为给storybroad中的view拖线的时候,有时不小心线拖错了,或者再次拖线导致代码中控件的名字与之前拖线时定义的名字不同导致的. 解决 ...

  7. [CentOS]安装命令行终端Terminator工具

    摘要 Terminator是一款跨平台的终端工具,使用的是 GPL 许可证,提供了很多高级的功能.它没有 Guake 和 Yakuake 那样光鲜,但绝对是一款重型武器.它提供的功能包括界面分块,将自 ...

  8. [Bug]IIs Cannot read configuration file due to insufficient permissions

    摘要 在部署站点的时候,遇到这样的问题Cannot read configuration file due to insufficient permissions 解决办法 在服务器上部署站点,浏览的 ...

  9. css1-css3的那些模糊点

    css很重要, 但也不是万能的, 也不能抛弃dom 元素和 元素的属性!! 很多时候, dom "元素" 的 "属性" 也很重要 也很实用! 要结合属性来写 包 ...

  10. editplus如何配置php编译环境?

    为什么要配置php编译? 因为,要先看看 php文件是否能够 编译得过去, 有没有错误, 如果有错误, 不能通过编译, 则肯定不能运行. 所以, 可以先看一下编译 得不得行. 在preferences ...