fix(download): fetch only the paid minutes of a long or endless source
A capped job (MAX_SOURCE_MINUTES, or the SOURCE_CAP_MINUTES safety cap on every metered job) still downloaded the whole source and cut it after. On a finished livestream (hours of 1080p60) that meant GBs through the proxy and a job stuck downloading for tens of minutes, long enough to outlive a deploy drain and restart on the next instance. When the source is longer than the cap (plus a margin) or has no known duration, yt-dlp now downloads only that range (download_ranges); cap_source_duration still makes the exact cut. Sources within the cap download exactly as before. The early audio track is skipped for ranged downloads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
bb24a5e5ca
commit
696659a828
1 changed files with 29 additions and 2 deletions
31
main.py
31
main.py
|
|
@ -935,6 +935,23 @@ def download_youtube_video(url, output_dir=".", on_audio=None):
|
|||
|
||||
_early = {"started": False}
|
||||
|
||||
# A job only paid for so many minutes (MAX_SOURCE_MINUTES: the quota-wall
|
||||
# offer; SOURCE_CAP_MINUTES: every metered job, a safety cap). yt-dlp used
|
||||
# to fetch the whole source anyway and cap_source_duration cut it after:
|
||||
# on a finished livestream (no duration, hours of 1080p60) that was GBs of
|
||||
# proxy and a job stuck downloading for tens of minutes. Past the cap (or
|
||||
# with no known duration) only the paid range plus a margin is fetched;
|
||||
# cap_source_duration still makes the exact cut afterwards.
|
||||
_range_cap = None
|
||||
for _var, _margin in (("MAX_SOURCE_MINUTES", 5.0), ("SOURCE_CAP_MINUTES", 60.0)):
|
||||
_raw = os.environ.get(_var, "").strip()
|
||||
if _raw:
|
||||
try:
|
||||
_range_cap = float(_raw) * 60.0 + _margin
|
||||
except ValueError:
|
||||
pass
|
||||
break
|
||||
|
||||
def _early_audio(info, extractor_args, proxy, cookies):
|
||||
"""Fetch the audio track alone and hand it to on_audio (background)."""
|
||||
import copy
|
||||
|
|
@ -966,9 +983,14 @@ def download_youtube_video(url, output_dir=".", on_audio=None):
|
|||
with yt_dlp.YoutubeDL(_base_opts(extractor_args, proxy, cookies)) as ydl:
|
||||
info = ydl.extract_info(url, download=False, process=False)
|
||||
sanitized = sanitize_filename(info.get('title', 'youtube_video'))
|
||||
ranged = False
|
||||
if _range_cap:
|
||||
_dur = info.get('duration')
|
||||
ranged = not _dur or float(_dur) > _range_cap
|
||||
# Once per download, and not on the per-GB proxy (that is paid bytes,
|
||||
# and it is the last resort anyway).
|
||||
if (on_audio and not _early["started"] and info.get('formats')
|
||||
# and it is the last resort anyway). Not for a ranged download either:
|
||||
# the early audio would be the whole source.
|
||||
if (on_audio and not _early["started"] and info.get('formats') and not ranged
|
||||
and not (_proxy and proxy == _proxy)):
|
||||
_early["started"] = True
|
||||
threading.Thread(target=_early_audio, args=(info, extractor_args, proxy, cookies),
|
||||
|
|
@ -983,6 +1005,11 @@ def download_youtube_video(url, output_dir=".", on_audio=None):
|
|||
'merge_output_format': 'mp4', 'overwrites': True,
|
||||
'progress_hooks': [_progress_hook],
|
||||
}
|
||||
if ranged:
|
||||
from yt_dlp.utils import download_range_func
|
||||
dl_opts['download_ranges'] = download_range_func(None, [(0, _range_cap)])
|
||||
print(f"✂️ Source is {'of unknown length' if not info.get('duration') else 'longer than the paid minutes'}"
|
||||
f": downloading only the first {_range_cap / 60:.0f} min.")
|
||||
with yt_dlp.YoutubeDL(dl_opts) as ydl:
|
||||
if info.get('_type', 'video') == 'video' and info.get('formats'):
|
||||
ydl.process_ie_result(info, download=True)
|
||||
|
|
|
|||
Loading…
Reference in a new issue