From 7f93eb7a28fd0618c59689ee1f38d72df6c4d800 Mon Sep 17 00:00:00 2001 From: Elyse <26639800+elyse0@users.noreply.github.com> Date: Sat, 6 May 2023 23:05:38 -0600 Subject: [PATCH] Support for epoch timestamps --- yt_dlp/__init__.py | 18 +++++++++++++++++- yt_dlp/extractor/youtube.py | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py index 39d638cba..8e0281b72 100644 --- a/yt_dlp/__init__.py +++ b/yt_dlp/__init__.py @@ -13,6 +13,7 @@ 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_outtmpl(tmpl, msg): 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: diff --git a/yt_dlp/extractor/youtube.py b/yt_dlp/extractor/youtube.py index f268bd901..4e4a3b733 100644 --- a/yt_dlp/extractor/youtube.py +++ b/yt_dlp/extractor/youtube.py @@ -2765,8 +2765,8 @@ def _live_dash_fragments(self, video_id, format_id, live_start_time, mpd_feed, m 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}')