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:
@@ -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