feat(dl): warn before the downloads when the temp volume cannot hold the selected tracks

This commit is contained in:
imSp4rky
2026-11-28 15:02:12 -07:00
parent fd1b9254ee
commit 127e8e0dcc
4 changed files with 217 additions and 26 deletions
+34 -1
View File
@@ -104,7 +104,7 @@ from unshackle.core.utilities import (
time_elapsed_since,
)
from unshackle.core.utils import tags
from unshackle.core.utils.bitrate import apply_real_bitrates
from unshackle.core.utils.bitrate import apply_real_bitrates, manifest_seconds
from unshackle.core.utils.click_types import (
AUDIO_CODEC_LIST,
LANGUAGE_RANGE,
@@ -393,6 +393,36 @@ def low_free_space(e: BaseException) -> str:
return note
DISK_NEED_FACTOR = 2
"""Times its estimated track size that a title needs on the temp volume: each encrypted track and its decrypted copy."""
def low_disk_warning(tracks: Tracks) -> str | None:
"""The warning line when the temp volume has less free space than a title's video and audio need, else None.
The size of a track is its manifest bitrate times its playing time; a track without both counts as 0 bytes.
Any error gives None, because the check only advises and must never stop a download.
"""
playable: list[Video | Audio] = [*tracks.videos, *tracks.audio]
temp = temp_root()
try:
size = sum(
track.bitrate * seconds / 8 for track in playable if track.bitrate and (seconds := manifest_seconds(track))
)
if not size:
return None
need = int(size * DISK_NEED_FACTOR)
free = shutil.disk_usage(temp).free
except Exception as e:
logging.getLogger("download").debug(f"Free-space check skipped: {e!r}")
return None
if free >= need:
return None
return (
f"Temp volume {temp} has {filesize.decimal(free)} free, the selected tracks need about {filesize.decimal(need)}"
)
class dl:
def find_custom_font(self, font_name: str) -> Path | None:
"""
@@ -2165,6 +2195,9 @@ class dl:
f"{self.service} gives each one as one file with no segment index."
)
if not skip_dl and not sample and (disk_warning := low_disk_warning(title.tracks)):
self.log.warning(disk_warning)
dl_start_time = time.time()
try:
+31 -25
View File
@@ -274,6 +274,33 @@ def uniform_segments(
return [Segment(url, byte_range, range_size(byte_range), per_segment) for url, byte_range in raw_segments]
def manifest_seconds(track: Track) -> float | None:
"""The playing time of a DASH or ISM track from the manifest it holds, else None; it sends no request.
A DASH track counts the content periods that carry its Representation, else the whole presentation.
"""
from unshackle.core.manifests import DASH
if (dash := track.data.get("dash")) and (manifest := dash.get("manifest")) is not None:
filtered = dash.get("filtered_period_ids", [])
rep_id = dash.get("representation_id") or dash["representation"].get("id")
durations = [
duration
for period, duration in zip(manifest.findall("Period"), DASH.period_durations(manifest), strict=True)
if DASH.is_content_period(period, filtered)
and (period is dash.get("period") or DASH.resolve_representation(period, rep_id, dash["adaptation_set"]))
]
if durations and None not in durations:
return sum(cast(list[float], durations))
total = manifest.get("mediaPresentationDuration")
return DASH.pt_to_sec(total) if total else None
if (ism := track.data.get("ism")) and (manifest := ism.get("manifest")) is not None:
ticks = int(manifest.get("Duration") or 0)
timescale = int(manifest.get("TimeScale") or ISM_DEFAULT_TIMESCALE)
return ticks / timescale if ticks and timescale else None
return None
def extract_dash(track: Track, session: Session | RnetSession) -> list[Segment]:
from unshackle.core.manifests import DASH
@@ -291,7 +318,6 @@ def extract_dash(track: Track, session: Session | RnetSession) -> list[Segment]:
content_periods = [p for p in periods if DASH.is_content_period(p, filtered_period_ids)]
raw_segments: list[tuple[str, str | None]] = []
kept: list[float | None] = []
for period in content_periods:
# rep ids are only unique within an AdaptationSet, so resolve like DASH.download_track
if period is stored_period:
@@ -312,15 +338,8 @@ def extract_dash(track: Track, session: Session | RnetSession) -> list[Segment]:
duration=durations[id(period)],
)
raw_segments.extend(period_segments)
kept.append(durations[id(period)])
# the kept periods only, so filtered ad time does not lower the estimate
total_duration: float | None = sum(cast(list[float], kept)) if kept and None not in kept else None
mpd_duration = manifest.get("mediaPresentationDuration")
if total_duration is None and mpd_duration:
total_duration = DASH.pt_to_sec(mpd_duration)
return uniform_segments(raw_segments, total_duration)
return uniform_segments(raw_segments, manifest_seconds(track))
def extract_hls(track: Track, session: Session | RnetSession) -> list[Segment]:
@@ -345,15 +364,8 @@ def extract_hls(track: Track, session: Session | RnetSession) -> list[Segment]:
def extract_ism(track: Track, session: Session | RnetSession) -> list[Segment]:
data = track.data["ism"]
segments: list[str] = data.get("segments") or []
manifest = data["manifest"]
timescale = int(manifest.get("TimeScale") or ISM_DEFAULT_TIMESCALE)
duration_ticks = int(manifest.get("Duration") or 0)
total_duration = (duration_ticks / timescale) if timescale else 0.0
return uniform_segments([(url, None) for url in segments], total_duration)
segments: list[str] = track.data["ism"].get("segments") or []
return uniform_segments([(url, None) for url in segments], manifest_seconds(track))
def extract_url(track: Track, session: Session | RnetSession, *, log: logging.Logger) -> list[Segment]:
@@ -371,13 +383,7 @@ def extract_url(track: Track, session: Session | RnetSession, *, log: logging.Lo
if mpd_duration:
duration = DASH.pt_to_sec(mpd_duration)
else:
ism_data = track.data.get("ism")
if ism_data and ism_data.get("manifest") is not None:
manifest = ism_data["manifest"]
timescale = int(manifest.get("TimeScale") or ISM_DEFAULT_TIMESCALE)
duration_ticks = int(manifest.get("Duration") or 0)
if timescale and duration_ticks:
duration = duration_ticks / timescale
duration = manifest_seconds(track)
if not duration or duration <= 0:
# Some services clear the manifest data after collapsing to a single