yt-dlp ruft zur Prüfung „ffmpeg -bsfs“ auf; das System-ffmpeg 6.1.1 (Ubuntu) legt dabei eine leere Datei „3“ im Arbeitsverzeichnis an. yt-dlp läuft jetzt in einem temporären Verzeichnis mit absolutem Zielpfad, die Datei verschwindet mit dem Verzeichnis. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
4.9 KiB
Python
Executable file
145 lines
4.9 KiB
Python
Executable file
#!/usr/bin/env python3
|
||
"""Lädt die Untertitel eines YouTube-Videos als VTT-Datei herunter (über yt-dlp),
|
||
als Eingabe für vtt2txt.py bzw. rede_korrigieren.py."""
|
||
from pathlib import Path
|
||
import argparse
|
||
import json
|
||
import shutil
|
||
import subprocess
|
||
import tempfile
|
||
|
||
YT_DLP = "yt-dlp"
|
||
|
||
|
||
def run_yt_dlp(arguments):
|
||
"""Run yt-dlp in a temporary working directory.
|
||
|
||
yt-dlp calls "ffmpeg -bsfs" to check ffmpeg; some ffmpeg builds (Ubuntu
|
||
6.1.1) then leave an empty file "3" in the working directory.
|
||
"""
|
||
with tempfile.TemporaryDirectory() as tmp:
|
||
return subprocess.run(
|
||
[YT_DLP, *arguments], capture_output=True, text=True, cwd=tmp
|
||
)
|
||
|
||
|
||
def fetch_info(url):
|
||
"""Return the video metadata from yt-dlp, without downloading anything."""
|
||
result = run_yt_dlp(["--dump-single-json", "--skip-download", "--no-playlist", url])
|
||
if result.returncode != 0:
|
||
raise SystemExit(f"yt-dlp konnte das Video nicht abrufen:\n{result.stderr.strip()}")
|
||
return json.loads(result.stdout)
|
||
|
||
|
||
def choose_track(info, lang):
|
||
"""Pick the subtitle track for `lang`: (track name, is_auto) or None.
|
||
|
||
Manual subtitles come first. Of the automatic captions only the original
|
||
language ("<lang>-orig") is used: plain "<lang>" there is a machine
|
||
translation whenever the video is in another language.
|
||
"""
|
||
if lang in (info.get("subtitles") or {}):
|
||
return lang, False
|
||
if f"{lang}-orig" in (info.get("automatic_captions") or {}):
|
||
return f"{lang}-orig", True
|
||
return None
|
||
|
||
|
||
def original_language(info):
|
||
"""Language of the video's audio, or None if yt-dlp does not know it.
|
||
|
||
YouTube marks the automatic captions in the spoken language with "-orig".
|
||
"""
|
||
for name in info.get("automatic_captions") or {}:
|
||
if name.endswith("-orig"):
|
||
return name.removesuffix("-orig")
|
||
return info.get("language")
|
||
|
||
|
||
def available_tracks(info):
|
||
manual = sorted((info.get("subtitles") or {}).keys())
|
||
original = sorted(
|
||
name.removesuffix("-orig")
|
||
for name in (info.get("automatic_captions") or {})
|
||
if name.endswith("-orig")
|
||
)
|
||
return manual, original
|
||
|
||
|
||
def no_track_message(info, lang):
|
||
"""Explain that no usable track exists for `lang` (None: original language)."""
|
||
manual, original = available_tracks(info)
|
||
wanted = f"„{lang}“" if lang else "der Originalsprache"
|
||
return (
|
||
f"Keine Untertitel in {wanted} (weder manuell noch automatisch in "
|
||
f"Originalsprache).\n"
|
||
f"Manuell: {', '.join(manual) or '–'}\n"
|
||
f"Automatisch (Originalsprache): {', '.join(original) or '–'}"
|
||
)
|
||
|
||
|
||
def download(url, track, is_auto, target_base):
|
||
"""Download one subtitle track; yt-dlp writes <target_base>.<track>.vtt."""
|
||
result = run_yt_dlp([
|
||
"--skip-download", "--no-playlist", "--force-overwrites",
|
||
"--write-auto-subs" if is_auto else "--write-subs",
|
||
"--sub-langs", track, "--sub-format", "vtt",
|
||
# Absolute, because yt-dlp runs in a temporary directory
|
||
"-o", f"{Path(target_base).resolve()}.%(ext)s",
|
||
url,
|
||
])
|
||
if result.returncode != 0:
|
||
raise SystemExit(f"Download fehlgeschlagen:\n{result.stderr.strip()}")
|
||
|
||
|
||
def parse_args():
|
||
parser = argparse.ArgumentParser(
|
||
description="Lädt die Untertitel eines YouTube-Videos als VTT-Datei herunter."
|
||
)
|
||
parser.add_argument("-u", "--url", required=True, help="YouTube-URL des Videos")
|
||
parser.add_argument(
|
||
"-n", "--name",
|
||
help="Dateiname ohne Endung, z. B. Lahnstein (Standard: Video-ID)",
|
||
)
|
||
parser.add_argument(
|
||
"-l", "--lang", default="de", help="Sprache der Untertitel (Standard: de)"
|
||
)
|
||
parser.add_argument(
|
||
"-d", "--dir", type=Path, default=Path("."),
|
||
help="Zielverzeichnis (Standard: aktuelles Verzeichnis)",
|
||
)
|
||
args = parser.parse_args()
|
||
|
||
if args.name is not None and (not args.name or Path(args.name).name != args.name):
|
||
parser.error("-n/--name ist ein Dateiname ohne Verzeichnis; dafür gibt es -d/--dir.")
|
||
if shutil.which(YT_DLP) is None:
|
||
parser.error(f"{YT_DLP} nicht gefunden (Installation: pip install yt-dlp).")
|
||
return args
|
||
|
||
|
||
def main():
|
||
args = parse_args()
|
||
|
||
info = fetch_info(args.url)
|
||
print(f"Video: {info.get('title', '?')}")
|
||
|
||
chosen = choose_track(info, args.lang)
|
||
if chosen is None:
|
||
raise SystemExit(no_track_message(info, args.lang))
|
||
track, is_auto = chosen
|
||
kind = "automatische" if is_auto else "manuelle"
|
||
print(f"Lade {kind} Untertitel „{track}“ …", flush=True)
|
||
|
||
args.dir.mkdir(parents=True, exist_ok=True)
|
||
target_base = args.dir / (args.name or info["id"])
|
||
download(args.url, track, is_auto, target_base)
|
||
|
||
target = Path(f"{target_base}.{track}.vtt")
|
||
if not target.is_file():
|
||
raise SystemExit(f"yt-dlp hat keine Datei {target} geschrieben.")
|
||
print(f"Gespeichert: {target}")
|
||
print(f"Weiter mit: ./vtt2txt.py -i {target}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|