编译:
1.下载源码
http://www.qtav.org/
我下载的版本是
2017-06-21: 1.12.0 Released - CMake Support
2.下载QtAV的依赖:ffmpeg and OpenAL
这是QtAV提供的依赖,不是自己去ffmpeg官网下载的
3.安装QtAV的依赖,本人使用的是Qt5.13.1+msvc2017(32位)
将下载的依赖库解压,得到QtAV-depends-windows-x86+x64
将QtAV-depends-windows-x86+x64\include中的目录复制到D:\Qt\Qt5.13.1\5.13.1\msvc2017\include
将QtAV-depends-windows-x86+x64\lib中的目录复制到D:\Qt\Qt5.13.1\5.13.1\msvc2017\lib
和
因为是32位版本,所以不是复制x64目录下的文件,qt安装路径也可能各异,
因为自己的项目也会用的ffmepg的库,为了避免冲突,所以上述libav相关的库和头文件,都是从自己项目的ffmpeg中复制过去的,有版本差异。
4.编译、安装AV
QtCreator打开项目后直接编译即可,会产生很多lib、dll、exe
因为要用到项目里,所以只需要lib_win_x86目录里面的东西即可,不需要手动复制,直接双击sdk_install.bat即可安装,
sdk_install.bat里面执行的也就是复制文件的操作而已
5.应用到项目
我是把QtAV集成到Qml中,所以没有用C++ code
pro文件中
QT += quick \
quickwidgets \
av
CONFIG += c++11
只需要添加av即可
qml文件中
import QtQuick 2.9
import QtQuick.Window 2.2
import QtAV 1.6
Rectangle {
visible: true
width: 640
height: 480
anchors.centerIn: parent
color: "#3A78E6"
Item {
anchors.fill: parent
VideoOutput2 {
anchors.fill: parent
source: player
}
AVPlayer { //or MediaPlayer
id: player
source: "file:///E:/1.mp4"
}
MouseArea {
anchors.fill: parent
onClicked: player.play()
}
}
}
项目不是纯qml项目,qml还是加入到QWidget中的,所以跟官方例子还是有点区别
6.发布
windeployqt部署后,
需要手动复制ffmpeg的动态库,因为是下载的QtAV官方的依赖库,所以把dll都放进去就可以了
还需要手动复制QtAV生成的两个动态库QtAVd1.dll、QtAVWidgetsd1.dll
qml依赖的QmlAVd.dll不用复制,windeployqt带--qmldir参数的时候就已经复制了
根据平台和位数的不同,编译出来的dll名字可能有所不同
效果:
qml代码:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtAV 1.6
Rectangle {
visible: true
width: 800
height: 600
property bool isPaused: true
Item {
anchors.fill: parent
VideoOutput2 {
anchors.fill: parent
source: player
}
AVPlayer { //or MediaPlayer
id: player
source: "file:///E:/1.mp4"
onPositionChanged: {
if(false === isPaused){
positionButton.x = position*progressBar.width/duration - positionButton.width/2
}
}
}
}
Rectangle{
id:progressBar
height: 4
width: parent.width
radius: height
color: "#ececec"
y:parent.height - height - 20
Rectangle{
height: progressBar.height
width: positionButton.x + positionButton.width/2
radius: height
color: "#3A78E6"
}
Rectangle{
id:positionButton
width: 10
height: width
radius: height
color: "#3A78E6"
anchors.verticalCenter: parent.verticalCenter
MouseArea{
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
drag.target: positionButton
drag.axis: Drag.XAxis
drag.minimumX: -positionButton.width/2
drag.maximumX: progressBar.width - positionButton.width/2
onReleased: {
player.seek(player.duration*(positionButton.x +positionButton.width/2)/progressBar.width)
}
}
}
}
Row{
height: pauseButton.height
anchors.horizontalCenter: parent.horizontalCenter
y:parent.height - height - 40
Rectangle{
id:pauseButton
width:pauseText.width + pauseText.height*2
height: pauseText.height*2
Text {
id: pauseText
text: isPaused?qsTr("播放"):qsTr("暂停")
anchors.centerIn: parent
}
MouseArea{
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: {
if(isPaused){
player.play()
}else{
player.pause()
}
isPaused = !isPaused
}
}
}
}
}
注意:
1.要把ffmpeg的动态库复制到项目输出目录,不然会报错
plugin cannot be loaded for module "QtAv": Cannot load library D:\Qt\Qt5.13.1\5.13.1\msvc2017\qml\QtAV\QmlAVd.dll
2.import QtAV 1.6要区分大小写,不然会报错
Module namespace 'QtAV' does not match import URI 'QtAv'