feat(subtitle): 文本字幕外置化仅限 TRANSCODE 源;右键菜单合并为级联子菜单

- 内嵌 ASS 抽取与字体抽取挂到 verdict == TRANSCODE(extract-subs /
  --extract-internal 仍可强制),直传片源不再生成外挂副本
- 外挂 SRT 兜底仅在 [Subtitles] export 不含 ass 时启用,避免同语言双份
- 新增同语言判重(含旧命名 <stem>.<lang>.srt),重复抽取不再产生重复字幕
- 识别中英双语轨为 zh-en,语言白名单加入 zh-en
- 外挂字幕统一 <stem>.internal.<lang>.srt 命名
- 右键菜单合并为 SceneMarshal 级联子菜单(ExtendedSubCommandsKey),
  四项:媒体整理 / 媒体归档 / 补外挂字幕 / 入库校验,图标 assets/menu 自绘
- HANDOFF 补右键菜单说明;新增 docs/movie-duplicates.md 记录电影库清理
This commit is contained in:
ali
2026-11-19 13:55:33 +08:00
parent fe5e211222
commit a57a392d7a
17 changed files with 375 additions and 67 deletions
+52 -16
View File
@@ -92,6 +92,15 @@ class SubtitleService:
lang = stream.get('tags', {}).get('language', 'und').lower()
title = stream.get('tags', {}).get('title', '').lower()
# 中英双语轨:标题里同时出现中文与英文标识(或直接写"双语")时优先判定,
# 否则会被下面的"简/繁"规则吞成单语。输出 zh-en,与 zh-CN / zh-TW 区分。
has_cn = any(k in title for k in ('中', '简', '繁', 'chs', 'chi', 'zho'))
has_en = any(k in title for k in ('英', 'eng', 'english'))
if '双语' in title or 'bilingual' in title or (has_cn and has_en):
return 'zh-en'
if has_en and not has_cn:
return 'en'
lang_code = "und"
if lang in ['chi', 'zho', 'zh', 'chi-cn', 'zh-cn']: lang_code = 'zh-CN'
elif lang in ['tw', 'hk', 'zh-tw']: lang_code = 'zh-TW'
@@ -99,8 +108,12 @@ class SubtitleService:
elif lang in ['jpn', 'ja']: lang_code = 'ja'
elif lang in ['kor', 'ko']: lang_code = 'ko'
if '繁' in title or 'tw' in title or 'hk' in title or 'cht' in title: lang_code = 'zh-TW'
elif '简' in title or 'cn' in title or '双语' in title or '中' in title or 'chs' in title: lang_code = 'zh-CN'
if ('繁' in title or 'traditional' in title or '传统' in title or '正体' in title
or 'tw' in title or 'hk' in title or 'cht' in title):
lang_code = 'zh-TW'
elif ('简' in title or 'simplified' in title or 'cn' in title
or '中' in title or 'chs' in title):
lang_code = 'zh-CN'
elif '英' in title and lang_code == 'und': lang_code = 'en'
elif '中' in title and lang_code == 'und': lang_code = 'zh-CN'
@@ -149,12 +162,34 @@ class SubtitleService:
log.warning(f"⚠️ 内置字幕扫描失败: {file_path.name} -> {e}")
return queued
@staticmethod
def _existing_sidecar(dest_dir: Path, target_stem: str, lang: str, suffix: str) -> Path | None:
"""同语言外挂字幕是否已存在(含早期不带 .internal 的命名与其它封装格式)。
老库里的字幕多为 <stem>.<lang>.srt(旧命名),若不认这些,重复抽取会在
同一语言下再生成 .internal.<lang>.ass,Jellyfin 里就会出现两条同语言字幕。
"""
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:
return p
return None
@staticmethod
def _queue_ass(file_path, task_mgr, target_stem, dest_dir, stream_idx, codec, lang_code, suffix) -> int:
out_ass = dest_dir / f"{target_stem}.internal.{lang_code}{suffix}.ass"
if out_ass.exists() and out_ass.stat().st_size > 0:
log.debug(f"✨ [跳过导出] 目标内置 ASS 已存在: {out_ass.name}")
return 1
existing = SubtitleService._existing_sidecar(dest_dir, target_stem, lang_code, suffix)
if existing is not None:
log.debug(f"✨ [跳过导出] 同语言外挂字幕已存在: {existing.name}")
return 1
temp_ass = dest_dir / f".scenemarshal-{uuid4().hex[:12]}.ass"
task_mgr.add_extract(
@@ -171,6 +206,9 @@ 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:
log.debug(f"✨ [跳过提取] 同语言外挂字幕已存在: {target_stem}.{lang_code}{suffix}")
return 1
temp_ass = dest_dir / f".scenemarshal-{uuid4().hex[:12]}.ass"
task_mgr.add_extract(
@@ -297,20 +335,12 @@ class SubtitleService:
def export_text_sidecars(file_path: Path, dest_dir: Path, out_stem: str) -> int:
"""给必须转码的片源补外挂 SRT(客户端本地渲染,避开内嵌字幕投递)。
- 已有同名外挂字幕(srt/ass/ssa)时整体跳过,避免覆盖用户的字幕;
- 按轨逐条补:只导出目录里还缺的语言,已有的不动(便于老库补齐,
例如后来才发现的中英双语轨);
- 只处理文本字幕轨,图形字幕(PGS/DVD/DVB)跳过;
- 命名 <out_stem>.<lang>.srt,已存在则跳过。
- 命名 <out_stem>.internal.<lang>.srt,与内嵌抽取共用 .internal 标记;
中英双语轨出 <out_stem>.internal.zh-en.srt。
"""
prefix = f"{out_stem}."
try:
for existing in dest_dir.iterdir():
if (existing.is_file() and existing.name.startswith(prefix)
and existing.suffix.lower() in {'.srt', '.ass', '.ssa'}):
log.debug(f"✨ [跳过外挂字幕] 已存在: {existing.name}")
return 0
except OSError:
pass
data = FFprobeUtil.get_json(file_path, "-show_streams", "-select_streams", "s")
if not data:
return 0
@@ -323,11 +353,17 @@ class SubtitleService:
continue
lang = SubtitleService._lang_code(s)
# 与内嵌抽取共用一份语言白名单,避免把马来语/泰米尔语这类杂轨也导出来
if config.SUBTITLE_LANGUAGES and lang not in config.SUBTITLE_LANGUAGES:
continue
lang_counts[lang] = lang_counts.get(lang, 0) + 1
suffix = "" if lang_counts[lang] == 1 else f".{lang_counts[lang]}"
out_srt = dest_dir / f"{out_stem}.{lang}{suffix}.srt"
out_srt = dest_dir / f"{out_stem}.internal.{lang}{suffix}.srt"
# 早期版本不带 .internal 标记,同语言外挂已存在时同样视为已补齐,避免重复
legacy_srt = dest_dir / f"{out_stem}.{lang}{suffix}.srt"
if out_srt.exists() and out_srt.stat().st_size > 0:
if (out_srt.exists() and out_srt.stat().st_size > 0) or (
legacy_srt.exists() and legacy_srt.stat().st_size > 0):
continue
temp = dest_dir / f".scenemarshal-{uuid4().hex[:12]}.srt"