ffmpeg 录音 利用avdevice
2015-09-28 16:00
351 查看
可以直接使用ffmpeg录音,参考代码如下:
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVStream* in_stream;
AVPacket pkt;
av_register_all();
avdevice_register_all();
//初始化音频编码器,自己封装的类,这个类的初始化,跟录音没关系。
AudioEncoder ae;
ae.initAudioEncoder();
//打开输入设备,
pFormatCtx = avformat_alloc_context();
AVInputFormat *pInputFmt=av_find_input_format("dshow");
avformat_open_input(&pFormatCtx,"audio=Realtek HD Audio Input",pInputFmt,NULL);
//查找音频流
avformat_find_stream_info(pFormatCtx,NULL);
int audioStreamIndex = -1;
for(int i=0; i<pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
{
audioStreamIndex =i;
break;
}
}
//得到编解码环境,采样率,样本格式,通道数这三个值一开始就有了默认的值,貌似不能改变。
pCodecCtx=pFormatCtx->streams[audioStreamIndex ]->codec;
//设置一下通道布局,
pCodecCtx->channel_layout =AV_CH_LAYOUT_STEREO;
in_stream = pFormatCtx->streams[audioStreamIndex ];
//找到解码器并打开
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
avcodec_open2(pCodecCtx, pCodec,NULL);
//之后就可以使用av_read_frame采集音频数据了
av_read_frame(pFormatCtx, &pkt);
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVStream* in_stream;
AVPacket pkt;
av_register_all();
avdevice_register_all();
//初始化音频编码器,自己封装的类,这个类的初始化,跟录音没关系。
AudioEncoder ae;
ae.initAudioEncoder();
//打开输入设备,
pFormatCtx = avformat_alloc_context();
AVInputFormat *pInputFmt=av_find_input_format("dshow");
avformat_open_input(&pFormatCtx,"audio=Realtek HD Audio Input",pInputFmt,NULL);
//查找音频流
avformat_find_stream_info(pFormatCtx,NULL);
int audioStreamIndex = -1;
for(int i=0; i<pFormatCtx->nb_streams; i++)
{
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
{
audioStreamIndex =i;
break;
}
}
//得到编解码环境,采样率,样本格式,通道数这三个值一开始就有了默认的值,貌似不能改变。
pCodecCtx=pFormatCtx->streams[audioStreamIndex ]->codec;
//设置一下通道布局,
pCodecCtx->channel_layout =AV_CH_LAYOUT_STEREO;
in_stream = pFormatCtx->streams[audioStreamIndex ];
//找到解码器并打开
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
avcodec_open2(pCodecCtx, pCodec,NULL);
//之后就可以使用av_read_frame采集音频数据了
av_read_frame(pFormatCtx, &pkt);
相关文章推荐
- PowerShell学习笔记(1)-基础
- SVN(VisualSVN )详细教程(六)--建立用户和组,并且需要分配权限
- Swift学习笔记5——流程控制语句
- apache/iis访问日志转到sql存储的程序思维
- 数据库版本管理工具flyway
- Decoda1.17编译实录
- Libvirt学习总结
- Ios小记整理
- Swift - 闭包的介绍及用法(以数组排序为例)
- Swift - 产生不重复数字的随机数生成器
- 如何去除iOS和Android中的输入URL的控件条
- 人人网的账号登录及api操作
- 高性能的开源HTTP加速器varnish
- JAVA Pattern和Matcher 的用法
- jquery autocomplete插件结合ajax使用demo
- 简单实用的Android ORM框架TigerDB
- python学习(二):python基本语法
- #8 LeetCode——String to Integer (atoi)
- 【转】Android设置虚线、圆角、渐变
- Android ViewPager