feat: Zeitraum-Auswahl (-s/-e) auch in rede_korrigieren.py und extract_text.py
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1668a8dcad
commit
82489ee83c
1 changed files with 111 additions and 17 deletions
|
|
@ -7,7 +7,7 @@ import shutil
|
|||
import tempfile
|
||||
import textwrap
|
||||
|
||||
from vtt2txt import vtt_to_lines
|
||||
from vtt2txt import add_time_options, check_time_range, time_suffix, vtt_to_lines
|
||||
from yt_untertitel import (
|
||||
YT_DLP, choose_track, default_name, download, fetch_info, no_track_message,
|
||||
original_language,
|
||||
|
|
@ -27,13 +27,16 @@ def parse_args():
|
|||
)
|
||||
parser.add_argument(
|
||||
"-o", "--output", type=Path,
|
||||
help="Ausgabedatei (Standard: <Kanal>_<Titel>_<Datum>.txt)",
|
||||
help="Ausgabedatei (Standard: <Kanal>_<Titel>_<Datum>.txt, "
|
||||
"mit -s/-e zusätzlich der Zeitraum)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-f", "--fliesstext", action="store_true",
|
||||
help=f"Fließtext, auf {FLOW_WIDTH} Zeichen umbrochen, statt Untertitelzeilen",
|
||||
)
|
||||
add_time_options(parser)
|
||||
args = parser.parse_args()
|
||||
check_time_range(parser, args)
|
||||
|
||||
if args.output is not None and args.output.is_dir():
|
||||
parser.error(f"-o/--output ist ein Verzeichnis: {args.output}")
|
||||
|
|
@ -42,10 +45,11 @@ def parse_args():
|
|||
return args
|
||||
|
||||
|
||||
def fetch_text(url, lang=None):
|
||||
def fetch_text(url, lang=None, start=None, end=None):
|
||||
"""Download the subtitles of a video; return (video info, text lines).
|
||||
|
||||
Without `lang` the language of the original audio is used.
|
||||
Without `lang` the language of the original audio is used. `start`/`end`
|
||||
(seconds) keep only the captions of that time range.
|
||||
"""
|
||||
info = fetch_info(url)
|
||||
print(f"Video: {info.get('title', '?')}")
|
||||
|
|
@ -65,17 +69,20 @@ def fetch_text(url, lang=None):
|
|||
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"))
|
||||
lines = vtt_to_lines(vtt.read_text(encoding="utf-8-sig"), start, end)
|
||||
|
||||
if not lines:
|
||||
raise SystemExit("Die Untertitel enthalten keinen Text.")
|
||||
if start is None and end is None:
|
||||
raise SystemExit("Die Untertitel enthalten keinen Text.")
|
||||
raise SystemExit("Die Untertitel enthalten im gewählten Zeitraum keinen Text.")
|
||||
return info, lines
|
||||
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
info, lines = fetch_text(args.url, args.lang)
|
||||
output = args.output or Path(f"{default_name(info)}.txt")
|
||||
info, lines = fetch_text(args.url, args.lang, args.start, args.ende)
|
||||
suffix = time_suffix(args.start, args.ende)
|
||||
output = args.output or Path(f"{default_name(info)}{suffix}.txt")
|
||||
|
||||
if args.fliesstext:
|
||||
# Never split words, not even at hyphens ("EU-Gipfel")
|
||||
|
|
|
|||
Loading…
Reference in a new issue