Add per-video subtitle language picker on Watch page

- fetch_available_subs() queries yt-dlp for manual + auto-generated
  subtitle langs available on YouTube for any given video
- GET /api/videos/by-yt/{ytId}/subs exposes this to the frontend
- DownloadRequest now accepts subtitle_langs to override the global
  setting on a per-download basis
- Watch page fetches available subtitle langs on load (in parallel),
  shows a CC dropdown with manual langs + auto-generated langs labeled
  "(auto)"; selected lang is passed through to the download

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-26 20:57:57 +02:00
parent ea99b74ba8
commit 399c5fcada
5 changed files with 87 additions and 8 deletions

View File

@@ -384,6 +384,36 @@ def fetch_channel_links(channel_id: str) -> list[str]:
return list(channel_ids)
def fetch_available_subs(video_id: str) -> dict:
"""Return subtitle languages available on YouTube for a video.
Returns {"manual": [...], "auto": [...]} where both are sorted lists of
BCP-47 lang codes. Manual = human-made; auto = auto-generated captions.
"""
url = f"https://www.youtube.com/watch?v={video_id}"
base_cmd = ["yt-dlp", url, "--dump-json", "--no-download", "--no-playlist"]
cookie_args = _cookie_args()
stdout, _, code = _run([*base_cmd, *cookie_args], timeout=30)
if code != 0 and cookie_args:
stdout, _, code = _run(base_cmd, timeout=30)
for line in stdout.splitlines():
line = line.strip()
if not line:
continue
try:
info = json.loads(line)
manual = sorted(info.get("subtitles") or {})
auto = sorted(set(
lang for lang in (info.get("automatic_captions") or {})
if not lang.endswith("-orig")
))
return {"manual": manual, "auto": auto}
except json.JSONDecodeError:
continue
return {"manual": [], "auto": []}
def fetch_video_comments(youtube_video_id: str, max_comments: int = 20) -> list[dict]:
"""Fetch top comments via yt-dlp CLI writing to a temp file. Returns empty list on failure."""
import os