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

@@ -19,6 +19,7 @@ router = APIRouter()
class DownloadRequest(BaseModel):
youtube_video_id: str
quality: Optional[str] = None
subtitle_langs: Optional[str] = None # overrides user setting when provided
TRASH_TTL_DAYS = 7
@@ -127,7 +128,10 @@ def create_download(
user_settings = db.query(UserSettings).filter_by(user_id=current_user.id).first()
default_quality = user_settings.preferred_quality if user_settings else "best"
quality = body.quality if body.quality in ytdlp.QUALITY_FORMATS else default_quality
subtitle_langs = (user_settings.subtitle_langs or "") if user_settings else ""
if body.subtitle_langs is not None:
subtitle_langs = body.subtitle_langs.strip()
else:
subtitle_langs = (user_settings.subtitle_langs or "") if user_settings else ""
_DL_SELECT = """
SELECT d.id, d.status, d.progress_percent, d.resolution,

View File

@@ -657,6 +657,15 @@ def delete_bookmark(
db.commit()
@router.get("/by-yt/{youtube_video_id}/subs")
def get_available_subs(
youtube_video_id: str,
current_user: User = Depends(get_current_user),
):
"""Return subtitle languages available on YouTube for a video."""
return ytdlp.fetch_available_subs(youtube_video_id)
@router.get("/by-yt/{youtube_video_id}/comments")
def get_comments(
youtube_video_id: str,

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