#!/usr/bin/env bash # Builds the small FFmpeg that Shutter Vault Hub ships. # # build-ffmpeg.sh win32-x64 cross-compiled on Linux with mingw-w64 # build-ffmpeg.sh darwin-arm64 built on an Apple Silicon Mac # # The Hub uses FFmpeg for one job: one still frame from a video, saved as a # JPEG. So this build can only read the kinds of video a phone's photo library # holds, and can only write a JPEG. It is LGPL (no --enable-gpl, no # --enable-nonfree, no --enable-version3) and links no outside library. # # The source is the unmodified release tarball named below. It is taken from # the folder given in FFMPEG_SOURCE_DIR (default: this script's folder) or # downloaded from ffmpeg.org, and its SHA-256 must match either way. set -euo pipefail FFMPEG_VERSION=9.0.2 FFMPEG_TARBALL="ffmpeg-$FFMPEG_VERSION.tar.xz" FFMPEG_SHA256=8c3850283eb25fa026482078a04051e0be17347b09ef81a0849bec15a96e002e FFMPEG_URL="https://ffmpeg.org/releases/$FFMPEG_TARBALL" MACOS_MIN=13.0 # the Mac Hub's deployment target (MacApp/project.yml) # The tools stamp this time into the file instead of "now", so the same source # and tools give the same bytes. It is the 9.0.2 release date, 2026-09-18 UTC. export SOURCE_DATE_EPOCH=1789689600 TARGET="${1:-}"; OUT="${2:-}" [[ -n "$TARGET" && -n "$OUT" ]] || { echo "usage: build-ffmpeg.sh " >&2; exit 2; } HERE="$(cd "$(dirname "$0")" && pwd)" SOURCE_DIR="${FFMPEG_SOURCE_DIR:-$HERE}" WORK="${FFMPEG_WORK_DIR:-$(mktemp -d)}" JOBS="${FFMPEG_JOBS:-$( (nproc || sysctl -n hw.ncpu) 2>/dev/null || echo 2)}" mkdir -p "$OUT" "$WORK" OUT="$(cd "$OUT" && pwd)" sha256() { if command -v sha256sum >/dev/null; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi; } # --- 1. The exact source ------------------------------------------------------ TARBALL="$SOURCE_DIR/$FFMPEG_TARBALL" if [[ ! -f "$TARBALL" ]]; then TARBALL="$WORK/$FFMPEG_TARBALL" echo "== downloading $FFMPEG_URL ==" curl -fsSL --retry 3 -o "$TARBALL" "$FFMPEG_URL" fi ACTUAL="$(sha256 "$TARBALL")" [[ "$ACTUAL" == "$FFMPEG_SHA256" ]] || { echo "SHA-256 mismatch for $FFMPEG_TARBALL: expected $FFMPEG_SHA256, got $ACTUAL" >&2; exit 1; } rm -rf "$WORK/src" "$WORK/build" mkdir -p "$WORK/src" "$WORK/build" tar -xf "$TARBALL" -C "$WORK/src" --strip-components=1 # --- 2. What goes in ---------------------------------------------------------- # Containers: mov covers .mov, .mp4, .m4v, .3gp and .3g2; avi is for clips from # old digital cameras. Video: H.264 and HEVC (iPhone), ProRes (iPhone Pro), # MPEG-4 Part 2 and H.263 (old phones, 3GP), Motion JPEG (old cameras). # No audio decoder: the one frame we write has no sound. # Filters: scale is the one the Hub asks for; the rest are the ones the ffmpeg # program adds by itself (pixel format, and turning a frame the right way up). COMMON=( --disable-everything --disable-autodetect --disable-network --disable-doc --disable-debug --disable-programs --enable-ffmpeg --disable-avdevice --enable-static --disable-shared --enable-protocol=file --enable-demuxer=mov,avi --enable-decoder=h264,hevc,prores,mpeg4,h263,mjpeg --enable-parser=h264,hevc,mpeg4video,h263,mjpeg --enable-encoder=mjpeg --enable-muxer=image2 --enable-filter=scale,format,null,transpose,hflip,vflip,rotate,crop,trim ) case "$TARGET" in win32-x64) BINARY=ffmpeg.exe # w32threads, not pthreads, and a fully static link: the .exe needs only # DLLs that are part of Windows. No timestamp in the file, so the same # source and tools give the same bytes. PLATFORM=( --arch=x86_64 --target-os=mingw32 --cross-prefix=x86_64-w64-mingw32- --enable-w32threads --disable-pthreads --extra-ldflags="-static -Wl,--no-insert-timestamp" ) TOOLCHAIN="$(x86_64-w64-mingw32-gcc --version | head -1); $(x86_64-w64-mingw32-ld --version | head -1); $(nasm -v)" ;; darwin-arm64) BINARY=ffmpeg [[ "$(uname -s)-$(uname -m)" == "Darwin-arm64" ]] || { echo "darwin-arm64 builds on an Apple Silicon Mac" >&2; exit 1; } PLATFORM=( --arch=arm64 --target-os=darwin --cc=clang --extra-cflags="-mmacosx-version-min=$MACOS_MIN" --extra-ldflags="-mmacosx-version-min=$MACOS_MIN" ) TOOLCHAIN="$(clang --version | head -1); macOS $(sw_vers -productVersion); SDK $(xcrun --show-sdk-version)" ;; *) echo "unknown target: $TARGET" >&2; exit 2 ;; esac # --- 3. Build ----------------------------------------------------------------- cd "$WORK/build" echo "== configure ($TARGET) ==" "$WORK/src/configure" "${COMMON[@]}" "${PLATFORM[@]}" || { tail -40 ffbuild/config.log >&2; exit 1; } echo "== make -j$JOBS ==" make -j"$JOBS" >"$WORK/make.log" 2>&1 || { tail -60 "$WORK/make.log" >&2; exit 1; } # --- 4. Refuse anything that is not the LGPL build we meant ------------------- grep -q '^#define CONFIG_GPL 0$' config.h || { echo "refusing: GPL parts are enabled" >&2; exit 1; } grep -q '^#define CONFIG_NONFREE 0$' config.h || { echo "refusing: nonfree parts are enabled" >&2; exit 1; } grep -q '^#define CONFIG_VERSION3 0$' config.h || { echo "refusing: version 3 licence is enabled" >&2; exit 1; } grep -q '^#define FFMPEG_LICENSE "LGPL version 2.1 or later"$' config.h || { echo "refusing: unexpected licence line" >&2; grep FFMPEG_LICENSE config.h >&2; exit 1; } # --- 5. Hand over the binary, its licence and how it was made ----------------- cp "$BINARY" "$OUT/$BINARY" cp "$WORK/src/COPYING.LGPLv2.1" "$OUT/COPYING.LGPLv2.1" cp "$WORK/src/LICENSE.md" "$OUT/FFMPEG-LICENSE.md" CONFIGURATION="$(sed -n 's/^#define FFMPEG_CONFIGURATION "\(.*\)"$/\1/p' config.h)" json() { printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'; } cat >"$OUT/BUILD-INFO.json" <