Files
ytdlp-tui/README.claude.md
T
ingo fccd2def59 1) Uses textuals CSS_PATH and an external tcss file to bundle all DEFAULT_CSS into one file
2) Added keyboard shortcut bindings for switching the language (Ctrl-T) and showing the help (Ctrl-A) The help should use a markdown viewer
3) Include a test folder containing all the tests
2026-09-19 14:28:40 +02:00

6.2 KiB

yt-dlp Download Manager (Textual TUI)

A small Textual user interface around a specific 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 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).

  • Python packages from requirements.txt:

    pip install -r requirements.txt
    

Running

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).

Tests

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
  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)

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".