feat(subtitle): 抽出 mkv 内嵌字体附件
- [Subtitles] extract_fonts / fonts_dir 控制开关与目录(默认 true / fonts) - 按附件顺序用 mkvextract 抽取,保留原始文件名;已存在则跳过(幂等) (ffmpeg -dump_attachment 会读完整个文件,1.1G 的集要几分钟;mkvextract 0.2s) - 字体落在每季的 <Season>/fonts/ 目录,供 mpv/VLC 等支持 libass 的播放器加载
This commit is contained in:
@@ -49,6 +49,10 @@ languages = zh-CN,zh-TW,en,und
|
||||
codecs = ass,ssa,webvtt,subrip,mov_text
|
||||
# 内嵌优先:有内嵌字幕时不再处理外挂字幕
|
||||
prefer_internal = true
|
||||
# 是否把 mkv 内嵌的字体附件一起抽出来(ASS 特效字体还原)
|
||||
extract_fonts = true
|
||||
# 字体存放的子目录(相对每季目录)
|
||||
fonts_dir = fonts
|
||||
|
||||
[Features]
|
||||
include_tmdb_id = true
|
||||
@@ -140,6 +144,8 @@ else:
|
||||
'Features', 'extract_subtitle_languages', fallback='all')
|
||||
|
||||
PREFER_INTERNAL = _parser.getboolean('Subtitles', 'prefer_internal', fallback=True)
|
||||
EXTRACT_FONTS = _parser.getboolean('Subtitles', 'extract_fonts', fallback=True)
|
||||
FONTS_DIR_NAME = _parser.get('Subtitles', 'fonts_dir', fallback='fonts').strip() or 'fonts'
|
||||
|
||||
# 5. 功能开关
|
||||
INCLUDE_TMDB_ID = _parser.getboolean('Features', 'include_tmdb_id', fallback=True)
|
||||
|
||||
@@ -141,6 +141,10 @@ class MediaPipeline:
|
||||
if (not has_ext_sub or self.args.extract_internal or self.args.mode == "extract-subs") and (
|
||||
not skip_heavy or self.args.extract_internal or self.args.mode == "extract-subs"):
|
||||
SubtitleService.scan_subtitles(f, self.task_mgr, target_stem, dest_dir)
|
||||
|
||||
# 内嵌字体附件(ASS 特效字体还原)
|
||||
if config.EXTRACT_FONTS:
|
||||
SubtitleService.scan_fonts(f, self.task_mgr, dest_dir)
|
||||
|
||||
if self.args.mode != "extract-subs" and not skip_heavy:
|
||||
MetadataService.scan_ads(f, self.task_mgr)
|
||||
|
||||
@@ -177,6 +177,63 @@ class SubtitleService:
|
||||
)
|
||||
return 1
|
||||
|
||||
@staticmethod
|
||||
def scan_fonts(file_path: Path, task_mgr, dest_dir: Path) -> int:
|
||||
"""把 mkv 内嵌字体附件抽到 dest_dir/<fonts_dir>/,返回排队/已存在数量。"""
|
||||
if not config.EXTRACT_FONTS:
|
||||
return 0
|
||||
data = FFprobeUtil.get_json(file_path, "-show_streams", "-select_streams", "t")
|
||||
if not data:
|
||||
return 0
|
||||
|
||||
fonts_dir = dest_dir / config.FONTS_DIR_NAME
|
||||
queued = 0
|
||||
for ordinal, s in enumerate(data.get('streams', []), 1):
|
||||
if s.get('codec_type') != 'attachment':
|
||||
continue
|
||||
tags = s.get('tags', {}) or {}
|
||||
raw_name = tags.get('filename') or tags.get('title') or f"attachment-{ordinal}.ttf"
|
||||
safe_name = Path(str(raw_name).replace('\\', '/')).name.strip() or f"attachment-{ordinal}.ttf"
|
||||
target = fonts_dir / safe_name
|
||||
|
||||
if target.exists() and target.stat().st_size > 0:
|
||||
log.debug(f"✨ [跳过字体] 已存在: {safe_name}")
|
||||
queued += 1
|
||||
continue
|
||||
|
||||
task_mgr.add_extract(
|
||||
f"[导出内嵌字体] {file_path.name} ({safe_name})",
|
||||
SubtitleService.do_dump_font,
|
||||
(file_path, ordinal, target),
|
||||
)
|
||||
queued += 1
|
||||
return queued
|
||||
|
||||
@staticmethod
|
||||
def do_dump_font(file_path, ordinal, target):
|
||||
"""用 mkvextract 抽取字体附件(比 ffmpeg -dump_attachment 快得多)。"""
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
temp = target.with_name(f".{target.stem}.part{target.suffix}")
|
||||
temp.unlink(missing_ok=True)
|
||||
|
||||
cmd = ["mkvextract", "attachments", str(file_path), f"{ordinal}:{temp}"]
|
||||
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
text=True, errors="ignore")
|
||||
|
||||
if res.returncode == 0 and temp.exists() and temp.stat().st_size > 0:
|
||||
try:
|
||||
temp.replace(target)
|
||||
log.debug(f"🔤 已导出内嵌字体: {target.name}")
|
||||
except OSError as e:
|
||||
log.warning(f"⚠️ 字体重命名失败: {target.name} -> {e}")
|
||||
temp.unlink(missing_ok=True)
|
||||
else:
|
||||
tail = [line for line in (res.stderr or "").strip().splitlines() if line.strip()]
|
||||
detail = tail[-1][:200] if tail else ""
|
||||
log.warning(f"⚠️ 内嵌字体导出失败: {file_path.name} #{ordinal} "
|
||||
f"mkvextract={res.returncode} {detail}")
|
||||
temp.unlink(missing_ok=True)
|
||||
|
||||
# ------------------------------------------------------------------ 执行
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user