# Replacing the FFmpeg and video player that ship with Grand Media Station

Grand Media Station (GMS) ships two pieces of free software under the GNU
Lesser General Public License (LGPL):

- **FFmpeg** — the `ffmpeg` and `ffprobe` programs GMS runs, as separate
  processes, to inspect files, convert them and pull frames.
- **The in-app video player** — the mpv library (`libmpv`) and the FFmpeg
  libraries built into or beside it, which the player loads while GMS runs.

The LGPL gives you the right to swap either one for your own build, modified
or not, and to keep using GMS with it. This page says where each component
lives, how to replace it, and how the shipped copies were built so a
replacement can match them. Nothing here changes your licence to use GMS.

The licence texts (`LGPL-2.1.txt`, `LGPL-3.0.txt`), the full third-party
notices and the source offer are installed next to this page: in the
`licenses` folder under the install folder on Windows, and under
`usr/share/licenses/grand-media-station/` inside the AppImage on Linux.
The complete source for every LGPL component, with the scripts that built
them, is published at <https://downloads.grandmediastation.com/source/>.

## Windows

### Where the files are

GMS installs into the folder you chose at first install; the default is
`C:\Program Files\Grand Media Station`. Inside it:

| Component | Files |
|---|---|
| FFmpeg programs | `ffmpeg\ffmpeg.exe`, `ffmpeg\ffprobe.exe` |
| How they were built | `ffmpeg\BUILDINFO.txt` (versions, the exact `configure` line, `ffmpeg -buildconf`), `ffmpeg\licenses\` |
| Video player | `libmpv-2.dll`, next to `grand_media_station.exe` |
| Licence texts and notices | `licenses\` |

These are ordinary files. GMS does not check their version or signature; it
runs `ffmpeg.exe` and `ffprobe.exe` by that path with ordinary command-line
arguments, and the player opens `libmpv-2.dll` by name from the install
folder.

### Replacing them

1. Quit GMS.
2. Replace `ffmpeg\ffmpeg.exe` and `ffmpeg\ffprobe.exe`, or `libmpv-2.dll`,
   with your own build. Keep the same file names. Replacing a file under
   `C:\Program Files` needs administrator rights.
3. Start GMS.

### Keep the updater from putting the shipped FFmpeg back

GMS updates its bundled tools on their own between app releases, so a
security fix to FFmpeg reaches you without a full app update. This is on by
default. The updater compares the version recorded in
`ffmpeg\gms-binaries-installed.json` with the version GMS publishes, and
when a newer one is published it replaces the files in the `ffmpeg` folder
(keeping the previous copy in `ffmpeg\.previous\`). Today GMS publishes
these updates for FFmpeg and for the speech-recognition engine; the player
is not among them. So a replacement `ffmpeg.exe` or `ffprobe.exe` stays in place
until the next FFmpeg update is published, and that update overwrites it.

To turn the updater off there is, today, no switch in Settings; the
preference exists but is set by hand. With GMS not running, open

    %APPDATA%\GMS\GrandMediaStation\shared_preferences.json

in a text editor and add the key `gms_binary_autoupdate_enabled` with the
value `false`, for example:

```json
{
  "gms_binary_autoupdate_enabled": false
}
```

If the file already has other entries, add the key beside them (keep the
JSON valid, one comma between entries). Delete the key, or set it to `true`,
to turn automatic updates back on. Settings → About → Bundled tools still
shows the installed version and lets you move between the current and the
kept previous version by hand.

A full app update (a new GMS version) reinstalls the shipped `ffmpeg` folder
and `libmpv-2.dll`, so repeat the replacement after upgrading GMS.

## Linux

GMS for Linux is an AppImage, `GrandMediaStation-<version>-x86_64.AppImage`.
An AppImage is a read-only disk image, so its files cannot be edited in
place. Unpack it, replace the files, then either run the unpacked copy or
pack it back into a new AppImage.

### Where the files are

Unpack with

```sh
./GrandMediaStation-<version>-x86_64.AppImage --appimage-extract
```

which creates a `squashfs-root` folder in the current directory. Inside
`squashfs-root`:

| Component | Files |
|---|---|
| FFmpeg programs | `usr/bin/ffmpeg/ffmpeg`, `usr/bin/ffmpeg/ffprobe` (static executables) |
| How they were built | `usr/bin/ffmpeg/BUILDINFO.txt`, `usr/bin/ffmpeg/licenses/` |
| Video player | `usr/lib/libmpv.so.2`, and the FFmpeg libraries it loads in `usr/lib/player/` (`libavcodec.so.*`, `libavformat.so.*`, `libavutil.so.*`, `libavfilter.so.*`, `libswscale.so.*`, `libswresample.so.*`) |
| How the player was built | `usr/share/licenses/grand-media-station/linux-player/` (the build script, the licence texts, and a list of what went in) |
| Every bundled library and where it came from | `usr/share/licenses/grand-media-station/BUNDLED_LIBRARIES.tsv` |
| Licence texts and notices | `usr/share/licenses/grand-media-station/` |

GMS runs `usr/bin/ffmpeg/ffmpeg` and `ffprobe` by that path, with ordinary
command-line arguments, and does not check their version. The player loads
`libmpv.so.2` by name from `usr/lib`. The shipped `libmpv.so.2` finds its
FFmpeg libraries through its own run path, which points at the `player`
folder beside it, so they are not on the general library path.

The bundled-tool updater described above does nothing on Linux: it sees the
read-only image and skips. Bundled tools change only with a new AppImage.

### Replacing them

1. Quit GMS.
2. Unpack the AppImage as above.
3. Replace the files, keeping the same names:
   - `squashfs-root/usr/bin/ffmpeg/ffmpeg` and `ffprobe`. Make them
     executable (`chmod 755`). A static build needs nothing else; a
     dynamically linked build must find its libraries on the system or
     on the AppImage's library path (`usr/lib`, `usr/bin/lib`, `usr/bin`).
   - `squashfs-root/usr/lib/libmpv.so.2`, and the FFmpeg libraries in
     `squashfs-root/usr/lib/player/`. The simplest replacement keeps the
     shipped layout: your `libmpv.so.2` with a run path of `$ORIGIN/player`
     (`patchelf --set-rpath '$ORIGIN/player' usr/lib/libmpv.so.2`) and your
     FFmpeg libraries in `usr/lib/player/` with a run path of `$ORIGIN`.
     Putting the FFmpeg libraries straight into `usr/lib` also works, since
     that folder is on the library path.
4. Run the unpacked copy with `squashfs-root/AppRun`, or pack it back into
   a single file with [appimagetool](https://github.com/AppImage/appimagetool):

   ```sh
   ARCH=x86_64 appimagetool squashfs-root GrandMediaStation-modified.AppImage
   ```

## How the shipped components were built

Each build is reproducible from scripts that ship with the source. Matching
the shipped build is the easiest way to be sure GMS keeps working: use the
same FFmpeg major version and the same set of enabled codecs and libraries,
and build FFmpeg without `--enable-gpl` or `--enable-nonfree` if you want
the result to stay LGPL. GMS itself only needs the standard `ffmpeg` and
`ffprobe` command-line behaviour (it reads `ffprobe` output in its JSON and
default formats) and a `libmpv` that exports the mpv client API.

| Component | Built from | Source |
|---|---|---|
| FFmpeg programs, Windows and Linux (FFmpeg 9.0.2, LGPL variant) | `scripts/release/build_ffmpeg_program.sh <linux-x64\|win-x64> lgpl <outdir>`. Static executables, `--disable-autodetect`, with each external library (opus, dav1d, SVT-AV1, libaom, libvpx, libwebp, libass, FreeType, FriBidi, HarfBuzz, kvazaar, openh264, zimg and the hardware-codec headers) built from a pinned, checksummed release. The full `configure` line and every component's checksum are in `BUILDINFO.txt` beside the programs. The Windows programs are cross-compiled with MinGW-w64 and linked `-static`. | `source/ffmpeg-program/` on the source page: every pinned source archive, the build script and `BUILDINFO.txt` |
| Video player, Linux (mpv 0.41.0 and FFmpeg 8.1.3) | `scripts/release/build_lgpl_player_linux.sh`. FFmpeg as shared libraries, configured without `--enable-gpl`, `--enable-nonfree` or `--enable-version3`, with `--disable-autodetect`, `--disable-programs`, `--disable-avdevice`, dav1d, VA-API, DRM and the NVIDIA decode headers; mpv as `libmpv` with `-Dgpl=false` and every optional feature off unless the script names it. The same script, with its pins and flags, ships inside the AppImage under `usr/share/licenses/grand-media-station/linux-player/`. | `source/linux-player/` on the source page |
| Video player, Windows (`libmpv-2.dll`) | Not built by GMS: it is the `libmpv-2.dll` that the media_kit Flutter package (`media_kit_libs_windows_video` 1.0.11) bundles from the media-kit `libmpv-win32-video-build` project, release 2023-09-24. mpv at commit `652a1dd9` built with `-Dgpl=false`; FFmpeg 6.0 built with `--enable-version3` and without `--enable-gpl`; FriBidi, soxr and uchardet alongside. | `source/windows-player/` on the source page: the source of each LGPL library, the build recipe at the commit used, and a `README.txt` naming the versions |

The source page is <https://downloads.grandmediastation.com/source/>. If
that address is ever unreachable, `SOURCE_OFFER.txt`, installed next to
this page, says how to ask for a copy.
