feat(pipeline): 批量归档确认、字幕判重与综艺幂等收尾
- 批量归档拆 plan/execute,多目录一次确认;worker 按目录计数 - 字幕按已有 sidecar 判重,避免重抽 - 综艺按集号锁定、已规范不计入 rename,预览显示完整目标名 - 特典在整理阶段写 NFO(airs/display),归档后 Jellyfin 直接生效 - docs/rules/项目记忆同步
This commit is contained in:
+113
-34
@@ -201,12 +201,37 @@ class VarietyService:
|
||||
|
||||
# ---------------------------------------------------------------- 打分与分配
|
||||
|
||||
@staticmethod
|
||||
def _score(candidate: dict, file_name: str, hint: str | None, compact: str) -> int:
|
||||
# 归一化用:去掉分隔符与空白,方便做「集名 ↔ 文件名」重合判断
|
||||
_SEP_RE = re.compile(r"[\s._\-·::,,()()\[\]【】]+")
|
||||
_SEASON_WORD_RE = re.compile(r"第\s*[0-9一二三四五六七八九十]+\s*季")
|
||||
_DATE_DIGITS_RE = re.compile(r"(?:19|20)\d{2}[-._]?\d{2}[-._]?\d{2}")
|
||||
|
||||
@classmethod
|
||||
def _name_affinity(cls, episode_name: str, file_name: str) -> int:
|
||||
"""集名与文件名的重合度。同一天多集(先导片上/下/纯享)靠它区分。
|
||||
|
||||
没有这一项时,同日期候选打分完全并列,贪心分配会按遍历顺序乱点鸳鸯,
|
||||
于是「先导片上」被改名成「先导片纯享」。命中越高说明文件自称的期名对得上。
|
||||
"""
|
||||
ep = cls._SEP_RE.sub("", cls._SEASON_WORD_RE.sub("", episode_name or ""))
|
||||
if not ep:
|
||||
return 0
|
||||
file_part = cls._SEP_RE.sub("", cls._DATE_DIGITS_RE.sub("", file_name or ""))
|
||||
if not file_part:
|
||||
return 0
|
||||
if ep in file_part:
|
||||
return 6 # 集名完整出现在文件名里
|
||||
if len(file_part) >= 2 and file_part in ep:
|
||||
return 5 # 文件名是集名的一部分(集名带副标题)
|
||||
return 0
|
||||
|
||||
@classmethod
|
||||
def _score(cls, candidate: dict, file_name: str, hint: str | None, compact: str) -> int:
|
||||
score = 0
|
||||
episode_name = candidate["name"]
|
||||
if compact and compact in re.sub(r"[-.\s]", "", episode_name):
|
||||
score += 6 # TMDB 集名里直接写了这个日期
|
||||
score += cls._name_affinity(episode_name, file_name)
|
||||
file_special = bool(_SPECIAL_WORDS.search(file_name))
|
||||
ep_special = bool(_EP_NAME_SPECIAL.search(episode_name))
|
||||
if file_special and ep_special:
|
||||
@@ -247,8 +272,11 @@ class VarietyService:
|
||||
# ---------------------------------------------------------------- 主流程
|
||||
|
||||
@classmethod
|
||||
def plan(cls, folder: Path, show_id: int, show_name: str, season_years: dict, show_year: str = "") -> dict:
|
||||
def plan(cls, folder: Path, show_id: int, show_name: str, season_years: dict,
|
||||
show_year: str = "", media_title: str = "") -> dict:
|
||||
index = cls.build_index(show_id)
|
||||
# 文件名前缀用 media_title(可能与目录标题不同);不传就退回剧名
|
||||
title_for_name = media_title or show_name
|
||||
videos = [p for p in sorted(folder.rglob("*"))
|
||||
if p.is_file() and p.suffix.lower() in cls.VIDEO_EXTS
|
||||
and ".qkdownloading" not in p.name
|
||||
@@ -266,14 +294,21 @@ class VarietyService:
|
||||
for cand in candidates:
|
||||
ep_index[(cand["season"], cand["ep"])] = air
|
||||
|
||||
# 文件自称的集号:已经带 SxxEyy 的,分配时按集号锁定,不再重新打分。
|
||||
# 综艺同一天常有多集(先导片上/下/纯享),重新打分会让它们互相顶替,
|
||||
# 于是「已经整理好的目录再跑一次」反而改名。
|
||||
file_se: dict[Path, tuple[str, int]] = {}
|
||||
|
||||
for file in videos:
|
||||
date, complete = cls.extract_date(file.name)
|
||||
# 已有 SxxEyy 时以集号为准(文件名里的其它数字可能被误当成日期)
|
||||
match = re.search(r"S(\d{2})E(\d{1,4})", file.stem, re.I)
|
||||
if match:
|
||||
by_ep = ep_index.get((match.group(1), int(match.group(2))))
|
||||
key = (match.group(1), int(match.group(2)))
|
||||
by_ep = ep_index.get(key)
|
||||
if by_ep:
|
||||
date, complete = by_ep, True
|
||||
file_se[file] = key
|
||||
if date and not complete:
|
||||
hint = cls.season_hint(file.relative_to(folder)) or cls.season_hint(folder)
|
||||
year = season_years.get(hint or "", "")
|
||||
@@ -288,7 +323,10 @@ class VarietyService:
|
||||
groups[date].append(file)
|
||||
|
||||
plan = {"show_id": show_id, "show": show_name, "folder": folder, "show_year": show_year,
|
||||
"renames": [], "extras": [], "deletes": [], "undated": undated}
|
||||
"renames": [], "extras": [], "deletes": [], "undated": undated,
|
||||
# file → 目标路径(计划阶段算一次,预览/执行直接复用);
|
||||
# normalized = 目标路径与现路径完全一致、无需改动的条目数
|
||||
"targets": {}, "normalized": 0}
|
||||
|
||||
for date, files in sorted(groups.items()):
|
||||
candidates = index.get(date) or []
|
||||
@@ -298,31 +336,67 @@ class VarietyService:
|
||||
|
||||
hint = cls.season_hint(files[0].relative_to(folder)) or cls.season_hint(folder)
|
||||
|
||||
# 文件数多于候选数 → 同一天多份,按码率取前 N 个参与分配,
|
||||
# 其余:同期数的低码率版本删除,无法归期的进 extras(绝不静默丢弃)
|
||||
if len(files) > len(candidates):
|
||||
ranked = sorted(files, key=cls._quality, reverse=True)
|
||||
keep, surplus = ranked[:len(candidates)], ranked[len(candidates):]
|
||||
assigned, _ = cls._assign(keep, candidates, hint)
|
||||
kept_sigs = {cls._signature(f.name) for f in keep}
|
||||
for file in surplus:
|
||||
# 只有「期数 + 类型关键词」完全相同才算同一集的重复;
|
||||
# 加更 / 超前探班 / 正片 是不同内容,绝不能当重复删掉。
|
||||
if cls._signature(file.name) in kept_sigs:
|
||||
plan["deletes"].append((file, "同集高码率已保留,此为重复版本"))
|
||||
else:
|
||||
plan["extras"].append(file)
|
||||
else:
|
||||
assigned, leftover = cls._assign(files, candidates, hint)
|
||||
plan["extras"].extend(leftover)
|
||||
# ① 先按「文件自称的集号」锁定,一集只锁一个文件
|
||||
by_key = {(c["season"], c["ep"]): c for c in candidates}
|
||||
assigned: list[tuple[Path, dict]] = []
|
||||
pending: list[Path] = []
|
||||
taken: set[tuple[str, int]] = set()
|
||||
for file in files:
|
||||
key = file_se.get(file)
|
||||
if key in by_key and key not in taken:
|
||||
taken.add(key)
|
||||
assigned.append((file, by_key[key]))
|
||||
else:
|
||||
pending.append(file)
|
||||
rest = [c for c in candidates if (c["season"], c["ep"]) not in taken]
|
||||
|
||||
for file, candidate, _score in assigned:
|
||||
# ② 剩下的文件再按日期/关键词打分分配
|
||||
if pending and rest:
|
||||
if len(pending) > len(rest):
|
||||
ranked = sorted(pending, key=cls._quality, reverse=True)
|
||||
keep, surplus = ranked[:len(rest)], ranked[len(rest):]
|
||||
got, _ = cls._assign(keep, rest, hint)
|
||||
assigned.extend(got)
|
||||
# 已锁定的文件也算「已保留」,否则同集低码率副本不会被判重复
|
||||
kept_sigs = {cls._signature(item[0].name) for item in assigned}
|
||||
for file in surplus:
|
||||
# 只有「期数 + 类型关键词」完全相同才算同一集的重复;
|
||||
# 加更 / 超前探班 / 正片 是不同内容,绝不能当重复删掉。
|
||||
if cls._signature(file.name) in kept_sigs:
|
||||
plan["deletes"].append((file, "同集高码率已保留,此为重复版本"))
|
||||
else:
|
||||
plan["extras"].append(file)
|
||||
else:
|
||||
got, leftover = cls._assign(pending, rest, hint)
|
||||
assigned.extend(got)
|
||||
plan["extras"].extend(leftover)
|
||||
else:
|
||||
plan["extras"].extend(pending)
|
||||
|
||||
# ③ 目标名算一次:与现路径相同的直接记为「已规范」,不进变更清单。
|
||||
# 否则重跑已整理目录会报出上百条「改名」,确认后却一条也不改。
|
||||
# assigned 里既有锁定的 (file, cand),也有 _assign 返回的 (file, cand, score)
|
||||
for file, candidate, *_score in assigned:
|
||||
target = cls._target_path(folder, file, candidate, title_for_name, season_years)
|
||||
if target == file:
|
||||
plan["normalized"] += 1
|
||||
continue
|
||||
plan["targets"][file] = target
|
||||
plan["renames"].append((file, candidate))
|
||||
|
||||
# 无日期的一律进 extras(无法定位)
|
||||
plan["extras"].extend(plan.pop("undated"))
|
||||
return plan
|
||||
|
||||
@classmethod
|
||||
def _target_path(cls, folder: Path, file: Path, candidate: dict,
|
||||
media_title: str, season_years: dict) -> Path:
|
||||
"""按 TMDB 季号归位后的目标路径(特典进 Season 00,正片进 Season XX)。"""
|
||||
name = cls._target_name(file, candidate, media_title, season_years)
|
||||
if candidate["season"]:
|
||||
return folder / f"Season {candidate['season']}" / name
|
||||
return file.with_name(name)
|
||||
|
||||
@staticmethod
|
||||
def _quality(path: Path) -> tuple:
|
||||
tags = MediaInfoService.get_scene_tags(path)
|
||||
@@ -372,17 +446,22 @@ class VarietyService:
|
||||
def preview(cls, plan: dict, season_years: dict, media_title: str) -> None:
|
||||
log.info("=" * 60)
|
||||
log.info(f"📺 综艺整理:{plan['show']}(tv/{plan['show_id']})")
|
||||
log.info(f" 改名 {len(plan['renames'])} | 进 extras {len(plan['extras'])} | 删除 {len(plan['deletes'])}")
|
||||
extra = f" | 已规范 {plan['normalized']}" if plan.get("normalized") else ""
|
||||
log.info(f" 改名 {len(plan['renames'])} | 进 extras {len(plan['extras'])} | "
|
||||
f"删除 {len(plan['deletes'])}{extra}")
|
||||
targets = plan.get("targets") or {}
|
||||
for file, candidate in plan["renames"][:8]:
|
||||
target = cls._target_name(file, candidate, media_title, season_years)
|
||||
log.info(f" [改名] {file.name[:46]}")
|
||||
log.info(f" → {target[:80]}")
|
||||
target = targets.get(file)
|
||||
if target is None:
|
||||
target = cls._target_path(plan["folder"], file, candidate, media_title, season_years)
|
||||
log.info(f" [改名] {file.name}")
|
||||
log.info(f" → {target.name}")
|
||||
if len(plan["renames"]) > 8:
|
||||
log.info(f" …… 其余 {len(plan['renames']) - 8} 个改名略")
|
||||
for file in plan["extras"][:5]:
|
||||
log.info(f" [extras] {file.name[:60]}")
|
||||
log.info(f" [extras] {file.name}")
|
||||
for file, reason in plan["deletes"][:5]:
|
||||
log.info(f" [删除] {file.name[:60]} ← {reason}")
|
||||
log.info(f" [删除] {file.name} ← {reason}")
|
||||
log.info("=" * 60)
|
||||
|
||||
@classmethod
|
||||
@@ -463,12 +542,12 @@ class VarietyService:
|
||||
extras_dir = folder / "extras"
|
||||
stats = {"renamed": 0, "extras": 0, "deleted": 0, "skipped": 0, "sidecars": 0}
|
||||
|
||||
targets = plan.get("targets") or {}
|
||||
for file, candidate in plan["renames"]:
|
||||
target = file.with_name(cls._target_name(file, candidate, media_title, season_years))
|
||||
# 一律按 TMDB 季号归位:特典进 Season 00,正片进 Season XX。
|
||||
# 不这样做的话,「源文件本来就在 Season 04」的特典会一直留在 Season 04。
|
||||
if candidate["season"]:
|
||||
target = folder / f"Season {candidate['season']}" / target.name
|
||||
# 目标路径在 plan 阶段已算好(含「一律按 TMDB 季号归位」),这里直接复用
|
||||
target = targets.get(file)
|
||||
if target is None:
|
||||
target = cls._target_path(folder, file, candidate, media_title, season_years)
|
||||
if target == file:
|
||||
cls._follow_sidecars(file, target)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user