feat: extract_text.py --fliesstext für lesbaren Fließtext
Umbruch auf 80 Zeichen ohne Worttrennung, auch nicht am Bindestrich. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0413455ea5
commit
19b6bbf5a2
1 changed files with 39 additions and 3 deletions
|
|
@ -5,12 +5,15 @@ from pathlib import Path
|
|||
import argparse
|
||||
import shutil
|
||||
import tempfile
|
||||
import textwrap
|
||||
|
||||
from vtt2txt import vtt_to_lines
|
||||
from yt_untertitel import (
|
||||
YT_DLP, choose_track, download, fetch_info, no_track_message, original_language,
|
||||
)
|
||||
|
||||
FLOW_WIDTH = 80 # Zeilenbreite für --fliesstext
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
|
|
@ -25,6 +28,10 @@ def parse_args():
|
|||
"-o", "--output", type=Path, default=Path("output.txt"),
|
||||
help="Ausgabedatei (Standard: output.txt)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-f", "--fliesstext", action="store_true",
|
||||
help=f"Fließtext, auf {FLOW_WIDTH} Zeichen umbrochen, statt Untertitelzeilen",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.output.is_dir():
|
||||
|
|
@ -60,10 +67,21 @@ def main():
|
|||
if not lines:
|
||||
raise SystemExit("Die Untertitel enthalten keinen Text.")
|
||||
|
||||
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)
|
||||
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||
words = sum(len(line.split()) for line in lines)
|
||||
print(f"Gespeichert: {args.output} ({len(lines)} Zeilen, {words} Wörter)")
|
||||
args.output.write_text(text + "\n", encoding="utf-8")
|
||||
print(
|
||||
f"Gespeichert: {args.output} "
|
||||
f"({len(text.splitlines())} Zeilen, {len(text.split())} Wörter)"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Reference in a new issue