2026-10-10 19:51:32 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""Lädt die Untertitel eines YouTube-Videos und speichert ihren Text als Datei
|
|
|
|
|
(yt_untertitel.py + vtt2txt.py in einem Schritt)."""
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
import argparse
|
|
|
|
|
import shutil
|
|
|
|
|
import tempfile
|
2026-10-10 19:53:42 +02:00
|
|
|
import textwrap
|
2026-10-10 19:51:32 +02:00
|
|
|
|
|
|
|
|
from vtt2txt import vtt_to_lines
|
|
|
|
|
from yt_untertitel import (
|
2026-10-10 19:52:20 +02:00
|
|
|
YT_DLP, choose_track, download, fetch_info, no_track_message, original_language,
|
2026-10-10 19:51:32 +02:00
|
|
|
)
|
|
|
|
|
|
2026-10-10 19:53:42 +02:00
|
|
|
FLOW_WIDTH = 80 # Zeilenbreite für --fliesstext
|
|
|
|
|
|
2026-10-10 19:51:32 +02:00
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
description="Lädt die Untertitel eines YouTube-Videos und speichert den Text."
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument("-u", "--url", required=True, help="YouTube-URL des Videos")
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"-l", "--lang",
|
|
|
|
|
help="Sprache der Untertitel, z. B. de (Standard: Sprache des Original-Audios)",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
"-o", "--output", type=Path, default=Path("output.txt"),
|
|
|
|
|
help="Ausgabedatei (Standard: output.txt)",
|
|
|
|
|
)
|
2026-10-10 19:53:42 +02:00
|
|
|
parser.add_argument(
|
|
|
|
|
"-f", "--fliesstext", action="store_true",
|
|
|
|
|
help=f"Fließtext, auf {FLOW_WIDTH} Zeichen umbrochen, statt Untertitelzeilen",
|
|
|
|
|
)
|
2026-10-10 19:51:32 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
if args.output.is_dir():
|
|
|
|
|
parser.error(f"-o/--output ist ein Verzeichnis: {args.output}")
|
|
|
|
|
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', '?')}")
|
|
|
|
|
|
|
|
|
|
lang = args.lang or original_language(info)
|
|
|
|
|
chosen = choose_track(info, lang) if lang else None
|
|
|
|
|
if chosen is None:
|
2026-10-10 19:52:20 +02:00
|
|
|
raise SystemExit(no_track_message(info, lang))
|
2026-10-10 19:51:32 +02:00
|
|
|
track, is_auto = chosen
|
|
|
|
|
kind = "automatische" if is_auto else "manuelle"
|
|
|
|
|
print(f"Lade {kind} Untertitel „{track}“ …", flush=True)
|
|
|
|
|
|
|
|
|
|
# The VTT file is only an intermediate step
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
|
|
|
target_base = Path(tmp) / "untertitel"
|
|
|
|
|
download(args.url, track, is_auto, target_base)
|
|
|
|
|
vtt = Path(f"{target_base}.{track}.vtt")
|
|
|
|
|
if not vtt.is_file():
|
|
|
|
|
raise SystemExit("yt-dlp hat keine Untertitel-Datei geschrieben.")
|
|
|
|
|
lines = vtt_to_lines(vtt.read_text(encoding="utf-8-sig"))
|
|
|
|
|
|
|
|
|
|
if not lines:
|
|
|
|
|
raise SystemExit("Die Untertitel enthalten keinen Text.")
|
|
|
|
|
|
2026-10-10 19:53:42 +02:00
|
|
|
if args.fliesstext:
|
|
|
|
|
# Never split words, not even at hyphens ("EU-Gipfel")
|
|
|
|
|
text = textwrap.fill(
|
|
|
|
|
" ".join(lines), width=FLOW_WIDTH,
|
|
|
|
|
break_long_words=False, break_on_hyphens=False,
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
text = "\n".join(lines)
|
|
|
|
|
|
2026-10-10 19:51:32 +02:00
|
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
2026-10-10 19:53:42 +02:00
|
|
|
args.output.write_text(text + "\n", encoding="utf-8")
|
|
|
|
|
print(
|
|
|
|
|
f"Gespeichert: {args.output} "
|
|
|
|
|
f"({len(text.splitlines())} Zeilen, {len(text.split())} Wörter)"
|
|
|
|
|
)
|
2026-10-10 19:51:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|