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

说明

MacOS平台的ffmpeg编译脚本,包含x264、x265、fdk-aac、opus以及openssl和rtmp库。

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

#!/bin/bash
set -eo pipefail

export LOCAL_PATH=$(pwd)
export BUILD_PATH=$LOCAL_PATH/ffmpeg_build_macos
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-apple-darwin
export BASE_PATH=/usr/bin
export CC=$BASE_PATH/clang
export CXX=$BASE_PATH/clang++
export AR=$BASE_PATH/ar
export RANLIB=$BASE_PATH/ranlib
export STRIP=$BASE_PATH/strip

export CPU_CORES=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu)

# brew install cmake git autoconf automake pkg-conf libtool
# brew install automake fdk-aac git lame libass libtool libvorbis libvpx opus sdl shtool texi2html theora wget x264 x265 xvid nasm

# 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 -j${CPU_CORES} && 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 -j${CPU_CORES} && 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 -j${CPU_CORES} && 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 -j${CPU_CORES} && 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 -j${CPU_CORES} SHARED= SO_INST= CRYPTO=OPENSSL SYS=posix \
# make -j${CPU_CORES} 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 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 \
    --enable-shared=no \
    --enable-static \
    # --disable-cli \
    # --disable-asm \
make -j${CPU_CORES} && 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=ON
make -j${CPU_CORES} && 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 -j${CPU_CORES} && 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 -j${CPU_CORES} && 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 -j${CPU_CORES} && 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=darwin \
    --cc=$CC \
    --ranlib=$RANLIB \
    --strip=$STRIP \
    --extra-cflags="-std=c11 -I$BUILD_PATH/include -I$BUILD_PATH/include/opus" \
    --extra-cxxflags="-std=c++11" \
    --extra-ldflags="-L$BUILD_PATH/lib" \
    --extra-libs="-lpthread -lm -lc++" \
    --host-cflags= \
    --host-ldflags= \
    --enable-static \
    --disable-shared \
    --disable-alsa \
    --disable-doc \
    --disable-openssl \
    --disable-libvpx \
    --disable-libwebp \
    --disable-lzma \
    --disable-bzlib \
    --disable-xlib \
    --disable-libxcb \
    --disable-vaapi \
    --disable-sndio \
    --enable-gpl \
    --enable-nonfree \
    --disable-librtmp \
    --enable-avresample \
    --enable-libx264 \
    --enable-libx265 \
    --enable-libfdk-aac \
    # --disable-asm \
    # --enable-libopus
make -j${CPU_CORES} && make install
make clean
cd $LOCAL_PATH
posted @ 2022-01-22 15:59  至尊王者  阅读(1161)  评论(0编辑  收藏  举报