aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/utils/_utils.py
diff options
context:
space:
mode:
authorpukkandan <pukkandan.ytdlp@gmail.com>2023-06-22 04:54:39 +0530
committerpukkandan <pukkandan.ytdlp@gmail.com>2023-06-22 13:03:07 +0530
commitb4e0d75848e9447cee2cd3646ce54d4744a7ff56 (patch)
tree2146220a62f40ace502ed17b1653b9632d29dcbe /yt_dlp/utils/_utils.py
parent71dc18fa29263a1ff0472c23d81bfc8dd4422d48 (diff)
downloadhypervideo-pre-b4e0d75848e9447cee2cd3646ce54d4744a7ff56.tar.lz
hypervideo-pre-b4e0d75848e9447cee2cd3646ce54d4744a7ff56.tar.xz
hypervideo-pre-b4e0d75848e9447cee2cd3646ce54d4744a7ff56.zip
Improve `--download-sections`
* Support negative time-ranges * Add `*from-url` to obey time-ranges in URL Closes #7248
Diffstat (limited to 'yt_dlp/utils/_utils.py')
-rw-r--r--yt_dlp/utils/_utils.py22
1 files changed, 18 insertions, 4 deletions
diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py
index bc1bc9116..56acadd73 100644
--- a/yt_dlp/utils/_utils.py
+++ b/yt_dlp/utils/_utils.py
@@ -3753,11 +3753,11 @@ def match_filter_func(filters, breaking_filters=None):
class download_range_func:
- def __init__(self, chapters, ranges):
- self.chapters, self.ranges = chapters, ranges
+ def __init__(self, chapters, ranges, from_info=False):
+ self.chapters, self.ranges, self.from_info = chapters, ranges, from_info
def __call__(self, info_dict, ydl):
- if not self.ranges and not self.chapters:
+ if not any((self.ranges, self.chapters, self.from_info)):
yield {}
warning = ('There are no chapters matching the regex' if info_dict.get('chapters')
@@ -3770,7 +3770,21 @@ class download_range_func:
if self.chapters and warning:
ydl.to_screen(f'[info] {info_dict["id"]}: {warning}')
- yield from ({'start_time': start, 'end_time': end} for start, end in self.ranges or [])
+ for start, end in self.ranges or []:
+ yield {
+ 'start_time': self._handle_negative_timestamp(start, info_dict),
+ 'end_time': self._handle_negative_timestamp(end, info_dict),
+ }
+
+ if self.from_info and (info_dict.get('start_time') or info_dict.get('end_time')):
+ yield {
+ 'start_time': info_dict.get('start_time'),
+ 'end_time': info_dict.get('end_time'),
+ }
+
+ @staticmethod
+ def _handle_negative_timestamp(time, info):
+ return max(info['duration'] + time, 0) if info.get('duration') and time < 0 else time
def __eq__(self, other):
return (isinstance(other, download_range_func)