Support for epoch timestamps

This commit is contained in:
Elyse 2023-05-06 23:05:38 -06:00
parent 128d30492b
commit 7f93eb7a28
2 changed files with 19 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import optparse
import os
import re
import sys
import time
from .compat import compat_shlex_quote
from .cookies import SUPPORTED_BROWSERS, SUPPORTED_KEYRINGS
@ -322,15 +323,30 @@ def validate_options(opts):
def parse_chapters(name, value):
chapters, ranges = [], []
parse_timestamp = lambda x: float('inf') if x in ('inf', 'infinite') else parse_duration(x)
current_time = time.time()
for regex in value or []:
if regex.startswith('*'):
for range_ in map(str.strip, regex[1:].split(',')):
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"')
ranges.append(dur)
continue
elif regex.startswith('#'):
for range_ in map(str.strip, regex[1:].split(',')):
mobj = range_ != '-' and re.fullmatch(r'(-?[^-]+)\s*-\s*(-?[^-]+)?', range_)
if not mobj:
raise ValueError(f'invalid {name} time range "{regex}". Must be of the form "#start-end"')
start_section = parse_timestamp(mobj.group(1) or '0')
end_section = parse_timestamp(mobj.group(2) or 'inf')
if start_section is None or end_section is None:
raise ValueError(f'invalid {name} time range "{regex}". Must be of the form "#start-end"')
ranges.append((current_time + start_section, current_time + end_section))
continue
try:
chapters.append(re.compile(regex))
except re.error as err:

View File

@ -2765,8 +2765,8 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
begin_index = 0
download_start_time = ctx.get('start') or time.time()
section_start = 0 if ctx.get('section_start') is None else download_start_time + ctx['section_start']
section_end = math.inf if ctx.get('section_end') is None else download_start_time + ctx['section_end']
section_start = ctx.get('section_start') or 0
section_end = ctx.get('section_end') or math.inf
self.write_debug(f'Selected section: {section_start} -> {section_end}')