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
+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