复跑时 scan_existing_subtitles 会把流水线自己产出的 *.internal.und.srt 当成外挂字幕, 改名成 <目标名>.zh-CN.2.srt(并随每次复跑不断追加 .3/.4 后缀)。 现对 <target_stem>.* 开头的字幕直接跳过,保持幂等。
102 lines
5.3 KiB
Python
102 lines
5.3 KiB
Python
# src/services/renamer.py
|
|
|
|
from pathlib import Path
|
|
from core import config
|
|
from core.logger import log
|
|
from core.constants import RegexPatterns
|
|
from core.utils import MediaUtil
|
|
from services.fileops import FileOpsService
|
|
|
|
class RenamerService:
|
|
"""媒体文件规范重命名编排器"""
|
|
|
|
@staticmethod
|
|
def scan_existing_subtitles(f_path: Path, target_stem: str, m_type: str, dest_dir: Path, task_mgr) -> bool:
|
|
has_external_sub = False
|
|
for sub in f_path.parent.glob("*"):
|
|
if sub.is_file() and sub.suffix.lower() in {'.srt', '.ass', '.ssa'}:
|
|
# 🛡️ 幂等保护:本工具已按目标名生成过的字幕(含 *.internal.<lang>.srt 与去重用的 .2/.3 后缀)
|
|
# 不再参与"外挂字幕规范化",否则复跑时会被改名成 <目标名>.zh-CN.2.srt,丢掉 internal 标识且不断产生新后缀。
|
|
if sub.name.startswith(f"{target_stem}."):
|
|
continue
|
|
is_match = False
|
|
if f_path.stem in sub.name: is_match = True
|
|
elif m_type == "tv":
|
|
match = RegexPatterns.SUB_EP_MATCH.search(sub.name)
|
|
if match:
|
|
ep = next((g for g in match.groups() if g), None)
|
|
target_ep_match = RegexPatterns.SUB_TARGET_EP.search(target_stem)
|
|
if target_ep_match and ep and ep.zfill(2) == target_ep_match.group(1):
|
|
is_match = True
|
|
|
|
if is_match:
|
|
has_external_sub = True
|
|
lang_tag = MediaUtil.detect_subtitle_language(sub)
|
|
|
|
if sub.suffix.lower() != '.srt':
|
|
desired_path = dest_dir / f"{target_stem}.{lang_tag}.srt"
|
|
|
|
# ==========================================
|
|
# 🛡️ 恢复丢失的外挂转换前置拦截器
|
|
# ==========================================
|
|
if desired_path.exists() and desired_path.stat().st_size > 0:
|
|
log.debug(f"✨ [跳过转换] 孪生 SRT 已存在,忽略外挂源文件: {sub.name}")
|
|
continue
|
|
|
|
reserved_path = task_mgr.reserve_path(desired_path, source_path=sub)
|
|
|
|
from services.subtitle import SubtitleService
|
|
task_mgr.add_extract(
|
|
f"[外挂字幕转换] {sub.name} --> {reserved_path.name}",
|
|
SubtitleService.convert_to_srt,
|
|
(sub, reserved_path)
|
|
)
|
|
else:
|
|
desired_path = dest_dir / f"{target_stem}.{lang_tag}.srt"
|
|
reserved_path = task_mgr.reserve_path(desired_path, source_path=sub)
|
|
|
|
if sub.resolve() != reserved_path.resolve():
|
|
task_mgr.add_file_rename(
|
|
f"[规范外挂字幕] {sub.name} --> {reserved_path.name}",
|
|
FileOpsService.move_file,
|
|
(sub, reserved_path)
|
|
)
|
|
|
|
return has_external_sub
|
|
|
|
@staticmethod
|
|
def scan_rename(f_path: Path, target_stem: str, m_type: str, dest_dir: Path, target_season_folder: str, root_dir_path: Path, target_root_folder: str, task_mgr):
|
|
new_path = dest_dir / f"{target_stem}{f_path.suffix.lower()}"
|
|
reserved_new_path = task_mgr.reserve_path(new_path, source_path=f_path)
|
|
|
|
if f_path.resolve() != reserved_new_path.resolve():
|
|
task_mgr.add_file_rename(
|
|
f"[主视频] {f_path.name} --> {reserved_new_path.name}",
|
|
FileOpsService.move_file,
|
|
(f_path, reserved_new_path)
|
|
)
|
|
|
|
for asset in f_path.parent.glob(f"{f_path.stem}*"):
|
|
if asset.is_file() and asset.suffix.lower() in config.SIDECAR_EXTS:
|
|
rest_of_name = asset.name[len(f_path.stem):]
|
|
if rest_of_name and rest_of_name[0].isalnum():
|
|
continue
|
|
|
|
new_asset_name = f"{target_stem}{rest_of_name}"
|
|
reserved_asset = task_mgr.reserve_path(dest_dir / new_asset_name, source_path=asset)
|
|
|
|
if asset.resolve() != reserved_asset.resolve():
|
|
task_mgr.add_file_rename(
|
|
f"[附属元数据] {asset.name} --> {reserved_asset.name}",
|
|
FileOpsService.move_file,
|
|
(asset, reserved_asset)
|
|
)
|
|
|
|
# 这里的目录重命名逻辑保持不变,但增加一个防爆锁
|
|
if m_type == "tv" and target_season_folder and f_path.parent.name != target_season_folder:
|
|
if f_path.parent != root_dir_path: # 🛡️ 防爆锁:绝不允许对根目录执行季目录改名
|
|
task_mgr.add_season_dir_rename(f_path.parent, f_path.parent.with_name(target_season_folder))
|
|
|
|
if root_dir_path.name != target_root_folder:
|
|
task_mgr.add_root_dir_rename(root_dir_path, root_dir_path.with_name(target_root_folder))
|