Repository navigation
benchmarks: use reproducible ffmpeg container for 220.video-processing #336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,34 +9,25 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" | |
|
|
||
| set -euo pipefail | ||
|
|
||
| # This currently points to 7.0.2 - will likely change in the future, but we do not have | ||
| # a persistent link. | ||
| if [[ "${TARGET_ARCHITECTURE}" == "arm64" ]]; then | ||
| FFMPEG_URL="https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-arm64-static.tar.xz" | ||
| else | ||
| FFMPEG_URL="https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz" | ||
| # Static glibc-based ffmpeg build (https://github.com/BtbN/FFmpeg-Builds), archived | ||
| # by tools/ffmpeg_archive.py and pinned by digest of the multi-arch index | ||
| # so that every deployment gets the exact same binary. | ||
| # TODO: append "@sha256:<digest>" printed by tools/ffmpeg_archive.py --push. | ||
| FFMPEG_IMAGE="spcleth/serverless-benchmarks:ffmpeg-reproducibility-archive-9.0.2" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pin The tag alone can resolve to a different image after a registry update. Later benchmark packages could then contain different FFmpeg binaries under the same version label. Replace this reference with the
|
||
|
|
||
| # SeBS calls the x86_64 architecture "x64", Docker calls it "amd64". | ||
| DOCKER_ARCH="${TARGET_ARCHITECTURE}" | ||
| if [[ "${DOCKER_ARCH}" == "x64" ]]; then | ||
| DOCKER_ARCH="amd64" | ||
| fi | ||
|
|
||
| if command -v wget &>/dev/null; then | ||
| wget "${FFMPEG_URL}" -P "${DIR}" | ||
| elif command -v curl &>/dev/null; then | ||
| curl -fsL "${FFMPEG_URL}" -o "${DIR}/$(basename ${FFMPEG_URL})" | ||
| else | ||
| echo "ERROR: neither wget nor curl available" >&2 | ||
| exit 1 | ||
| fi | ||
| docker build --quiet --platform "linux/${DOCKER_ARCH}" \ | ||
| --output "type=local,dest=${DIR}/ffmpeg" - <<EOF | ||
| FROM scratch | ||
| COPY --from=${FFMPEG_IMAGE} / / | ||
| EOF | ||
|
|
||
| pushd "${DIR}" >/dev/null | ||
| tar -xf ffmpeg-release-*-static.tar.xz | ||
| rm *.tar.xz | ||
| mv ffmpeg-* ffmpeg | ||
| rm -f ffmpeg/ffprobe | ||
| # make the binary executable | ||
| chmod 755 ffmpeg/ffmpeg | ||
| popd >/dev/null | ||
|
|
||
| # Fail loudly if the binary isn't actually there. | ||
| # Sanity check - avoid issues where download silently fails. | ||
| # Sanity check - avoid issues where extraction silently fails. | ||
| if [ ! -x "${DIR}/ffmpeg/ffmpeg" ]; then | ||
| echo "ERROR: ffmpeg binary missing after packaging" >&2 | ||
| exit 1 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright 2020-2025 ETH Zurich and the SeBS authors. All rights reserved. | ||
| """Archive a static ffmpeg build as a SeBS image for the 220.video-processing benchmark. | ||
|
|
||
| The benchmark uses a static, glibc-based ffmpeg from BtbN/FFmpeg-Builds. Upstream only | ||
| keeps its builds for a limited time, so we archive them as a multi-arch image, | ||
| spcleth/serverless-benchmarks:ffmpeg-reproducibility-archive-<version>, similar to the | ||
| Minio archive. | ||
|
|
||
| The binaries come from one of two sources: | ||
|
|
||
| * download (default): the pinned BtbN release tarballs, verified against their sha256. | ||
| * build: BtbN's build scripts at a pinned commit, for when the release has been | ||
| deleted upstream. This takes several hours per architecture. BtbN can only build | ||
| a git tag or branch, so this builds the FFmpeg release tag, not the exact snapshot | ||
| of the downloaded build. It is pushed under a separate <tag>-rebuild tag. | ||
|
|
||
| Building needs a buildx builder that supports multi-platform images, e.g., | ||
| `docker buildx create --use`; pushing also needs `docker login` with access to the | ||
| repository. After pushing, copy the printed FFMPEG_IMAGE line into | ||
| benchmarks/200.multimedia/220.video-processing/init.sh. | ||
| """ | ||
|
|
||
| import argparse | ||
| import hashlib | ||
| import json | ||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tarfile | ||
| import tempfile | ||
| import urllib.request | ||
| from pathlib import Path | ||
|
|
||
| REPOSITORY = "spcleth/serverless-benchmarks" | ||
| TAG_PREFIX = "ffmpeg-reproducibility-archive" | ||
| # The build context contains dist/<arch>/{ffmpeg,LICENSE.txt,SOURCE}. | ||
| DOCKERFILE = """FROM scratch | ||
| ARG TARGETARCH | ||
| COPY dist/${TARGETARCH}/ / | ||
| """ | ||
| BTBN_REPO = "https://github.com/BtbN/FFmpeg-Builds" | ||
|
|
||
| # Docker architecture -> BtbN target. | ||
| TARGETS = {"amd64": "linux64", "arm64": "linuxarm64"} | ||
|
|
||
| RELEASES = { | ||
| "9.0.2": { | ||
| # Month-end autobuilds are retained upstream for two years. | ||
| "btbn_release": "autobuild-2026-10-31-12-59", | ||
| "build_name": "ffmpeg-n9.0.2-39-g7adfbe40dc", | ||
| "sha256": { | ||
| "amd64": "73e29a56974ae5a651133a04584c7483b21afe941e17dc91d79814a758b608d3", | ||
| "arm64": "6cad88ae5bcec356cfc5d7979a036cc4007128201928a0cf243df3db483276cc", | ||
| }, | ||
| # Used only for --source build: the commit of btbn_release, and the FFmpeg tag | ||
| # to build (the downloaded build is 39 commits ahead of it on release/9.0). | ||
| "btbn_commit": "5031c6f1082414e53688d39e521849905071070d", | ||
| "ffmpeg_ref": "n9.0.2", | ||
| "addin": "9.0", | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| def run(cmd: list, **kwargs) -> None: | ||
| """Run a command, echoing it first, and fail on a non-zero exit code.""" | ||
| print("+", " ".join(str(c) for c in cmd), flush=True) | ||
| subprocess.run(cmd, check=True, **kwargs) | ||
|
|
||
|
|
||
| def sha256sum(path: Path) -> str: | ||
| """Compute the sha256 digest of a file.""" | ||
| digest = hashlib.sha256() | ||
| with open(path, "rb") as f: | ||
| for chunk in iter(lambda: f.read(1 << 20), b""): | ||
| digest.update(chunk) | ||
| return digest.hexdigest() | ||
|
|
||
|
|
||
| def download(release: dict, arch: str, work: Path) -> tuple: | ||
| """Download and verify the BtbN release tarball; return its path and source note.""" | ||
| name = f"{release['build_name']}-{TARGETS[arch]}-gpl-{release['addin']}.tar.xz" | ||
| url = f"{BTBN_REPO}/releases/download/{release['btbn_release']}/{name}" | ||
| tarball = work / name | ||
| print(f"Downloading {url}", flush=True) | ||
| with urllib.request.urlopen(url) as response, open(tarball, "wb") as f: | ||
| shutil.copyfileobj(response, f) | ||
|
|
||
| actual = sha256sum(tarball) | ||
| if actual != release["sha256"][arch]: | ||
| raise RuntimeError( | ||
| f"Checksum mismatch for {name}: expected {release['sha256'][arch]}, got {actual}" | ||
| ) | ||
| return tarball, f"{BTBN_REPO}/releases/tag/{release['btbn_release']} {name}" | ||
|
|
||
|
|
||
| def build(release: dict, arch: str, work: Path) -> tuple: | ||
| """Rebuild with BtbN's scripts at the pinned commit; return the tarball and source note.""" | ||
| repo = work / f"btbn-{arch}" | ||
| run(["git", "clone", "--quiet", BTBN_REPO, repo]) | ||
| run(["git", "-C", repo, "checkout", "--quiet", release["btbn_commit"]]) | ||
|
|
||
| target = TARGETS[arch] | ||
| env = dict(os.environ, GIT_BRANCH_OVERRIDE=release["ffmpeg_ref"]) | ||
| run(["./makeimage.sh", target, "gpl", release["addin"]], cwd=repo, env=env) | ||
| run(["./build.sh", target, "gpl", release["addin"]], cwd=repo, env=env) | ||
|
|
||
| tarballs = list((repo / "artifacts").glob("*.tar.xz")) | ||
| if len(tarballs) != 1: | ||
| raise RuntimeError(f"Expected one build artifact, found {tarballs}") | ||
| note = f"{BTBN_REPO}/commit/{release['btbn_commit']} built from FFmpeg {release['ffmpeg_ref']}" | ||
| return tarballs[0], note | ||
|
|
||
|
|
||
| def extract(tarball: Path, source_note: str, dest: Path) -> None: | ||
| """Place ffmpeg, its license, and a note on its source into dest.""" | ||
| dest.mkdir(parents=True) | ||
| with tarfile.open(tarball) as tar: | ||
| for member in tar.getmembers(): | ||
| # Layout: <build name>/bin/ffmpeg and <build name>/LICENSE.txt | ||
| parts = Path(member.name).parts[1:] | ||
| if parts in (("bin", "ffmpeg"), ("LICENSE.txt",)): | ||
| member.name = parts[-1] | ||
| tar.extract(member, dest, filter="data") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Support the declared Python 3.10 range. On Python 3.10.0–3.10.11,
|
||
|
|
||
| if not (dest / "ffmpeg").is_file() or not (dest / "LICENSE.txt").is_file(): | ||
| raise RuntimeError(f"{tarball} does not contain bin/ffmpeg and LICENSE.txt") | ||
| (dest / "ffmpeg").chmod(0o755) | ||
| (dest / "SOURCE").write_text( | ||
| f"FFmpeg source: https://github.com/FFmpeg/FFmpeg\n{source_note}\n" | ||
| ) | ||
|
|
||
|
|
||
| def main() -> int: | ||
| """Fetch or build the binaries for all architectures and build the archive image.""" | ||
| parser = argparse.ArgumentParser( | ||
| description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
| ) | ||
| parser.add_argument("version", nargs="?", default="9.0.2", choices=sorted(RELEASES)) | ||
| parser.add_argument("--source", choices=["download", "build"], default="download") | ||
| parser.add_argument( | ||
| "--push", action="store_true", help="push the image; otherwise only build it" | ||
| ) | ||
| parser.add_argument("--keep", action="store_true", help="keep the working directory") | ||
| args = parser.parse_args() | ||
|
|
||
| release = RELEASES[args.version] | ||
| image = f"{REPOSITORY}:{TAG_PREFIX}-{args.version}" | ||
| if args.source == "build": | ||
| image += "-rebuild" | ||
| fetch = download if args.source == "download" else build | ||
|
|
||
| work = Path(tempfile.mkdtemp(prefix="sebs-ffmpeg-")) | ||
| try: | ||
| for arch in TARGETS: | ||
| tarball, note = fetch(release, arch, work) | ||
| extract(tarball, note, work / "context" / "dist" / arch) | ||
|
|
||
| metadata = work / "metadata.json" | ||
| cmd = [ | ||
| "docker", | ||
| "buildx", | ||
| "build", | ||
| "--platform", | ||
| ",".join(f"linux/{arch}" for arch in TARGETS), | ||
| "--provenance=false", | ||
| "--file", | ||
| "-", | ||
| "--tag", | ||
| image, | ||
| "--metadata-file", | ||
| metadata, | ||
| ] | ||
| if args.push: | ||
| cmd.append("--push") | ||
| run(cmd + [work / "context"], input=DOCKERFILE.encode()) | ||
|
Comment on lines
+175
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if args.push: | ||
| digest = json.loads(metadata.read_text())["containerimage.digest"] | ||
| print(f'\nFFMPEG_IMAGE="{image}@{digest}"') | ||
| else: | ||
| print(f"\nBuilt {image} without pushing; rerun with --push to publish it.") | ||
| finally: | ||
| if args.keep: | ||
| print(f"Working directory: {work}") | ||
| else: | ||
| shutil.rmtree(work) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I documented musl here briefly, it sadly really seems like we're unable to use it.