fix(downloads): a YouTube clip outside the catalog takes its title from the fetch

It was queued as "Clip (clip)" and stayed so: nothing renamed it once yt-dlp had answered.
This commit is contained in:
2026-10-10 02:43:13 +02:00
parent 14598ec320
commit d466ae915b
3 changed files with 44 additions and 13 deletions
+21 -9
View File
@@ -907,9 +907,12 @@ def _process_edit(job_id: int, asset_id: int) -> None:
shutil.rmtree(staging, ignore_errors=True)
def _clip_source(job_id: int, asset_id: int, spec: dict, staging: Path) -> tuple[Path, float]:
"""The file a clip is cut from and where in it the clip starts. A YouTube clip fetches ONLY its
range into `staging` (so it starts at 0 there); a Plex clip reads the library file in place."""
def _clip_source(
job_id: int, asset_id: int, spec: dict, staging: Path
) -> tuple[Path, float, str | None]:
"""The file a clip is cut from, where in it the clip starts, and the source's title when only
the fetch could tell it. A YouTube clip fetches ONLY its range into `staging` (so it starts at
0 there); a Plex clip reads the library file in place."""
if spec["source"] == "youtube":
with SessionLocal() as db:
job = db.get(DownloadJob, job_id)
@@ -929,7 +932,9 @@ def _clip_source(job_id: int, asset_id: int, spec: dict, staging: Path) -> tuple
max_bytes=remaining,
section=(spec["start_s"], spec["end_s"]),
)
return _produced_file(_media_info(dict(info)), staging), 0.0
info = _media_info(dict(info))
title = normalize_title(adapters.resolve_adapter("youtube", spec["ref"]).title(info))
return _produced_file(info, staging), 0.0, title
with SessionLocal() as db:
item = db.execute(
select(PlexItem).where(PlexItem.rating_key == spec["ref"])
@@ -937,7 +942,7 @@ def _clip_source(job_id: int, asset_id: int, spec: dict, staging: Path) -> tuple
src = plex_paths.local_media_path(db, item.file_path) if item else None
if src is None:
raise StatedFailure("The library file is not reachable from here.")
return src, spec["start_s"]
return src, spec["start_s"], None
def _process_clip(job_id: int, asset_id: int) -> None:
@@ -954,7 +959,7 @@ def _process_clip(job_id: int, asset_id: int) -> None:
if staging.exists():
shutil.rmtree(staging, ignore_errors=True)
staging.mkdir(parents=True, exist_ok=True)
src, start = _clip_source(job_id, asset_id, spec, staging)
src, start, fetched_title = _clip_source(job_id, asset_id, spec, staging)
data = probe.probe_json(src) or {}
audio_track = spec.get("audio_track")
if audio_track is not None and audio_track >= clipmod.audio_track_count(data):
@@ -1008,10 +1013,17 @@ def _process_clip(job_id: int, asset_id: int) -> None:
asset.error = None
asset.last_access_at = datetime.now(timezone.utc)
asset.expires_at = datetime.now(timezone.utc) + timedelta(days=retention_days)
fields: dict = {"status": JobStatus.DONE, "progress": 100, "phase": None}
# A video outside the catalog was queued as "Clip (clip)": the fetch has its title now.
placeholder = service.clip_title(None, None)
if asset and fetched_title and asset.title == placeholder:
named = service.clip_title(fetched_title, None)
asset.title = named
if job_row := db.get(DownloadJob, job_id):
if job_row.display_name == placeholder:
fields["display_name"] = named[:DISPLAY_TEXT_MAX]
title = asset.title if asset else None
finalized = service.finalize_if_running(
db, job_id, status=JobStatus.DONE, progress=100, phase=None
)
finalized = service.finalize_if_running(db, job_id, **fields)
db.commit()
if finalized:
_notify_download_done(db, user_id, title, job_id, kind="edit")