Skip to content

About

Generate brat-style lyric videos from a Spotify link - FastAPI + Whisper + Pillow + ffmpeg pipeline with an async job queue.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Lyric Generator

CI License: MIT Python 3.10+

Generate brat-style lyric videos from a Spotify track link.

This project runs a local FastAPI app with a small frontend. You paste a Spotify track URL; the backend fetches track metadata, finds or downloads audio, transcribes word-level lyric timing with Whisper (LRCLIB as fallback), renders the animated text to frames with Pillow, and muxes them with the song using ffmpeg into an .mp4.

Demo

Real pipeline output — Radiohead, No Surprises (1080×1920, character-by- character typewriter timing synced to the vocal):

Lyric video demo: Radiohead - No Surprises

Pipeline At A Glance

flowchart LR
    A([Spotify track URL]) --> B[Spotify API<br/>metadata]
    B --> C{Local audio<br/>in library?}
    C -- yes --> D[Use local file]
    C -- no --> E[yt-dlp<br/>download MP3]
    D --> F[Whisper<br/>word-level timing]
    E --> F
    F -- fails --> G[LRCLIB<br/>synced / plain fallback]
    F --> H[Pillow<br/>frame layout]
    G --> H
    H --> I[ffmpeg<br/>encode + mux audio]
    I --> J([🎬 1080×1920 MP4<br/>lyric video])
Loading

Each failure path produces a clear job error — never a crash or a blank video.

What It Does

  • Accepts a Spotify track URL
  • Fetches song metadata with the Spotify API
  • Reuses local audio from data/library/ when available
  • Falls back to yt-dlp to download audio if no local match is found
  • Transcribes word-level lyric timing with openai-whisper
  • Falls back to LRCLIB synced/plain lyrics if Whisper fails
  • Renders lyric frames with Pillow and encodes the video with ffmpeg

Stack

  • Python 3.10+
  • FastAPI + Uvicorn
  • Static frontend in frontend/
  • openai-whisper for word-level lyric timing
  • Pillow for frame rendering
  • ffmpeg for final video encoding
  • yt-dlp for download fallback

Prerequisites

  • Python 3.10 or newer
  • ffmpeg available on your PATH
  • yt-dlp available on your PATH if you want automatic audio downloads
  • Spotify API credentials (client ID + secret)

Spotify API Tutorial

This project uses standard Spotify API app credentials: a client_id and client_secret (not a "bot token").

  1. Go to https://developer.spotify.com/dashboard.
  2. Log in with your Spotify account.
  3. Click Create app.
  4. Enter any app name and description.
  5. Accept Spotify's terms and create the app.
  6. Open the app and copy the Client ID.
  7. Click View client secret and copy the Client Secret.
  8. Create a local .env from .env.example and paste both values.
SPOTIFY_CLIENT_ID=your_client_id_here
SPOTIFY_CLIENT_SECRET=your_client_secret_here

These values stay local. Do not commit them to GitHub.

Setup

One-step (creates venv/, installs everything, checks ffmpeg):

bash setup.sh                # core + Whisper
bash setup.sh --no-whisper   # skip the heavy torch/Whisper stack

Or manually:

python -m venv venv
# activate — Windows: .\venv\Scripts\Activate.ps1   POSIX: source venv/bin/activate
python -m pip install --upgrade pip
pip install -e ".[whisper,test]"   # or ".[test]" to skip Whisper
cp .env.example .env               # Windows: Copy-Item .env.example .env

Dependencies are defined in pyproject.toml. [whisper] (local word-level transcription, pulls torch) is optional — without it the pipeline falls back to LRCLIB lyrics.

Then edit .env:

SPOTIFY_CLIENT_ID=your_spotify_client_id
SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
# tiny | base | small | medium | large-v3
WHISPER_MODEL=base
# blank = auto-select (CUDA if available, else CPU); or force cpu / cuda
WHISPER_DEVICE=
# prune finished/failed jobs from memory after this many hours
JOB_TTL_HOURS=24

Smaller WHISPER_MODEL values (tiny, base) are faster and lighter on memory; larger ones are more accurate but need more RAM/VRAM. Whisper downloads the chosen model on first run.

Running The App

python -m uvicorn backend.main:app --reload --port 8000

Open http://localhost:8000. On Windows you can also use start.bat after creating the venv and installing deps.

CLI

python generate.py <spotify_url>

How The Pipeline Works

  1. backend/services/spotify.py validates the Spotify URL and fetches metadata.
  2. backend/services/library.py scans data/library/ for a matching local file.
  3. backend/services/downloader.py uses yt-dlp if no local match is found.
  4. backend/services/lyrics.py transcribes word-level timing with Whisper, falling back to LRCLIB synced/plain lyrics if Whisper fails.
  5. backend/services/layout.py lays out the lyric frames.
  6. backend/services/render.py renders frames with Pillow and ffmpeg combines them with the audio into data/outputs/.

The web app (backend/main.py) wraps this in an in-memory job queue; generate.py runs the same pipeline from the CLI.

Project Layout

backend/            FastAPI app, pipeline, models, services
frontend/           Static UI served by FastAPI
tests/              Test suite (pytest)
data/{library,outputs,temp}/   Runtime dirs (audio in, mp4 out, scratch)
fonts/              Bundled render font
assets/             README media (demo GIF)
generate.py         CLI entry point (same pipeline as the web app)
pyproject.toml      Packaging, dependencies, pytest config
.github/workflows/  CI (pytest on push/PR)

Notes On Dependencies

  • ffmpeg is required for final .mp4 output. The job fails with a clear message if it is missing from PATH.
  • yt-dlp is optional but strongly recommended. Without it, place audio files in data/library/ manually.
  • Whisper (openai-whisper) provides word-level timing. If it fails, the app falls back to LRCLIB synced lyrics, then LRCLIB plain lyrics.

Running Tests

pytest              # full suite
pytest -m "not live" # skip tests that hit live Spotify/LRCLIB (this is what CI runs)

live-marked tests need network access and valid Spotify credentials; CI skips them and runs the rest on every push.

Known Limitations

  • Designed to run locally, not as a stateless cloud deployment.
  • Background jobs are in-memory only; restarting the server loses job status. Jobs are pruned after JOB_TTL_HOURS.
  • Output filenames are generated from song metadata and may be long.

License

Released under the MIT License.

About

Generate brat-style lyric videos from a Spotify link - FastAPI + Whisper + Pillow + ffmpeg pipeline with an async job queue.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages