Linux、Windows静态编译ffmpeg 4.4.1、x264、x265等编解码库的脚本


说明

Ubuntu、Centos和Windows三平台的ffmpeg编译脚本,包含x264、x265、cuda加速、fdk-aac、opus以及openssl和rtmp库。

注释中有相关包的下载地址,需要先下载解压。
Windows需要安装msys2,并在mingw64下编译。
如果需要openssl则把openssl的相关的脚本注释去掉,并在ffmpeg脚本参数--extra-libs中添加上 -lssl -lcrypto
如果需要rtmp库,则必须启用openssl,并在ffmpeg脚本参数中启用--enable-rtmp,现在是禁用状态。(其实没必要启用,只要支持flv格式就推拉流)

ubuntu

#!/bin/bash
set -eo pipefail

export LOCAL_PATH=$(pwd)
export BUILD_PATH=$LOCAL_PATH/ffmpeg_build_ubuntu
export PKG_CONFIG_PATH=$BUILD_PATH/lib/pkgconfig
export LD_LIBRARY_PATH=$BUILD_PATH/lib
export CFLAGS=-I$BUILD_PATH/include
export CPPFLAGS=-I$BUILD_PATH/include
export LDFLAGS=-L$BUILD_PATH/lib
export PATH=$BUILD_PATH/bin:$PATH

export CHOST=x86_64
export HOST=x86_64-linux
export BASE_PATH=/usr/bin
export CC=$BASE_PATH/gcc
export CXX=$BASE_PATH/g++
export AR=$BASE_PATH/ar
export RANLIB=$BASE_PATH/ranlib
export STRIP=$BASE_PATH/strip

sudo apt install -y build-essential cmake git autoconf automake pkgconf libtool

# build yasm
# aria2c http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
# tar -zxvf yasm-1.3.0.tar.gz
cd $LOCAL_PATH/yasm-1.3.0
rm -rf build && mkdir -p build && cd build
../configure --prefix="$BUILD_PATH" --host=$CHOST
make -j8 && make install
make clean
cd $LOCAL_PATH

# # build nasm
# aria2c https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz
# tar -zxvf nasm-2.15.05.tar.gz
cd $LOCAL_PATH/nasm-2.15.05
./autogen.sh
./configure --prefix="$BUILD_PATH"
make -j8 && make install
make clean
cd $LOCAL_PATH

# build zlib
# aria2c https://www.zlib.net/zlib-1.2.11.tar.gz
# tar -zxvf zlib-1.2.11.tar.gz
cd $LOCAL_PATH/zlib-1.2.11
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --static
make -j8 && make install
make clean
cd $LOCAL_PATH

# # build openssl
# # aria2c https://www.openssl.org/source/openssl-1.1.1k.tar.gz
# # tar -zxvf openssl-1.1.1k.tar.gz
# cd $LOCAL_PATH/openssl-1.1.1k
# rm -rf build && mkdir -p build && cd build
# ../Configure \
#     --prefix=$BUILD_PATH \
#     no-shared \
#     no-tests \
#     -static \
#     threads \
#     zlib \
#     linux-x86_64 \
#     '-Wl,-rpath,$($BUILD_PATH/lib)'
# make -j8 && make install_sw
# make clean
# cd $LOCAL_PATH

# # build librtmp git@github.com:rise-worlds/rtmpdump.git
# cd $LOCAL_PATH/rtmpdump/librtmp
# git clean -fdx
# make -j8 SHARED= SO_INST= CRYPTO=OPENSSL SYS=posix \
#     prefix=$BUILD_PATH \
#     CFLAGS=-I$BUILD_PATH/include \
#     LDFLAGS=-L$BUILD_PATH/lib \
#     XLIBS="-ldl" \
#     install
# make clean
# cd $LOCAL_PATH

# build nv-codec
# aria2c https://github.com/FFmpeg/nv-codec-headers/releases/download/n11.1.5.0/nv-codec-headers-11.1.5.0.tar.gz -o nv-codec-headers-11.1.5.0.tar.gz
# tar -zxvf nv-codec-headers-11.1.5.0.tar.gz
cd $LOCAL_PATH/nv-codec-headers-11.1.5.0
make -j8 PREFIX=$BUILD_PATH install
cd $LOCAL_PATH

# build x264 https://code.videolan.org/videolan/x264.git
# aria2c https://code.videolan.org/videolan/x264/-/archive/stable/x264-stable.tar.gz -o x264-stable.tar.gz
# tar xzvf x264-stable.tar.gz
cd $LOCAL_PATH/x264-stable/
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --host=$HOST \
    --disable-cli \
    --disable-asm \
    --enable-shared=no \
    --enable-static
make -j8 && make install
make clean
cd $LOCAL_PATH

# build x265
# aria2c http://ftp.videolan.org/pub/videolan/x265/x265_3.5.tar.gz -o x265_3.5.tar.gz
# tar xzvf x265_3.5.tar.gz
cd $LOCAL_PATH/x265_3.5/source
rm -rf build && mkdir -p build && cd build
cmake .. -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$BUILD_PATH -DENABLE_SHARED=OFF -DENABLE_ASSEMBLY=OFF
make -j8 && make install
make clean
cd $LOCAL_PATH

# build fdk-aac git@github.com:mstorsjo/fdk-aac.git
# aria2c https://sourceforge.net/projects/opencore-amr/files/fdk-aac/fdk-aac-2.0.2.tar.gz/download -o fdk-aac-2.0.2.tar.gz
# tar -zxvf fdk-aac-2.0.2.tar.gz
cd $LOCAL_PATH/fdk-aac-2.0.2
./autogen.sh
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --host=$HOST \
    --enable-shared=no \
    --enable-static
make -j8 && make install
make clean
cd $LOCAL_PATH

# build opus https://github.com/xiph/opus.git
# aria2c https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz
# tar -zxvf opus-1.3.1.tar.gz
cd $LOCAL_PATH/opus-1.3.1
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix="$BUILD_PATH" \
    --host=$HOST \
    --disable-shared
make -j8 && make install
make clean
cd $LOCAL_PATH

# build ffmpeg https://git.ffmpeg.org/ffmpeg.git
# aria2c http://ffmpeg.org/releases/ffmpeg-4.4.1.tar.xz
# tar -xvJf ffmpeg-4.4.1.tar.xz
cd $LOCAL_PATH/ffmpeg-4.4.1
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --disable-debug \
    --target-os=linux \
    --cc=$CC \
    --ranlib=$RANLIB \
    --strip=$STRIP \
    --pkg-config-flags="--static" \
    --extra-cflags="-std=c11 -I$BUILD_PATH/include -I$BUILD_PATH/include/opus" \
    --extra-ldflags="-L$BUILD_PATH/lib" \
    --extra-libs="-latomic -lpthread -lm" \
    --enable-static \
    --disable-shared \
    --disable-asm \
    --disable-doc \
    --disable-openssl \
    --disable-libvpx \
    --disable-libwebp \
    --disable-lzma \
    --disable-bzlib \
    --enable-gpl \
    --enable-nonfree \
    --disable-librtmp \
    --enable-libx264 \
    --enable-libx265 \
    --enable-libfdk-aac \
    --enable-cuda --enable-cuvid --enable-nvenc \
    --enable-libopus
make -j8 && make install
make clean
cd $LOCAL_PATH

centos

#!/bin/bash
set -eo pipefail

export LOCAL_PATH=$(pwd)
export BUILD_PATH=$LOCAL_PATH/ffmpeg_build_centos
export PKG_CONFIG_PATH=$BUILD_PATH/lib/pkgconfig
export LD_LIBRARY_PATH=$BUILD_PATH/lib
export CFLAGS=-I$BUILD_PATH/include
export CPPFLAGS=-I$BUILD_PATH/include
export LDFLAGS=-L$BUILD_PATH/lib
export PATH=$BUILD_PATH/bin:$PATH

export CHOST=x86_64
export HOST=x86_64-linux
export BASE_PATH=/opt/rh/devtoolset-9/root/usr/bin
export CC=$BASE_PATH/gcc
export CXX=$BASE_PATH/g++
export AR=$BASE_PATH/ar
export RANLIB=$BASE_PATH/ranlib
export STRIP=$BASE_PATH/strip

yum -y install centos-release-scl
yum -y install llvm-toolset-7-cmake
yum -y install devtoolset-9
yum -y install gcc gcc-c++ gdb make automake autoconf glibc-static libstdc++-static git libtool tcl tcl-devel

source /opt/rh/llvm-toolset-7/enable
source /opt/rh/devtoolset-9/enable

# build yasm
# aria2c http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
# tar -zxvf yasm-1.3.0.tar.gz
cd $LOCAL_PATH/yasm-1.3.0
rm -rf build && mkdir -p build && cd build
../configure --prefix="$BUILD_PATH" --host=$CHOST
make -j8 && make install
make clean
cd $LOCAL_PATH

# # build nasm
# aria2c https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz
# tar -zxvf nasm-2.15.05.tar.gz
cd $LOCAL_PATH/nasm-2.15.05
./autogen.sh
./configure --prefix="$BUILD_PATH"
make -j8 && make install
make clean
cd $LOCAL_PATH

# build zlib
# aria2c https://www.zlib.net/zlib-1.2.11.tar.gz
# tar -zxvf zlib-1.2.11.tar.gz
cd $LOCAL_PATH/zlib-1.2.11
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --static
make -j8 && make install
make clean
cd $LOCAL_PATH

# # build openssl
# # aria2c https://www.openssl.org/source/openssl-1.1.1k.tar.gz
# # tar -zxvf openssl-1.1.1k.tar.gz
# cd $LOCAL_PATH/openssl-1.1.1k
# rm -rf build && mkdir -p build && cd build
# ../Configure \
#     --prefix=$BUILD_PATH \
#     no-shared \
#     no-tests \
#     -static \
#     threads \
#     zlib \
#     linux-x86_64 \
#     '-Wl,-rpath,$($BUILD_PATH/lib)'
# make -j8 && make install_sw
# make clean
# cd $LOCAL_PATH

# # build librtmp git@github.com:rise-worlds/rtmpdump.git
# cd $LOCAL_PATH/rtmpdump/librtmp
# git clean -fdx
# # make -j8 SHARED= SO_INST= CRYPTO=OPENSSL SYS=posix \
# make -j8 SHARED= SO_INST= CRYPTO= SYS=posix \
#     prefix=$BUILD_PATH \
#     CFLAGS=-I$BUILD_PATH/include \
#     LDFLAGS=-L$BUILD_PATH/lib \
#     XLIBS="-ldl" \
#     install
# make clean
# cd $LOCAL_PATH

# build nv-codec
# aria2c https://github.com/FFmpeg/nv-codec-headers/releases/download/n11.1.5.0/nv-codec-headers-11.1.5.0.tar.gz -o nv-codec-headers-11.1.5.0.tar.gz
# tar -zxvf nv-codec-headers-11.1.5.0.tar.gz
cd $LOCAL_PATH/nv-codec-headers-11.1.5.0
make -j8 PREFIX=$BUILD_PATH install
cd $LOCAL_PATH

# build x264 https://code.videolan.org/videolan/x264.git
# aria2c https://code.videolan.org/videolan/x264/-/archive/stable/x264-stable.tar.gz -o x264-stable.tar.gz
# tar xzvf x264-stable.tar.gz
cd $LOCAL_PATH/x264-stable/
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --host=$HOST \
    --disable-cli \
    --disable-asm \
    --enable-shared=no \
    --enable-static
make -j8 && make install
make clean
cd $LOCAL_PATH

# build x265
# aria2c http://ftp.videolan.org/pub/videolan/x265/x265_3.5.tar.gz -o x265_3.5.tar.gz
# tar xzvf x265_3.5.tar.gz
cd $LOCAL_PATH/x265_3.5/source
rm -rf build && mkdir -p build && cd build
cmake .. -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$BUILD_PATH -DENABLE_SHARED=OFF -DENABLE_ASSEMBLY=OFF
make -j8 && make install
make clean
cd $LOCAL_PATH

# build fdk-aac git@github.com:mstorsjo/fdk-aac.git
# aria2c https://sourceforge.net/projects/opencore-amr/files/fdk-aac/fdk-aac-2.0.2.tar.gz/download -o fdk-aac-2.0.2.tar.gz
# tar -zxvf fdk-aac-2.0.2.tar.gz
cd $LOCAL_PATH/fdk-aac-2.0.2
./autogen.sh
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --host=$HOST \
    --enable-shared=no \
    --enable-static
make -j8 && make install
make clean
cd $LOCAL_PATH

# build opus https://github.com/xiph/opus.git
# aria2c https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz
# tar -zxvf opus-1.3.1.tar.gz
cd $LOCAL_PATH/opus-1.3.1
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix="$BUILD_PATH" \
    --host=$HOST \
    --disable-shared
make -j8 && make install
make clean
cd $LOCAL_PATH

## build lame https://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
#cd $LOCAL_PATH/lame-3.100
#./configure --prefix="$BUILD_PATH" --disable-shared --enable-nasm
#make -j8 && make install
#make clean

# build ffmpeg https://git.ffmpeg.org/ffmpeg.git
# aria2c http://ffmpeg.org/releases/ffmpeg-4.4.1.tar.xz
# tar -xvJf ffmpeg-4.4.1.tar.xz
cd $LOCAL_PATH/ffmpeg-4.4.1
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --disable-debug \
    --target-os=linux \
    --cc=$CC \
    --ranlib=$RANLIB \
    --strip=$STRIP \
    --pkg-config-flags="--static" \
    --extra-cflags="-std=c11 -I$BUILD_PATH/include -I$BUILD_PATH/include/opus" \
    --extra-ldflags="-L$BUILD_PATH/lib" \
    --extra-libs="-lpthread -lm" \
    --enable-static \
    --disable-shared \
    --disable-asm \
    --disable-doc \
    --disable-openssl \
    --disable-libvpx \
    --disable-libwebp \
    --disable-lzma \
    --disable-bzlib \
    --enable-gpl \
    --enable-nonfree \
    --disable-librtmp \
    --enable-libx264 \
    --enable-libx265 \
    --enable-libfdk-aac \
    --enable-cuda --enable-cuvid --enable-nvenc \
    --enable-libopus \
make -j8 && make install
make clean
cd $LOCAL_PATH

windows

#!/bin/bash
set -eo pipefail

export LOCAL_PATH=$(pwd)
export BUILD_PATH=$LOCAL_PATH/ffmpeg_build_msys2
export PKG_CONFIG_PATH=$BUILD_PATH/lib/pkgconfig
export LD_LIBRARY_PATH=$BUILD_PATH/lib
export CFLAGS=-I$BUILD_PATH/include
export CPPFLAGS=-I$BUILD_PATH/include
export LDFLAGS=-L$BUILD_PATH/lib
export PATH=$BUILD_PATH/bin:$PATH

export CHOST=x86_64
export HOST=x86_64-w64-mingw32
export BASH_PATH=/mingw64/bin
export CC=$BASH_PATH/gcc
export CXX=$BASH_PATH/g++
export AR=$BASH_PATH/ar
export RANLIB=$BASH_PATH/ranlib
export STRIP=$BASH_PATH/strip

pacman -Syu --noconfirm
pacman -Sy --noconfirm git mingw-w64-x86_64-toolchain mingw-w64-x86_64-cmark mingw-w64-x86_64-diffutils mingw-w64-x86_64-autotools mingw-w64-x86_64-gdb mingw-w64-x86_64-nasm mingw-w64-x86_64-yasm

# build yasm
# aria2c http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
# tar -zxvf yasm-1.3.0.tar.gz
cd $LOCAL_PATH/yasm-1.3.0
rm -rf build && mkdir -p build && cd build
../configure --prefix="$BUILD_PATH" --host=$CHOST
make -j8 && make install
make clean
cd $LOCAL_PATH

# # build nasm
# aria2c https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.gz
# tar -zxvf nasm-2.15.05.tar.gz
cd $LOCAL_PATH/nasm-2.15.05
./autogen.sh
./configure --prefix="$BUILD_PATH"
make -j8 && make install
make clean
cd $LOCAL_PATH

# build zlib
# aria2c https://www.zlib.net/zlib-1.2.11.tar.gz
# tar -zxvf zlib-1.2.11.tar.gz
cd $LOCAL_PATH/zlib-1.2.11
sed -i 's/defined(_WIN32) || defined(__CYGWIN__)/defined(_WIN32)/g' gzguts.h
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --static
make -j8 && make install
make clean
cd $LOCAL_PATH

# # build openssl
# # aria2c https://www.openssl.org/source/openssl-1.1.1k.tar.gz
# # tar -zxvf openssl-1.1.1k.tar.gz
# cd $LOCAL_PATH/openssl-1.1.1k
# rm -rf build && mkdir -p build && cd build
# ../Configure \
#     --prefix=$BUILD_PATH \
#     no-shared \
#     no-tests \
#     -static \
#     threads \
#     zlib \
#     Cygwin-x86_64 \
#     '-Wl,-rpath,$($BUILD_PATH/lib)'
# make -j8 && make install_sw
# make clean
# cd $LOCAL_PATH

# # build librtmp git@github.com:rise-worlds/rtmpdump.git
# cd $LOCAL_PATH/rtmpdump/librtmp
# git clean -fdx
# make -j8 SHARED= SO_INST= CRYPTO=OPENSSL SYS=posix \
#     prefix=$BUILD_PATH \
#     CFLAGS=-I$BUILD_PATH/include \
#     LDFLAGS=-L$BUILD_PATH/lib \
#     XLIBS="-ldl" \
#     install
# make clean
# cd $LOCAL_PATH

# build nv-codec
# aria2c https://github.com/FFmpeg/nv-codec-headers/releases/download/n11.1.5.0/nv-codec-headers-11.1.5.0.tar.gz -o nv-codec-headers-11.1.5.0.tar.gz
# tar -zxvf nv-codec-headers-11.1.5.0.tar.gz
cd $LOCAL_PATH/nv-codec-headers-11.1.5.0
make -j8 PREFIX=$BUILD_PATH install
cd $LOCAL_PATH

# build x264 https://code.videolan.org/videolan/x264.git
# aria2c https://code.videolan.org/videolan/x264/-/archive/stable/x264-stable.tar.gz -o x264-stable.tar.gz
# tar xzvf x264-stable.tar.gz
cd $LOCAL_PATH/x264-stable/
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --host=$HOST \
    --disable-cli \
    --disable-asm \
    --enable-shared=no \
    --enable-static
make -j8 && make install
make clean
cd $LOCAL_PATH

# build x265
# aria2c http://ftp.videolan.org/pub/videolan/x265/x265_3.5.tar.gz -o x265_3.5.tar.gz
# tar xzvf x265_3.5.tar.gz
cd $LOCAL_PATH/x265_3.5/source
rm -rf build && mkdir -p build && cd build
cmake .. -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$BUILD_PATH -DENABLE_SHARED=OFF -DENABLE_ASSEMBLY=OFF
make -j8 && make install
make clean
cd $LOCAL_PATH

# build fdk-aac git@github.com:mstorsjo/fdk-aac.git
# aria2c https://sourceforge.net/projects/opencore-amr/files/fdk-aac/fdk-aac-2.0.2.tar.gz/download -o fdk-aac-2.0.2.tar.gz
# tar -zxvf fdk-aac-2.0.2.tar.gz
cd $LOCAL_PATH/fdk-aac-2.0.2
./autogen.sh
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --host=$HOST \
    --enable-shared=no \
    --enable-static
make -j8 && make install
make clean
cd $LOCAL_PATH

# build opus https://github.com/xiph/opus.git
# aria2c https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz
# tar -zxvf opus-1.3.1.tar.gz
cd $LOCAL_PATH/opus-1.3.1
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix="$BUILD_PATH" \
    --host=$HOST \
    --disable-shared
make -j8 && make install
make clean
cd $LOCAL_PATH

## build lame https://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz
#cd $LOCAL_PATH/lame-3.100
#./configure --prefix="$BUILD_PATH" --disable-shared --enable-nasm
#make -j8 && make install
#make clean

# build ffmpeg https://git.ffmpeg.org/ffmpeg.git
# aria2c http://ffmpeg.org/releases/ffmpeg-4.4.1.tar.xz
# tar -xvJf ffmpeg-4.4.1.tar.xz
cd $LOCAL_PATH/ffmpeg-4.4.1
rm -rf build && mkdir -p build && cd build
../configure \
    --prefix=$BUILD_PATH \
    --target-os=mingw32 \
    --cc=$CC \
    --ranlib=$RANLIB \
    --strip=$STRIP \
    --pkg-config-flags="--static" \
    --extra-cflags="-std=c11 -I$BUILD_PATH/include -I$BUILD_PATH/include/opus" \
    --extra-ldflags="-L$BUILD_PATH/lib" \
    --extra-libs="-latomic -lpthread -lm" \
    --disable-debug \
    --enable-static \
    --disable-shared \
    --disable-asm \
    --disable-doc \
    --disable-openssl \
    --disable-libvpx \
    --disable-libwebp \
    --disable-lzma \
    --disable-bzlib \
    --enable-gpl \
    --enable-nonfree \
    --disable-librtmp \
    --enable-libx264 \
    --enable-libx265 \
    --enable-libfdk-aac \
    --enable-cuda --enable-cuvid --enable-nvenc \
    --enable-libopus \
make -j8 && make install
make clean
cd $LOCAL_PATH