ffmpeg开发教程(2) :编译第一个示例项目

ffmpeg-tutorial 克隆到本地, 其项目结构如下:

.
├── CMakeLists.txt
├── README.md
├── apps
│   ├── CMakeLists.txt
│   └── tutorial01
│       └── CMakeLists.txt
├── bunny.mp4
├── cmake-build-debug
├── docs
│   └── CMakeLists.txt
├── include
│   ├── fact.hpp
│   └── tutorial.hpp
├── libs
│   ├── CMakeLists.txt
│   └── FFmpeg
│       └── CMakeLists.txt
├── src
│   ├── fact.cpp
│   ├── tutorial01.cpp
│   └── tutorial02.cpp
└── tests
    ├── CMakeLists.txt
    └── all_tests.cpp

根目录下CMakeLists.txt为总控CMakeLists文件,子目录下的CMakeLists.txt比如apps, docs, libs, tests下为子目录CMakeLists文件,分别用来编译应用,文档,引用到的软件包和单元测试。

我们先来看下根目录下的CMakeLists定义:

cmake_minimum_required(VERSION 3.12)
if (DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
            CACHE STRING "")
endif ()

set(VCPKG_TARGET_TRIPLET $ENV{VCPKG_DEFAULT_TRIPLET} CACHE INTERNAL "" FORCE)

project(ffmpeg_tutorial)

function(TUTORIAL name)
    set(TUTORIAL_TARGET ${name})
    add_executable(${TUTORIAL_TARGET} ${CMAKE_HOME_DIRECTORY}/src/${TUTORIAL_TARGET}.cpp)
    target_link_libraries(${TUTORIAL_TARGET} PRIVATE FFmpeg)
    if (WIN32)
        install(TARGETS ${name}
                RUNTIME DESTINATION ${CMAKE_BINARY_DIR})
    endif ()

endfunction(TUTORIAL)

include_directories("include" )
add_subdirectory(libs)
add_subdirectory(apps)
add_subdirectory(docs)
add_subdirectory(tests)

上篇文章我们提到使用vcpkg来管理C++软件包,因此在根CMakeLists文件中我们可以设定MAKE_TOOLCHAIN_FILE,指到vcpkg的目录,这样使用vcpkg安装所依赖的软件包后,CMake可以找到所需的库文件定义和引用。

而set(VCPKG_TARGET_TRIPLET $ENV{VCPKG_DEFAULT_TRIPLET} CACHE INTERNAL “” FORCE)
定义库定义对应的操作系统和计算机硬件架构比如32位或是64位, 使用静态库还是动态库。
最后添加各个子目录的CMakeLists定义。

使用命令行编译 (Mac,Linux,Windows)

mkdir build
cd build
cmake ..
ninja

使用CLion(Mac,Linux,Windows)

使用CLion前需要配置CMake

-DCMAKE_TOOLCHAIN_FILE=~/Workspace/vcpkg/scripts/buildsystems/vcpkg.cmake -GNinja

使用XCode (Mac)

首先使用CMake 生成XCode workspace 文件。

mkdir xcode
cd xcode
cmake -G Xcode ..

-- The C compiler identification is AppleClang 11.0.0.11000033
-- The CXX compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done

生成ffmpeg_tutorial.xcodeproj,就可以使用XCode编译调试该项目了:

使用 Visual Studio (Windows)

同样可以使用-G 来生成 Visual Studio 项目

cmake -G "Visual Studio 16 2019" -A x64

cmake -G "Visual Studio 15 2017" -A Win32

cmake -G "Visual Studio 15 2017" -A x64

cmake -G "Visual Studio 15 2017" -A ARM

cmake -G "Visual Studio 15 2017" -A ARM64

对于不同的Visual Studio 版本和 32位,64位静态使用不同的参数,比如使用Visual Studio 2017,64位机器

C:\Workspace\ffmpeg-tutorial\vs2017 (master -> origin)
λ cmake -G "Visual Studio 15 2017" -A x64 ..
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
-- The C compiler identification is MSVC 19.16.27035.0
-- The CXX compiler identification is MSVC 19.16.27035.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe - works -- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Workspace/ffmpeg-tutorial/vs2017

然后使用Visual Studio 2017打开ffmpeg_tutorial.sln

使用VSCode (Mac,Linux, Windows)

VSCode可以直接打开该项目,只是需要创建一个运行的配置文件,参考如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/apps/tutorial01/tutorial01",
            "args": ["./bunny.mp4"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb"
        }
    ]
}

之后的教程,你可以选择自己喜欢的开发环境,或者直接命令行。