# jellyfin/jellyfin-mpv-shim architecture

> Architecture diagram of the GitHub repository jellyfin/jellyfin-mpv-shim, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jellyfin/jellyfin-mpv-shim
- Repository: https://github.com/jellyfin/jellyfin-mpv-shim

## Overview

#### Purpose and entry points
Jellyfin MPV Shim is a cross-platform Python desktop/cast-target client for Jellyfin. It plays media through MPV, can expose itself for remote control/casting, and optionally provides a native library browser and offline downloads. Primary launch surfaces are `run.py`, command-line handling in `jellyfin_mpv_shim/args.py`, and application orchestration in `jellyfin_mpv_shim/mpv_shim.py`.

#### Startup, process lifecycle, and configuration
Startup parses CLI/configuration, establishes one instance per config directory, initializes UI/player services, and supports restart/stop behavior. Configuration and persisted credentials/preferences form a boundary between local state and Jellyfin sessions. Key paths: `jellyfin_mpv_shim/conf.py`, `jellyfin_mpv_shim/conffile.py`, `jellyfin_mpv_shim/single_instance.py`, `jellyfin_mpv_shim/restart.py`.

#### Jellyfin connectivity and session control
The client communicates with user-configured Jellyfin servers for authentication, library metadata, playback control, progress reporting, Live TV, and SyncPlay. Multi-server/user behavior is concentrated in client/session abstractions and browser gateway adapters. Key paths: `jellyfin_mpv_shim/clients.py`, `jellyfin_mpv_shim/session_reporter.py`, `jellyfin_mpv_shim/mpvtk_browser/gateway/servers.py`, `jellyfin_mpv_shim/mpvtk_browser/gateway/playback.py`.

#### Playback engine and remote-control boundary
Playback control translates Jellyfin play requests and local input into MPV operations, then reports player state back to Jellyfin. MPV events, OSC integration, subtitles/audio selection, trickplay, and external-MPV support belong on this boundary. Key paths: `jellyfin_mpv_shim/player.py`, `jellyfin_mpv_shim/mpv_events.py`, `jellyfin_mpv_shim/osc_bridge.py`, `jellyfin_mpv_shim/media.py`.

#### Native browser and rendering stack
The optional desktop browser is a custom MPV-rendered UI rather than a web application. `mpvtk` supplies rendering/layout/widgets; `mpvtk_browser` owns navigation, pages, UI state, library presentation, settings, and user interactions. Browser code reaches backend behavior through the gateway layer, avoiding direct page-to-player/server coupling. Key paths: `jellyfin_mpv_shim/mpvtk/app.py`, `jellyfin_mpv_shim/mpvtk_browser/app.py`, `jellyfin_mpv_shim/mpvtk_browser/navigator.py`, `jellyfin_mpv_shim/mpvtk_browser/pages/`.

#### Specialized media, synchronization, and integrations
Offline sync persists download state locally and reconciles it with Jellyfin; SyncPlay coordinates group playback. Separate flows support books/comics/EPUB handling, Live TV, shaders/SVP, gamepads, tray behavior, and Discord presence. Key paths: `jellyfin_mpv_shim/sync/manager.py`, `jellyfin_mpv_shim/sync/db.py`, `jellyfin_mpv_shim/syncplay.py`, `jellyfin_mpv_shim/live_tv.py`, `jellyfin_mpv_shim/books.py`.

#### Runtime and distribution boundaries
Runtime dependencies include Python, libmpv/MPV, and embedded Lua scripts for MPV-side rendering and playback features. Jellyfin is the principal external network service; optional desktop integration includes system tray, Discord, SVP, and external MPV. Windows builds package the application with PyInstaller/Inno Setup; Linux packaging includes Flatpak. Key paths: `pyproject.toml`, `jellyfin_mpv_shim/mpvtk/renderer.lua`, `jellyfin_mpv_shim/thumbfast.lua`, `flatpak/com.github.iwalton3.jellyfin-mpv-shim.json`, `.github/workflows/main.yml`.

Main entry points: [`jellyfin_mpv_shim/mpv_shim.py`](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpv_shim.py) (Application Orchestrator), [`jellyfin_mpv_shim/player.py`](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/player.py) (Playback Controller), [`jellyfin_mpv_shim/clients.py`](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/clients.py) (Jellyfin Clients & Sessions), [`jellyfin_mpv_shim/mpvtk_browser/app.py`](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk_browser/app.py) (Browser App & Navigation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Client Core"]
  node_launch["Launch &amp; CLI<br/>entrypoint<br/>[run.py]"]
  node_orchestrator["Application Orchestrator<br/>lifecycle<br/>[mpv_shim.py]"]
  node_config[("Config &amp; Local State<br/>persistence<br/>[conf.py]")]
  node_single_instance{{"Single Instance<br/>process-control<br/>[single_instance.py]"}}
  node_jellyfin_clients["Jellyfin Clients &amp; Sessions<br/>service-client<br/>[clients.py]"]
  node_session_reporting["Session Reporting<br/>playback-reporting"]
end

subgraph group_browser["Native Browser"]
  node_browser_app["Browser App &amp; Navigation<br/>native-ui<br/>[app.py]"]
  node_mpvtk["MPV UI Toolkit<br/>rendering-toolkit<br/>[app.py]"]
  node_gateway["Browser Gateway<br/>adapter-layer<br/>[servers.py]"]
end

subgraph group_media["Playback &amp; Media"]
  node_player["Playback Controller<br/>media-controller<br/>[player.py]"]
  node_mpv_events["MPV Event Bridge<br/>event-bridge<br/>[mpv_events.py]"]
  node_media_features["Media Features<br/>media-support<br/>[media.py]"]
end

subgraph group_sync["Offline &amp; Sync"]
  node_offline_sync["Offline Sync Manager<br/>download-sync<br/>[manager.py]"]
  node_sync_db[("Offline Download DB<br/>local-database<br/>[db.py]")]
  node_syncplay["SyncPlay<br/>group-playback<br/>[syncplay.py]"]
end

subgraph group_runtime["Runtime &amp; Distribution"]
  node_mpv{{"MPV / libmpv<br/>external-runtime"}}
  node_lua["Embedded Lua Scripts<br/>mpv-extension<br/>[renderer.lua]"]
  node_packaging["Packaging Pipelines<br/>distribution<br/>[main.yml]"]
end

node_jellyfin{{"Jellyfin Server<br/>external-service"}}

node_launch -->|"starts"| node_orchestrator
node_orchestrator -->|"loads state"| node_config
node_orchestrator -->|"acquires lock"| node_single_instance
node_orchestrator -->|"initializes"| node_jellyfin_clients
node_orchestrator -->|"initializes"| node_player
node_orchestrator -.->|"optionally launches"| node_browser_app
node_config -->|"supplies credentials"| node_jellyfin_clients
node_jellyfin_clients -->|"server API"| node_jellyfin
node_browser_app -->|"renders through"| node_mpvtk
node_browser_app -->|"uses"| node_gateway
node_gateway -->|"server operations"| node_jellyfin_clients
node_gateway -->|"playback requests"| node_player
node_player -->|"controls"| node_mpv
node_mpv_events -->|"state events"| node_player
node_player -->|"applies media options"| node_media_features
node_player -->|"playback state"| node_session_reporting
node_session_reporting -->|"reports progress"| node_jellyfin
node_offline_sync -->|"persists downloads"| node_sync_db
node_offline_sync -->|"reconciles with"| node_jellyfin_clients
node_syncplay -->|"coordinates through"| node_jellyfin_clients
node_lua -->|"runs in"| node_mpv
node_packaging -.->|"packages application"| node_launch

click node_launch "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/run.py"
click node_orchestrator "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpv_shim.py"
click node_config "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/conf.py"
click node_single_instance "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/single_instance.py"
click node_jellyfin_clients "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/clients.py"
click node_session_reporting "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/session_reporter.py"
click node_browser_app "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk_browser/app.py"
click node_mpvtk "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk/app.py"
click node_gateway "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk_browser/gateway/servers.py"
click node_player "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/player.py"
click node_mpv_events "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpv_events.py"
click node_media_features "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/media.py"
click node_offline_sync "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/sync/manager.py"
click node_sync_db "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/sync/db.py"
click node_syncplay "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/syncplay.py"
click node_lua "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk/renderer.lua"
click node_packaging "https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/.github/workflows/main.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_launch,node_orchestrator,node_config,node_single_instance,node_jellyfin_clients,node_session_reporting toneBlue
class node_browser_app,node_mpvtk,node_gateway toneAmber
class node_player,node_mpv_events,node_media_features toneMint
class node_offline_sync,node_sync_db,node_syncplay toneRose
class node_mpv,node_lua,node_packaging toneIndigo
class node_jellyfin toneNeutral
```

## Components

### Client Core

- [Launch & CLI](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/run.py): entrypoint · Starts the desktop or cast-target client. · `run.py`
- [Application Orchestrator](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpv_shim.py): lifecycle · Coordinates startup, UI, player, and shutdown. · `jellyfin_mpv_shim/mpv_shim.py`
- [Config & Local State](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/conf.py): persistence · Preferences and credentials scoped by configuration directory. · `jellyfin_mpv_shim/conf.py`
- [Single Instance](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/single_instance.py): process-control · Enforces one process per config directory. · `jellyfin_mpv_shim/single_instance.py`
- [Jellyfin Clients & Sessions](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/clients.py): service-client · Manages server, user, authentication, and session access. · `jellyfin_mpv_shim/clients.py`
- [Session Reporting](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/session_reporter.py): playback-reporting · Reports playback progress and state to Jellyfin. · `jellyfin_mpv_shim/session_reporter.py`

### Native Browser

- [Browser App & Navigation](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk_browser/app.py): native-ui · Owns pages, navigation, UI state, and user interaction. · `jellyfin_mpv_shim/mpvtk_browser/app.py`
- [MPV UI Toolkit](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk/app.py): rendering-toolkit · Provides MPV-rendered layout and widgets. · `jellyfin_mpv_shim/mpvtk/app.py`
- [Browser Gateway](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk_browser/gateway/servers.py): adapter-layer · Decouples pages from server and playback behavior. · `jellyfin_mpv_shim/mpvtk_browser/gateway/servers.py`

### Playback & Media

- [Playback Controller](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/player.py): media-controller · Translates play requests and input into MPV commands. · `jellyfin_mpv_shim/player.py`
- [MPV Event Bridge](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpv_events.py): event-bridge · Handles MPV events, OSC, and player state changes. · `jellyfin_mpv_shim/mpv_events.py`
- [Media Features](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/media.py): media-support · Handles media selection, subtitles, trickplay, and specialized formats. · `jellyfin_mpv_shim/media.py`

### Offline & Sync

- [Offline Sync Manager](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/sync/manager.py): download-sync · Reconciles local downloads with Jellyfin. · `jellyfin_mpv_shim/sync/manager.py`
- [Offline Download DB](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/sync/db.py): local-database · Persists offline download state. · `jellyfin_mpv_shim/sync/db.py`
- [SyncPlay](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/syncplay.py): group-playback · Coordinates synchronized group playback. · `jellyfin_mpv_shim/syncplay.py`

### Runtime & Distribution

- **MPV / libmpv**: external-runtime · Media engine and rendering host.
- [Embedded Lua Scripts](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/jellyfin_mpv_shim/mpvtk/renderer.lua): mpv-extension · MPV-side rendering and playback extensions. · `jellyfin_mpv_shim/mpvtk/renderer.lua`
- [Packaging Pipelines](https://github.com/jellyfin/jellyfin-mpv-shim/blob/master/.github/workflows/main.yml): distribution · Builds desktop packages for supported platforms. · `.github/workflows/main.yml`

### Other components

- **Jellyfin Server**: external-service · Provides authentication, metadata, playback control, and media APIs.

## Connections

- Launch & CLI → Application Orchestrator: starts
- Application Orchestrator → Config & Local State: loads state
- Application Orchestrator → Single Instance: acquires lock
- Application Orchestrator → Jellyfin Clients & Sessions: initializes
- Application Orchestrator → Playback Controller: initializes
- Application Orchestrator → Browser App & Navigation: optionally launches
- Config & Local State → Jellyfin Clients & Sessions: supplies credentials
- Jellyfin Clients & Sessions → Jellyfin Server: server API
- Browser App & Navigation → MPV UI Toolkit: renders through
- Browser App & Navigation → Browser Gateway: uses
- Browser Gateway → Jellyfin Clients & Sessions: server operations
- Browser Gateway → Playback Controller: playback requests
- Playback Controller → MPV / libmpv: controls
- MPV Event Bridge → Playback Controller: state events
- Playback Controller → Media Features: applies media options
- Playback Controller → Session Reporting: playback state
- Session Reporting → Jellyfin Server: reports progress
- Offline Sync Manager → Offline Download DB: persists downloads
- Offline Sync Manager → Jellyfin Clients & Sessions: reconciles with
- SyncPlay → Jellyfin Clients & Sessions: coordinates through
- Embedded Lua Scripts → MPV / libmpv: runs in
- Packaging Pipelines → Launch & CLI: packages application
