-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_ffmpeg_mac.sh
executable file
·241 lines (204 loc) · 6.28 KB
/
build_ffmpeg_mac.sh
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env bash
# Build ffmpeg with dynamic linking for macOS
# Get the directory where the script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Parse command line arguments
ENABLE_LOGGING=false
for arg in "$@"; do
case $arg in
--log)
ENABLE_LOGGING=true
shift
;;
esac
done
# Set up logging if --log flag is passed
if [[ "$ENABLE_LOGGING" == "true" ]]; then
LOG_FILE="${SCRIPT_DIR}/ffmpeg_build_mac.log"
exec 1> >(tee -a "${LOG_FILE}")
exec 2>&1
echo "Build started at $(date)"
echo "======================="
fi
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check and install Homebrew dependencies
check_brew_deps() {
local missing_deps=()
local required_pkgs=(
# Build tools
"autoconf"
"automake"
"libtool"
"pkg-config"
"git"
"cmake"
"ninja"
"nasm"
"yasm"
# Required libraries
"opus"
"dav1d"
)
echo "Checking Homebrew dependencies..."
for pkg in "${required_pkgs[@]}"; do
if ! brew list "$pkg" >/dev/null 2>&1; then
missing_deps+=("$pkg")
else
echo "Found ${pkg}: $(brew list --versions ${pkg})"
fi
done
if [ ${#missing_deps[@]} -ne 0 ]; then
echo "Missing required packages:"
printf '%s\n' "${missing_deps[@]}"
read -p "Would you like to install them now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
brew install "${missing_deps[@]}"
else
echo "Cannot proceed without required packages."
exit 1
fi
fi
}
# Check system dependencies
check_dependencies() {
if ! command_exists brew; then
echo "Error: Homebrew is not installed. Please install it first."
echo "Visit https://brew.sh for installation instructions"
exit 1
fi
check_brew_deps
echo "All required dependencies are installed."
echo "Using Homebrew from: $(brew --prefix)"
}
# Create build directories
BUILD_DIR="${SCRIPT_DIR}/ffmpeg_build"
INSTALL_DIR="${SCRIPT_DIR}/ffmpeg_install"
mkdir -p "${BUILD_DIR}" "${INSTALL_DIR}"
# Check dependencies before starting
check_dependencies
cd "${BUILD_DIR}"
# Build SVT-AV1 from source
echo "Building SVT-AV1 from source..."
if [ -d "SVT-AV1" ]; then
rm -rf "SVT-AV1"
fi
git clone --depth=1 https://gitlab.com/AOMediaCodec/SVT-AV1.git
cd SVT-AV1
mkdir -p build
cd build
cmake .. \
-GNinja \
-DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=ON
ninja
ninja install
# Add SVT-AV1 to pkg-config path
export PKG_CONFIG_PATH="${INSTALL_DIR}/lib/pkgconfig:${PKG_CONFIG_PATH}"
# After building SVT-AV1, add these lines to fix the library installation
echo "Fixing SVT-AV1 library installation..."
install_name_tool -id "@rpath/libSvtAv1Enc.2.dylib" "${INSTALL_DIR}/lib/libSvtAv1Enc.2.dylib"
# Before configuring FFmpeg, add these environment variables
export DYLD_LIBRARY_PATH="${INSTALL_DIR}/lib:${DYLD_LIBRARY_PATH}"
export LIBRARY_PATH="${INSTALL_DIR}/lib:${LIBRARY_PATH}"
# Build FFmpeg
cd "${BUILD_DIR}"
if [ -d "FFmpeg" ]; then
rm -rf "FFmpeg"
fi
git clone --depth=1 https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg
# Add these environment variables before FFmpeg configuration
export MACOSX_DEPLOYMENT_TARGET=10.15
export CFLAGS="-I${INSTALL_DIR}/include -O3"
export LDFLAGS="-L${INSTALL_DIR}/lib"
# Configure FFmpeg
./configure \
--prefix="${INSTALL_DIR}" \
--enable-gpl \
--enable-version3 \
--enable-shared \
--disable-static \
--enable-videotoolbox \
--enable-audiotoolbox \
--enable-libdav1d \
--enable-libopus \
--enable-libsvtav1 \
--disable-doc \
--disable-debug \
--disable-ffplay \
--enable-ffprobe \
--disable-network \
--disable-protocols \
--enable-protocol=file \
--pkg-config-flags="--static" \
--extra-cflags="${CFLAGS}" \
--extra-ldflags="${LDFLAGS} -Wl,-rpath,${INSTALL_DIR}/lib" \
--extra-libs="-lpthread -lm" \
--cc=clang || {
echo "FFmpeg configure failed. Checking config.log..."
cat ffbuild/config.log
exit 1
}
# Build using available CPU cores
NPROC=$(sysctl -n hw.ncpu)
make -j"${NPROC}"
make install
# After make install, add these lines to fix library paths
echo "Fixing library paths..."
for lib in "${INSTALL_DIR}"/lib/*.dylib; do
if [ -f "$lib" ]; then
install_name_tool -id "@rpath/$(basename $lib)" "$lib"
install_name_tool -add_rpath "${INSTALL_DIR}/lib" "$lib"
fi
done
for bin in "${INSTALL_DIR}"/bin/*; do
if [ -f "$bin" ] && [ -x "$bin" ]; then
install_name_tool -add_rpath "${INSTALL_DIR}/lib" "$bin"
install_name_tool -add_rpath "@executable_path/../lib" "$bin"
fi
done
# Copy and fix binaries
echo "Copying and fixing binaries..."
cp "${INSTALL_DIR}/bin/ffmpeg" "${SCRIPT_DIR}/ffmpeg"
cp "${INSTALL_DIR}/bin/ffprobe" "${SCRIPT_DIR}/ffprobe"
install_name_tool -add_rpath "@executable_path/ffmpeg_install/lib" "${SCRIPT_DIR}/ffmpeg"
install_name_tool -add_rpath "@executable_path/ffmpeg_install/lib" "${SCRIPT_DIR}/ffprobe"
# Validation steps
echo "Validating FFmpeg build..."
# Test FFmpeg execution with detailed error output
if ! "${INSTALL_DIR}/bin/ffmpeg" -version > ffmpeg_test.log 2>&1; then
echo "Error: FFmpeg binary test failed"
echo "Error output:"
cat ffmpeg_test.log
# Check dynamic library dependencies
echo "Checking dynamic library dependencies:"
otool -L "${INSTALL_DIR}/bin/ffmpeg"
exit 1
fi
# Print library versions and configuration
echo "Checking library versions..."
echo "SVT-AV1:"
pkg-config --modversion SvtAv1Enc
echo "Opus:"
pkg-config --modversion opus
echo "dav1d:"
pkg-config --modversion dav1d
echo "FFmpeg configuration:"
"${INSTALL_DIR}/bin/ffmpeg" -version | grep configuration
# Clean up build directory
echo "Cleaning up build directory..."
cd "${SCRIPT_DIR}"
if [ -d "${BUILD_DIR}" ]; then
rm -rf "${BUILD_DIR}"
echo "Build directory cleaned up"
fi
echo "Build completed successfully!"
echo "Binaries and libraries are available at:"
echo " ffmpeg: ${SCRIPT_DIR}/ffmpeg"
echo " ffprobe: ${SCRIPT_DIR}/ffprobe"
echo " install: ${INSTALL_DIR}"