提取内置字幕
This commit is contained in:
@@ -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