> For the complete documentation index, see [llms.txt](https://docs.fivemstats.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fivemstats.net/tgiann-fivem-stats-script/media-service.md).

# Media Service

The small program that stores screenshots, videos, logs, statistics and backups next to FXServer.

The script starts a small program (`media/bin/tgiann-media`) beside FXServer as its own process. It stores screenshots, videos, audio, JSON data files, logs, statistics and database backups, and converts uploads. Heavy work (video conversion, queries, backups) never slows the game server down.

* It starts and stops with the resource and restarts itself after a crash (after 1, 5, 15 and 60 seconds).
* It shuts down by itself when FXServer exits.
* It runs at a lower priority than FXServer.
* FXServer's sandbox only lets it start with `add_unsafe_child_process_permission "tgiann-fivem-stats-script"` in `server.cfg`, see [Installation](https://docs.fivemstats.net/tgiann-fivem-stats-script/installation.md).

## Windows and Linux

`media/bin` holds both builds, the script picks the right one. Nothing to download or install, also on hosts where you cannot install programs.

| File                             | Used on |
| -------------------------------- | ------- |
| `tgiann-media.exe`, `ffmpeg.exe` | Windows |
| `tgiann-media`, `ffmpeg`         | Linux   |

On Linux the script makes both files executable on start. `ffmpeg` is a small LGPL build of FFmpeg with only the parts the service uses. Keep `THIRD-PARTY-LICENSES.txt` next to it.

## Where files are stored

In `tgiann-media/` inside the script folder (`tgiann-fivem-stats-script/tgiann-media/`). FXServer's sandbox only lets a resource use its own folder, so this place cannot be changed.

```
tgiann-media/
├── api-key          made on the first start, see API key below
├── settings.json    the settings you edit in the panel
├── stats.db         statistics
├── media.db         the list of files
├── logs.db          server logs
├── files/           screenshots, videos, audio, JSON files
├── backups/         database backups
├── bin/             a copy of the service the script runs
└── tls/             the HTTPS certificate
```

{% hint style="warning" %}
When you update the script, keep this folder: copy it into the new script folder before you start the server. Deleting the old script folder deletes your statistics, logs, files and backups. See [Updating from an Older Version](https://docs.fivemstats.net/tgiann-fivem-stats-script/updating-from-an-older-version.md).
{% endhint %}

{% hint style="warning" %}
Do not edit `settings.json` by hand while the server runs. Change settings in the panel instead: [Settings > Game script](https://docs.fivemstats.net/panel/settings.md#game-script). They apply at once, no restart.
{% endhint %}

To keep statistics, logs and media when you move to a new machine, copy the whole `tgiann-media` folder. [Database Backups](https://docs.fivemstats.net/tgiann-fivem-stats-script/database-backups.md) only cover your game database.

## Convars

All optional, set them before `ensure tgiann-fivem-stats-script`.

| Convar                                           | Default                  | What it does                                                                                                          |
| ------------------------------------------------ | ------------------------ | --------------------------------------------------------------------------------------------------------------------- |
| `tgiann_media_port`                              | `6174`                   | The media port, see [Ports and Firewall](https://docs.fivemstats.net/tgiann-fivem-stats-script/ports-and-firewall.md) |
| `tgiann_media_host`                              | `0.0.0.0`                | The address it listens on. `127.0.0.1` behind a reverse proxy                                                         |
| `tgiann_media_https`                             | `auto`                   | `auto`, `custom` or `off`, see [HTTPS](https://docs.fivemstats.net/tgiann-fivem-stats-script/https.md)                |
| `tgiann_media_public_ip`                         | found on start           | The IP the `auto` certificate is made for                                                                             |
| `tgiann_media_tls_cert` / `tgiann_media_tls_key` |                          | Certificate files for `custom`                                                                                        |
| `tgiann_media_ffmpeg`                            | the build in `media/bin` | Use your own FFmpeg instead                                                                                           |
| `tgiann_media_api_key`                           | made on the first start  | Use your own API key instead of the `api-key` file                                                                    |

The address in links (a domain, a reverse proxy) is not a convar. It is set in the panel: [Settings > Media storage > Public address](https://docs.fivemstats.net/panel/settings.md#media-storage).

The service reads your framework tables and takes backups with oxmysql's `mysql_connection_string`. If `server.cfg` limits who can read it, add `add_convar_permission tgiann-fivem-stats-script read mysql_connection_string`.

## File formats

Files a browser already plays are stored as they are: WebM (VP8, VP9, AV1 with Opus or Vorbis), MP3, OGG, WebP. MP4 (H.264) and M4A (AAC) are only rearranged so they start playing at once. Everything else is converted:

| Upload | Stored as                                                                                                                                  | Size limit |
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------ | ---------- |
| Image  | WebP, quality from [Settings > Media storage > Image quality](https://docs.fivemstats.net/panel/settings.md#media-storage) (85 by default) | 25 MB      |
| Video  | WebM, VP9 and Opus                                                                                                                         | 250 MB     |
| Audio  | Opus in OGG                                                                                                                                | 50 MB      |
| JSON   | As it is: `.json`, or `.gz` when it is gzip compressed                                                                                     | 50 MB      |

JSON files are data your scripts save, like bodycam recordings. The service checks that the file is complete JSON (a gzip file is checked after unpacking) and stores it byte for byte. Links serve them as `application/json` or `application/gzip`; a gzip file stays compressed, the script that reads it unpacks it.

How long files are kept is set per type in [Settings > Media storage](https://docs.fivemstats.net/panel/settings.md#media-storage) (forever by default). Files marked **Keep forever** on the [Media](https://docs.fivemstats.net/panel/media.md) page are never removed.

## API key

`tgiann-media/api-key` is the key the script uses to talk to the service, and the key tools outside FiveM send in the `Authorization` header. Keep it private. To change it, delete the file (or set `tgiann_media_api_key`) and restart the resource.

{% hint style="info" %}
The API key is not the panel's secret key. The panel uses `tgiann_stats_secret_key` and never sees the API key.
{% endhint %}
