ffmpeg解码音频流
//初始化、注册编解码器
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解码音频流的更多相关文章
- FFmpeg解码H264及swscale缩放详解
本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何解码h264码流,比较详细阐述了其h264码流输入过程,解码原理,解码过程.同时,大部分应用环境下,以原始码流视频大小展示并不是最佳方式,因此 ...
- 【图像处理】FFmpeg解码H264及swscale缩放详解
http://blog.csdn.net/gubenpeiyuan/article/details/19548019 主题 FFmpeg 本文概要: 本文介绍著名开源音视频编解码库ffmpeg如何 ...
- FFmpeg开发笔记(四):ffmpeg解码的基本流程详解
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- FFmpeg开发笔记(五):ffmpeg解码的基本流程详解(ffmpeg3新解码api)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- FFmpeg开发笔记(九):ffmpeg解码rtsp流并使用SDL同步播放
前言 ffmpeg播放rtsp网络流和摄像头流. Demo 使用ffmpeg播放局域网rtsp1080p海康摄像头:延迟0.2s,存在马赛克 使用ffmpeg播放网络rtsp文件流 ...
- WebRTC VideoEngine超详细教程(三)——集成X264编码和ffmpeg解码
转自:http://blog.csdn.net/nonmarking/article/details/47958395 本系列目前共三篇文章,后续还会更新 WebRTC VideoEngine超详细教 ...
- FFMPEG解码流程
FFMPEG解码流程: 1. 注册所有容器格式和CODEC: av_register_all() 2. 打开文件: av_open_input_file() 3. 从文件中提取流信息: av_f ...
- 使用FFmpeg解码H264-2016.01.14
使用jni方式调用FFmepg项目中接口,对H264裸码进行解码. 该Demo主要实现从文件中读取H264编码的视频流,然后使用FFmpeg解码,将解码后的码流保存到文件. 工程目录结构如图所示: A ...
- 新版本ffmpeg解码非完整H264帧失败
按照ffmpeg/doc/examples/decoding_encoding.c中video_decode_example解码H264,新版本ffmpeg解码非完整H264帧,定量读取数据直接给av ...
随机推荐
- ecshop Admin后台商品列表处(上架、下架、精品...取消精品)增加操作
相关文件:goods.php,goods_list.htm 思路: a.增添连接“转移仓库” b.在goods.php,读取仓库列表数据,并且实例化 c. 在goods_list.htm循环数据.点击 ...
- IE6完美解决fix问题
解决代码: _position: absolute; _top: expression(documentElement.scrollTop + + "px"); 完整代码: kef ...
- UIAlertController 标题文字大小 颜色
NSString *title = [NSString stringWithFormat:]; NSString *msg = @"\n把红包分享给微信好友,金额随机,可用于购买雪票和雪卡& ...
- RobotFramework——介绍篇
1.简介Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行.主要用于轮次很多的验 ...
- 改变Vim在iTerm2中的光标
vim ~/.vimrc 添加 " Change cursor shape between insert and normal mode in iTerm2.appif $TERM_PROG ...
- PhpStorm 快捷键
不容易记住的: Ctrl + Shift + F 查找文本,在项目目录或指定的目录 Ctrl + Shift + R 查找文本并替换,在项目目录或指定的目录 Ctrl + E 打开最近关闭的 ...
- 关于MySQL的SLEEP(N)函数
都知道通过在MySQL中执行select sleep(N)可以让此语句运行N秒钟: ? 1 2 3 4 5 6 7 mysql> select sleep(1); +----------+ | ...
- Eclipse 安装插件后不显示的解决办法
有时候一些 eclipse 插件安装之后,打开 eclipse 死活都不显示,这时候可以: ① 把 eclipse/configuration/org.eclipse.update 删除掉.出现这种情 ...
- mysql 简单练习
1.查找全部学生的信息 [SQL]select * from student 受影响的行: 0 时间: 0.000s 2.查出成绩及格的所有人 [SQL]select * from student w ...
- 关于web服务器访问速度慢的一些简单解决方法
首先要考虑下 访问的用户: [root@app opt2]# netstat -ant | grep $ip: tcp ::: :::* LISTEN tcp ::ffff: ::ffff: TIME ...