本文详细描述Window 10 和Ubuntu 14.04使用Qt Creator编译QtAV 1.12的步骤
第一种方法:
下载编译好的ffmpeg-3.4.2-win64-dev和ffmpeg-3.4.2-win64-shared,解压得到
D:\qt-workspace\ffmpeg-3.4.2-win64-dev
D:\qt-workspace\ffmpeg-3.4.2-win64-shared
第二种方法:
QtAV官方有提供编译好的依赖库 QtAV-depends-windows-x86%2Bx64.7z,解压得到
D:\qt-workspace\QtAV-depends-windows-x86+x64
下载QtAV-1.12.0,解压得到QtAV-1.12.0,在Qt Creator中导入QtAV项目
然后在Qt Creator设置编译时候的环境变量,换成自己ffmpeg解压的路径
变量 | 值 |
---|---|
CPATH | D:\qt-workspace\ffmpeg-3.4.2-win64-dev\include |
LD_LIBRARY_PATH | D:\qt-workspace\ffmpeg-3.4.2-win64-dev\lib |
LIBRARY_PATH | D:\qt-workspace\ffmpeg-3.4.2-win64-dev\lib |
如果使用的是QtAV提供编译好的依赖库的话,需要注意压缩包里提供了32位和64位的库,我要编译64位的,所以路径是…\lib\x64,如果要编译32位的,则路径是…/lib,变量值如下
变量 | 值 |
---|---|
CPATH | D:\qt-workspace\QtAV-depends-windows-x86+x64\include |
LD_LIBRARY_PATH | D:\qt-workspace\QtAV-depends-windows-x86+x64\lib\x64 |
LIBRARY_PATH | D:\qt-workspace\QtAV-depends-windows-x86+x64\lib\x64 |
编译的时候报错
error: avutil is required, but compiler can not find it
原因可能没有安装好ffmpeg或者上述的环境变量设置的路径有误,或者ffmpeg版本不对
编译完成,点击Qt Creator侧边栏的小电脑图标,选择需要运行的程序
比如选择了player,再点击运行按钮,可能运行会报如下的错误
22:02:28: Starting D:\qt-workspace\build-QtAV-Desktop_Qt_5_12_3_MinGW_64_bit-Release\bin\Player.exe ...
22:02:29: 程序异常结束。
22:02:29: The process was ended forcefully.
22:02:29: D:/qt-workspace/build-QtAV-Desktop_Qt_5_12_3_MinGW_64_bit-Release/bin/Player.exe crashed.
在1.1小节解压得到的ffmpeg-3.4.2-win64-shared中,把bin目录下的动态链接库复制到QtAV的编译目录的bin目录下,比如我的目录是
D:\qt-workspace\ffmpeg-3.4.2-win64-shared\bin
D:\qt-workspace\build-QtAV-Desktop_Qt_5_12_3_MinGW_64_bit-Release\bin
需要复制的动态链接库是
再次运行就不会有错误了
如果使用的是QtAV提供编译好的依赖库的话,注意编译64位要…bin/x64目录下的,32位要…/bin目录下的,如下所示
D:\qt-workspace\QtAV-depends-windows-x86+x64\bin\x64
D:\qt-workspace\build-QtAV-Desktop_Qt_5_12_3_MinGW_64_bit-Release\bin
在QtAV编译目录下双击sdk_install.bat即可,比如我的目录是
D:\qt-workspace\build-QtAV-Desktop_Qt_5_12_3_MinGW_64_bit-Release
之后库文件和头文件等就会安装到Qt Sdk相关路径下,其他使用QtAV的应用就可以依赖上了
新建一个Demo
#include
#include
#include
#include
#include
using namespace QtAV;
int main(int argc,char *argv[])
{
QApplication a(argc, argv);
WidgetRenderer renderer;
renderer.show();
Widgets::registerRenderers();
AVPlayer player;
player.setRenderer(&renderer);
player.play("test.mov");
return a.exec();
}
在Demo的pro文件中加入
QT += av avwidgets
LIBS += -lQtAV1 -lQtAVWidgets1
如果Demo的pro文件没有添加LIBS += -lQtAV1 -lQtAVWidgets1,编译的时候可能会报错undefined reference to,注意是-lQtAV1 -lQtAVWidgets1,不是-lQtAV -lQtAVWidgets
g++ -Wl,-subsystem,console -mthreads -o debug\test.exe debug/main.o -LF:\Qt5.12.3\5.12.3\mingw73_64\lib F:\Qt5.12.3\5.12.3\mingw73_64\lib\libQt5OpenGLd.a F:\Qt5.12.3\5.12.3\mingw73_64\lib\libQt5Widgetsd.a F:\Qt5.12.3\5.12.3\mingw73_64\lib\libQt5Guid.a F:\Qt5.12.3\5.12.3\mingw73_64\lib\libQt5Cored.a
debug/main.o: In function `main':
D:\qt-workspace\build-test-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/../test/main.cpp:12: undefined reference to `__imp__ZN4QtAV14WidgetRendererC1EP7QWidget6QFlagsIN2Qt10WindowTypeEE'
D:\qt-workspace\build-test-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/../test/main.cpp:14: undefined reference to `__imp__ZN4QtAV7Widgets17registerRenderersEv'
D:\qt-workspace\build-test-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/../test/main.cpp:15: undefined reference to `__imp__ZN4QtAV8AVPlayerC1EP7QObject'
D:\qt-workspace\build-test-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/../test/main.cpp:16: undefined reference to `__imp__ZN4QtAV8AVPlayer11setRendererEPNS_13VideoRendererE'
D:\qt-workspace\build-test-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/../test/main.cpp:17: undefined reference to `__imp__ZN4QtAV8AVPlayer4playERK7QString'
D:\qt-workspace\build-test-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/../test/main.cpp:15: undefined reference to `__imp__ZN4QtAV8AVPlayerD1Ev'
D:\qt-workspace\build-test-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/../test/main.cpp:15: undefined reference to `__imp__ZN4QtAV8AVPlayerD1Ev'
debug/main.o: In function `QtAV::QPainterRenderer::~QPainterRenderer()':
F:/Qt5.12.3/5.12.3/mingw73_64/include/QtAV/QPainterRenderer.h:31: undefined reference to `__imp__ZTVN4QtAV16QPainterRendererE'
F:/Qt5.12.3/5.12.3/mingw73_64/include/QtAV/QPainterRenderer.h:31: undefined reference to `__imp__ZN4QtAV13VideoRendererD2Ev'
debug/main.o: In function `QtAV::WidgetRenderer::~WidgetRenderer()':
F:/Qt5.12.3/5.12.3/mingw73_64/include/QtAVWidgets/WidgetRenderer.h:35: undefined reference to `__imp__ZTVN4QtAV14WidgetRendererE'
F:/Qt5.12.3/5.12.3/mingw73_64/include/QtAVWidgets/WidgetRenderer.h:35: undefined reference to `__imp__ZTVN4QtAV14WidgetRendererE'
F:/Qt5.12.3/5.12.3/mingw73_64/include/QtAVWidgets/WidgetRenderer.h:35: undefined reference to `__imp__ZTVN4QtAV14WidgetRendererE'
collect2.exe: error: ld returned 1 exit status
mingw32-make[1]: *** [Makefile.Debug:65: debug/test.exe] Error 1
mingw32-make: *** [Makefile:38: debug] Error 2
mingw32-make[1]: Leaving directory 'D:/qt-workspace/build-test-Desktop_Qt_5_12_3_MinGW_64_bit-Debug'
23:18:26: 进程"F:\Qt5.12.3\Tools\mingw730_64\bin\mingw32-make.exe"退出,退出代码 2 。
Error while building/deploying project test (kit: Desktop Qt 5.12.3 MinGW 64-bit)
When executing step "Make"
编译完成之后,把ffmpeg的动态库复制到应用的目录下,参照1.2小节运行Demo的做法
如果运行的时候报错QWidget: Must construct a QApplication before a QWidget
23:47:18: Starting D:\qt-workspace\build-test1-Desktop_Qt_5_12_3_MinGW_64_bit-Debug\debug\test1.exe ...
QWidget: Must construct a QApplication before a QWidget
23:47:24: 程序异常结束。
23:47:24: The process was ended forcefully.
23:47:24: D:/qt-workspace/build-test1-Desktop_Qt_5_12_3_MinGW_64_bit-Debug/debug/test1.exe crashed.
可能是应用编译成debug版本的了,QtAV库是release版本的,把应用编译成release版本即可
安装以下包,其中libopenal和libpulse是两个音频输出库
sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev
sudo apt-get install libavutil-dev libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libopenal-dev libpulse-dev libva-dev libxv-dev libass-dev libegl1-mesa-dev
然后下载ffmpeg3.4.6,解压得到ffmpeg-3.4.6,在解压目录里执行
./configure --enable-shared --disable-x86asm --prefix=/youpath/ffmpeg-3.4.6/install
下载QtAV-1.12.0,解压得到QtAV-1.12.0,在Qt Creator中导入QtAV项目
需要注意的是在qmake之前,必须安装libopenal等库,不然会没有声音输出
然后在Qt Creator设置编译时候的环境变量
变量 | 值 |
---|---|
CPATH | /youpath/ffmpeg-3.4.6/install/include |
LD_LIBRARY_PATH | /opt/Qt5.12.3/Tools/QtCreator/lib/Qt/lib:/youpath/ffmpeg-3.4.6/install/lib |
LIBRARY_PATH | /youpath/ffmpeg-3.4.6/install/lib |
LD_LIBRARY_PATH这个环境变量可能本来就有,那么在原来的值后面追加ffmpeg lib安装路径即可
编译的时候报错
error: avutil is required, but compiler can not find it
原因可能没有安装好ffmpeg或者上述的环境变量设置的路径有误,或者ffmpeg版本不对
编译完成,点击Qt Creator侧边栏的小电脑图标,选择需要运行的程序
比如选择了player,再点击运行按钮,就可以看到Demo程序了
进入QtAV的编译目录,比如我的是
/home/anruliu/Desktop/workspace/sourcecode/qt/build-QtAV-Desktop_Qt_5_12_3_GCC_64bit-Debug
里面有个sdk_install.sh文件,执行
chmod +x sdk_install.sh
sudo ./sdk_install.sh
之后库文件和头文件等就会安装到Qt Sdk相关路径下,其他使用QtAV的应用就可以依赖上了
新建一个Demo
#include
#include
#include
#include
#include
using namespace QtAV;
int main(int argc,char *argv[])
{
QApplication a(argc, argv);
WidgetRenderer renderer;
renderer.show();
Widgets::registerRenderers();
AVPlayer player;
player.setRenderer(&renderer);
player.play("test.mov");
return a.exec();
}
在Demo的pro文件中加入
QT += av avwidgets
LIBS += -lQtAV -lQtAVWidgets
再按照2.2小节的步骤添加CPATH、LD_LIBRARY_PATH、LIBRARY_PATH等环境变量,编译运行即可
如果Demo的pro文件没有添加LIBS += -lQtAV -lQtAVWidgets,编译的时候可能会报错undefined reference to
g++ -c -pipe -g -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_AVWIDGETS_LIB -DQT_AV_LIB -DQT_OPENGL_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I../xxx -I. -I/opt/Qt5.12.3/5.12.3/gcc_64/include -I/opt/Qt5.12.3/5.12.3/gcc_64/include/QtAVWidgets -I/opt/Qt5.12.3/5.12.3/gcc_64/include/QtAV -I/opt/Qt5.12.3/5.12.3/gcc_64/include/QtOpenGL -I/opt/Qt5.12.3/5.12.3/gcc_64/include/QtWidgets -I/opt/Qt5.12.3/5.12.3/gcc_64/include/QtGui -I/opt/Qt5.12.3/5.12.3/gcc_64/include/QtCore -I. -isystem /usr/include/libdrm -I/opt/Qt5.12.3/5.12.3/gcc_64/mkspecs/linux-g++ -o main.o ../xxx/main.cpp
g++ -Wl,-rpath,/opt/Qt5.12.3/5.12.3/gcc_64/lib -o xxx main.o -L/opt/Qt5.12.3/5.12.3/gcc_64/lib -lQt5OpenGL -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
main.o: In function `main':
/home/anruliu/Desktop/workspace/sourcecode/qt/build-xxx-Desktop_Qt_5_12_3_GCC_64bit-Debug/../xxx/main.cpp:12: undefined reference to `QtAV::WidgetRenderer::WidgetRenderer(QWidget*, QFlags)'
/home/anruliu/Desktop/workspace/sourcecode/qt/build-xxx-Desktop_Qt_5_12_3_GCC_64bit-Debug/../xxx/main.cpp:14: undefined reference to `QtAV::Widgets::registerRenderers()'
/home/anruliu/Desktop/workspace/sourcecode/qt/build-xxx-Desktop_Qt_5_12_3_GCC_64bit-Debug/../xxx/main.cpp:15: undefined reference to `QtAV::AVPlayer::AVPlayer(QObject*)'
/home/anruliu/Desktop/workspace/sourcecode/qt/build-xxx-Desktop_Qt_5_12_3_GCC_64bit-Debug/../xxx/main.cpp:16: undefined reference to `QtAV::AVPlayer::setRenderer(QtAV::VideoRenderer*)'
/home/anruliu/Desktop/workspace/sourcecode/qt/build-xxx-Desktop_Qt_5_12_3_GCC_64bit-Debug/../xxx/main.cpp:17: undefined reference to `QtAV::AVPlayer::play(QString const&)'
/home/anruliu/Desktop/workspace/sourcecode/qt/build-xxx-Desktop_Qt_5_12_3_GCC_64bit-Debug/../xxx/main.cpp:15: undefined reference to `QtAV::AVPlayer::~AVPlayer()'
/home/anruliu/Desktop/workspace/sourcecode/qt/build-xxx-Desktop_Qt_5_12_3_GCC_64bit-Debug/../xxx/main.cpp:15: undefined reference to `QtAV::AVPlayer::~AVPlayer()'
main.o: In function `QtAV::QPainterRenderer::~QPainterRenderer()':
/opt/Qt5.12.3/5.12.3/gcc_64/include/QtAV/QPainterRenderer.h:31: undefined reference to `vtable for QtAV::QPainterRenderer'
/opt/Qt5.12.3/5.12.3/gcc_64/include/QtAV/QPainterRenderer.h:31: undefined reference to `QtAV::VideoRenderer::~VideoRenderer()'
main.o: In function `QtAV::WidgetRenderer::~WidgetRenderer()':
/opt/Qt5.12.3/5.12.3/gcc_64/include/QtAVWidgets/WidgetRenderer.h:35: undefined reference to `vtable for QtAV::WidgetRenderer'
/opt/Qt5.12.3/5.12.3/gcc_64/include/QtAVWidgets/WidgetRenderer.h:35: undefined reference to `vtable for QtAV::WidgetRenderer'
/opt/Qt5.12.3/5.12.3/gcc_64/include/QtAVWidgets/WidgetRenderer.h:35: undefined reference to `vtable for QtAV::WidgetRenderer'
collect2: error: ld returned 1 exit status
make: *** [xxx] Error 1
20:05:29: 进程"/usr/bin/make"退出,退出代码 2 。
Error while building/deploying project xxx (kit: Desktop Qt 5.12.3 GCC 64bit)
The kit Desktop Qt 5.12.3 GCC 64bit has configuration issues which might be the root cause for this problem.
When executing step "Make"
[1] Qt第三方库QtAV— ubuntu编译与运行
[2] QtAV官方文档