102 lines
3.0 KiB
Python
102 lines
3.0 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
import importlib
|
|
import subprocess
|
|
import tempfile
|
|
|
|
from .subtitle_tools import format_timestamp
|
|
|
|
|
|
class LocalWhisperError(RuntimeError):
|
|
pass
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LocalWhisperConfig:
|
|
enabled: bool
|
|
model: str
|
|
device: str
|
|
compute_type: str
|
|
|
|
|
|
def transcribe_video_to_vtt(video_path: Path, config: LocalWhisperConfig) -> str:
|
|
if not config.enabled:
|
|
raise LocalWhisperError("本地 Whisper 未启用。")
|
|
if not video_path.exists():
|
|
raise LocalWhisperError("视频文件不存在,无法生成字幕。")
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
audio_path = Path(temp_dir) / "whisper-audio.wav"
|
|
extract_audio_for_whisper(video_path, audio_path)
|
|
segments = transcribe_audio(audio_path, config)
|
|
return build_vtt_from_segments(segments)
|
|
|
|
|
|
def extract_audio_for_whisper(video_path: Path, audio_path: Path) -> None:
|
|
command = [
|
|
"ffmpeg",
|
|
"-y",
|
|
"-i",
|
|
str(video_path),
|
|
"-vn",
|
|
"-ac",
|
|
"1",
|
|
"-ar",
|
|
"16000",
|
|
str(audio_path),
|
|
]
|
|
try:
|
|
subprocess.run(command, check=True, capture_output=True)
|
|
except FileNotFoundError as error:
|
|
raise LocalWhisperError("未找到 ffmpeg,请先安装 ffmpeg。") from error
|
|
except subprocess.CalledProcessError as error:
|
|
detail = error.stderr.decode("utf-8", errors="replace")
|
|
raise LocalWhisperError(f"ffmpeg 提取音频失败:{detail}") from error
|
|
|
|
|
|
def transcribe_audio(audio_path: Path, config: LocalWhisperConfig) -> list[dict[str, object]]:
|
|
faster_whisper = import_optional("faster_whisper")
|
|
if faster_whisper is None:
|
|
raise LocalWhisperError("未安装 faster-whisper,请先安装后再生成本地字幕。")
|
|
|
|
model = faster_whisper.WhisperModel(
|
|
config.model,
|
|
device=config.device,
|
|
compute_type=config.compute_type,
|
|
)
|
|
segments, _info = model.transcribe(
|
|
str(audio_path),
|
|
language="en",
|
|
vad_filter=True,
|
|
)
|
|
return [
|
|
{
|
|
"start": float(segment.start),
|
|
"end": float(segment.end),
|
|
"text": str(segment.text).strip(),
|
|
}
|
|
for segment in segments
|
|
if str(segment.text).strip()
|
|
]
|
|
|
|
|
|
def import_optional(module_name: str):
|
|
try:
|
|
return importlib.import_module(module_name)
|
|
except ImportError:
|
|
return None
|
|
|
|
|
|
def build_vtt_from_segments(segments: list[dict[str, object]]) -> str:
|
|
blocks: list[str] = []
|
|
for index, segment in enumerate(segments, 1):
|
|
text = str(segment.get("text", "")).strip()
|
|
if not text:
|
|
continue
|
|
start = float(segment.get("start", 0) or 0)
|
|
end = float(segment.get("end", start) or start)
|
|
blocks.append(f"{index}\n{format_timestamp(start)} --> {format_timestamp(end)}\n{text}")
|
|
return "WEBVTT\n\n" + "\n\n".join(blocks).strip() + "\n"
|