一、概述
官网链接:
CompilationGuide – FFmpeghttps://trac.ffmpeg.org/wiki/CompilationGuide在官网上介绍了四种windows上的编译方法,目前仅试了第一种
CompilationGuide/MinGW – FFmpeghttps://trac.ffmpeg.org/wiki/CompilationGuide/MinGW
二、具体操作步骤
1、预置条件:
操作系统:win10 64bit
vs:vs2019
2、下载软件
从官网上下载安装x86_64版本安装程序。
MSYS2Software Distribution and Building Platform for Windowshttps://www.msys2.org/ 安装完成之后,先把安装目录下的msys2_shell.cmd中注释掉的rem set MSYS2_PATH_TYPE=inherit改成set MSYS2_PATH_TYPE=inherit。这是为了将vs的环境继承给MSYS2。
3、安装软件包
a) 按照官网指导,安装gcc、pkg-config等软件包。
pacman -Syu
pacman -Su
pacman -S mingw-w64-x86_64-toolchain
pacman -S make gcc diffutils pkg-config
pacman -S git
b) yasm下载
下载地址:Download - The Yasm Modular Assembler Project,
下载其64位版本Win64 .exe (64 位 Windows 通用),即页面中的Win64 .exe (for general use on 64-bit Windows)。下载后,将yasm-1.3.0-win64.exe 改名为yasm.exe,并放置于 MSYS2 安装目录:/msys64/usr/bin/ 中。
三、注意事项
1、打开编译界面
使用x64 Native Tools Command Prompt for VS 2019打开msys2界面。命令行:
msys2_shell.cmd -mingw64
2、检查编译环境
//配置cl和link的环境变量
export PATH="/c/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/HostX64/x64":$PATH
# which cl link yasm cpp
/c/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/HostX64/x64/cl
/c/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/HostX64/x64/link
/usr/bin/yasm
/mingw64/bin/cpp
注意:重命名msys64/usr/bin/link.exe 为msys64/usr/bin/link.bak, 避免和MSVC 的link.exe冲突。
3、配置include和lib路径。
需要注意的是写include文件和lib文件路径是不要加tab键,否则可能会导致生成的makefile异常。
./configure --toolchain=msvc --target-os=win64 \
--arch=x86_64 \
--enable-shared \
--enable-small \
--enable-version3 \
--enable-gpl \
--enable-nonfree \
--disable-stripping \
--disable-encoders \
--disable-decoders \
--enable-decoder=h264 \
--enable-encoder=libx264 \
--enable-encoder=mjpeg \
--enable-encoder=mpeg4 \
--enable-libx264 \
--extra-cflags="-I/mingw64/include -I/usr/local/include" \
--extra-ldflags="-LIBPATH:/mingw64/lib -LIBPATH:/usr/local/lib/"
4、异常排查
若是执行./configure,生成makefile失败,可以查看ffmpeg\ffbuild\config.log,排查问题。
这里面有比较详细的报错信息。
5、关于NV硬编码
1)报错信息
ERROR: nvenc requested, but not all dependencies are satisfied: ffnvcodec
2)报错原因
NVIDIA headers已经不包含在ffmpeg工程里面,需要自己在本地编译。
3)解决办法
a) 预置条件:安装CUDA toolkitCUDA Toolkit 11.5 Update 1 Downloads | NVIDIA Developerhttps://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exe_local
b)安装对应版本的SDK文件,SDK下载路径:
下载的SDK要与nv-codec-headers版本一致,并且满足ffmpeg对NV版本的要求
ffmpeg里面的configure文件对nv-codec-headers版本的要求。
https://github.com/FFmpeg/nv-codec-headers/tagshttps://github.com/FFmpeg/nv-codec-headers/tags
d) 编译步骤:
//首先要保证PKG_CONFIG_PATH里面路径包含ffnvcodec.pc
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
make
make install
6、关于Zilb
1)报错信息
zlib requested but not found
2)解决办法
Platform Specific Informationhttps://ffmpeg.org/platform.html
参见:4.2 Microsoft Visual C++ or Intel C++ Compiler for Windows
- Grab the zlib sources. zlib Home Site
- Edit win32/Makefile.msc so that it uses -MT instead of -MD, since this is how FFmpeg is built as well.
- Edit zconf.h and remove its inclusion of unistd.h. This gets erroneously included when building FFmpeg.
- Run nmake -f win32/Makefile.msc.
- Move zlib.lib, zconf.h, and zlib.h to somewhere MSVC can see.