# nyniraula/whytube architecture

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

- Interactive diagram: https://gitdiagram.com/nyniraula/whytube
- Repository: https://github.com/nyniraula/whytube

## Overview

WhyTube is a Python 3.10+ CLI downloader centered around one orchestration loop in `main.py`. The app takes a YouTube URL from the user, reloads `config.json` each cycle, resolves the media, chooses the best stream combination, downloads via `yt-dlp`, and post-processes with FFmpeg.

The top-level boundary is simple: user input and local config on one side, YouTube/yt-dlp/FFmpeg/JS runtime dependencies on the other. There is no server, database, or persistent service layer; the repo is a single-machine download tool with filesystem output.

`main.py` is the control plane. It coordinates startup checks, prompts, dispatches work into the utility modules, and repeats in a loop so config changes apply to the next download without restarting. This makes the app stateful only within the current terminal session.

`utils/source_handler.py` appears to own configuration loading and download-directory preparation. The download target is fixed to `~/Downloads/WT_Downloads/`, while `outtmpl` only affects the filename. That means filesystem layout is centralized and not user-directed.

`utils/dependencies.py` is the environment gatekeeper. It detects FFmpeg and a JavaScript runtime (Node.js or Deno), both of which are required for the download workflow to function reliably. This is an important architectural dependency because YouTube bot checks and media merging happen outside Python itself.

`utils/url_resolver.py` handles URL validation and metadata retrieval. Its role is the ingestion boundary between a pasted URL and structured media information from YouTube, likely via `yt-dlp` extraction. This is where the app distinguishes valid video or playlist sources from invalid input.

`utils/format_ranking.py` and `utils/format_resolver.py` form the selection engine. They rank available formats by resolution, codec preference, and container preference, then enforce rules like the 720p minimum for single videos and the 1080p cap for playlists. This subsystem turns raw format metadata into a concrete download plan.

`utils/media_pipeline.py` is the decision router. It branches by content type and config into single-video, playlist, or audio-only paths, so the rest of the app can treat “what to do” separately from “how to do it.” This is the main functional boundary in the repository.

`utils/downloader.py` is the execution layer around `yt-dlp`. It performs the actual media fetch, merge, and post-processing steps, including FFmpeg-driven muxing and AAC audio re-encoding. This is where remote media becomes local files.

Post-processing cleanup is isolated in `utils/cleanup.py`. Its main visible responsibility is removing leftover thumbnail artifacts after embedding, keeping the output directory clean. That makes the final filesystem state part of the app’s contract, not an accident.

`utils/terminal.py` is a small UX helper that clears the console, so it supports the CLI flow but does not affect download logic. It is an auxiliary boundary, not a core subsystem.

The repository’s external integration points are all local tooling and third-party services: YouTube content via `yt-dlp`, FFmpeg for merging and encoding, and Node.js/Deno for runtime support during extraction. The Python package metadata in `pyproject.toml` and lockfile in `uv.lock` indicate `uv`-managed dependency resolution, but the runtime architecture remains a single-process CLI pipeline.

Main entry points: [`main.py`](https://github.com/nyniraula/whytube/blob/main/main.py) (Main loop), [`utils/downloader.py`](https://github.com/nyniraula/whytube/blob/main/utils/downloader.py) (Downloader), [`utils/dependencies.py`](https://github.com/nyniraula/whytube/blob/main/utils/dependencies.py) (Dependency check), [`utils/media_pipeline.py`](https://github.com/nyniraula/whytube/blob/main/utils/media_pipeline.py) (Media router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["CLI control plane"]
  node_main["Main loop<br/>orchestrator<br/>[main.py]"]
  node_terminal["Terminal UI<br/>cli-ux<br/>[terminal.py]"]
end

subgraph group_ingest["Ingestion"]
  node_source_handler["Config source<br/>config+paths<br/>[source_handler.py]"]
  node_dependencies["Dependency check<br/>env-gatekeeper<br/>[dependencies.py]"]
  node_url_resolver["URL resolver<br/>ingestion<br/>[url_resolver.py]"]
end

subgraph group_selection["Format selection"]
  node_format_ranking["Format ranking<br/>[format_ranking.py]"]
  node_format_resolver["Format resolver<br/>planner<br/>[format_resolver.py]"]
end

subgraph group_execution["Download pipeline"]
  node_media_pipeline["Media router<br/>pipeline-router<br/>[media_pipeline.py]"]
  node_downloader[("Downloader<br/>yt-dlp wrapper<br/>[downloader.py]")]
  node_cleanup["Cleanup<br/>post-process<br/>[cleanup.py]"]
end

subgraph group_external["External tools"]
  node_yt_dlp{{"yt-dlp<br/>third-party extractor"}}
  node_ffmpeg{{"FFmpeg<br/>media tool"}}
  node_nodejs{{"Node.js<br/>js runtime"}}
  node_deno{{"Deno<br/>js runtime"}}
  node_youtube(("YouTube<br/>content source"))
  node_filesystem[("Downloads<br/>local storage")]
end

node_main -->|"clears"| node_terminal
node_main -->|"reloads config"| node_source_handler
node_main -->|"checks tools"| node_dependencies
node_main -->|"resolves input"| node_url_resolver
node_main -->|"dispatches"| node_media_pipeline
node_url_resolver -->|"fetches metadata"| node_youtube
node_dependencies -->|"requires"| node_ffmpeg
node_dependencies -->|"accepts"| node_nodejs
node_dependencies -->|"accepts"| node_deno
node_media_pipeline -->|"ranks"| node_format_ranking
node_media_pipeline -->|"selects"| node_format_resolver
node_format_resolver -->|"uses"| node_format_ranking
node_format_resolver -->|"drives"| node_yt_dlp
node_media_pipeline -->|"executes"| node_downloader
node_downloader -->|"wraps"| node_yt_dlp
node_downloader -->|"invokes"| node_ffmpeg
node_downloader -->|"writes output"| node_filesystem
node_downloader -->|"post-processes"| node_cleanup
node_cleanup -->|"cleans"| node_filesystem
node_source_handler -->|"prepares"| node_filesystem

click node_main "https://github.com/nyniraula/whytube/blob/main/main.py"
click node_source_handler "https://github.com/nyniraula/whytube/blob/main/utils/source_handler.py"
click node_dependencies "https://github.com/nyniraula/whytube/blob/main/utils/dependencies.py"
click node_url_resolver "https://github.com/nyniraula/whytube/blob/main/utils/url_resolver.py"
click node_format_ranking "https://github.com/nyniraula/whytube/blob/main/utils/format_ranking.py"
click node_format_resolver "https://github.com/nyniraula/whytube/blob/main/utils/format_resolver.py"
click node_media_pipeline "https://github.com/nyniraula/whytube/blob/main/utils/media_pipeline.py"
click node_downloader "https://github.com/nyniraula/whytube/blob/main/utils/downloader.py"
click node_cleanup "https://github.com/nyniraula/whytube/blob/main/utils/cleanup.py"
click node_terminal "https://github.com/nyniraula/whytube/blob/main/utils/terminal.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,node_terminal toneBlue
class node_source_handler,node_dependencies,node_url_resolver toneAmber
class node_format_ranking,node_format_resolver toneMint
class node_media_pipeline,node_downloader,node_cleanup toneRose
class node_yt_dlp,node_ffmpeg,node_nodejs,node_deno,node_youtube,node_filesystem toneIndigo
```

## Components

### CLI control plane

Top-level orchestration and user-facing entrypoint.

- [Main loop](https://github.com/nyniraula/whytube/blob/main/main.py): orchestrator · Terminal entrypoint that reloads config, prompts the user, and coordinates each download cycle. · `main.py`
- [Terminal UI](https://github.com/nyniraula/whytube/blob/main/utils/terminal.py): cli-ux · Clears the console to keep the session readable. · `utils/terminal.py`

### Ingestion

Configuration, URL validation, and environment checks.

- [Config source](https://github.com/nyniraula/whytube/blob/main/utils/source_handler.py): config+paths · Loads config and prepares the fixed download directory. · `utils/source_handler.py`
- [Dependency check](https://github.com/nyniraula/whytube/blob/main/utils/dependencies.py): env-gatekeeper · Detects required local tools such as FFmpeg and a JavaScript runtime. · `utils/dependencies.py`
- [URL resolver](https://github.com/nyniraula/whytube/blob/main/utils/url_resolver.py): ingestion · Validates input URLs and extracts structured media metadata. · `utils/url_resolver.py`

### Format selection

Ranking and resolving the best media formats.

- [Format ranking](https://github.com/nyniraula/whytube/blob/main/utils/format_ranking.py): ranking · Scores candidate formats by resolution, codec, and container preference. · `utils/format_ranking.py`
- [Format resolver](https://github.com/nyniraula/whytube/blob/main/utils/format_resolver.py): planner · Chooses the concrete stream combination and applies policy limits. · `utils/format_resolver.py`

### Download pipeline

Branching, download execution, and cleanup.

- [Media router](https://github.com/nyniraula/whytube/blob/main/utils/media_pipeline.py): pipeline-router · Selects the single-video, playlist, or audio-only path. · `utils/media_pipeline.py`
- [Downloader](https://github.com/nyniraula/whytube/blob/main/utils/downloader.py): yt-dlp wrapper · Runs media fetch, merge, and FFmpeg post-processing. · `utils/downloader.py`
- [Cleanup](https://github.com/nyniraula/whytube/blob/main/utils/cleanup.py): post-process · Removes leftover artifacts such as embedded thumbnails. · `utils/cleanup.py`

### External tools

Non-Python runtime and media dependencies.

- **yt-dlp**: third-party extractor · External downloader and metadata extraction engine.
- **FFmpeg**: media tool · External muxing and audio encoding backend.
- **Node.js**: js runtime · Optional required JavaScript runtime for extraction support.
- **Deno**: js runtime · Alternative JavaScript runtime for extraction support.
- **YouTube**: content source · Remote media source queried through yt-dlp.
- **Downloads**: local storage · Local output directory and downloaded media files.

## Connections

- Main loop → Terminal UI: clears · Maintains a readable CLI session.
- Main loop → Config source: reloads config · Re-reads config on each loop iteration.
- Main loop → Dependency check: checks tools · Verifies local runtime prerequisites before download work.
- Main loop → URL resolver: resolves input · Converts pasted URLs into structured media data.
- Main loop → Media router: dispatches · Hands control to the media workflow chosen for the content type.
- URL resolver → YouTube: fetches metadata · Extracts source information from YouTube.
- Dependency check → FFmpeg: requires · Needs FFmpeg for muxing and encoding.
- Dependency check → Node.js: accepts · Can use Node.js as a JavaScript runtime.
- Dependency check → Deno: accepts · Can use Deno as a JavaScript runtime.
- Media router → Format ranking: ranks · Scores available formats before selection.
- Media router → Format resolver: selects · Applies policy and chooses the download plan.
- Format resolver → Format ranking: uses · Depends on ranking output to compare candidates.
- Format resolver → yt-dlp: drives · Consumes extractor format metadata and download options.
- Media router → Downloader: executes · Hands off the chosen path to the download layer.
- Downloader → yt-dlp: wraps · Uses yt-dlp for fetching and merging media.
- Downloader → FFmpeg: invokes · Uses FFmpeg for muxing and audio re-encoding.
- Downloader → Downloads: writes output · Stores downloaded files under the local download directory.
- Downloader → Cleanup: post-processes · Cleans artifacts after embedding and merge steps.
- Cleanup → Downloads: cleans · Removes leftover files from the output directory.
- Config source → Downloads: prepares · Ensures the fixed download directory exists.
