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:
@@ -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"')
|
||||
|
||||
Reference in New Issue
Block a user