82 lines
3.8 KiB
Python
82 lines
3.8 KiB
Python
# src/services/subtitle.py
|
|
|
|
import re
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
import pysubs2
|
|
from core import config
|
|
|
|
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)]
|
|
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 = {}
|
|
|
|
for s in streams:
|
|
codec = s.get('codec_name', '')
|
|
if codec not in config.EXTRACT_SUBTITLE_CODECS:
|
|
continue
|
|
|
|
lang = s.get('tags', {}).get('language', 'und').lower()
|
|
title = s.get('tags', {}).get('title', '').lower()
|
|
stream_idx = s['index']
|
|
|
|
lang_code = "und"
|
|
if lang in ['chi', 'zho', 'zh', 'chi-cn', 'zh-cn']: lang_code = 'zh-CN'
|
|
elif lang in ['tw', 'hk', 'zh-tw']: lang_code = 'zh-TW'
|
|
elif lang in ['eng', 'en']: lang_code = 'en'
|
|
elif lang in ['jpn', 'ja']: lang_code = 'ja'
|
|
elif lang in ['kor', 'ko']: lang_code = 'ko'
|
|
|
|
if '繁' in title or 'tw' in title or 'hk' in title or 'cht' in title: lang_code = 'zh-TW'
|
|
elif '简' in title or 'cn' in title or '双语' in title or '中' in title or 'chs' in title: lang_code = 'zh-CN'
|
|
elif '英' in title and lang_code == 'und': lang_code = 'en'
|
|
elif '中' in title and lang_code == 'und': lang_code = 'zh-CN'
|
|
|
|
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]}"
|
|
|
|
# 💡 核心新增:确保提取出的字幕直接落地在目标季目录
|
|
desired_out_srt = dest_dir / f"{target_stem}.{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}",
|
|
SubtitleService.do_extract,
|
|
(file_path, stream_idx, temp_ass, out_srt)
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
@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)
|
|
|
|
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")
|
|
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:
|
|
print(f"\n⚠️ 字幕 {out_srt.name} 洗白失败,已跳过。报错信息: {e}")
|
|
finally:
|
|
temp_ass.unlink(missing_ok=True)
|
|
else:
|
|
temp_ass.unlink(missing_ok=True) |