fix(subtitle): 内置字幕临时文件改用短随机名,避免超长标题触发 ENAMETOOLONG

临时 ass 原名拼成 <目标名>.temp.<目标名>.ass,日文长标题下 UTF-8 字节数
轻松超过 255 触发 [Errno 36] File name too long,导致所有内置提取任务失败。
现改为 .scenerename-<12位hex>.ass;同时 do_extract 捕获 ffmpeg stderr 并
在失败时记录原因,scan_subtitles 的异常不再静默吞掉。
This commit is contained in:
ali
2026-11-17 12:47:38 +08:00
parent cf2e4e66d7
commit 6720dc8ef3
+15 -5
View File
@@ -3,6 +3,8 @@
import re import re
import subprocess import subprocess
from pathlib import Path from pathlib import Path
from uuid import uuid4
import pysubs2 import pysubs2
from core import config from core import config
from core.logger import log from core.logger import log
@@ -73,20 +75,22 @@ class SubtitleService:
out_srt = task_mgr.reserve_path(desired_out_srt) out_srt = task_mgr.reserve_path(desired_out_srt)
temp_ass = dest_dir / f"{target_stem}.temp.{out_srt.name}.ass" # ⚠️ 临时文件必须短:用 <目标名>.temp.<目标名>.ass 这种拼法在长标题(尤其日文/多字节)下
# 会超过 Linux 的 255 字节文件名上限,导致 [Errno 36] File name too long。
temp_ass = dest_dir / f".scenerename-{uuid4().hex[:12]}.ass"
task_mgr.add_extract( task_mgr.add_extract(
f"[提取内置字幕] {file_path.name} (轨道 {stream_idx}) --> {out_srt.name}", f"[提取内置字幕] {file_path.name} (轨道 {stream_idx}) --> {out_srt.name}",
SubtitleService.do_extract, SubtitleService.do_extract,
(file_path, stream_idx, temp_ass, out_srt) (file_path, stream_idx, temp_ass, out_srt)
) )
except Exception: except Exception as e:
pass log.warning(f"⚠️ 内置字幕扫描失败: {file_path.name} -> {e}")
@staticmethod @staticmethod
def do_extract(file_path, stream_index, temp_ass, out_srt): def do_extract(file_path, stream_index, temp_ass, out_srt):
cmd = ["ffmpeg", "-i", str(file_path), "-map", f"0:{stream_index}", "-c:s", "ass", "-f", "ass", "-y", str(temp_ass)] cmd = ["ffmpeg", "-i", str(file_path), "-map", f"0:{stream_index}", "-c:s", "ass", "-f", "ass", "-y", str(temp_ass)]
res = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) res = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, errors="ignore")
if res.returncode == 0 and temp_ass.exists() and temp_ass.stat().st_size > 0: if res.returncode == 0 and temp_ass.exists() and temp_ass.stat().st_size > 0:
try: try:
@@ -95,9 +99,15 @@ class SubtitleService:
line.text = re.sub(r'\{.*?\}', '', line.text) line.text = re.sub(r'\{.*?\}', '', line.text)
subs.save(str(out_srt), format="srt", encoding="utf-8") subs.save(str(out_srt), format="srt", encoding="utf-8")
except Exception as e: except Exception as e:
print(f"\n⚠️ 字幕 {out_srt.name} 洗白失败,已跳过。报错信息: {e}") log.warning(f"⚠️ 字幕 {out_srt.name} 洗白失败,已跳过。报错信息: {e}")
finally: finally:
temp_ass.unlink(missing_ok=True) temp_ass.unlink(missing_ok=True)
else: else:
detail = ""
if res.stderr:
tail = [line for line in res.stderr.strip().splitlines() if line.strip()]
detail = tail[-1][:200] if tail else ""
log.warning(f"⚠️ 内置字幕提取失败: {file_path.name} (轨道 {stream_index}) "
f"ffmpeg={res.returncode} {detail}")
temp_ass.unlink(missing_ok=True) temp_ass.unlink(missing_ok=True)