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格式就推拉流)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/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