提取内置字幕
This commit is contained in:
+9
-5
@@ -119,12 +119,16 @@ def run_pipeline(target_dir: Path, args, auto_execute=False):
|
||||
log.debug(f"✨ [完美豁免] {f.name} 已达最高工业标准,直接放行~")
|
||||
|
||||
if args.mode != "clean-ads":
|
||||
# 💡 传入 dest_dir
|
||||
RenamerService.scan_existing_subtitles(f, target_stem, m_type, dest_dir, task_mgr)
|
||||
if (config.EXTRACT_INTERNAL_SUBS or args.extract_internal) and (not skip_heavy or args.extract_internal):
|
||||
# 💡 传入 dest_dir
|
||||
SubtitleService.scan_subtitles(f, task_mgr, target_stem, dest_dir)
|
||||
# 💡 步骤 1:先扫描外挂字幕,接收状态反馈
|
||||
has_ext_sub = RenamerService.scan_existing_subtitles(f, target_stem, m_type, dest_dir, task_mgr)
|
||||
|
||||
# 💡 步骤 2:联动决策
|
||||
# 如果没有外挂字幕,或者用户在 config 开了全局强制,或者使用了 --extract-internal 参数,才执行内置提取
|
||||
should_extract_internal = (not has_ext_sub) or config.EXTRACT_INTERNAL_SUBS or args.extract_internal
|
||||
|
||||
if should_extract_internal and (not skip_heavy or args.extract_internal or args.mode == "extract-subs"):
|
||||
SubtitleService.scan_subtitles(f, task_mgr, target_stem, dest_dir)
|
||||
|
||||
if args.mode != "extract-subs" and not skip_heavy:
|
||||
MetadataService.scan_ads(f, task_mgr)
|
||||
MetadataService.scan_audio_tracks(f, task_mgr)
|
||||
|
||||
@@ -128,7 +128,8 @@ class RenamerService:
|
||||
except: return "und"
|
||||
|
||||
@staticmethod
|
||||
def scan_existing_subtitles(f_path: Path, target_stem: str, m_type: str, dest_dir: Path, task_mgr):
|
||||
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'}:
|
||||
is_match = False
|
||||
@@ -142,18 +143,33 @@ class RenamerService:
|
||||
is_match = True
|
||||
|
||||
if is_match:
|
||||
has_external_sub = True # 💡 只要匹配到,就说明存在外挂字幕
|
||||
lang_tag = RenamerService.detect_subtitle_language(sub)
|
||||
desired_path = dest_dir / f"{target_stem}.{lang_tag}{sub.suffix.lower()}"
|
||||
# 💡 传入 source_path,如果计算出的名字存在且就是自己,就不会强制加 .2
|
||||
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}",
|
||||
RenamerService.do_rename_single,
|
||||
# 💡 核心修改 1.2: 如果不是 srt,执行转换任务
|
||||
if sub.suffix.lower() != '.srt':
|
||||
desired_path = dest_dir / f"{target_stem}.{lang_tag}.srt"
|
||||
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:
|
||||
# 是 srt,正常走重命名
|
||||
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}",
|
||||
RenamerService.do_rename_single,
|
||||
(sub, reserved_path)
|
||||
)
|
||||
|
||||
return has_external_sub # 💡 把结果返回给 main.py
|
||||
|
||||
@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):
|
||||
|
||||
@@ -8,6 +8,21 @@ import pysubs2
|
||||
from core import config
|
||||
|
||||
class SubtitleService:
|
||||
|
||||
# 💡 NEW: 新增专属的格式转换方法
|
||||
@staticmethod
|
||||
def convert_to_srt(in_sub: Path, out_srt: Path):
|
||||
"""将非 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")
|
||||
|
||||
except Exception as e:
|
||||
log.warning(f"⚠️ 外挂字幕转换 srt 失败: {in_sub.name} -> {e}")
|
||||
|
||||
@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)]
|
||||
@@ -49,14 +64,14 @@ class SubtitleService:
|
||||
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]}"
|
||||
|
||||
# 💡 核心新增:确保提取出的字幕直接落地在目标季目录
|
||||
desired_out_srt = dest_dir / f"{target_stem}.{lang_code}{lang_suffix}.srt"
|
||||
# 💡 核心修改 1.1:加上 .internal 标签,明确表示是由内置流提取的
|
||||
desired_out_srt = dest_dir / f"{target_stem}.internal.{lang_code}{lang_suffix}.srt"
|
||||
out_srt = task_mgr.reserve_path(desired_out_srt)
|
||||
|
||||
temp_ass = dest_dir / f"{target_stem}.temp.{out_srt.name}.ass"
|
||||
|
||||
task_mgr.add_extract(
|
||||
f"{file_path.name} (内置轨道 {stream_idx} | 格式: {codec.upper()}) --> {out_srt.name}",
|
||||
f"[提取内置字幕] {file_path.name} (轨道 {stream_idx}) --> {out_srt.name}",
|
||||
SubtitleService.do_extract,
|
||||
(file_path, stream_idx, temp_ass, out_srt)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user