QT+FFMPEG Windows环境下实现录音

在windows中需要通过windows api来获取mic设备

    // windows api 获取音频设备列表(ffmpeg好像没有提供获取音频设备的api)
    unsigned int nDeviceNum = waveInGetNumDevs();
    vector<string> vecDeviceName;

    for (unsigned int i = 0; i < nDeviceNum; i++){
        WAVEINCAPS wic;
        waveInGetDevCaps(i, &wic, sizeof (wic));
        
        printf("\n音频输入设备:%s\n", wic.szPname);

        //转成utf-8
        int nSize = WideCharToMultiByte(CP_UTF8, 0, wic.szPname,
                    static_cast<int>(wcslen(wic.szPname)), nullptr, 0, nullptr, nullptr);
        shared_ptr<char> spDeviceName(new char[nSize + 1]);
        memset(spDeviceName.get(), 0, static_cast<size_t>(nSize + 1));
        WideCharToMultiByte(CP_UTF8, 0, wic.szPname, static_cast<int>(wcslen(wic.szPname)),
                            spDeviceName.get(), nSize, nullptr, nullptr);
        vecDeviceName.push_back(spDeviceName.get());
        printf("audio input device:%s \n", spDeviceName.get());
    }

获取音频设备的调试界面
在这里插入图片描述
注:目前在打印音频设备时会显示乱码,qt的应用程序输出界面的编码格式是GB2312,如果相应打印不乱码,需要使用WideCharToMultiByte将字符串转化成GB2312,即CP_ACP。

调用windows api时,需要在ffmpeg_study.pro下引入winmm库,具体如下:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += \
        main.cpp

INCLUDEPATH += $$PWD/lib/ffmpeg/include
LIBS += -L$$PWD/lib/ffmpeg/lib/ \
        -lavformat   \
        -lavcodec    \
        -lavdevice   \
        -lavfilter   \
        -lavutil     \
        -lpostproc   \
        -lswresample \
        -lswscale


#LIBS += -luser32
LIBS += -lWinmm

ffmpeg采集音频流程:
打开输入设备->获取数据(分析为音频或视频)->输出文件
注册设备:avdevice_register_all();
设置采集方式:(mac)avfoundation/ (windows) dshow / (linux)alsa
打开音频设备:avformat_open_input
AVFormatContext:上下文。后面的API都依赖上下文。打开一个多媒体文件,去封装,编解码等操作都依赖于上下文件。
url:可以是网络地址也可以是本地设备地址。
*fmt:打开音频设备的方式
**options:打开设备的方法。解码时会用。
读取音频数据:
av_read_frame(读取音频数据它会自己判断是音频数据还是视频数据)
AVFormatContext(相当于上下文)
AVPackt(音视频包)
返回值为0表示成功
与AVPacket相关的API
av_init_packet //通过这个将数据包进行初始化
av_packet_unref //对缓存区数据包进行释放
av_packet_alloc //分配空间
av_packet_free //释放分配的空间
采集的数据文件为audio.pcm,可以通过命令:ffplay.exe -ar 44100 -ac 2 -f s16le …\av_base\audio.pcm进行播放
完整的示例代码:

#include <iostream>

using namespace std;

//包含ffmpeg头文件
extern "C"
{
#include "libavutil/avutil.h"
#include "libavdevice/avdevice.h"
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
}

#include <windows.h>
#include <vector>
#include <string>
#include <memory>

using std::vector;
using std::string;
using std::shared_ptr;

void capture_audio()
{
    // windows api 获取音频设备列表(ffmpeg好像没有提供获取音频设备的api)
    unsigned int nDeviceNum = waveInGetNumDevs();
    vector<string> vecDeviceName;

    for (unsigned int i = 0; i < nDeviceNum; i++){
        WAVEINCAPS wic;
        waveInGetDevCaps(i, &wic, sizeof (wic));
        printf("\n音频输入设备:%s\n", wic.szPname);

        //转成utf-8
        int nSize = WideCharToMultiByte(CP_UTF8, 0, wic.szPname,
                    static_cast<int>(wcslen(wic.szPname)), nullptr, 0, nullptr, nullptr);
        shared_ptr<char> spDeviceName(new char[nSize + 1]);
        memset(spDeviceName.get(), 0, static_cast<size_t>(nSize + 1));
        WideCharToMultiByte(CP_UTF8, 0, wic.szPname, static_cast<int>(wcslen(wic.szPname)),
                            spDeviceName.get(), nSize, nullptr, nullptr);
        vecDeviceName.push_back(spDeviceName.get());
        printf("audio input device:%s \n", spDeviceName.get());
    }

    if (vecDeviceName.size() <= 0){
        printf("not find audio input device.\n");
        return;
    }

    //使用第一个音频设备
    string sDeviceName = "audio=" + vecDeviceName[0];

    //ffmpeg
    int ret = 0;
    char errors[1024];
    AVFormatContext *fmt_ctx = nullptr;
    AVDictionary *options = nullptr;

    //register audio device
    avdevice_register_all();

    //get format
    const AVInputFormat *iformat = av_find_input_format("dshow");

    //open device
    if ((ret = avformat_open_input(&fmt_ctx, sDeviceName.data(),
        iformat, &options)) < 0) {
        av_strerror(ret, errors, 1024);
        printf("Failed to open audio device, [%d]%s\n", ret, errors);
    }

    //create file
    FILE *outflie = fopen("D:/Study/ffmpeg/av_base/audio.pcm", "wb");

    //read frame form device
    int count = 0;
    AVPacket pkt;
    av_init_packet(&pkt);
    while ((ret = av_read_frame(fmt_ctx, &pkt)) == 0 && count++ < 50) {
        fwrite(pkt.data, static_cast<size_t>(pkt.size), 1, outflie);
        //刷新缓冲区,使数据写入磁盘
        fflush(outflie);
        printf("pkt size is %d count: %d\n", pkt.size, count);
        //release pkt
        av_packet_unref(&pkt);
    }

    //close device and release ctx
    avformat_close_input(&fmt_ctx);
    return;
}

int main()
{
    capture_audio();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值