# qarabaierooom/youtube-mp3 architecture

> Architecture diagram of the GitHub repository qarabaierooom/youtube-mp3, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/qarabaierooom/youtube-mp3
- Repository: https://github.com/qarabaierooom/youtube-mp3

## Overview

#### Purpose and Entry Points
A single-process Python media-downloader service combining a Telegram bot and local FastAPI dashboard. It accepts user-supplied YouTube links or audio files, downloads/transcodes audio, stores per-user music, and exposes operations through Telegram and HTTP/WebSocket interfaces. Runtime entry point: `main.py`; browser shell: `index.html`.

#### Runtime Composition
`main.py` is the monolithic application boundary: it is expected to initialize FastAPI/uvicorn, Aiogram bot/dispatcher, SQLite access, background workers, monitoring, and shutdown/lifespan coordination. `index.html` is the dashboard client served or used by the FastAPI layer.

#### Telegram Intake and Authorization
Telegram is the primary request ingress. Aiogram command, callback, and message handlers support `/start`, administrator approval and folder assignment, user link submission, duplicate handling/forced download, and direct audio uploads. Authorization state, users, settings, and download history persist in SQLite, created at runtime as `qarabai_music.db`. Paths: `main.py`.

#### Queue and Worker Execution
Accepted URLs become jobs in a priority asynchronous queue. Four workers consume jobs concurrently; administrator jobs are inserted at the third queue position. Workers form the key asynchronous boundary between bot/web intake and external downloading/transcoding. Pause/resume and status reporting must coordinate with this queue. Paths: `main.py`.

#### Download and Media Pipeline
Each worker invokes yt-dlp with a primary extraction strategy (including curl_cffi browser impersonation) and fallback client strategies. Raw media crosses into FFmpeg post-processing: loudness normalization, fades, optional CBR output (128 kbps/44.1 kHz), ID3 tagging, cover art, and a 15-second preview clip. Final media is stored under the configurable local music base, documented as `~/Downloads/Music`. Paths: `main.py`.

#### Web Control Plane
FastAPI provides the local dashboard at `http://127.0.0.1:8000`; `index.html` supplies the Tailwind-based UI. WebSocket connections receive live logs, queue/download progress, and likely filesystem/history updates. HTTP operations cover folders, history/playback, settings, ZIP export, error copying, and USB synchronization. This UI is a local operational control plane, not a separate service. Paths: `main.py`, `index.html`.

#### Operations, Storage, and External Dependencies
Persistent state is SQLite via aiosqlite; media and temporary artifacts are local filesystem state. Disk monitoring via psutil halts workers and triggers SOS behavior below 2 GB, while cleanup removes temporary files and inactivity drives aggregate reporting. USB sync copies music to removable storage; Windows may announce completion through pyttsx3. Material dependencies are Python 3.10+, FastAPI/uvicorn, Aiogram, yt-dlp/curl_cffi, FFmpeg, SQLite, psutil, and the Telegram API. Paths: `main.py`.

#### Key Boundaries for Change
Keep Telegram handlers, FastAPI/WebSocket endpoints, and worker tasks from directly duplicating media logic; they should converge on shared queue/job and persistence interfaces inside `main.py`. External failure boundaries are Telegram API delivery, yt-dlp source extraction, FFmpeg subprocess processing, disk/removable-media I/O, and SQLite writes. The repository currently has no package/module separation beyond `main.py`, so extracting these boundaries into modules is the natural evolution path.

Main entry points: [`main.py`](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py) (Download workers), [`index.html`](https://github.com/qarabaierooom/youtube-mp3/blob/main/index.html) (Dashboard client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Single-process runtime"]
  node_main{{"Application composition<br/>Python runtime<br/>[main.py]"}}
end

subgraph group_ingress["Request interfaces"]
  node_telegram["Telegram handlers<br/>Aiogram ingress<br/>[main.py]"]
  node_telegram_api(("Telegram API<br/>external API"))
  node_fastapi["Dashboard API<br/>FastAPI HTTP endpoints<br/>[main.py]"]
  node_websocket["Live updates<br/>WebSocket endpoint<br/>[main.py]"]
  node_dashboard["Dashboard client<br/>HTML UI<br/>[index.html]"]
end

subgraph group_pipeline["Job and media pipeline"]
  node_auth_jobs["Authorization and job intake<br/>shared application logic<br/>[main.py]"]
  node_priority_queue["Priority job queue<br/>async queue<br/>[main.py]"]
  node_workers["Download workers<br/>async worker pool<br/>[main.py]"]
  node_yt_dlp["Source extraction<br/>yt-dlp + curl_cffi<br/>[main.py]"]
  node_ffmpeg["Audio post-processing<br/>FFmpeg subprocess<br/>[main.py]"]
end

subgraph group_storage["State and local I/O"]
  node_sqlite[("Application database<br/>SQLite / aiosqlite<br/>[main.py]")]
  node_media_fs["Music and temporary files<br/>local filesystem<br/>[main.py]"]
  node_disk_monitor["Disk and cleanup monitor<br/>background task<br/>[main.py]"]
  node_usb_sync["Removable-media sync<br/>filesystem operation<br/>[main.py]"]
end

node_main -->|"initializes"| node_telegram
node_main -->|"initializes"| node_fastapi
node_main -->|"starts four"| node_workers
node_main -->|"starts"| node_disk_monitor
node_telegram -->|"receives and sends"| node_telegram_api
node_telegram -->|"submits requests"| node_auth_jobs
node_fastapi -->|"controls jobs"| node_auth_jobs
node_fastapi -->|"hosts"| node_websocket
node_dashboard -->|"HTTP operations"| node_fastapi
node_dashboard -->|"live status"| node_websocket
node_auth_jobs -->|"reads and writes state"| node_sqlite
node_auth_jobs -->|"enqueues accepted URLs"| node_priority_queue
node_priority_queue -->|"jobs"| node_workers
node_workers -->|"downloads source media"| node_yt_dlp
node_yt_dlp -->|"raw media"| node_ffmpeg
node_ffmpeg -->|"final audio and preview"| node_media_fs
node_workers -->|"updates history and status"| node_sqlite
node_workers -->|"progress and logs"| node_websocket
node_disk_monitor -->|"halts on low disk"| node_workers
node_disk_monitor -->|"cleans temporary files"| node_media_fs
node_fastapi -->|"starts sync"| node_usb_sync
node_usb_sync -->|"copies music"| node_media_fs

click node_main "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_telegram "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_fastapi "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_websocket "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_dashboard "https://github.com/qarabaierooom/youtube-mp3/blob/main/index.html"
click node_auth_jobs "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_priority_queue "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_workers "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_yt_dlp "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_ffmpeg "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_sqlite "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_media_fs "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_disk_monitor "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"
click node_usb_sync "https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py"

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_main toneBlue
class node_telegram,node_telegram_api,node_fastapi,node_websocket,node_dashboard toneAmber
class node_auth_jobs,node_priority_queue,node_workers,node_yt_dlp,node_ffmpeg toneMint
class node_sqlite,node_media_fs,node_disk_monitor,node_usb_sync toneRose
```

## Components

### Single-process runtime

All application boundaries currently live in main.py.

- [Application composition](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): Python runtime · Coordinates lifespan, bot, API, workers, and shutdown. · `main.py`

### Request interfaces

- [Telegram handlers](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): Aiogram ingress · Commands, approvals, links, callbacks, and audio uploads. · `main.py`
- **Telegram API**: external API
- [Dashboard API](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): FastAPI HTTP endpoints · Local control operations for folders, history, settings, export, and USB sync. · `main.py`
- [Live updates](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): WebSocket endpoint · Streams logs and queue/download progress. · `main.py`
- [Dashboard client](https://github.com/qarabaierooom/youtube-mp3/blob/main/index.html): HTML UI · `index.html`

### Job and media pipeline

- [Authorization and job intake](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): shared application logic · Validates users, handles duplicates, and creates download jobs. · `main.py`
- [Priority job queue](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): async queue · Four consumers; administrator jobs enter at position three. · `main.py`
- [Download workers](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): async worker pool · Pause, resume, status, and concurrent job execution boundary. · `main.py`
- [Source extraction](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): yt-dlp + curl_cffi · Uses primary and fallback extraction clients. · `main.py`
- [Audio post-processing](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): FFmpeg subprocess · Normalizes, tags, adds art, and creates previews. · `main.py`

### State and local I/O

- [Application database](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): SQLite / aiosqlite · Users, authorization, settings, folders, and download history. · `main.py`
- [Music and temporary files](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): local filesystem · Final music is stored under ~/Downloads/Music. · `main.py`
- [Disk and cleanup monitor](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): background task · Halts workers below 2 GB and removes temporary artifacts. · `main.py`
- [Removable-media sync](https://github.com/qarabaierooom/youtube-mp3/blob/main/main.py): filesystem operation · Copies music to USB storage; Windows may announce completion. · `main.py`

## Connections

- Application composition → Telegram handlers: initializes
- Application composition → Dashboard API: initializes
- Application composition → Download workers: starts four
- Application composition → Disk and cleanup monitor: starts
- Telegram handlers → Telegram API: receives and sends
- Telegram handlers → Authorization and job intake: submits requests
- Dashboard API → Authorization and job intake: controls jobs
- Dashboard API → Live updates: hosts
- Dashboard client → Dashboard API: HTTP operations
- Dashboard client → Live updates: live status
- Authorization and job intake → Application database: reads and writes state
- Authorization and job intake → Priority job queue: enqueues accepted URLs
- Priority job queue → Download workers: jobs
- Download workers → Source extraction: downloads source media
- Source extraction → Audio post-processing: raw media
- Audio post-processing → Music and temporary files: final audio and preview
- Download workers → Application database: updates history and status
- Download workers → Live updates: progress and logs
- Disk and cleanup monitor → Download workers: halts on low disk
- Disk and cleanup monitor → Music and temporary files: cleans temporary files
- Dashboard API → Removable-media sync: starts sync
- Removable-media sync → Music and temporary files: copies music
