# ffmpeg.exe and ffprobe.exe of LudicoAI for Windows

LudicoAI for Windows includes `ffmpeg.exe` and `ffprobe.exe` (in `resources\bin\`), built from FFmpeg's official
source code - nothing in them is LudicoAI's own code. This folder holds what builds them; the published package
(https://ludicoai.com/download/ffmpeg-source/) holds the same files plus the four source tarballs.

## Sources

They are built from these releases, unmodified:

| Source | Licence | From | SHA-256 |
| --- | --- | --- | --- |
| FFmpeg 6.1.6 (git tag `n6.1.6`, commit `f1e3a2bf7a2f2cde936d1ed97f09a26853d20125`) | LGPL 2.1 or later | https://ffmpeg.org/releases/ffmpeg-6.1.6.tar.xz (signed by FFmpeg's release key `FCF9 86EA 15E6 E293 A564 4F10 B432 2F04 D676 58D8`) | `d4fcb164028dd3beee5d92c0ac72e46aac6973c75ea12dc14de07bf8f407370a` |
| LAME 3.100 | LGPL 2.0 or later | https://downloads.sourceforge.net/project/lame/lame/3.100/lame-3.100.tar.gz | `ddfe36cab873794038ae2c1210557ad34857a4b6bdc515785d1da9e175b1da1e` |
| dav1d 1.5.4 | BSD-2-Clause | https://downloads.videolan.org/pub/videolan/dav1d/1.5.4/dav1d-1.5.4.tar.xz | `686616b7c69eb88d44459391ab25cac13b6647a3b288835c5784e71c1514a5c5` |
| zlib 1.3.1 | zlib | https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.xz | `38ef96b8dfe510d42707d9c781877914792541133e1870841463bfa73f883e32` |

Each project's licence text is inside its tarball (FFmpeg: `COPYING.LGPLv2.1`, `LICENSE.md`).

## What LudicoAI asks of them

Every command the app runs (version 1.1.2), and what it needs:

| Command | Where | Needs |
| --- | --- | --- |
| `ffmpeg -i <video> -vn -ac 1 -ar 16000 -f f32le pipe:1` | `tools/whisper-engine.js` (transcription) | every demuxer and audio decoder, the resampler, `pcm_f32le` encoder + muxer, the pipe protocol |
| `ffmpeg -i <video> -vn -ac 1 -ar 16000 -f s16le <file>` | `tools/voices-engine.js` (who speaks, accents) | the same, `pcm_s16le`, the file protocol |
| `ffmpeg -i <video> -vn -ac 1 -ar 16000 -f s16le -` | `accent-detector/detect_accents.py` (development) | the same, to stdout |
| `ffmpeg -ss <t> -i <video> -frames:v 1 -vf scale=640:-2 -q:v 5 <file>.jpg` | `tools/local-library.js` (the Library's pictures) | every video decoder (AV1: dav1d), the `scale` filter, `mjpeg` encoder, `image2` muxer |
| `ffmpeg -i <video> -vn -ar 16000 -ac 1 -b:a 64k <file>.mp3` | `main.js` (the development cloud path) | LAME (`libmp3lame`), `mp3` muxer |
| `ffprobe -show_entries format=duration ... <video>` | `main.js` (a video's length) | every demuxer |

Nothing there needs a GPL part of FFmpeg (x264, x265, xvid, rubberband, vid.stab, postproc...) or a non-free one:
the build is configured **without** `--enable-gpl` and without `--enable-nonfree`, so the result is under the
LGPL 2.1 or later. The build stops if that ever changes.

## The configuration

- **In:** every FFmpeg decoder, demuxer, parser, bitstream filter and filter - a learner's video of any kind opens,
  as before; LAME, dav1d (AV1 videos' pictures: FFmpeg has no software AV1 decoder of its own) and zlib (compressed
  MKV/MOV headers, PNG).
- **Only what the commands above use:** the encoders `pcm_s16le, pcm_f32le, mjpeg, png, libmp3lame, wrapped_avframe,
  rawvideo` and the muxers `pcm_s16le, pcm_f32le, wav, mp3, image2, image2pipe, null, md5, framemd5`.
- **Out:** the network (`--disable-network`; only the `file` and `pipe` protocols), devices (`--disable-avdevice`),
  `ffplay`, hardware acceleration and every other library FFmpeg could detect (`--disable-autodetect`), the docs.
- Statically linked, cross-compiled for 64-bit Windows with mingw-w64 (win32 threads); the two programs load only
  Windows' own DLLs (`KERNEL32`, `msvcrt`, `bcrypt`, `SHELL32`).

The full configure line, the toolchain's package versions and the SHA-256 of the two programs are in
`BUILDINFO.txt`.

## Compared with what LudicoAI shipped until 1.1.1

Until 1.1.1 the app shipped npm's ffmpeg-static 5.3.0 (gyan.dev's FFmpeg 6.1.1 "essentials", GPL v3, with about 40
external libraries) and ffprobe-static 3.1.0 (a 2018 FFmpeg 4.0.2, GPL v3). Their complete source could not be
published: their builders released neither their build scripts nor several of their libraries' exact versions.

| | Until 1.1.1 | Since 1.1.2 |
| --- | --- | --- |
| ffmpeg.exe | 82.8 MB, GPL v3 | 22.3 MB, LGPL 2.1+ |
| ffprobe.exe | 63.1 MB, GPL v3 | 22.2 MB, LGPL 2.1+ |
| Together | 145.9 MB | 44.5 MB (-101.4 MB) |

Checked before release, on 20 generated test videos (MP4, MKV, WebM, MOV, AVI, TS, 3GP, FLV, WMV, OGG, M4A, MP3,
FLAC, WAV, Opus; H.264, HEVC, VP9, AV1, MPEG-4, Theora, WMV2; AAC, Opus, Vorbis, ALAC, MP3, AC-3, E-AC-3, AMR-NB,
WMA, 5.1) and 6 real ones: the sound the app reads is the same, byte for byte, in both formats (float and 16-bit);
the lengths are the same; the pictures come out wherever the old build made them; and the app's transcription,
speakers and accents come out identical.

## Building them again

You need Docker. Put the four tarballs in a folder, with `build.sh`, `Dockerfile` and `mingw64-cross.ini` in
another, then:

```sh
docker build -t ludicoai-ffmpeg-toolchain:1 <recipe folder>
docker run --rm -v <tarball folder>:/sources:ro -v <recipe folder>:/recipe:ro -v <output folder>:/out \
    ludicoai-ffmpeg-toolchain:1 sh /recipe/build.sh
```

- `Dockerfile` installs the toolchain: Debian 13 as it was on a fixed date (snapshot.debian.org), with the
  mingw-w64 cross compiler for 64-bit Windows, nasm, meson and ninja.
- `build.sh` checks each tarball's SHA-256, builds zlib, LAME and dav1d as static libraries, then FFmpeg.
- `<output folder>` then holds `ffmpeg.exe`, `ffprobe.exe` and `BUILDINFO.txt`. They are the same bytes as the
  files LudicoAI ships.

In LudicoAI's repository, `npm run ffmpeg:build` (`node tools/build-ffmpeg.js`) does all of this.
`node tools/build-ffmpeg.js --verify` rebuilds from the published package alone and checks that the result is
identical.
