您的位置:首页 > 其它

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: