This commit is contained in:
2026-01-05 07:51:19 -08:00
parent 8545367e28
commit 1f765cffda
32 changed files with 3447 additions and 3250 deletions

View File

@@ -29,6 +29,9 @@ from SYS.models import (
from SYS.pipeline_progress import PipelineProgress
from SYS.utils import ensure_directory, sha256_file
_YTDLP_TRANSFER_STATE: Dict[str, Dict[str, Any]] = {}
try:
import yt_dlp # type: ignore
from yt_dlp.extractor import gen_extractors # type: ignore
@@ -565,9 +568,35 @@ class YtDlpTool:
# Progress + utility helpers for yt-dlp driven downloads (previously in cmdlet/download_media).
_YTDLP_PROGRESS_BAR = ProgressBar()
_YTDLP_TRANSFER_STATE: Dict[str, Dict[str, Any]] = {}
_SUBTITLE_EXTS = (".vtt", ".srt", ".ass", ".ssa", ".lrc")
def _progress_label(status: Dict[str, Any]) -> str:
info_dict = status.get("info_dict") if isinstance(status.get("info_dict"), dict) else {}
candidates = [
status.get("filename"),
info_dict.get("_filename"),
info_dict.get("filepath"),
info_dict.get("title"),
info_dict.get("id"),
]
for cand in candidates:
if not cand:
continue
try:
name = Path(str(cand)).name
except Exception:
name = str(cand)
label = str(name or "").strip()
if label:
return label
return "download"
def _live_ui_and_pipe_index() -> tuple[Optional[Any], int]:
ui = None
try:
@@ -937,19 +966,53 @@ def _extract_sha256(info: Dict[str, Any]) -> Optional[str]:
def _progress_callback(status: Dict[str, Any]) -> None:
label = _progress_label(status)
event = status.get("status")
if event == "downloading":
downloaded = status.get("downloaded_bytes")
total = status.get("total_bytes") or status.get("total_bytes_estimate")
downloaded = status.get("downloaded_bytes")
total = status.get("total_bytes") or status.get("total_bytes_estimate")
_YTDLP_PROGRESS_BAR.update(
downloaded=int(downloaded) if downloaded is not None else None,
total=int(total) if total is not None else None,
label="download",
file=sys.stderr,
)
pipeline = PipelineProgress(pipeline_context)
live_ui, _ = pipeline.ui_and_pipe_index()
use_live = live_ui is not None
def _total_bytes(value: Any) -> Optional[int]:
try:
if isinstance(value, (int, float)) and value > 0:
return int(value)
except Exception:
pass
return None
if event == "downloading":
if use_live:
try:
if not _YTDLP_TRANSFER_STATE.get(label, {}).get("started"):
pipeline.begin_transfer(label=label, total=_total_bytes(total))
_YTDLP_TRANSFER_STATE[label] = {"started": True}
pipeline.update_transfer(
label=label,
completed=int(downloaded) if downloaded is not None else None,
total=_total_bytes(total),
)
except Exception:
pass
else:
_YTDLP_PROGRESS_BAR.update(
downloaded=int(downloaded) if downloaded is not None else None,
total=int(total) if total is not None else None,
label=label,
file=sys.stderr,
)
elif event == "finished":
_YTDLP_PROGRESS_BAR.finish()
if use_live:
try:
if _YTDLP_TRANSFER_STATE.get(label, {}).get("started"):
pipeline.finish_transfer(label=label)
except Exception:
pass
_YTDLP_TRANSFER_STATE.pop(label, None)
else:
_YTDLP_PROGRESS_BAR.finish()
elif event in ("postprocessing", "processing"):
return