How janisadhi/music-sync works
Purpose and Entry Points
Music Sync is a self-hosted system that mirrors configured YouTube Music playlists into a local audio library, enriches tracks with tags/artwork and synchronized lyrics, and exposes management APIs plus a browser dashboard. Backend startup and worker lifecycle are centralized in app/main.py; the SPA starts at dashboard/src/main.jsx and API access is routed through dashboard/src/services/api.js.
Backend Application Boundary
FastAPI provides authenticated REST operations for playlists, songs, synchronization, metadata, settings, and dashboard status. API handlers should remain orchestration-facing; persistent state and domain behavior live behind service modules. Core runtime/configuration, authentication, filesystem paths, and shared yt-dlp setup form the application boundary.
Paths: app/api/sync.py, app/api/playlists.py, app/core/runtime.py, app/core/ytdlp.py.
Persistent State and Library Storage
PostgreSQL is the source of truth for users, app settings, playlists, songs, download/retry state, metadata history, and cookie-backed YouTube access. SQLAlchemy models/session management support all workers; Alembic evolves this schema. Audio, artwork, and .lrc files are persisted in the mounted local library rather than the database.
Paths: app/database/models.py, app/database/session.py, alembic/env.py, app/core/paths.py.
Discovery, Reconciliation, and Sync
A synchronization run reads configured playlists, uses yt-dlp flat extraction to discover remote video/track state, and reconciles it with stored playlist/song records. Discovery creates or updates work candidates; it does not directly couple playlist scanning to media extraction. This is the first stage of the stated worker contract.
Paths: app/sync/service.py, app/watcher/youtube.py, app/reconciler/service.py.
Download Pipeline
The downloader asynchronously drains eligible database-backed work, invokes yt-dlp to retrieve native/high-quality Opus audio, and records outcomes, failure classification, and exponential-backoff retry state. The backend image supplies yt-dlp’s runtime dependencies, including Deno for YouTube challenge solving, FFmpeg for media processing, and tagging tools.
Paths: app/downloader/service.py, app/downloader/worker.py, Dockerfile.
Lyrics and Metadata Enrichment
After download, a separate lyrics worker fetches timed lyrics from LRCLIB and writes .lrc output; tracks without lyrics are organized separately. A distinct metadata service can scan/process the local library, match music metadata through MusicBrainz and Beets, write tags/artwork, and organize files. This is an inter-service/media-library boundary rather than frontend logic.
Paths: app/lyrics/service.py, app/lyrics/worker.py, metadata_service/main.py, metadata_service/processor.py, metadata_service/musicbrainz.py.
Scheduling and Operations
APScheduler triggers periodic synchronization while download and lyric workers operate independently, preventing a slow external integration from blocking discovery or API serving. Persistent settings control scheduling, playlist-watch behavior, directories, retries, and cookies.
Paths: app/scheduler/service.py, app/settings/service.py, app/core/events.py.
Frontend and Deployment
React 19/Vite provides the management and playback SPA: protected routes, playlist/song/metadata/settings views, and synchronized lyric display. In production Nginx serves the static dashboard and proxies API requests. Docker Compose runs PostgreSQL, backend, and dashboard; GitHub Actions supports container deployment to GHCR.
Paths: dashboard/src/App.jsx, dashboard/src/layouts/DashboardLayout.jsx, dashboard/src/components/AudioPlayer.jsx, dashboard/nginx.conf, docker-compose.yml, .github/workflows/deploy.yaml.
Main entry points: models.py PostgreSQL state · sync.py FastAPI REST API · runtime.py Runtime and yt-dlp setup · docker-compose.yml Docker Compose stack
Components 20
Dashboard
- React management SPA · Protected management, playback, and lyric views.
dashboard/src/App.jsx - Audio player · Plays library audio with synchronized lyrics.
dashboard/src/components/AudioPlayer.jsx - Nginx dashboard server · Serves the SPA and proxies API requests.
dashboard/nginx.conf
Backend application
- FastAPI REST API · Authenticated orchestration endpoints for library management.
app/api/sync.py - Runtime and yt-dlp setup · Centralizes startup, worker lifecycle, and yt-dlp configuration.
app/core/runtime.py - Settings service · Provides persistent scheduling, retry, directory, and cookie settings.
app/settings/service.py - Sync scheduler · Triggers periodic synchronization independently of workers.
app/scheduler/service.py
Sync and enrichment workers
- Sync orchestration · Reads configured playlists and drives discovery reconciliation.
app/sync/service.py - YouTube playlist discovery · Uses flat extraction to obtain remote playlist and track state.
app/watcher/youtube.py - Playlist reconciler · Creates or updates database-backed work candidates.
app/reconciler/service.py - Download worker · Drains eligible work and records outcomes and retry state.
app/downloader/worker.py - Media downloader · Retrieves native or high-quality Opus audio.
app/downloader/service.py - Lyrics worker · Fetches timed lyrics after download.
app/lyrics/worker.py - Metadata service · Matches metadata, writes tags/artwork, and organizes local media.
metadata_service/processor.py
Persistent storage
- PostgreSQL state · Source of truth for users, playlists, songs, work state, and settings.
app/database/models.py - Mounted media library · Persists audio, artwork, and .lrc files.
app/core/paths.py - Schema migrations
alembic/env.py
Deployment
- Docker Compose stack · Runs PostgreSQL, backend, and dashboard services.
docker-compose.yml - Backend runtime image · Supplies yt-dlp, Deno, FFmpeg, and tagging tools.
Dockerfile - Container deployment CI · Builds and deploys container images to GHCR.
.github/workflows/deploy.yaml
Connections 27
- React management SPA → Audio player: renders
- React management SPA → Nginx dashboard server: served by
- Nginx dashboard server → FastAPI REST API: proxies API
- FastAPI REST API → Settings service: manages
- FastAPI REST API → Sync orchestration: starts sync
- FastAPI REST API → PostgreSQL state: reads and writes
- Runtime and yt-dlp setup → FastAPI REST API: starts
- Runtime and yt-dlp setup → Download worker: runs
- Runtime and yt-dlp setup → Lyrics worker: runs
- Settings service → PostgreSQL state: persists settings
- Sync scheduler → Sync orchestration: triggers
- Settings service → Sync scheduler: controls
- Sync orchestration → YouTube playlist discovery: discovers playlists
- YouTube playlist discovery → Playlist reconciler: remote track state
- Playlist reconciler → PostgreSQL state: creates work candidates
- Download worker → PostgreSQL state: claims and updates work
- Download worker → Media downloader: retrieves media
- Media downloader → Mounted media library: writes audio
- Lyrics worker → PostgreSQL state: finds downloaded tracks
- Lyrics worker → Mounted media library: writes .lrc files
- Metadata service → Mounted media library: tags, artworks, organizes
- Schema migrations → PostgreSQL state: evolves schema
- Docker Compose stack → PostgreSQL state: runs
- Docker Compose stack → Runtime and yt-dlp setup: runs backend
- Docker Compose stack → Nginx dashboard server: runs dashboard
- Backend runtime image → Runtime and yt-dlp setup: provides runtime dependencies
- Container deployment CI → Backend runtime image: builds and deploys