Files
ytdlp-tui/README.claude.md
ingo 1471a342d8 1) set the required textual version to version 8.2.8
2) generated installers which bundle the required .venv
2026-09-19 14:38:49 +02:00

152 lines
7.1 KiB
Markdown

# yt-dlp Download Manager (Textual TUI)
A small [Textual](https://textual.textualize.io/) user interface around a
specific [yt-dlp](https://github.com/yt-dlp/yt-dlp) workflow: enter a film
URL, inspect the `m3u8_native` formats yt-dlp reports for it, pick the
part(s) to download, and download (merging audio/video with `ffmpeg` when
they come as separate streams).
## Requirements
* Python 3.10+
* [`yt-dlp`](https://pypi.org/project/yt-dlp/) and `ffmpeg` available on
your `PATH` (`ffmpeg` is a system package, not a Python one — install it
with your OS package manager, e.g. `apt install ffmpeg` /
`brew install ffmpeg`). Needed regardless of how you run the app,
including the prebuilt installers below, since it's invoked as an
external command, never imported as a Python library.
* Python packages from `requirements.txt` (pins `textual==8.2.8`):
```bash
pip install -r requirements.txt
```
## Running
```bash
python main.py
```
### Keyboard shortcuts
* **Ctrl+T** — switch language (currently English/German; shows a picker).
* **Ctrl+A** — show help, rendered from `i18n/<lang>/help.md` in a Markdown
viewer. Works even while the Url/Name input is focused (it overrides
`Input`'s own "select all" binding on that key).
## Installers
For distributing the app without asking someone to clone the repo and
`pip install`, see [`packaging/README.md`](packaging/README.md):
`packaging/build_pyinstaller.sh` builds a single native executable with
Python bundled in, and `packaging/build_pex.sh` builds a small `.pex`
zipapp that needs a system `python3` but no `pip install` step. Both
still require `yt-dlp` and `ffmpeg` on `PATH` at runtime.
## Tests
```bash
pip install -r requirements-dev.txt
pytest
```
`tests/` covers:
* `test_models.py` — the format-filtering / split-vs-combined detection
logic, run against both worked examples from the requirements document
(copied into `tests/fixtures/`), plus a couple of edge cases (no
survivable formats, missing `formats` key).
* `test_subprocess_clients.py` — the `yt-dlp`/`ffmpeg` async wrappers, with
`asyncio.create_subprocess_exec` monkeypatched to a fake process so no
real binaries or network access are needed.
* `test_i18n_integration.py` — the bundled i18n library against this app's
own translation data: fallback behaviour, language switching, listener
notification, `get_help()`.
* `test_app_workflow.py` — full end-to-end runs of the Textual UI via
`Pilot`: the split and combined selection flows, the error paths (empty
URL, cancelled selection), download + merge + temp-file cleanup, and the
Ctrl+T / Ctrl+A shortcuts.
## Project layout
```
main.py Entry point; wires up i18n, then starts the app
app/
tui_app.py The Textual App: layout + all screen wiring
runtime_paths.py Resolves data-file locations the same way for a
plain checkout, a PyInstaller build, and a pex
zipapp (see packaging/README.md)
styles.tcss External stylesheet for the app and every modal
screen (loaded via DownloadManagerApp.CSS_PATH)
models.py FormatEntry / VideoInfo + the filtering logic
that decides split vs. combined audio/video
ytdlp_client.py async wrappers for `yt-dlp --dump-json` and
`yt-dlp -f <id> -o <file>` (with progress)
ffmpeg_client.py async wrapper for the `ffmpeg -c copy` merge
screens/
format_select_screen.py Modal format-selection table (audio & video)
error_screen.py Modal error dialog
language_select_screen.py Modal language picker (Ctrl+T)
help_screen.py Modal help viewer, renders help.md (Ctrl+A)
lib/i18n/ The provided i18n library (unmodified aside from
a small `__init__.py` re-export so that
`from i18n import I18n` works as documented)
i18n/en/, i18n/de/ This application's own translation data
(strings.json + help.md), as expected by the
library at <project_root>/i18n/<lang>/
tests/ pytest suite (see "Tests" below)
packaging/ PyInstaller spec + build scripts for both
installers (see "Installers" above)
```
The i18n *library* is kept in `lib/i18n` (added to `sys.path` by
`main.py`), separate from the i18n *data* directory `i18n/` at the project
root, since the library expects its translation data at
`<project_root>/i18n/<lang>/strings.json` — the same name as the library's
own package directory would use if it were placed at the project root.
## How the app maps to the requirements
* **Info gathering** — `yt-dlp -i --dump-json <url>` is run in
`ytdlp_client.fetch_info`. Only formats with `protocol == "m3u8_native"`
are kept (`models.VideoInfo.from_json`); anything else (including
`https`) is discarded.
* **Split vs. combined** — if any surviving format has
`resolution == "audio only"`, the film is treated as split and two
selection dialogs are shown (audio, then video). Otherwise a single
dialog is shown.
* **Selection dialogs** — `screens/format_select_screen.py` renders a
table with `format_id`, `resolution`, `acodec`, `vcodec`, `format`.
Cancelling, pressing Escape, or an empty table all resolve to "no valid
entry", which the main screen turns into an error dialog and a disabled
**Start download** button.
* **Main screen** — shows `Audio: ...` / `Video: ...` lines (underscores
in the format text replaced with spaces), a progress line + bar, and the
three buttons from the layout sketch.
* **Downloading** — `yt-dlp -i url -o filename -f stream_id`, run with
`--newline` so `ytdlp_client.download` can parse `[download] NN.N%`
lines and call back into the UI at most once per whole percentage point.
The progress text is `<fulltitle> - Audio|Video|Audio + Video - NN%`.
* **Merging** — for split downloads, both parts land in temporary files
next to the destination; once both finish, `ffmpeg -i video -i audio
-c copy <destination>` merges them, after which the temp files are
deleted (in a `finally` block, so this also happens if something fails
partway through).
* **Async** — all subprocess-driving and status-bar-updating code
(`select_parts`, `start_download`, the `ytdlp_client`/`ffmpeg_client`
functions, and the progress callbacks) is `async` and is launched from
button presses via Textual workers so the UI stays responsive.
## Notes / assumptions
* The two example JSON documents from the requirements are used as the
basis for the filtering logic and are checked into `tests/fixtures/` so
the test suite exercises them directly.
* Temporary files for split downloads are named
`<destination-stem>.video.tmp.mp4` / `<destination-stem>.audio.tmp.mp4`
next to the destination file.
* If "Select parts" is used again after a successful selection, the
previous selection is only replaced once a new one completes
successfully; cancelling out of the new attempt clears the selection and
disables **Start download**, matching "no default selection".