diff --git a/media/src/services/subtitle.py b/media/src/services/subtitle.py index 3811f8a..7972708 100644 --- a/media/src/services/subtitle.py +++ b/media/src/services/subtitle.py @@ -3,6 +3,8 @@ import re import subprocess from pathlib import Path +from uuid import uuid4 + import pysubs2 from core import config from core.logger import log @@ -72,22 +74,24 @@ class SubtitleService: continue 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( f"[提取内置字幕] {file_path.name} (轨道 {stream_idx}) --> {out_srt.name}", SubtitleService.do_extract, (file_path, stream_idx, temp_ass, out_srt) ) - except Exception: - pass + except Exception as e: + log.warning(f"⚠️ 内置字幕扫描失败: {file_path.name} -> {e}") @staticmethod 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)] - 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: try: subs = pysubs2.load(str(temp_ass), encoding="utf-8") @@ -95,9 +99,15 @@ class SubtitleService: line.text = re.sub(r'\{.*?\}', '', line.text) subs.save(str(out_srt), format="srt", encoding="utf-8") except Exception as e: - print(f"\n⚠️ 字幕 {out_srt.name} 洗白失败,已跳过。报错信息: {e}") + log.warning(f"⚠️ 字幕 {out_srt.name} 洗白失败,已跳过。报错信息: {e}") finally: temp_ass.unlink(missing_ok=True) 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)