Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions ffmpeg/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ INSTALL_DIR="$DIR/ffmpeg/install"
NJOBS="$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2)"
CC="ccache ${CC:-cc}"
PREFIX="$DIR/build/prefix"
mkdir -p "$DIR/build"
rm -rf "$PREFIX" "$DIR/build/lib"
mkdir -p "$DIR/build" "$PREFIX"

# --- Build zlib (static) ---
if [ ! -d "zlib-src/.git" ]; then
Expand Down Expand Up @@ -113,10 +114,17 @@ git -C ffmpeg-src fetch --depth 1 origin "n${FFMPEG_VERSION}"
git -C ffmpeg-src checkout --force FETCH_HEAD

cd ffmpeg-src
make distclean >/dev/null 2>&1 || true

# Platform-specific hardware acceleration flags
HW_FLAGS=()
LOADER_FLAGS=()
FFMPEG_LDEXEFLAGS=
FFMPEG_LDSOFLAGS=
if [ "$PLATFORM" = "Linux" ]; then
FFMPEG_LDEXEFLAGS='-Wl,-rpath,\$$ORIGIN/../lib -Wl,-z,origin'
FFMPEG_LDSOFLAGS='-Wl,-rpath,\$$$$ORIGIN -Wl,-z,origin'

HW_FLAGS+=(
# NVIDIA CUDA/NVDEC (uses dlopen at runtime)
--enable-ffnvcodec --enable-cuda --enable-cuvid --enable-nvdec
Expand All @@ -138,6 +146,11 @@ if [ "$PLATFORM" = "Linux" ]; then
--enable-encoder=h264_vulkan,hevc_vulkan
)
elif [ "$PLATFORM" = "Darwin" ]; then
FFMPEG_LDEXEFLAGS='-Wl,-rpath,@loader_path/../lib'
LOADER_FLAGS+=(
--install-name-dir=@rpath
)

HW_FLAGS+=(
# VideoToolbox (Apple Silicon / macOS)
--enable-videotoolbox
Expand All @@ -147,12 +160,14 @@ elif [ "$PLATFORM" = "Darwin" ]; then
fi

PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" \
LDEXEFLAGS="$FFMPEG_LDEXEFLAGS" \
LDSOFLAGS="$FFMPEG_LDSOFLAGS" \
./configure \
--cc="${CC:-cc}" \
--prefix="$PREFIX" \
--enable-gpl \
--enable-static \
--disable-shared \
--disable-static \
--enable-shared \
--enable-zlib \
--enable-libx264 \
--enable-pic \
Expand All @@ -171,6 +186,7 @@ PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}" \
--enable-bsf=extract_extradata,h264_mp4toannexb,hevc_mp4toannexb \
--extra-cflags="-I$PREFIX/include" \
--extra-ldflags="-L$PREFIX/lib" \
"${LOADER_FLAGS[@]}" \
"${HW_FLAGS[@]}"
make -j"$NJOBS"
make install
Expand All @@ -184,22 +200,46 @@ mkdir -p "$INSTALL_DIR"/{bin,lib,include}
cp "$PREFIX/bin/ffmpeg" "$INSTALL_DIR/bin/"
cp "$PREFIX/bin/ffprobe" "$INSTALL_DIR/bin/"

# Libraries
LIBS="libavformat.a libavcodec.a libavutil.a libswresample.a libx264.a libz.a"
FFMPEG_LIBS=(avdevice avfilter avformat avcodec postproc swresample swscale avutil)

copy_linux_ffmpeg_lib() {
local lib="$1"
local soname

soname="$(readelf -d "$PREFIX/lib/lib${lib}.so" | sed -n 's/.*Library soname: \[\(.*\)\]/\1/p')"
[ -n "$soname" ] || { echo "error: could not find SONAME for lib${lib}.so" >&2; exit 1; }

cp -L "$PREFIX/lib/$soname" "$INSTALL_DIR/lib/$soname"
printf 'INPUT(%s)\n' "$soname" > "$INSTALL_DIR/lib/lib${lib}.so"
}

copy_darwin_ffmpeg_lib() {
local lib="$1"
local dylib

dylib="$(otool -D "$PREFIX/lib/lib${lib}.dylib" | sed -n '2s|.*/||p')"
[ -n "$dylib" ] || { echo "error: could not find install name for lib${lib}.dylib" >&2; exit 1; }

cp -L "$PREFIX/lib/$dylib" "$INSTALL_DIR/lib/$dylib"
}

if [ "$PLATFORM" = "Linux" ]; then
LIBS="$LIBS libva.a libva-drm.a libdrm.a"
for lib in "${FFMPEG_LIBS[@]}"; do
copy_linux_ffmpeg_lib "$lib"
done
elif [ "$PLATFORM" = "Darwin" ]; then
for lib in "${FFMPEG_LIBS[@]}"; do
copy_darwin_ffmpeg_lib "$lib"
done
fi
for lib in $LIBS; do
cp "$PREFIX/lib/$lib" "$INSTALL_DIR/lib/"
done

# Headers
for dir in libavformat libavcodec libavutil libswresample; do
cp -r "$PREFIX/include/$dir" "$INSTALL_DIR/include/"
done

# Strip binaries
strip "$INSTALL_DIR/bin/ffmpeg" "$INSTALL_DIR/bin/ffprobe" 2>/dev/null || true
# Strip binaries and shared libraries
strip "$INSTALL_DIR/bin/ffmpeg" "$INSTALL_DIR/bin/ffprobe" "$INSTALL_DIR/lib/"*.so.* "$INSTALL_DIR/lib/"*.dylib 2>/dev/null || true

echo "Installed ffmpeg to $INSTALL_DIR"
du -sh "$INSTALL_DIR"
2 changes: 1 addition & 1 deletion ffmpeg/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ffmpeg = "ffmpeg:_run_ffmpeg"
ffprobe = "ffmpeg:_run_ffprobe"

[tool.setuptools.packages.find]
include = ["ffmpeg*"]
include = ["ffmpeg"]

[tool.setuptools.package-data]
ffmpeg = ["install/**/*"]
Loading