windows ffmpeg2.8.x(4.2.x) SDK 编译(引入x264,opus)

环境

所有库都是在 msys 中 进行32位编译

msys环境安装

修改 msys 程序目录的 msys2_shell.cmd

rem set MSYS2_PATH_TYPE=inherit 
改为
set MSYS2_PATH_TYPE=inherit

vs Command Prompt 中 打开 msys 继承 x86(x64) vs 的环境变量,

msys 可以使用 cl 编译器,使用对应x86(x64)下的系统依赖库

vsx86 Native Tools Command Prompt for 2019 中输入

msys2_shell.cmd -mingw32 启动 msys

64位 就打开 x64Command Prompt ,和 -mingw64

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.11.17
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community>msys2_shell.cmd -mingw32

opus-1.3.1

根据github的描述,

To build from a distribution tarball, you only need to do the following:

    % ./configure
    % make

To build from the git repository, the following steps are necessary:

0) Set up a development environment:

1) Clone the repository:

    % git clone https://gitlab.xiph.org/xiph/opus.git
    % cd opus

2) Compiling the source

    % ./autogen.sh
    % ./configure
    % make

直接在官网下载压缩包opus-1.3.1.tar.gz进行编译可以少一步

CC=cl ./configure --prefix=`pwd`/build --disable-rtcd
--enable-shared (--enable-static) --disable-asm --disable-rtcd 
make -j8 && make install

指定 --prefix= 可以在make install 的时候把头文件,库和相关文档放到指定路径里面

--disable-asm 纯c实现,不使用汇编代码优化

--disable--rtcd 关闭运行时CPU能力检测,按字面意思理解是少了一个进程或者线程一直检测cpu

x264

git clone http://git.videolan.org/git/x264.git
./configure --disable-asm --enable-static --disable-cli --enable-debug --prefix=`pwd`/build --host=mingw32

静态库后缀是.a,直接加载即可

动态库可以通过vs工具把动态库的.a转为windows.lib

CC=cl \
./configure --disable-asm --enable-shared --enable-static  --disable-cli --enable-debug --prefix=`pwd`/build

编译出来的静态库动态库windows的lib和dll

make -j8 && make install
  • 64位 x64 Native Tools Command Prompt

ffmpeg SDK编译

  • 官方编译文档1

  • 官方编译文档2

  • 安装 yasm

    pacman -S yasm

  • 引入库

    export PKG_CONFIG_PATH=/t/library/deps/opus-1.3.1/lib/pkgconfig/:/t/library/deps/x264/lib/pkgconfig/:$PKG_CONFIG_PATH
    
    pkg-config --cflags opus(x264) 检查头文件引用路径
    pkg-config --libs opus(x264) 检查库路径
    

    不对的话,修改/库路径/pkgconfig/*.pc

  • 编译

    ./configure --prefix=`pwd`/build --toolchain=msvc \
    --disable-doc \
    --enable-gpl \
    --arch=x86 \
    --target-os=win32 \
    --enable-shared 
    
    x64
    把x86和win32的选项去掉
    
    关闭exe的生成,加速编译
    --disable-programs
    
    引入其他库
    --enable-libx264 
    --enable-libopus
    
    不使用 pkg-config 手动引入库路径 
    --extra-cflags="-I`pwd`/../deps/x264/include"  
    --extra-ldflags="-LIBPATH:`pwd`/../deps/x264/lib" 
    
  • 编译动态库Debug

    ./configure --prefix=`pwd`/builddebug --toolchain=msvc \
    --disable-x86asm \
    --disable-doc \
    --arch=x86 \
    --target-os=win32 \
    --enable-gpl \
    --enable-shared \
    --enable-debug \
    --enable-libx264 \
    --disable-stripping \
    --disable-optimizations \
    --disable-programs \
    --extra-cflags="-MDd" \
    --extra-cxxflags="-MDd" 
    

    生成的pdb文件在对应模块目录里面

  • 编译静态库Debug

    ./configure --prefix=`pwd`/buildstaticdebug --toolchain=msvc \
    --disable-x86asm \
    --disable-doc \
    --arch=x86 \
    --target-os=win32 \
    --enable-gpl \
    --enable-static \
    --disable-shared \
    --enable-debug \
    --enable-libx264 \
    --disable-stripping \
    --disable-optimizations \
    --disable-programs \
    --extra-cflags="-MTd" \
    --extra-cxxflags="-MTd" \
    --extra-ldflags="-nodefaultlib:LIBCMT"
    

    编译失败,出现未定义符号,将 x264.libBcrypt.lib 引用到工程中

  • 安装

    make -j8 && make install
    
  • 更多

    关于 WINAPI_FAMILY

    更多配置配置项查看 configure 文件

    make 失败,输出常量中有换行符,删除 config.h#define CC_IDENT 的中文

编译ffmpeg常见问题

编译失败查看 ffmpeg 目录下的config.log,看最后的报错

ERROR: libx264 not found

x264.lib 复制改名为libx264.lib

找不到cl.exe 或者系统.lib一般都是没有从 Native Tools Command Prompt 中打开,例如

cl not found
找不到 msvcrt.lib
找不到 kernel32.lib

windows 使用ffmpeg常见问题

  • c++ 使用 ffmpeg 头文件没用 extern "C" {} 包裹起来,编译失败

  • 无法解析的外部符号

    1>avutil.a(random_seed.o) : error LNK2019: 无法解析的外部符号 __imp__CryptAcquireContextA@20,函数 _av_get_random_seed 中引用了该符号
    1>avutil.a(random_seed.o) : error LNK2019: 无法解析的外部符号 __imp__CryptReleaseContext@8,函数 _av_get_random_seed 中引用了该符号
    1>avutil.a(random_seed.o) : error LNK2019: 无法解析的外部符号 __imp__CryptGenRandom@12,函数 _av_get_random_seed 中引用了该符号
    
    #pragma comment(lib, "Advapi32.lib")
    
posted @ 2022-08-17 19:45  blackTree  阅读(425)  评论(0编辑  收藏  举报