From 3975b4d2e83a2d425a7b88752cd2d6ec83110f20 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Wed, 22 Jun 2022 02:32:14 +0530 Subject: [PATCH] Allow extractors to specify section_start/end for clips --- yt_dlp/YoutubeDL.py | 17 ++++++++++++----- yt_dlp/extractor/common.py | 5 +++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 7e065daa1..c59502161 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -1596,9 +1596,13 @@ class YoutubeDL: if not info: return info + exempted_fields = {'_type', 'url', 'ie_key'} + if not ie_result.get('section_end') and ie_result.get('section_start') is None: + # For video clips, the id etc of the clip extractor should be used + exempted_fields |= {'id', 'extractor', 'extractor_key'} + new_result = info.copy() - new_result.update(filter_dict(ie_result, lambda k, v: ( - v is not None and k not in {'_type', 'url', 'id', 'extractor', 'extractor_key', 'ie_key'}))) + new_result.update(filter_dict(ie_result, lambda k, v: v is not None and k not in exempted_fields)) # Extracted info may not be a video result (i.e. # info.get('_type', 'video') != video) but rather an url or @@ -2369,6 +2373,8 @@ class YoutubeDL: sanitize_string_field(info_dict, 'id') sanitize_numeric_fields(info_dict) + if info_dict.get('section_end') and info_dict.get('section_start') is not None: + info_dict['duration'] = round(info_dict['section_end'] - info_dict['section_start'], 3) if (info_dict.get('duration') or 0) <= 0 and info_dict.pop('duration', None): self.report_warning('"duration" field is negative, there is an error in extractor') @@ -2604,10 +2610,11 @@ class YoutubeDL: for fmt, chapter in itertools.product(formats_to_download, requested_ranges or [{}]): new_info = self._copy_infodict(info_dict) new_info.update(fmt) - if chapter: + offset, duration = info_dict.get('section_start') or 0, info_dict.get('duration') or float('inf') + if chapter or offset: new_info.update({ - 'section_start': chapter.get('start_time'), - 'section_end': chapter.get('end_time', 0), + 'section_start': offset + chapter.get('start_time', 0), + 'section_end': offset + min(chapter.get('end_time', 0), duration), 'section_title': chapter.get('title'), 'section_number': chapter.get('index'), }) diff --git a/yt_dlp/extractor/common.py b/yt_dlp/extractor/common.py index 3e3e55798..90af41575 100644 --- a/yt_dlp/extractor/common.py +++ b/yt_dlp/extractor/common.py @@ -385,6 +385,11 @@ class InfoExtractor: release_year: Year (YYYY) when the album was released. composer: Composer of the piece + The following fields should only be set for clips that should be cut from the original video: + + section_start: Start time of the section in seconds + section_end: End time of the section in seconds + Unless mentioned otherwise, the fields should be Unicode strings. Unless mentioned otherwise, None is equivalent to absence of information.