Building Qt Multimedia with FFmpeg: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Mention the QT_DEPLOY_FFMPEG variable)
No edit summary
(One intermediate revision by the same user not shown)
Line 8: Line 8:
* Main (host) build for dev branch: D:\qt\build\dev\
* Main (host) build for dev branch: D:\qt\build\dev\
* Visual Studio 2022 (MSVC)
* Visual Studio 2022 (MSVC)
* Don't have perl in path as it can cause hard to understand build failures
* Have cmake in path
* Have cmake in path


==Developer setup for Qt Multimedia on Windows==
==Developer setup for Qt Multimedia on Windows==
===Initializing Qt Repo for Qt 6 with necessary dependencies for QtMultimedia===
This init-repository initializes the necessary Qt submodules for working with QtMultimedia. By only initializing the repository with the necessary modules, the main Qt build is faster than doing a full build.<blockquote>C:\Strawberry\perl\bin\perl init-repository --module-subset=qtbase,qtquick3d,qtdeclarative,qtsvg,qtshadertools,qtmultimedia,qtrepotools -f --codereview-username johanseg</blockquote>


=== Building FFmpeg ===
=== Building FFmpeg ===
Line 32: Line 28:


===Configure Qt main build===
===Configure Qt main build===
I build Qt as stand-alone module to make builds run faster when working in QtMultimedia. But first, we need to build the necessary dependencies. I do this from command line with D:\qt\build\dev as current directory. Note that if you did build FFmpeg with vcpkg, you need to update the -DFFMPEG_DIR argument to point to the C:\dev\vcpkg\installed\x64-windows directory (not the bin-directory inside).<blockquote>..\..\qt6\configure.bat -debug -developer-build -submodules QtMultimedia -nomake tests -nomake examples -- -DFFMPEG_DIR=C:\FFmpeg-n6.0\build\msvc\installed --fresh & ninja</blockquote>I work almost exclusively using debug builds. If you need a release build, configure the release build in a separate build directory. This is a developer build, and we can use D:\qt\build\dev\qtbase\ as CMAKE_PREFIX_PATH when developing other Qt modules. By configuring this way, QtMultimedia is built, which is not strictly necessary since I develop QtMultimedia in a separate build tree, but it is an easy way to make sure all QtMultimedia dependencies are built.
I build Qt as stand-alone module to make builds run faster when working in QtMultimedia. But first, we need to build the necessary dependencies. I do this from command line with D:\qt\build\dev as current directory. Note that if you did build FFmpeg with vcpkg, you need to update the -DFFMPEG_DIR argument to point to the C:\dev\vcpkg\installed\x64-windows directory (not the bin-directory inside).<blockquote>..\..\qt6\configure.bat -debug -developer-build -init-submodules -submodules qtmultimedia -nomake tests -nomake examples -codereview-username johanseg -- -DFFMPEG_DIR=C:\FFmpeg-n6.0\build\msvc\installed --fresh & ninja</blockquote>I work almost exclusively using debug builds. If you need a release build, configure the release build in a separate build directory. This is a developer build, and we can use D:\qt\build\dev\qtbase\ as CMAKE_PREFIX_PATH when developing other Qt modules. By configuring this way, QtMultimedia is built, which is not strictly necessary since I develop QtMultimedia in a separate build tree, but it is an easy way to make sure all QtMultimedia dependencies are built.


===Configure Qt multimedia===
===Configure Qt multimedia===
Line 46: Line 42:
     "configurePresets": [
     "configurePresets": [
         {
         {
             "name": "msvc-64",
             "name": "msvc",
             "hidden": true,
             "hidden": true,
             "condition": {
             "condition": {
Line 68: Line 64:
         },
         },
         {
         {
             "name": "Debug",
             "name": "qtmultimedia",
             "hidden": true,
             "inherits": "msvc",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            }
        },
        {
            "name": "qt6-dev-base",
            "hidden": true,
             "cacheVariables": {
             "cacheVariables": {
                 "FEATURE_developer_build": true,
                 "FEATURE_developer_build": true,
                "BUILD_qttools": false,
                 "QT_BUILD_EXAMPLES": true,
                "BUILD_qtdoc": false,
                "BUILD_qttranslations": false,
                 "QT_BUILD_EXAMPLES": false,
                 "QT_BUILD_EXAMPLES_BY_DEFAULT": false,
                 "QT_BUILD_EXAMPLES_BY_DEFAULT": false,
                 "QT_BUILD_EXAMPLES_AS_EXTERNAL": false,
                 "QT_BUILD_EXAMPLES_AS_EXTERNAL": false,
                 "QT_BUILD_TESTS": false,
                 "QT_BUILD_TESTS": true,
                 "QT_BUILD_TESTS_BY_DEFAULT": false
                 "QT_BUILD_TESTS_BY_DEFAULT": false,
            },
                "CMAKE_BUILD_TYPE": "Debug",
            "environment": {
                 "CMAKE_PREFIX_PATH": "D:/qt/build/dev/qtbase/",
                 "PATH": "D:/qt/build/dev/qtbase/bin;$penv{PATH}"
                "QT_DEPLOY_FFMPEG": true,
            }
                 "FFMPEG_DIR": "C:/ffmpeg-n6.1.1/build/msvc/installed"
        },
        {
            "name": "qt6-dev-debug",
            "hidden": true,
            "inherits": [
                 "qt6-dev-base",
                "msvc-64",
                "Debug"
            ],
            "cacheVariables": {
                "CMAKE_PREFIX_PATH": "D:/qt/build/dev/qtbase/"
             },
             },
             "environment": {
             "environment": {
                 "PATH": "D:/qt/build/dev/qtbase/bin;$penv{PATH}"
                 "PATH": "D:/qt/build/dev/qtbase/bin;$penv{PATH}"
            }
        },
        {
            "name": "QtMultimedia",
            "inherits": "qt6-dev-debug",
            "binaryDir": "D:/qt/build/qtmultimedia/",
            "cacheVariables": {
                "QT_BUILD_EXAMPLES": true,
                "QT_BUILD_EXAMPLES_BY_DEFAULT": true,
                "QT_BUILD_TESTS": true,
                "QT_BUILD_TESTS_BY_DEFAULT": true,
                "QT_BUILD_EXAMPLES_AS_EXTERNAL": false,
                "QT_BUILD_MANUAL_TESTS": false,
                "QT_DEPLOY_FFMPEG": true,
                "FEATURE_ffmpeg": true,
                "FFMPEG_DIR": "C:\\ffmpeg-n6.0\\build\\msvc\\installed"
             }
             }
         }
         }
Line 126: Line 85:
     "buildPresets": [
     "buildPresets": [
         {
         {
             "name": "Build-Qt-Multimedia-MSVC-Debug-64",
             "name": "Build Qt Multimedia",
            "displayName": "Build Qt Multimedia MSVC Debug 64",
             "configurePreset": "qtmultimedia"
             "configurePreset": "QtMultimedia"
         }
         }
     ]
     ]
}
}
</code>
</code>

Revision as of 17:27, 24 March 2024

This page shows one example of how to build Qt Multimedia with the FFmpeg backend on Windows. Note that there are many ways to achieve the same result with Qt, but this is how I do it.

Build setup assumed in this description

The commands in this description assumes that the build setup is like in the below list. If your setup is different, adjust the commands accordingly.

  • OS: Windows 11 with dev drive mounted on D: (Using a dev drive makes builds faster when using Windows Defender antivirus)
  • Qt source tree: D:\qt\qt6\
  • Build directory containing all builds: D:\qt\build\
  • Main (host) build for dev branch: D:\qt\build\dev\
  • Visual Studio 2022 (MSVC)
  • Have cmake in path

Developer setup for Qt Multimedia on Windows

Building FFmpeg

Option 1: Building FFmpeg on Windows using Qt's provisioning scripts

For building FFmpeg, Qt's own provisioning scripts can be very useful. These scripts are used in our CI infrastructure when building FFmpeg, but can also be used by developers

  1. Install 7-zip from provisioning: Run qt6\coin\provisioning\common\windows\install-sevenzip.ps1 as admin
    • Needed by provisioning script for FFmpeg to unzip downloaded FFmpeg
  2. Install msys2 from provisioning: Run qt6\coin\provisioning\common\windows\install-msys2.ps1 as admin
    • Needed by provisioning script for FFmpeg because FFmpeg requires tools from msys to configure
  3. Install FFmpeg from provisioning: Run qt6\coin\provisioning\common\windows\install-ffmpeg.ps1 as admin
    • Since I only need MSVC build, I edit the install-ffmpeg.ps1 first, to comment out unnecessary versions
    • This sets FFMPEG_DIR=C:\FFmpeg-n6.0\build\msvc\installed\

Option 2: Building FFmpeg on Windows using vcpkg

If you don't already have vcpkg, install vcpkg:

git clone https://github.com/microsoft/vcpkg

From within the new vcpkg directory, run

bootstrap-vcpkg.bat

Now, building FFmpeg is done by executing

vcpkg install ffmpeg[core,swresample,swscale,avdevice]:x64-windows

This will download all necessary dependencies and build FFmpeg into C:\dev\vcpkg\installed\x64-windows if vcpkg was cloned into C:\dev

Configure Qt main build

I build Qt as stand-alone module to make builds run faster when working in QtMultimedia. But first, we need to build the necessary dependencies. I do this from command line with D:\qt\build\dev as current directory. Note that if you did build FFmpeg with vcpkg, you need to update the -DFFMPEG_DIR argument to point to the C:\dev\vcpkg\installed\x64-windows directory (not the bin-directory inside).

..\..\qt6\configure.bat -debug -developer-build -init-submodules -submodules qtmultimedia -nomake tests -nomake examples -codereview-username johanseg -- -DFFMPEG_DIR=C:\FFmpeg-n6.0\build\msvc\installed --fresh & ninja

I work almost exclusively using debug builds. If you need a release build, configure the release build in a separate build directory. This is a developer build, and we can use D:\qt\build\dev\qtbase\ as CMAKE_PREFIX_PATH when developing other Qt modules. By configuring this way, QtMultimedia is built, which is not strictly necessary since I develop QtMultimedia in a separate build tree, but it is an easy way to make sure all QtMultimedia dependencies are built.

Configure Qt multimedia

I configure QtMultimedia using a CMakeUserPresets.json file that is located in D:\qt\qt6\qtmultimedia instead of using a configure script. This way, I can open the D:\qt\qt6\qtmultimedia as a folder in Visual Studio and configure it from there.

The benefit of using CMakeUserPresets.json is that Visual Studio knows how to debug the tests and examples. I have not found a way that allows Visual Studio to use an existing CMakeCache as a starting point.

To make sure FFmpeg is deployed from the provisioning or vcpkg build into the Qt bin directory, make sure to specify the QT_DEPLOY_FFMPEG=ON CMake variable.

{
    "version": 5,
    "configurePresets": [
        {
            "name": "msvc",
            "hidden": true,
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Windows"
            },
            "generator": "Ninja",
            "cacheVariables": {
                "CMAKE_CXX_COMPILER": "cl.exe",
                "CMAKE_C_COMPILER": "cl.exe"
            },
            "toolset": {
                "value": "v143,host=x64",
                "strategy": "external"
            },
            "architecture": {
                "value": "x64",
                "strategy": "external"
            }
        },
        {
            "name": "qtmultimedia",
            "inherits": "msvc",
            "cacheVariables": {
                "FEATURE_developer_build": true,
                "QT_BUILD_EXAMPLES": true,
                "QT_BUILD_EXAMPLES_BY_DEFAULT": false,
                "QT_BUILD_EXAMPLES_AS_EXTERNAL": false,
                "QT_BUILD_TESTS": true,
                "QT_BUILD_TESTS_BY_DEFAULT": false,
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_PREFIX_PATH": "D:/qt/build/dev/qtbase/",
                "QT_DEPLOY_FFMPEG": true,
                "FFMPEG_DIR": "C:/ffmpeg-n6.1.1/build/msvc/installed"
            },
            "environment": {
                "PATH": "D:/qt/build/dev/qtbase/bin;$penv{PATH}"
            }
        }
    ],
    "buildPresets": [
        {
            "name": "Build Qt Multimedia",
            "configurePreset": "qtmultimedia"
        }
    ]
}