feat(pipeline): 批量归档确认、字幕判重与综艺幂等收尾
- 批量归档拆 plan/execute,多目录一次确认;worker 按目录计数 - 字幕按已有 sidecar 判重,避免重抽 - 综艺按集号锁定、已规范不计入 rename,预览显示完整目标名 - 特典在整理阶段写 NFO(airs/display),归档后 Jellyfin 直接生效 - docs/rules/项目记忆同步
This commit is contained in:
+91
-21
@@ -73,14 +73,49 @@ class SubtitleService:
|
||||
return removed
|
||||
|
||||
@staticmethod
|
||||
def has_internal_sub(dest_dir: Path, target_stem: str) -> bool:
|
||||
"""目标目录里是否已有该集的内嵌字幕(ass 或 srt)。"""
|
||||
prefix = f"{target_stem}.internal."
|
||||
def _video_stems(file_path, target_stem: str) -> list[str]:
|
||||
"""该视频可能对应的文件名前缀:目标名 + 磁盘上的现用名。
|
||||
|
||||
流水线是「先抽字幕(阶段 3)、后改名(阶段 5)」,所以判断「已有字幕」时
|
||||
磁盘上的 sidecar 往往还挂在旧名下。只认 target_stem 会把旧 name 的
|
||||
`.internal.zh-CN.ass` 当成不存在,于是整条轨白抽一遍。
|
||||
"""
|
||||
stems = [target_stem]
|
||||
source_stem = getattr(file_path, "stem", "")
|
||||
if source_stem and source_stem != target_stem:
|
||||
stems.append(source_stem)
|
||||
return stems
|
||||
|
||||
# 同一语言的不同写法归一化:chi / zho-Hans / zh_cn / zh-CN 视为中文简体
|
||||
_LANG_CANON = {
|
||||
'chi': 'zh-cn', 'zho': 'zh-cn', 'zh': 'zh-cn', 'chs': 'zh-cn', 'sc': 'zh-cn',
|
||||
'zh-cn': 'zh-cn', 'zh-hans': 'zh-cn', 'zho-hans': 'zh-cn', 'zh-hans-cn': 'zh-cn',
|
||||
'cht': 'zh-tw', 'tc': 'zh-tw', 'zh-tw': 'zh-tw', 'zh-hant': 'zh-tw',
|
||||
'zho-hant': 'zh-tw', 'zh-hk': 'zh-tw',
|
||||
'zh-en': 'zh-en', 'zho-en': 'zh-en',
|
||||
'eng': 'en', 'en': 'en', 'jpn': 'ja', 'ja': 'ja', 'jp': 'ja',
|
||||
'kor': 'ko', 'ko': 'ko',
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _norm_lang(code: str) -> str:
|
||||
key = (code or "").strip().lower().replace("_", "-")
|
||||
return SubtitleService._LANG_CANON.get(key, key)
|
||||
|
||||
@staticmethod
|
||||
def _sidecar_lang(asset: Path) -> str:
|
||||
match = config.LANG_TAIL_PATTERN.search(asset.stem)
|
||||
return match.group(0).lstrip(".-_") if match else ""
|
||||
|
||||
@staticmethod
|
||||
def has_internal_sub(dest_dir: Path, target_stem: str, file_path=None) -> bool:
|
||||
"""目标目录里是否已有该视频的内嵌字幕(ass 或 srt)。"""
|
||||
stems = SubtitleService._video_stems(file_path, target_stem)
|
||||
try:
|
||||
for p in dest_dir.iterdir():
|
||||
if not p.is_file() or not p.name.startswith(prefix):
|
||||
if not p.is_file() or p.suffix.lower() not in {'.ass', '.srt'}:
|
||||
continue
|
||||
if p.suffix.lower() in {'.ass', '.srt'}:
|
||||
if any(p.name.startswith(f"{stem}.internal.") for stem in stems):
|
||||
return True
|
||||
except OSError:
|
||||
pass
|
||||
@@ -157,7 +192,7 @@ class SubtitleService:
|
||||
|
||||
if want_ass:
|
||||
present, spec = SubtitleService._plan_ass(
|
||||
target_stem, dest_dir, stream_idx, codec, lang_code, suffix)
|
||||
file_path, target_stem, dest_dir, stream_idx, codec, lang_code, suffix)
|
||||
if spec is not None:
|
||||
ass_specs.append(spec)
|
||||
if present or spec is not None:
|
||||
@@ -173,25 +208,58 @@ class SubtitleService:
|
||||
return queued
|
||||
|
||||
@staticmethod
|
||||
def _existing_sidecar(dest_dir: Path, target_stem: str, lang: str, suffix: str) -> Path | None:
|
||||
"""同语言外挂字幕是否已存在(含早期不带 .internal 的命名与其它封装格式)。
|
||||
def _existing_sidecar(file_path, dest_dir: Path, target_stem: str,
|
||||
lang: str, suffix: str) -> Path | None:
|
||||
"""同语言外挂字幕是否已存在(认旧名、旧命名、语言码异写)。
|
||||
|
||||
老库里的字幕多为 <stem>.<lang>.srt(旧命名),若不认这些,重复抽取会在
|
||||
同一语言下再生成 .internal.<lang>.ass,Jellyfin 里就会出现两条同语言字幕。
|
||||
三层判断,缺一层就会重复抽取:
|
||||
1. 精确文件名(含 `.2` 这类同语言多轨后缀);
|
||||
2. 目标名 / 源名两种前缀(整理先抽后改名,磁盘上常还是旧名);
|
||||
3. 语言尾归一化(`chi`、`zho-Hans`、`zh_cn` 都算 `zh-CN`),且目录里
|
||||
只有一个视频时允许前缀完全对不上(片名与文件名不一致的老库)。
|
||||
"""
|
||||
for name in (
|
||||
f"{target_stem}.internal.{lang}{suffix}.ass",
|
||||
f"{target_stem}.internal.{lang}{suffix}.srt",
|
||||
f"{target_stem}.{lang}{suffix}.srt",
|
||||
f"{target_stem}.{lang}{suffix}.ass",
|
||||
):
|
||||
p = dest_dir / name
|
||||
if p.exists() and p.stat().st_size > 0:
|
||||
stems = SubtitleService._video_stems(file_path, target_stem)
|
||||
want = SubtitleService._norm_lang(lang)
|
||||
|
||||
for stem in stems:
|
||||
for name in (
|
||||
f"{stem}.internal.{lang}{suffix}.ass",
|
||||
f"{stem}.internal.{lang}{suffix}.srt",
|
||||
f"{stem}.{lang}{suffix}.srt",
|
||||
f"{stem}.{lang}{suffix}.ass",
|
||||
):
|
||||
p = dest_dir / name
|
||||
if p.exists() and p.stat().st_size > 0:
|
||||
return p
|
||||
|
||||
# `.2` / `.3` 是同语言多轨的序号,语言层去重只对第一条轨生效
|
||||
if suffix:
|
||||
return None
|
||||
|
||||
try:
|
||||
entries = [p for p in dest_dir.iterdir() if p.is_file()]
|
||||
videos = [p for p in entries if p.suffix.lower() in config.SUPPORTED_EXTS]
|
||||
single_video = len(videos) == 1
|
||||
except OSError:
|
||||
return None
|
||||
|
||||
for p in entries:
|
||||
if p.suffix.lower() not in {'.ass', '.srt'} or p.name.startswith('.'):
|
||||
continue
|
||||
belongs = any(p.name.startswith(f"{stem}.") for stem in stems)
|
||||
if not belongs:
|
||||
if not single_video:
|
||||
continue
|
||||
if p.stem.strip().lower() in config.RESERVED_SIDECAR_STEMS:
|
||||
continue
|
||||
token = SubtitleService._sidecar_lang(p)
|
||||
if token and SubtitleService._norm_lang(token) == want:
|
||||
log.debug(f"✨ [跳过导出] 已有同语言外挂字幕: {p.name}")
|
||||
return p
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _plan_ass(target_stem, dest_dir, stream_idx, codec, lang_code, suffix):
|
||||
def _plan_ass(file_path, target_stem, dest_dir, stream_idx, codec, lang_code, suffix):
|
||||
"""判断该 ASS 轨是否还要抽;返回 (已有外挂副本, 待抽 spec)。
|
||||
|
||||
已有(含旧命名)只计数、不排队;待抽 spec 由 _queue_ass_exports
|
||||
@@ -201,7 +269,8 @@ class SubtitleService:
|
||||
if out_ass.exists() and out_ass.stat().st_size > 0:
|
||||
log.debug(f"✨ [跳过导出] 目标内置 ASS 已存在: {out_ass.name}")
|
||||
return True, None
|
||||
existing = SubtitleService._existing_sidecar(dest_dir, target_stem, lang_code, suffix)
|
||||
existing = SubtitleService._existing_sidecar(
|
||||
file_path, dest_dir, target_stem, lang_code, suffix)
|
||||
if existing is not None:
|
||||
log.debug(f"✨ [跳过导出] 同语言外挂字幕已存在: {existing.name}")
|
||||
return True, None
|
||||
@@ -245,7 +314,8 @@ class SubtitleService:
|
||||
log.debug(f"✨ [跳过提取] 目标内置字幕已存在: {out_srt.name}")
|
||||
SubtitleService._drop_external_copies(out_srt)
|
||||
return 1
|
||||
if SubtitleService._existing_sidecar(dest_dir, target_stem, lang_code, suffix) is not None:
|
||||
if SubtitleService._existing_sidecar(
|
||||
file_path, dest_dir, target_stem, lang_code, suffix) is not None:
|
||||
log.debug(f"✨ [跳过提取] 同语言外挂字幕已存在: {target_stem}.{lang_code}{suffix}")
|
||||
return 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user