FFmpeg编译及开发环境搭建
1.安装MSYS2
1.1 下载
可以从阿里云的镜像下载MSYS2,速度更快:mirrors.aliyun.com/msys2/
1.2 配置MSYS2
1.2.1 环境设置
在msys2的安装目录中,找到msys2_shell.cmd文件,将set MSYS2_PATH_TYPE=inherit前面的rem去掉,去掉该行的注释。
1.2.2 更新pacman源
在etc/pacman.d/底下的mirrorlist.mingw32, mirrorlist.mingw64, mirrorlist.msys三个文件中,添加国内源。
编辑/etc/pacman.d/mirrorlist.mingw32 ,在文件开头添加:
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686/
Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686/
编辑 /etc/pacman.d/mirrorlist.mingw64 ,在文件开头添加:
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64/
Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64/
编辑 /etc/pacman.d/mirrorlist.msys ,在文件开头添加:
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch/
Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch/
打开MSYS2,使用下列命令来安装编译依赖:
pacman -Syu
pacman -S nasm
pacman -S yasm
pacman -S make cmake
pacman -S diffutils
pacman -S pkg-config
pacman -S git
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw-w64-i686-toolchain
pacman -S mingw-w64-x86_64-SDL2
1.2.3 使用MSVC环境启动MSYS2
-
在开始菜单的Visual Studio子项中选择x64 Native Tool Command Prompt,以管理员身份运行。
-
进入msys2安装目录,输入msys2_shell.cmd -mingw64启动msys2
2. 编译相关库
2.1 编译libx264
- 创建
ffmpeg6_0_sources目录,用于存放源码文件。(这里默认home目录是在msys2的安装目录)
cd ~
mkdir ffmpeg6_0_sources
cd ffmpeg6_0_sources
2. 下载libx264源码
git clone --depth 1 https://github.com/mirror/x264.git
3. 编译
cd x264
CC=cl ./configure --enable-shared
make
4. 安装, 将对应的libx264.dll.lib, 拷贝并重命名为libx264.lib
make install
cp /usr/local/lib/libx264.dll.lib /usr/local/lib/libx264.lib
2.2 编译libx265
2.2.1 for Windows
- 下载libx265源码
git clone https://gitee.com/mirrors_videolan/x265.git
- 编译前注意
这里需要注意的是,不要使用msys2中的cmake,而是使用vs的。可以通过ls /usr/bin/ | grep cmake查看,如果有的话(其实按照上面的流程,我们已经使用pacman装了cmake了),可以先删掉这个cmake。
pacman -R cmake
之后可以看到在msys2中,可以访问到的cmake是vs目录下的(从vs的cmd进来的,已设置相关环境变量)。
3.编译及安装
cd x265/build/msys-cl
./make-Makefiles.sh
# 将编译好的文件拷贝到需要的目录,这里如果直接使用nmake install安装则实际是安装到vs2019对应的安装路径,所以手动拷贝
cp x265.exe libx265.dll /usr/local/bin
# 这里改下名字,265是打开x265.lib
cp libx265.lib /usr/local/lib/x265.lib
cp x265-static.lib /usr/local/lib/
cp x265_config.h /usr/local/include/
cp ../../source/x265.h /usr/local/include/
cp x265.pc /usr/local/lib/pkgconfig/
然后进到/usr/local/lib/pkgconfig/目录修改x265.pc的配置,将prefix=C:/Program Files (x86)/x265 改成 prefix=/usr/local
2.2.2 for Mac/Linux
cd x265/build
cmake ../source
make -j8
sudo make install
2.3 编译libfdk-aac
2.3.1 for Windows
- 下载fdx-aac源码
git clone https://github.com/mstorsjo/fdk-aac.git
cd fdk-aac
git checkout v2.0.1 #切换至v2.0.1版本
- 编译及安装
# 先把msys2的link改名,因为这里需要使用vs2019的link
mv /usr/bin/link.exe /usr/bin/link-bk.exe
# 编译
nmake -f Makefile.vc all
# 安装,安装后fdk相关的头文件、库文件就和x264 x265同样的目录了
nmake -f Makefile.vc prefix=/usr/local install
# 恢复link.exe
mv /usr/bin/link-bk.exe /usr/bin/link.exe
3.创建fdk-aac.pc
在/usr/local/lib/pkgconfig目录 创建fdk-aac.pc并添加以下内容:
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: Fraunhofer FDK AAC Codec Library
Description: AAC codec library
Version: 2.0.1
Libs: -L${libdir} -lfdk-aac
Libs.private:
Cflags: -I${includedir}
2.3.2 for Mac/Linux
autoreconf -i
./configure
make
make install
2.4 设置SDL2库
- 在github.com/libsdl-org/… 下载VC版本的库文件。
- 解压
将压缩包里面的include文件夹,移动至/usr/local/SDL2下
将压缩包里面的lib/x64文件夹下的SDL2.lib和SDL2main.lib,移动至/usr/local/lib下 - 设置SDL2的环境变量
export INCLUDE=$INCLUDE";D:\App\msys2\usr\local\include\SDL2"
export LIB=$LIB";D:\App\msys2\usr\local\lib"
- 配置SDL2对pkg-config可见 在/usr/local/lib/pkgconfig目录下新建sdl2.pc文件,添加如下内容:
prefix==/usr/local
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include
Name: sdl2
Description: Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer.
Version: 2.26.5
Requires:
Conflicts:
Libs: -L${libdir} -lSDL2main -lSDL2
Cflags: -I${includedir}
2.5 源码编译ffmpeg
- 下载源代码
cd ~/ffmpeg6.0_sources
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg
git checkout remotes/origin/release/6.0
# 返回上一级别
cd ..
# 拷贝一份ffmpeg源码,因为原始的ffmpeg编译代码在msvc编译器有些地方报错,需要做修改
cp -arf FFmpeg ffmpeg6.0
2. 配置, 编译
cd ffmpeg6.0
# 注意,下面这里是编译动态库,如果要编译静态库,去掉--enable-shared
# 加入 --prefix=./mybuildpath, 之后可以将生成的lib, dll, include文件等放在指定的文件夹下,方便window原生开发环境添加下使用,不依赖msys的环境目录。
CC=cl.exe PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/ ./configure --toolchain=msvc --enable-shared --enable-ffplay --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-sdl2 --enable-gpl --enable-nonfree --disable-optimizations --disable-stripping
make -j8
for Mac xcode15
注意,这里xcode15可能会有link的报错,需要在configure之前设置环境变量export LDFLAGS="-Wl,-ld_classic,-rpath,/usr/local/lib"
ld: building exports trie: duplicate symbol '_av_ac3_parse_header'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libavdevice/libavdevice.60.dylib] Error 1
make: *** Waiting for unfinished jobs....
这里如果不加"-rpath,/usr/local/lib",可能编译出来,找不到其他的动态库, 会出现Library not loaded
$ ffplay -i test.mp4
dyld[72939]: Library not loaded: @rpath/libx265.198.dylib
Referenced from: <A66A6E3B-007A-32F5-B52E-A4627283DD51> /usr/local/bin/ffplay
Reason: no LC_RPATH's found
[1] 72939 abort ffplay
这个就是对应的动态库没找到, 可以用otool命令行工具查看, 发现这里是相对于rpath
❯ otool -L libx265.198.dylib ✔ 12:19:46
libx265.198.dylib:
@rpath/libx265.198.dylib (compatibility version 198.0.0, current version 198.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1600.151.0)
3. 安装
make install
- 注意
使用VC编译在编译过程中可能会出现一些异常,主要是log及编码引起的。这种情况的错误一般是在输出log的代码中出现,如果有,注释掉出现错误的行则可。
如果生成静态库的话就是libxxxx.a, 在windows下与Qt联用一般要改成libxxxx.lib
3.测试
能够正常启动ffplay播放则可。
ffplay.exe http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
ffplay.exe http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4
参考资料
- Windows vs2019 ffmpeg6.0开发环境搭建
- 《FFmpeg入门详解——SDK二次开发与直播美颜原理及应用》 梅会东
- 指定运行时动态库搜索位置:-rpath链接指令的正确用法(相对路径or绝对路径)
- xcode15 duplicate symbol 问题
- Xcode关于dyld: Library not loaded解决方法