refactor(services): 引入 FileOpsService,统一 ffprobe 与缓存调用
- 新增 services/fileops.py:用 shutil.move 取代 Path.rename,解决跨盘/跨 Docker 卷移动报 Invalid cross-device link - task.py:季目录与根目录改名统一交给 FileOpsService,任务结构统一为 (old, new) 元组 - mediainfo/metadata/subtitle:ffprobe 调用收敛到 FFprobeUtil,去掉重复的 json.loads 与 stderr 处理 - subtitle:内置字幕提取、外挂字幕转换增加"目标已存在且非空则跳过"的幂等保护 - tmdb:缓存读写迁移到 JsonCacheManager,移除类内静态缓存状态
This commit is contained in:
@@ -1,22 +1,21 @@
|
||||
# src/services/subtitle.py
|
||||
|
||||
import re
|
||||
import json
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
import pysubs2
|
||||
from core import config
|
||||
from core.logger import log
|
||||
from core.utils import FFprobeUtil
|
||||
|
||||
class SubtitleService:
|
||||
|
||||
# 💡 NEW: 新增专属的格式转换方法
|
||||
@staticmethod
|
||||
def convert_to_srt(in_sub: Path, out_srt: Path):
|
||||
"""将非 srt 的外挂字幕洗白并转换为 srt,成功后抹除源文件"""
|
||||
"""将非 srt 的外挂字幕洗白并转换为 srt,安全保留源文件"""
|
||||
try:
|
||||
subs = pysubs2.load(str(in_sub), encoding="utf-8")
|
||||
for line in subs:
|
||||
# 洗白特效标签
|
||||
line.text = re.sub(r'\{.*?\}', '', line.text)
|
||||
subs.save(str(out_srt), format="srt", encoding="utf-8")
|
||||
|
||||
@@ -25,11 +24,11 @@ class SubtitleService:
|
||||
|
||||
@staticmethod
|
||||
def scan_subtitles(file_path: Path, task_mgr, target_stem: str, dest_dir: Path):
|
||||
cmd = ["ffprobe", "-v", "quiet", "-print_format", "json", "-show_streams", "-select_streams", "s", str(file_path)]
|
||||
data = FFprobeUtil.get_json(file_path, "-show_streams", "-select_streams", "s")
|
||||
if not data:
|
||||
return
|
||||
|
||||
try:
|
||||
res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
|
||||
data = json.loads(res.stdout)
|
||||
data = json.loads(res.stdout)
|
||||
streams = data.get('streams', [])
|
||||
lang_counts = {}
|
||||
|
||||
@@ -57,15 +56,21 @@ class SubtitleService:
|
||||
if lang_code == "und" and lang != "und":
|
||||
lang_code = lang[:3]
|
||||
|
||||
# 💡 核心新增:语言白名单拦截系统
|
||||
if config.EXTRACT_SUBTITLE_LANGUAGES and lang_code not in config.EXTRACT_SUBTITLE_LANGUAGES:
|
||||
continue
|
||||
|
||||
lang_counts[lang_code] = lang_counts.get(lang_code, 0) + 1
|
||||
lang_suffix = "" if lang_counts[lang_code] == 1 else f".{lang_counts[lang_code]}"
|
||||
|
||||
# 💡 核心修改 1.1:加上 .internal 标签,明确表示是由内置流提取的
|
||||
desired_out_srt = dest_dir / f"{target_stem}.internal.{lang_code}{lang_suffix}.srt"
|
||||
|
||||
# ==========================================
|
||||
# 🛡️ 恢复丢失的内置提取前置拦截器
|
||||
# ==========================================
|
||||
if desired_out_srt.exists() and desired_out_srt.stat().st_size > 0:
|
||||
log.debug(f"✨ [跳过提取] 目标内置字幕已存在,触发幂等保护: {desired_out_srt.name}")
|
||||
continue
|
||||
|
||||
out_srt = task_mgr.reserve_path(desired_out_srt)
|
||||
|
||||
temp_ass = dest_dir / f"{target_stem}.temp.{out_srt.name}.ass"
|
||||
@@ -94,4 +99,5 @@ class SubtitleService:
|
||||
finally:
|
||||
temp_ass.unlink(missing_ok=True)
|
||||
else:
|
||||
temp_ass.unlink(missing_ok=True)
|
||||
temp_ass.unlink(missing_ok=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user