C++调用FFmpeg时av_find_input_format("dshow")返回nullptr问题求解
问题解决方案
你遇到的问题核心为代码缺少libavdevice初始化逻辑,其次可能存在项目链接配置、运行时DLL加载错误的问题,不需要使用mingw-w64手动编译FFmpeg,按以下步骤排查即可:
- 补充设备注册逻辑
FFmpeg的设备输入输出模块不会默认自动注册,你需要在调用设备相关接口前执行avdevice_register_all()完成所有设备(包括dshow)的注册。 - 确认项目链接配置正确
检查你的C++项目是否已经添加了avdevice.lib的链接依赖,vcpkg安装的FFmpeg默认会自动导入依赖,如果你是手动配置项目,需要额外添加该库的链接配置,不能仅链接avformat、avcodec等基础库。 - 确认运行时DLL加载正确
运行程序时检查加载的avdevice-*.dll是否为vcpkg编译的带dshow支持的版本,避免路径优先级问题导致加载了其他不带dshow支持的FFmpeg DLL。
修正后的测试代码
extern "C" { #include <libavformat/avformat.h> #include <libavdevice/avdevice.h> } int main(int argc, char *argv[]) { // 注册所有音视频设备,必须在调用av_find_input_format前执行 avdevice_register_all(); AVFormatContext* inFormatContext = avformat_alloc_context(); AVInputFormat* inFormat = av_find_input_format("dshow"); if (!inFormat || !inFormat->priv_class || !AV_IS_INPUT_DEVICE(inFormat->priv_class->category)) { return 1; } AVDeviceInfoList* deviceList; int deviceCount = avdevice_list_input_sources(inFormat, nullptr, nullptr, &deviceList); // 打印设备数量验证结果 printf("dshow设备数量:%d\n", deviceCount); avdevice_free_list_devices(&deviceList); avformat_free_context(inFormatContext); return 0; }
关于手动编译的说明
不需要使用mingw-w64手动编译FFmpeg,vcpkg安装的ffmpeg[nvcodec]:x64-windows已经开启了dshow输入设备支持,完成上述配置即可正常调用。
内容的提问来源于stack exchange,提问作者terids
相关产品推荐
相关产品推荐

