You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt Creator中调用FFmpeg函数崩溃问题求助

问题描述

通过MSYS2编译FFmpeg后,调用avdevice_register_all()等FFmpeg设备相关函数时程序崩溃,但仅调用av_version_info()可正常运行。主文件代码如下:

#include "mainwindow.h"
#include <QApplication>
#include <QDebug>

extern "C" {
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
}


int main(int argc, char *argv[])
{
   //qDebug() << av_version_info();
  // avdevice_register_all();
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
排查方向与解决方案
  • 确认FFmpeg编译配置
    检查编译FFmpeg时是否启用了设备模块,执行ffmpeg -version或查看编译日志,确保--enable-avdevice选项已添加。若未开启该选项,avdevice模块无实现,调用相关函数会直接崩溃。

  • 验证链接库的一致性
    确保Qt项目链接的是MSYS2编译的FFmpeg库,而非系统其他版本。检查项目.pro文件,确认库路径、链接参数(如-lavdevice、-lavformat)正确,所有FFmpeg依赖库版本必须完全匹配,避免版本不兼容引发崩溃。

  • 调整初始化顺序
    尝试先初始化基础格式模块,再注册设备:

    int main(int argc, char *argv[])
    {
        av_register_all(); // 先完成基础格式注册
        avdevice_register_all();
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }
    

    注:FFmpeg 4.x及以上版本av_register_all()已标记废弃,但部分设备模块仍依赖其完成基础初始化。

  • 检查运行时库路径
    若为动态编译FFmpeg,需将编译生成的dll文件放入程序运行目录,或添加到系统PATH中,避免因动态库缺失导致加载崩溃。

  • 调试定位崩溃点
    用调试模式编译Qt项目和FFmpeg(编译FFmpeg时添加--enable-debug),触发崩溃后查看调用栈,确认崩溃发生在FFmpeg内部的具体函数,精准定位问题根源。

内容的提问来源于stack exchange,提问作者c47296

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 03:55:11