feat: -z -00:20 als Kurzform für 0-00:20 (vom Anfang bis)
Wörter hinter -z, die mit - und einer Ziffer beginnen, werden vor argparse zu 0-… umgeschrieben; parse_ranges nimmt -B auch in Anführungszeichen an. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8edef11c78
commit
955bdc2659
1 changed files with 74 additions and 22 deletions
36
vtt2txt.py
36
vtt2txt.py
|
|
@ -48,20 +48,24 @@ def parse_user_time(text):
|
|||
def parse_ranges(text):
|
||||
"""Sorted (start, end) seconds from "00:05:00-00:06:00 00:10-00:12 00:25".
|
||||
|
||||
"A" or "A-" runs to the end, "0-B" from the beginning; None marks an open side.
|
||||
"A" or "A-" runs to the end, "0-B" or "-B" from the beginning; None marks an
|
||||
open side.
|
||||
"""
|
||||
ranges = []
|
||||
for item in text.split():
|
||||
if item.startswith("-"):
|
||||
raise ValueError(f"ungültiger Bereich „{item}“, vom Anfang: 0-B schreiben")
|
||||
start, _, end = item.partition("-")
|
||||
if item == "-":
|
||||
raise ValueError(f"ungültiger Bereich „{item}“")
|
||||
try:
|
||||
span = (parse_user_time(start) or None, parse_user_time(end) if end else None)
|
||||
span = (
|
||||
parse_user_time(start) or None if start else None,
|
||||
parse_user_time(end) if end else None,
|
||||
)
|
||||
except ValueError:
|
||||
raise ValueError(f"ungültiger Bereich „{item}“") from None
|
||||
if span == (None, None):
|
||||
raise ValueError(f"Bereich „{item}“ ist das ganze Video: -z weglassen")
|
||||
if None not in span and span[0] >= span[1]:
|
||||
if span[1] is not None and (span[0] or 0) >= span[1]:
|
||||
raise ValueError(f"Bereich „{item}“: Start muss vor Ende liegen")
|
||||
ranges.append(span)
|
||||
if not ranges:
|
||||
|
|
@ -103,7 +107,7 @@ def time_suffix(ranges):
|
|||
|
||||
|
||||
RANGE_FORMS = (
|
||||
"A-B (von A bis B), A (ab A bis zum Ende) oder 0-B (vom Anfang bis B); "
|
||||
"A-B (von A bis B), A (ab A bis zum Ende) oder 0-B bzw. -B (vom Anfang bis B); "
|
||||
f"Zeiten {TIME_EXAMPLES}"
|
||||
)
|
||||
|
||||
|
|
@ -121,6 +125,24 @@ def add_time_options(parser):
|
|||
)
|
||||
|
||||
|
||||
def range_argv(argv):
|
||||
"""Copy of `argv` with "-00:20" after -z written as "0-00:20".
|
||||
|
||||
argparse takes a word starting with "-" for an option and would reject it;
|
||||
no option starts with "-" and a digit, so such a word can only be a range.
|
||||
"""
|
||||
result = []
|
||||
after_ranges = False
|
||||
for word in argv:
|
||||
if re.match(r"-\d", word):
|
||||
if after_ranges:
|
||||
word = f"0{word}"
|
||||
elif word.startswith("-"):
|
||||
after_ranges = word in ("-z", "--zeitraeume")
|
||||
result.append(word)
|
||||
return result
|
||||
|
||||
|
||||
def resolve_time_ranges(parser, args):
|
||||
"""Check -z and set args.bereiche: sorted list of (start, end), maybe empty."""
|
||||
if args.zeitraeume is None:
|
||||
|
|
@ -398,7 +420,7 @@ def parse_args():
|
|||
"mit -z zusätzlich die Bereiche im Namen",
|
||||
)
|
||||
add_time_options(parser)
|
||||
args = parser.parse_args()
|
||||
args = parser.parse_args(range_argv(sys.argv[1:]))
|
||||
resolve_time_ranges(parser, args)
|
||||
suffix = time_suffix(args.bereiche)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue