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

+86 -6
View File
@@ -6,7 +6,7 @@ from fastapi import FastAPI, File, Form, HTTPException, UploadFile
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from .ai_config import get_ai_status, load_deepseek_config
from .ai_config import get_ai_status, load_asr_config, load_local_whisper_config, load_text_ai_config
from .db import (
DATABASE_PATH,
create_video,
@@ -20,7 +20,9 @@ from .db import (
upsert_video_subtitle,
)
from .deepseek_client import DeepSeekClient, DeepSeekError
from .subtitle_alignment import align_first_sentence
from .local_whisper import LocalWhisperError, transcribe_video_to_vtt
from .openai_asr_client import OpenAIAsrClient
from .subtitle_alignment import align_first_sentence_with_asr
from .subtitle_ai import process_subtitles_with_ai
from .subtitle_tools import clean_english_subtitle
@@ -386,22 +388,31 @@ def api_process_video_subtitles(video_id: int) -> dict[str, object]:
raise HTTPException(status_code=400, detail="请先上传英文字幕。")
try:
client = DeepSeekClient(load_deepseek_config())
text_ai_config = load_text_ai_config()
asr_config = load_asr_config()
client = DeepSeekClient(text_ai_config, timeout_seconds=text_ai_config.timeout_seconds)
timestamp_offset_seconds = 0.0
alignment_note = "未执行首句校准。"
cleaned_for_alignment = clean_english_subtitle(str(english_subtitle["content"]))
video_path = upload_url_to_path(str(video.get("videoUrl") or ""))
if video_path and cleaned_for_alignment.cues:
alignment = align_first_sentence(video_path, cleaned_for_alignment.cues[0], client)
if asr_config.configured and video_path and cleaned_for_alignment.cues:
alignment = align_first_sentence_with_asr(
video_path,
cleaned_for_alignment.cues[0],
OpenAIAsrClient(asr_config),
)
timestamp_offset_seconds = alignment.offset_seconds if alignment.applied else 0.0
alignment_note = alignment.note
elif not asr_config.configured:
alignment_note = "未配置 OpenAI ASR,已跳过首句校准,继续执行 DeepSeek 字幕处理。"
elif not video_path:
alignment_note = "当前视频不是本地上传文件,已跳过自动首句校准。"
alignment_note = "当前视频不是本地上传文件,已跳过 OpenAI ASR 首句校准。"
result = process_subtitles_with_ai(
str(english_subtitle["content"]),
client,
timestamp_offset_seconds=timestamp_offset_seconds,
batch_size=text_ai_config.translation_batch_size,
)
result["report"]["alignment_note"] = alignment_note
except (DeepSeekError, ValueError) as error:
@@ -435,6 +446,75 @@ def api_process_video_subtitles(video_id: int) -> dict[str, object]:
}
def save_processed_subtitles(video_id: int, subtitles: dict[str, object], result: dict[str, object]) -> None:
english_subtitle = subtitles.get("en", {})
english_file_path = ""
if isinstance(english_subtitle, dict):
english_file_path = str(english_subtitle.get("filePath") or "")
upsert_video_subtitle(
video_id,
language_code="en",
language_name="English",
format="vtt",
file_path=english_file_path,
content=str(result["subtitle_en_content"]),
)
chinese_subtitle = subtitles.get("zh", {})
chinese_file_path = ""
if isinstance(chinese_subtitle, dict):
chinese_file_path = str(chinese_subtitle.get("filePath") or "")
upsert_video_subtitle(
video_id,
language_code="zh",
language_name="中文",
format="vtt",
file_path=chinese_file_path,
content=str(result["subtitle_zh_content"]),
)
@app.post("/api/videos/{video_id}/local-whisper/subtitles")
def api_generate_local_whisper_subtitles(video_id: int) -> dict[str, object]:
status = get_ai_status()
if not status["configured"]:
raise HTTPException(status_code=400, detail=status["message"])
video = get_video(video_id)
if video is None:
raise HTTPException(status_code=404, detail="视频不存在")
video_path = upload_url_to_path(str(video.get("videoUrl") or ""))
if not video_path:
raise HTTPException(status_code=400, detail="本地 Whisper 只能处理本地上传的视频文件。")
subtitles = video.get("subtitles", {})
if not isinstance(subtitles, dict):
subtitles = {}
try:
whisper_config = load_local_whisper_config()
english_vtt = transcribe_video_to_vtt(video_path, whisper_config)
text_ai_config = load_text_ai_config()
client = DeepSeekClient(text_ai_config, timeout_seconds=text_ai_config.timeout_seconds)
result = process_subtitles_with_ai(
english_vtt,
client,
batch_size=text_ai_config.translation_batch_size,
)
result["report"]["local_whisper_generated"] = True
result["report"]["alignment_note"] = "本地 Whisper 已生成英文字幕,DeepSeek 已翻译中文字幕。"
except (DeepSeekError, LocalWhisperError, ValueError) as error:
raise HTTPException(status_code=400, detail=str(error)) from error
save_processed_subtitles(video_id, subtitles, result)
return {
"video": get_video(video_id),
"report": result["report"],
}
@app.get("/")
def read_index() -> FileResponse:
return FileResponse(PROJECT_DIR / "index.html")