一、开发环境
Windows:win10 64位
Qt:Qt Creator 17.0.0,Qt 6.9.1
FFmpeg:7.1.1
开发环境搭建可以参考:一、Windows音视频开发环境搭建指南
二、创建Qt项目
三、音频采集
使用FFmpeg采集音频流程:
3.1 打开音频设备
ffmpeg关键api:avdevice_register_all、av_find_input_format、avformat_open_input
详细代码如下:
int Audio::OpenAudioDevice(AVFormatContext** audioCtx)
{
AVDictionary* options = nullptr; // 用于配置设备参数
AVDeviceInfoList* device_list = nullptr; // 当前系统支持的设备
int ret = 0;
// 1.设备注册
// 作用:激活 FFmpeg 编译时包含的所有音视频采集/输出设备(如 dshow、v4l2、alsa、avfoundation、sdl、opengl 等),
// 使后续能通过 av_find_input_format() 查找到这些设备
avdevice_register_all();
// 2.获取输入设备的格式(兼容多种Windows采集格式)
const AVInputFormat *iformat = nullptr;
const char* input_formats[] = {
"dshow", "vfwcap", "gdigrab", nullptr};
for(int i = 0; input_formats[i] != nullptr; i++) {
iformat = av_find_input_format(input_formats[i]);
if(iformat) {
qDebug() << "使用输入格式:" << input_formats[i];
break;
}
}
if (!iformat) {
qCritical() << "找不到可用的输入设备格式";
return -1;
}
// 3. 枚举设备(增加超时和重试机制)
int max_retries = 3;
for(int retry =