#!/bin/bash
# Builds apple/Vendor/FFmpegDemux.xcframework: FFmpeg as a pure demuxer/muxer with no codecs (docs/design/apple-remux.md).
# MKV / MPEG-TS in, fragmented MP4 out, no protocols (the app hands it the bytes); video and audio are copied, never decoded, so AVPlayer still decodes in
# hardware. LGPL 2.1: no --enable-gpl, no --enable-nonfree, no external codec libraries, unchanged source, and linked
# DYNAMICALLY: each slice is FFmpegDemux.framework (one dylib of libavformat + libavcodec + libavutil), embedded in the
# app, so it can be replaced with a rebuilt FFmpeg (LGPL 2.1 section 6; ADR 0014).
#
# bash apple/tools/build-ffmpeg-demux.sh # all four slices → apple/Vendor/FFmpegDemux.xcframework
# FFBUILD=
bash apple/tools/build-ffmpeg-demux.sh
# bash apple/tools/build-ffmpeg-demux.sh --host # also a macOS ffmpeg/ffprobe for tools/make-test-films.js
#
# The source tarball is fetched from ffmpeg.org once (FFBUILD, default $TMPDIR/prettytv-ffmpeg) and checked against
# FFSHA below. Needs Xcode (clang, libtool, xcodebuild); no Homebrew, no nasm (arm64 only), no pkg-config.
# The machine rule (docs/design/apple-paritet.md §3): run it when no other build runs; JOBS defaults to 4.
set -euo pipefail
FFVER=9.0.2
FFSHA=8c3850283eb25fa026482078a04051e0be17347b09ef81a0849bec15a96e002e
MINOS=17.0
JOBS=${JOBS:-4}
HERE=$(cd "$(dirname "$0")/.." && pwd) # apple/
OUT="$HERE/Vendor/FFmpegDemux.xcframework"
WORK=${FFBUILD:-${TMPDIR:-/tmp}/prettytv-ffmpeg}
SRC="$WORK/ffmpeg-$FFVER"
HOST=0
for a in "$@"; do
case "$a" in
--host) HOST=1 ;;
*) echo "unknown argument: $a" >&2; exit 2 ;;
esac
done
mkdir -p "$WORK"
TAR="$WORK/ffmpeg-$FFVER.tar.xz"
if [ ! -f "$TAR" ]; then
echo "downloading ffmpeg-$FFVER.tar.xz from ffmpeg.org"
curl -sSfL -o "$TAR.part" "https://ffmpeg.org/releases/ffmpeg-$FFVER.tar.xz"
mv "$TAR.part" "$TAR"
fi
GOT=$(shasum -a 256 "$TAR" | cut -d' ' -f1)
if [ "$GOT" != "$FFSHA" ]; then
echo "checksum mismatch for $TAR: $GOT" >&2
exit 1
fi
if [ ! -d "$SRC" ]; then
tar xf "$TAR" -C "$WORK"
fi
# What the app needs and nothing else (the playlists and WebVTT are the app's own): Matroska and MPEG-TS in (AVI is
# AVPlayer's, StreamProbe), fragmented MP4 out. No protocols and no network at all: FFmpegRemuxer reads through its
# own AVIOContext over the app's HttpReader (the same User-Agent, Range and reopens as SwiftRemuxer), so neither
# FFmpeg's HTTP/TLS nor its `file` protocol (fstat, a required-reason API) is in the app. zlib for Matroska's
# compressed tracks.
COMMON=(
--disable-everything --disable-autodetect --disable-programs --disable-doc --disable-debug
--disable-avdevice --disable-avfilter --disable-swscale --disable-swresample
--enable-static --disable-shared --enable-pic --enable-small
--enable-zlib --disable-network
--enable-demuxer=matroska,mpegts
--enable-muxer=mp4
--enable-parser=h264,hevc,aac,ac3,mpegaudio
--enable-bsf=extract_extradata,aac_adtstoasc
)
# slice name | SDK | clang target
SLICES=(
"ios|iphoneos|arm64-apple-ios$MINOS"
"ios-simulator|iphonesimulator|arm64-apple-ios$MINOS-simulator"
"tvos|appletvos|arm64-apple-tvos$MINOS"
"tvos-simulator|appletvsimulator|arm64-apple-tvos$MINOS-simulator"
)
build_slice() {
local name=$1 sdk=$2 target=$3
local bdir="$WORK/build-$name" pdir="$WORK/prefix-$name"
local sysroot cc
sysroot=$(xcrun --sdk "$sdk" --show-sdk-path)
cc="$(xcrun --sdk "$sdk" -f clang)"
rm -rf "${bdir:?}" "${pdir:?}"
mkdir -p "$bdir"
echo "== $name ($sdk, $target)"
(
cd "$bdir"
"$SRC/configure" --prefix="$pdir" \
--enable-cross-compile --target-os=darwin --arch=aarch64 \
--cc="$cc" --sysroot="$sysroot" \
--extra-cflags="-target $target" \
--extra-ldflags="-target $target" \
"${COMMON[@]}" > configure.log
# LGPL 2.1 only; no file or HTTP protocol, no network, no AVI in what ships
for c in CONFIG_GPL CONFIG_VERSION3 CONFIG_NONFREE CONFIG_PROTOCOLS CONFIG_FILE_PROTOCOL CONFIG_HTTP_PROTOCOL CONFIG_NETWORK CONFIG_AVI_DEMUXER; do
grep -qh "^#define $c 0" config.h config_components.h || { echo "$c is not 0 in $name" >&2; exit 1; }
done
# and no protocol at all, whatever its name
if grep -hE '^#define CONFIG_[A-Z0-9_]+_PROTOCOL 1' config.h config_components.h; then
echo "a protocol is on in $name" >&2
exit 1
fi
make -j"$JOBS" > make.log 2>&1
make install > install.log 2>&1
)
# one dynamic library for the slice, as a framework (App Store apps embed dylibs only inside frameworks)
local fw="$pdir/FFmpegDemux.framework"
mkdir -p "$fw"
# libavutil's file.o (av_file_map / av_file_unmap: mmap a local file, nothing in this build calls it) is the one
# object that uses fstat, a required-reason API; -all_load would keep it, so it is taken out of the archive first.
# The source stays unmodified (LGPL); only what is linked changes.
cp "$pdir/lib/libavutil.a" "$pdir/libavutil-link.a"
xcrun ar -d "$pdir/libavutil-link.a" file.o
"$cc" -dynamiclib -target "$target" -isysroot "$sysroot" -o "$fw/FFmpegDemux" \
-install_name @rpath/FFmpegDemux.framework/FFmpegDemux -compatibility_version 9.0 -current_version "$FFVER" \
-Wl,-all_load -Wl,-dead_strip "$pdir/lib/libavformat.a" "$pdir/lib/libavcodec.a" "$pdir/libavutil-link.a" \
-lz
# the unstripped dylib for symbolicating a crash in it: dist/ffmpeg-symbols/// (not in git; keep it with
# the release whose framework it is)
local sym="$HERE/../dist/ffmpeg-symbols/$FFVER/$name"
mkdir -p "$sym"
cp "$fw/FFmpegDemux" "$sym/FFmpegDemux"
xcrun strip -x "$fw/FFmpegDemux"
# no required-reason API from the library (Apple's C ones: file timestamps, disk space, system boot time; the
# others are Objective-C/Swift classes a C library cannot call); the privacy manifest below declares none
local RR='^_(fstat|stat|lstat|fstatat|getattrlist|getattrlistbulk|fgetattrlist|getattrlistat|statfs|fstatfs|statvfs|fstatvfs|mach_absolute_time)(\$|$)'
if xcrun nm -u "$fw/FFmpegDemux" | grep -qE "$RR"; then
echo "WARNING: a required-reason API is linked into $name:" >&2
xcrun nm -u "$fw/FFmpegDemux" | grep -E "$RR" >&2
[ "${FF_ALLOW_STAT:-0}" = 1 ] || exit 1
fi
local platform
case "$sdk" in
iphoneos) platform=iPhoneOS ;;
iphonesimulator) platform=iPhoneSimulator ;;
appletvos) platform=AppleTVOS ;;
*) platform=AppleTVSimulator ;;
esac
cat > "$fw/Info.plist" << PLIST
CFBundleDevelopmentRegionen
CFBundleExecutableFFmpegDemux
CFBundleIdentifierapp.prettytv.ffmpegdemux
CFBundleInfoDictionaryVersion6.0
CFBundleNameFFmpegDemux
CFBundlePackageTypeFMWK
CFBundleShortVersionString$FFVER
CFBundleVersion1
CFBundleSupportedPlatforms$platform
MinimumOSVersion$MINOS
PLIST
# the framework's own privacy manifest (App Store): it collects nothing, tracks nothing and uses no required-reason API
cat > "$fw/PrivacyInfo.xcprivacy" << 'PRIV'
NSPrivacyTracking
NSPrivacyTrackingDomains
NSPrivacyCollectedDataTypes
NSPrivacyAccessedAPITypes
PRIV
}
for s in "${SLICES[@]}"; do
IFS='|' read -r name sdk target <<< "$s"
build_slice "$name" "$sdk" "$target"
done
# Headers: FFmpeg's own, plus an umbrella header with what Swift cannot import from macros (AVERROR, AV_NOPTS_VALUE),
# and a framework module map, so `import FFmpegDemux` works from Swift. Inside a framework FFmpeg's
# "libavutil/x.h" includes are made relative ("../libavutil/x.h"), and only the headers the umbrella needs are kept.
HDR="$WORK/headers"
rm -rf "${HDR:?}"
cp -R "$WORK/prefix-ios/include" "$HDR"
for f in "$HDR"/lib*/*.h; do
sed -i '' -E 's#include "(libav[a-z]+/)#include "../\1#' "$f"
done
cat > "$HDR/FFmpegDemux.h" << 'EOF'
// The FFmpeg demux/remux build for PrettyTv (apple/tools/build-ffmpeg-demux.sh); LGPL 2.1, no codecs.
#ifndef FFMPEG_DEMUX_H
#define FFMPEG_DEMUX_H
#include
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/bsf.h"
#include "libavutil/avutil.h"
#include "libavutil/dict.h"
#include "libavutil/error.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
/* the macros Swift does not import */
static inline int64_t fd_nopts(void) { return AV_NOPTS_VALUE; }
static inline int fd_eof(void) { return AVERROR_EOF; }
static inline int fd_eagain(void) { return AVERROR(EAGAIN); }
static inline int fd_exit(void) { return AVERROR_EXIT; }
static inline int fd_http_not_found(void) { return AVERROR_HTTP_NOT_FOUND; }
static inline int fd_http_forbidden(void) { return AVERROR_HTTP_FORBIDDEN; }
static inline int fd_http_unauthorized(void) { return AVERROR_HTTP_UNAUTHORIZED; }
static inline int fd_http_other_4xx(void) { return AVERROR_HTTP_OTHER_4XX; }
static inline int fd_http_server_error(void) { return AVERROR_HTTP_SERVER_ERROR; }
static inline int fd_invalid_data(void) { return AVERROR_INVALIDDATA; }
static inline int fd_pkt_flag_key(void) { return AV_PKT_FLAG_KEY; }
static inline AVRational fd_ms(void) { return (AVRational){ 1, 1000 }; }
#endif
EOF
# keep only what the umbrella header includes (no Windows or Android headers in the module)
NEED=$(xcrun -sdk iphoneos clang -target "arm64-apple-ios$MINOS" -isysroot "$(xcrun --sdk iphoneos --show-sdk-path)" \
-M "$HDR/FFmpegDemux.h" | tr ' \\' '\n\n' | grep "^$HDR/" | sed "s#^$HDR/##" | sed -E -e ':a' -e 's#lib[a-z]+/\.\./##' -e 'ta' | sort -u)
for f in $(cd "$HDR" && find . -name '*.h' | sed 's#^\./##'); do
echo "$NEED" | grep -qx "$f" || rm -f "${HDR:?}/${f:?}"
done
cat > "$WORK/module.modulemap" << 'EOF'
framework module FFmpegDemux {
umbrella header "FFmpegDemux.h"
export *
}
EOF
rm -rf "${OUT:?}"
mkdir -p "$(dirname "$OUT")"
ARGS=()
for s in "${SLICES[@]}"; do
IFS='|' read -r name sdk target <<< "$s"
fw="$WORK/prefix-$name/FFmpegDemux.framework"
rm -rf "${fw:?}/Headers" "${fw:?}/Modules"
cp -R "$HDR" "$fw/Headers"
mkdir -p "$fw/Modules"
cp "$WORK/module.modulemap" "$fw/Modules/module.modulemap"
ARGS+=(-framework "$fw")
done
xcodebuild -create-xcframework "${ARGS[@]}" -output "$OUT" > /dev/null
cp "$SRC/COPYING.LGPLv2.1" "$OUT/COPYING.LGPLv2.1"
cat > "$OUT/BUILD.txt" << EOF
FFmpeg $FFVER (https://ffmpeg.org/releases/ffmpeg-$FFVER.tar.xz, sha256 $FFSHA), unmodified, LGPL 2.1.
Built by apple/tools/build-ffmpeg-demux.sh with:
${COMMON[*]}
--enable-cross-compile --target-os=darwin --arch=aarch64, minimum iOS/tvOS $MINOS, arm64 only.
EOF
echo "wrote $OUT"
du -sh "$OUT"/*/FFmpegDemux.framework/FFmpegDemux
# A macOS ffmpeg/ffprobe for making the test films without Homebrew (tools/make-test-films.js FFMPEG=…): lavfi
# sources, VideoToolbox H.264/HEVC encoders and FFmpeg's own LGPL audio encoders. A tool only, never shipped.
if [ "$HOST" = 1 ]; then
bdir="$WORK/build-host"
rm -rf "${bdir:?}"
mkdir -p "$bdir"
echo "== host tools"
(
cd "$bdir"
"$SRC/configure" --prefix="$WORK/prefix-host" --disable-doc --disable-debug --disable-autodetect \
--enable-zlib --enable-videotoolbox --enable-audiotoolbox --enable-securetransport \
--disable-ffplay > configure.log
make -j"$JOBS" > make.log 2>&1
make install > install.log 2>&1
)
echo "host tools: $WORK/prefix-host/bin/ffmpeg, ffprobe"
fi