# Rebuilding the FFmpeg engine

The Video & Audio Converter runs `ffmpeg-core.<fingerprint>.wasm` and `ffmpeg-core.<fingerprint>.js`, a build of **FFmpeg 5.1.4 under the
LGPL (version 2.1 or later)**, made WITHOUT `--enable-gpl` and WITHOUT `--enable-nonfree`. It contains no GPL library (no x264, no x265). It
is the official ffmpeg.wasm build recipe, reduced to the libraries the converter uses.

This folder is the corresponding source of that binary. Nothing here is hidden from the person who receives the engine: the two engine
files are loaded by the page at run time as separate files, so a rebuilt pair can replace them (see "Replacing the engine").

## What is inside the binary

| Component | Version / revision | License | Source |
| --- | --- | --- | --- |
| FFmpeg (libavcodec, libavformat, libavfilter, libavutil, libswscale, libswresample, libavdevice) | n5.1.4, `4729204c17f756e186d622060088371d10b34f7e` (unmodified) | LGPL-2.1-or-later | `source/FFmpeg-n5.1.4-4729204c.tar.gz` |
| libvpx (VP8 and VP9 codec) | 1.13.1, ffmpegwasm fork `10b9492dcf05b652e2e4b370e205bd605d421972` | BSD-3-Clause (+ patent grant) | `source/libvpx-ffmpegwasm-10b9492d.tar.gz` |
| LAME (MP3 encoder) | ffmpegwasm fork `2badea1974ae36cb8312afe99cff1e6b3b5decee` | LGPL-2.0-or-later | `source/lame-ffmpegwasm-2badea19.tar.gz` |
| zlib | 1.2.11, ffmpegwasm fork `cacf7f1d4e3d44d871b605da3b647f07d718623f` | zlib license | `source/zlib-ffmpegwasm-cacf7f1d.tar.gz` |
| ffmpeg.wasm bindings and command-line wrapper (`src/bind`, `src/fftools`) and build scripts | `b409e36475bc21f0451b5b1e1d126fa82871439a` (17 Jan 2025) | MIT (the fftools are derived from FFmpeg's LGPL tools) | `source/ffmpeg.wasm-b409e364.tar.gz` |
| Emscripten runtime (in the JS glue) | 3.1.40, image `emscripten/emsdk@sha256:c1e807a6e03ac5bd5b37bae2ace3c46c08579e2ddeb951037a3b8dac7067f2cc` | MIT / University of Illinois-NCSA | https://github.com/emscripten-core/emscripten/tree/3.1.40 |

The license text of each is in `licenses/`. `PROVENANCE.json` records every revision, the configuration and the SHA-256 of every file.

## FFmpeg configuration

```
--target-os=none --arch=x86_32 --enable-cross-compile --disable-asm --disable-stripping --disable-programs --disable-doc --disable-debug
--disable-runtime-cpudetect --disable-autodetect --nm=emnm --ar=emar --ranlib=emranlib --cc=emcc --cxx=em++ --objcc=emcc --dep-cc=emcc
--extra-cflags='-I/opt/include -O3 -msimd128' --extra-cxxflags='-I/opt/include -O3 -msimd128'
--disable-pthreads --disable-w32threads --disable-os2threads
--enable-libvpx --enable-libmp3lame --enable-zlib
```

The engine itself reports this with `-buildconf`, and its license with `-L` ("GNU Lesser General Public License ... version 2.1").

## Differences from the upstream ffmpeg.wasm recipe

`Dockerfile.lgpl` is upstream's `Dockerfile` at commit `b409e36` (the release commit of @ffmpeg/core 0.12.10, `71aa99d`, plus a later 5 MB stack-size change) with these changes only:

- no `libx264`, `libx265` and no `--enable-gpl`;
- no libass, freetype, fribidi, harfbuzz, zimg, theora, ogg, vorbis, opus or libwebp (the converter does not use them);
- `libpostproc` (which exists only in a GPL build of FFmpeg) is not linked: `sed -i '/-Llibpostproc/d;/-lpostproc/d' build.sh`;
- every source is pinned to an exact commit instead of a branch name, and the emscripten image to its digest;
- only the ES module build (`dist/esm`) is made.

FFmpeg itself is unmodified.

## Rebuilding

You need Docker with buildx and an internet connection.

```bash
# 1. the recipe and the ffmpeg.wasm sources
mkdir build && cd build
tar xzf ../source/ffmpeg.wasm-b409e364.tar.gz --strip-components=1
# the shell scripts must have LF line endings
find build src Dockerfile -type f \( -name '*.sh' -o -name '*.js' -o -name '*.c' -o -name '*.h' \) -exec sed -i 's/\r$//' {} +
cp ../Dockerfile.lgpl .

# 2. build (the same arguments as upstream's "make prd")
docker buildx build --progress=plain \
  --build-arg FFMPEG_ST=yes --build-arg EXTRA_CFLAGS="-O3 -msimd128" \
  -f Dockerfile.lgpl -o ./out .
# the result is ./out/dist/esm/ffmpeg-core.js and ffmpeg-core.wasm
```

`Dockerfile.lgpl` downloads FFmpeg, libvpx, LAME and zlib from the commits above. The same trees are in `source/`; to build from those
instead, replace each `ADD https://github.com/...#<commit> /src` line with `ADD <tarball> /src` and extract with `--strip-components=1`.
A rebuild is not guaranteed to be byte-identical (compiler output can differ), which is why the checksums above are of the binary shipped.

## Replacing the engine

The page loads `/ffmpeg/ffmpeg-core.<fingerprint>.js` and `.wasm` (names in `PROVENANCE.json`). To use a rebuilt engine, put your two files
in `third_party/ffmpeg-core/dist/`, update the checksums and names in `PROVENANCE.json`, and run `pnpm build`. The build refuses a core that
does not match its recorded checksum or whose recorded configuration is not LGPL-only.
