implement time-clipped downloads (closes #969, replaces #907)

This commit is contained in:
Alex Shnitman
2026-04-26 23:07:50 +03:00
parent 91ee8312bf
commit 4f83174d05
11 changed files with 426 additions and 4 deletions
+37 -1
View File
@@ -192,6 +192,8 @@ class DownloadInfo:
subtitle_mode="prefer_manual",
ytdl_options_presets=None,
ytdl_options_overrides=None,
clip_start=None,
clip_end=None,
):
self.id = id if len(custom_name_prefix) == 0 else f'{custom_name_prefix}.{id}'
self.title = title if len(custom_name_prefix) == 0 else f'{custom_name_prefix}.{title}'
@@ -216,6 +218,8 @@ class DownloadInfo:
self.subtitle_mode = subtitle_mode
self.ytdl_options_presets = list(ytdl_options_presets or [])
self.ytdl_options_overrides = dict(ytdl_options_overrides or {})
self.clip_start = clip_start
self.clip_end = clip_end
self.subtitle_files = []
def __setstate__(self, state):
@@ -284,6 +288,10 @@ class DownloadInfo:
self.subtitle_files = []
if not hasattr(self, "chapter_files"):
self.chapter_files = []
if not hasattr(self, "clip_start"):
self.clip_start = None
if not hasattr(self, "clip_end"):
self.clip_end = None
_PERSISTED_DOWNLOAD_FIELDS = (
@@ -303,6 +311,8 @@ _PERSISTED_DOWNLOAD_FIELDS = (
"subtitle_mode",
"ytdl_options_presets",
"ytdl_options_overrides",
"clip_start",
"clip_end",
"status",
"timestamp",
"error",
@@ -473,6 +483,16 @@ class Download:
'force_keyframes': False
})
clip_start = getattr(self.info, 'clip_start', None)
clip_end = getattr(self.info, 'clip_end', None)
if clip_start is not None or clip_end is not None:
start = float(clip_start) if clip_start is not None else 0.0
end = float(clip_end) if clip_end is not None else float('inf')
ytdl_params['download_ranges'] = yt_dlp.utils.download_range_func(
None,
[(start, end)],
)
ret = yt_dlp.YoutubeDL(params=ytdl_params).download([self.info.url])
self.status_queue.put({'status': 'finished' if ret == 0 else 'error'})
log.info(f"Finished download for: {self.info.title}")
@@ -890,6 +910,8 @@ class DownloadQueue:
subtitle_mode,
ytdl_options_presets,
ytdl_options_overrides,
clip_start,
clip_end,
already,
_add_gen=None,
):
@@ -924,6 +946,8 @@ class DownloadQueue:
subtitle_mode,
ytdl_options_presets,
ytdl_options_overrides,
clip_start,
clip_end,
already,
_add_gen,
)
@@ -975,6 +999,8 @@ class DownloadQueue:
subtitle_mode,
ytdl_options_presets,
ytdl_options_overrides,
clip_start,
clip_end,
already,
_add_gen,
)
@@ -1008,6 +1034,8 @@ class DownloadQueue:
subtitle_mode=subtitle_mode,
ytdl_options_presets=ytdl_options_presets,
ytdl_options_overrides=ytdl_options_overrides,
clip_start=clip_start,
clip_end=clip_end,
)
await self.__add_download(dl, auto_start)
return {'status': 'ok'}
@@ -1030,6 +1058,8 @@ class DownloadQueue:
subtitle_mode="prefer_manual",
ytdl_options_presets=None,
ytdl_options_overrides=None,
clip_start=None,
clip_end=None,
already=None,
_add_gen=None,
):
@@ -1038,7 +1068,7 @@ class DownloadQueue:
log.info(
f'adding {url}: {download_type=} {codec=} {format=} {quality=} {already=} {folder=} {custom_name_prefix=} '
f'{playlist_item_limit=} {auto_start=} {split_by_chapters=} {chapter_template=} '
f'{subtitle_language=} {subtitle_mode=} {ytdl_options_presets=}'
f'{subtitle_language=} {subtitle_mode=} {ytdl_options_presets=} {clip_start=} {clip_end=}'
)
if already is None:
_add_gen = self._add_generation
@@ -1072,6 +1102,8 @@ class DownloadQueue:
subtitle_mode,
ytdl_options_presets,
ytdl_options_overrides,
clip_start,
clip_end,
already,
_add_gen,
)
@@ -1093,6 +1125,8 @@ class DownloadQueue:
subtitle_mode="prefer_manual",
ytdl_options_presets=None,
ytdl_options_overrides=None,
clip_start=None,
clip_end=None,
):
if ytdl_options_presets is None:
ytdl_options_presets = []
@@ -1114,6 +1148,8 @@ class DownloadQueue:
subtitle_mode,
ytdl_options_presets,
ytdl_options_overrides,
clip_start,
clip_end,
already,
None,
)