1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-12-19 22:48:53 +00:00

Support negative durations

This commit is contained in:
Elyse
2023-03-10 15:20:40 -06:00
parent 9fc70f3f6d
commit e6e2eb00f1
3 changed files with 16 additions and 23 deletions

View File

@@ -321,25 +321,12 @@ def validate_options(opts):
del opts.outtmpl['default']
def parse_chapters(name, value):
def parse_timestamp(x):
# FIXME: Maybe there's a better way to remove parenthesis
x = x.replace('(', '').replace(')', '')
# FIXME: This should be smarter, e.g. 'inf-1day'?
if x in ('inf', 'infinite'):
return float('inf')
if re.match(r'[\d:]+', x):
return parse_duration(x)
return datetime_from_str(x, precision='second', use_utc=False).timestamp()
chapters, ranges = [], []
parse_timestamp = lambda x: float('inf') if x in ('inf', 'infinite') else parse_duration(x)
for regex in value or []:
if regex.startswith('*'):
for range_ in map(str.strip, regex[1:].split(',')):
# FIXME: This should match correctly '(now-1hour)-(now-20minutes)'
mobj = range_ != '-' and re.fullmatch(r'(.+)?\s*-\s*(.+)?', range_)
mobj = range_ != '-' and re.fullmatch(r'(-?[^-]+)?\s*-\s*(-?[^-]+)?', range_)
dur = mobj and (parse_timestamp(mobj.group(1) or '0'), parse_timestamp(mobj.group(2) or 'inf'))
if None in (dur or [None]):
raise ValueError(f'invalid {name} time range "{regex}". Must be of the form "*start-end"')