feat: align subtitles from first 10 seconds audio

This commit is contained in:
TTlaji committed 2026-06-03 15:10:49 +08:00
1 parent 93e06ca40a
commit 5228765473
11 files changed
+248 -22

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import subprocess
import tempfile
from .subtitle_tools import SubtitleCue, parse_timestamp
FIRST_SENTENCE_ALIGNMENT_PROMPT = """
你是字幕首句时间校准助手。你只需要分析这段音频的前 10 秒。
请找出给定英文字幕第一句在音频中真正开始说话的时间,单位是秒。
只返回 JSON:{"first_sentence_start":1.23,"matched_text":"...","confidence":0.95}
不要返回解释,不要校准后续句子。
""".strip()
@dataclass
class AlignmentResult:
applied: bool
offset_seconds: float
first_sentence_start: float | None
confidence: float
note: str
def align_first_sentence(video_path: Path, first_cue: SubtitleCue, client) -> AlignmentResult:
if not video_path.exists():
return AlignmentResult(False, 0.0, None, 0.0, "视频文件不存在,已跳过自动首句校准。")
with tempfile.TemporaryDirectory() as temp_dir:
audio_path = Path(temp_dir) / "first10.wav"
try:
extract_first_audio(video_path, audio_path)
response = client.audio_json(
FIRST_SENTENCE_ALIGNMENT_PROMPT,
audio_path,
f"第一句英文字幕:{first_cue.text}\n请只判断这句开始说话的秒数。",
)
except Exception as error:
return AlignmentResult(False, 0.0, None, 0.0, f"首句自动校准失败,已跳过:{error}")
first_sentence_start = parse_alignment_second(response.get("first_sentence_start"))
confidence = parse_alignment_second(response.get("confidence"))
if first_sentence_start is None:
return AlignmentResult(False, 0.0, None, confidence or 0.0, "AI 没有返回有效的首句开始时间,已跳过自动校准。")
offset = first_sentence_start - parse_timestamp(first_cue.start)
if abs(offset) > 5:
return AlignmentResult(False, 0.0, first_sentence_start, confidence or 0.0, f"检测到偏移 {offset:.2f} 秒,超过 5 秒安全阈值,已跳过自动校准。")
return AlignmentResult(True, offset, first_sentence_start, confidence or 0.0, f"首句自动校准成功,整体平移 {offset:.2f} 秒。")
def extract_first_audio(video_path: Path, audio_path: Path) -> None:
command = [
"ffmpeg",
"-y",
"-i",
str(video_path),
"-ss",
"0",
"-t",
"10",
"-vn",
"-ac",
"1",
"-ar",
"16000",
str(audio_path),
]
subprocess.run(command, check=True, capture_output=True)
def parse_alignment_second(value) -> float | None:
try:
return float(value)
except (TypeError, ValueError):
return None