feat: add local whisper subtitle generation

This commit is contained in:
TTlaji committed 2026-06-03 23:37:21 +08:00
1 parent 5228765473
commit 6a6fd347bd
17 files changed
+1041 -96

No files matched your search

+48 -4
View File
@@ -4,6 +4,7 @@ from dataclasses import dataclass
from pathlib import Path
import subprocess
import tempfile
from typing import Callable
from .subtitle_tools import SubtitleCue, parse_timestamp
@@ -41,16 +42,59 @@ def align_first_sentence(video_path: Path, first_cue: SubtitleCue, client) -> Al
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"))
return alignment_from_start(first_cue, response.get("first_sentence_start"), response.get("confidence"))
def align_first_sentence_with_asr(
video_path: Path,
first_cue: SubtitleCue,
asr_client,
extract_audio: Callable[[Path, Path], None] = None,
) -> AlignmentResult:
if not video_path.exists():
return AlignmentResult(False, 0.0, None, 0.0, "视频文件不存在,已跳过 OpenAI ASR 首句校准。")
audio_extractor = extract_audio or extract_first_audio
with tempfile.TemporaryDirectory() as temp_dir:
audio_path = Path(temp_dir) / "first10.wav"
try:
audio_extractor(video_path, audio_path)
response = asr_client.transcribe(audio_path)
except Exception as error:
return AlignmentResult(False, 0.0, None, 0.0, f"OpenAI ASR 首句校准失败,已跳过:{error}")
first_start = first_segment_start(response)
return alignment_from_start(first_cue, first_start, 0.9, source_name="OpenAI ASR")
def alignment_from_start(
first_cue: SubtitleCue,
start_value,
confidence_value=None,
source_name: str = "AI",
) -> AlignmentResult:
first_sentence_start = parse_alignment_second(start_value)
confidence = parse_alignment_second(confidence_value)
if first_sentence_start is None:
return AlignmentResult(False, 0.0, None, confidence or 0.0, "AI 没有返回有效的首句开始时间,已跳过自动校准。")
return AlignmentResult(False, 0.0, None, confidence or 0.0, f"{source_name} 没有返回有效的首句开始时间,已跳过自动校准。")
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} 秒。")
return AlignmentResult(True, offset, first_sentence_start, confidence or 0.0, f"{source_name} 首句校准成功,整体平移 {offset:.2f} 秒。")
def first_segment_start(response: dict[str, object]) -> float | None:
segments = response.get("segments")
if isinstance(segments, list):
for segment in segments:
if isinstance(segment, dict):
start = parse_alignment_second(segment.get("start"))
text = str(segment.get("text", "")).strip()
if start is not None and text:
return start
return None
def extract_first_audio(video_path: Path, audio_path: Path) -> None: