diff options
| author | 2025-07-31 01:22:01 -0400 | |
|---|---|---|
| committer | 2025-07-31 01:22:01 -0400 | |
| commit | a6e995c093de8aae2e91a0787281bb34c0b871eb (patch) | |
| tree | 2d79821b05300d34d8871eb6c9662b359a2de85d /gallery_dl/downloader/ytdl.py | |
| parent | 7672a750cb74bf31e21d76aad2776367fd476155 (diff) | |
New upstream version 1.30.2.upstream/1.30.2
Diffstat (limited to 'gallery_dl/downloader/ytdl.py')
| -rw-r--r-- | gallery_dl/downloader/ytdl.py | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/gallery_dl/downloader/ytdl.py b/gallery_dl/downloader/ytdl.py index 1fc2f82..69a59ff 100644 --- a/gallery_dl/downloader/ytdl.py +++ b/gallery_dl/downloader/ytdl.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2018-2022 Mike Fährmann +# Copyright 2018-2025 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -30,6 +30,7 @@ class YoutubeDLDownloader(DownloaderBase): } self.ytdl_instance = None + self.rate_dyn = None self.forward_cookies = self.config("forward-cookies", True) self.progress = self.config("progress", 3.0) self.outtmpl = self.config("outtmpl") @@ -67,18 +68,23 @@ class YoutubeDLDownloader(DownloaderBase): for cookie in self.session.cookies: set_cookie(cookie) - if self.progress is not None and not ytdl_instance._progress_hooks: - ytdl_instance.add_progress_hook(self._progress_hook) + if "__gdl_initialize" in ytdl_instance.params: + del ytdl_instance.params["__gdl_initialize"] + + if self.progress is not None: + ytdl_instance.add_progress_hook(self._progress_hook) + if rlf := ytdl_instance.params.pop("__gdl_ratelimit_func", False): + self.rate_dyn = rlf info_dict = kwdict.pop("_ytdl_info_dict", None) if not info_dict: url = url[5:] try: - manifest = kwdict.pop("_ytdl_manifest", None) - if manifest: + if manifest := kwdict.pop("_ytdl_manifest", None): info_dict = self._extract_manifest( ytdl_instance, url, manifest, - kwdict.pop("_ytdl_manifest_data", None)) + kwdict.pop("_ytdl_manifest_data", None), + kwdict.pop("_ytdl_manifest_headers", None)) else: info_dict = self._extract_info(ytdl_instance, url) except Exception as exc: @@ -96,8 +102,7 @@ class YoutubeDLDownloader(DownloaderBase): else: info_dict = info_dict["entries"][index] - extra = kwdict.get("_ytdl_extra") - if extra: + if extra := kwdict.get("_ytdl_extra"): info_dict.update(extra) return self._download_video(ytdl_instance, pathfmt, info_dict) @@ -131,26 +136,31 @@ class YoutubeDLDownloader(DownloaderBase): pathfmt.temppath = "" return True + if self.rate_dyn is not None: + # static ratelimits are set in ytdl.construct_YoutubeDL + ytdl_instance.params["ratelimit"] = self.rate_dyn() + self.out.start(pathfmt.path) if self.part: - pathfmt.kwdict["extension"] = pathfmt.prefix + "part" + pathfmt.kwdict["extension"] = pathfmt.prefix filename = pathfmt.build_filename(pathfmt.kwdict) pathfmt.kwdict["extension"] = info_dict["ext"] if self.partdir: path = os.path.join(self.partdir, filename) else: path = pathfmt.realdirectory + filename + path = path.replace("%", "%%") + "%(ext)s" else: - path = pathfmt.realpath + path = pathfmt.realpath.replace("%", "%%") - self._set_outtmpl(ytdl_instance, path.replace("%", "%%")) + self._set_outtmpl(ytdl_instance, path) try: ytdl_instance.process_info(info_dict) except Exception as exc: self.log.debug("", exc_info=exc) return False - pathfmt.temppath = info_dict["filepath"] + pathfmt.temppath = info_dict.get("filepath") or info_dict["_filename"] return True def _download_playlist(self, ytdl_instance, pathfmt, info_dict): @@ -159,13 +169,16 @@ class YoutubeDLDownloader(DownloaderBase): self._set_outtmpl(ytdl_instance, pathfmt.realpath) for entry in info_dict["entries"]: + if self.rate_dyn is not None: + ytdl_instance.params["ratelimit"] = self.rate_dyn() ytdl_instance.process_info(entry) return True def _extract_info(self, ytdl, url): return ytdl.extract_info(url, download=False) - def _extract_manifest(self, ytdl, url, manifest_type, manifest_data=None): + def _extract_manifest(self, ytdl, url, manifest_type, manifest_data=None, + headers=None): extr = ytdl.get_info_extractor("Generic") video_id = extr._generic_id(url) @@ -173,9 +186,10 @@ class YoutubeDLDownloader(DownloaderBase): if manifest_data is None: try: fmts, subs = extr._extract_m3u8_formats_and_subtitles( - url, video_id, "mp4") + url, video_id, "mp4", headers=headers) except AttributeError: - fmts = extr._extract_m3u8_formats(url, video_id, "mp4") + fmts = extr._extract_m3u8_formats( + url, video_id, "mp4", headers=headers) subs = None else: try: @@ -189,9 +203,10 @@ class YoutubeDLDownloader(DownloaderBase): if manifest_data is None: try: fmts, subs = extr._extract_mpd_formats_and_subtitles( - url, video_id) + url, video_id, headers=headers) except AttributeError: - fmts = extr._extract_mpd_formats(url, video_id) + fmts = extr._extract_mpd_formats( + url, video_id, headers=headers) subs = None else: if isinstance(manifest_data, str): @@ -228,8 +243,7 @@ class YoutubeDLDownloader(DownloaderBase): int(speed) if speed else 0, ) - @staticmethod - def _set_outtmpl(ytdl_instance, outtmpl): + def _set_outtmpl(self, ytdl_instance, outtmpl): try: ytdl_instance._parse_outtmpl except AttributeError: |
