ffmpeg 录音 利用avdevice

可以直接使用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);

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值