Repository navigation
Expand file tree
/
Copy pathtranscriber.py
More file actions
194 lines (156 loc) · 6.36 KB
/
Copy pathtranscriber.py
File metadata and controls
194 lines (156 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
# 使用 Hugging Face 国内镜像下载模型
os.environ.setdefault("HF_ENDPOINT", "https://hf-mirror.com")
import re
import subprocess
import webvtt
from google import genai
from faster_whisper import WhisperModel
from config import GOOGLE_API_KEY, GEMINI_MODEL, TEMP_DIR, get_whisper_model, _get_gpu_memory
from logger import logger
def get_transcript(video_path: str, subtitle_path: str | None) -> list[dict]:
"""
获取带时间戳的字幕列表。
优先解析 YouTube 字幕,无字幕时使用本地 Whisper 转录。
返回:[{text, start_time, end_time}, ...] 时间单位:秒(float)
"""
if subtitle_path and os.path.exists(subtitle_path):
logger.debug(f"解析 YouTube 字幕:{subtitle_path}")
return _parse_vtt(subtitle_path)
else:
logger.debug(f"使用 Whisper 转录:{video_path}")
return _transcribe_with_whisper(video_path)
def save_transcript(transcript: list[dict], output_path: str, source: str = "unknown") -> str:
"""
将转录结果保存为文本文件。
格式:[HH:MM:SS - HH:MM:SS] 字幕文本
返回:保存的文件路径
"""
lines = [f"# 字幕来源: {source}", ""]
for seg in transcript:
start_ts = _seconds_to_timestamp(seg["start_time"])
end_ts = _seconds_to_timestamp(seg["end_time"])
lines.append(f"[{start_ts} - {end_ts}] {seg['text']}")
with open(output_path, "w", encoding="utf-8") as f:
f.write("\n".join(lines))
logger.debug(f"字幕已保存:{output_path}")
return output_path
def _seconds_to_timestamp(seconds: float) -> str:
"""将秒数转换为 HH:MM:SS 格式。"""
seconds = int(seconds)
h = seconds // 3600
m = (seconds % 3600) // 60
s = seconds % 60
return f"{h:02d}:{m:02d}:{s:02d}"
def _parse_vtt(vtt_path: str) -> list[dict]:
"""解析 .vtt 字幕文件,返回带时间戳的文本列表。"""
segments = []
seen_texts = set() # 去除重复字幕行
for caption in webvtt.read(vtt_path):
text = caption.text.strip()
# 去掉 HTML 标签
text = re.sub(r"<[^>]+>", "", text).strip()
if not text or text in seen_texts:
continue
seen_texts.add(text)
segments.append({
"text": text,
"start_time": _timestamp_to_seconds(caption.start),
"end_time": _timestamp_to_seconds(caption.end),
})
logger.info(f"✅ 字幕解析完成,共 {len(segments)} 条")
return segments
def _timestamp_to_seconds(ts: str) -> float:
"""将 HH:MM:SS.mmm 格式时间戳转换为秒数。"""
parts = ts.replace(",", ".").split(":")
if len(parts) == 3:
h, m, s = parts
return int(h) * 3600 + int(m) * 60 + float(s)
elif len(parts) == 2:
m, s = parts
return int(m) * 60 + float(s)
return float(parts[0])
def _transcribe_with_whisper(video_path: str) -> list[dict]:
"""使用本地 faster-whisper 转录,返回带时间戳的文本列表。"""
audio_path = _extract_audio(video_path)
# 自动选择模型
model_name = get_whisper_model()
# 检测是否有 GPU
gpu_memory = _get_gpu_memory()
if gpu_memory:
device = "cuda"
compute_type = "float16" # GPU 使用 float16
logger.info(f"⏳ 使用 GPU (显存 {gpu_memory:.1f}GB) 转录,模型: {model_name}...")
else:
device = "cpu"
compute_type = "int8" # CPU 使用 int8 量化
logger.info(f"⏳ 使用 CPU 转录,模型: {model_name}(首次运行需下载模型)...")
model = WhisperModel(model_name, device=device, compute_type=compute_type)
segments, info = model.transcribe(audio_path, language=None)
logger.info(f"检测到语言: {info.language} (概率: {info.language_probability:.2f})")
result = []
for segment in segments:
result.append({
"text": segment.text.strip(),
"start_time": segment.start,
"end_time": segment.end,
})
# 清理临时音频文件
os.remove(audio_path)
logger.debug(f"已删除临时音频文件:{audio_path}")
logger.info(f"✅ Whisper 转录完成,共 {len(result)} 段")
return result
def _transcribe_with_gemini(video_path: str) -> list[dict]:
"""提取音频后调用 Gemini API 转录,返回带时间戳的文本列表。"""
audio_path = _extract_audio(video_path)
client = genai.Client(api_key=GOOGLE_API_KEY)
logger.info("⏳ 正在上传音频到 Gemini...")
with open(audio_path, "rb") as f:
audio_file = client.files.upload(file=f, config={"mime_type": "audio/mp3"})
prompt = """请对这段音频进行转录,输出带时间戳的文本。
要求:
- 每隔约30秒输出一段
- 严格按以下JSON格式返回,不要有其他内容:
[
{"text": "转录文本", "start_time": 0.0, "end_time": 30.0},
...
]
时间单位为秒(浮点数)。"""
response = client.models.generate_content(
model=GEMINI_MODEL,
contents=[audio_file, prompt],
)
segments = _parse_json_response(response.text)
# 清理临时音频文件
os.remove(audio_path)
logger.debug(f"已删除临时音频文件:{audio_path}")
logger.info(f"✅ Gemini 转录完成,共 {len(segments)} 段")
return segments
def _extract_audio(video_path: str) -> str:
"""用 ffmpeg 从视频中提取音频(mp3)。"""
audio_path = os.path.join(TEMP_DIR, "audio.mp3")
cmd = [
"ffmpeg", "-y", "-i", video_path,
"-vn", "-ar", "16000", "-ac", "1", "-b:a", "64k",
audio_path
]
subprocess.run(cmd, check=True, capture_output=True)
logger.info("✅ 音频提取完成")
logger.debug(f"音频路径:{audio_path}")
return audio_path
def _parse_json_response(text: str) -> list[dict]:
"""从 Gemini 响应中提取 JSON 数据,兼容截断情况。"""
import json
# 去掉 markdown 代码块包裹
text = re.sub(r"```(?:json)?\s*", "", text).strip().rstrip("```").strip()
try:
return json.loads(text)
except json.JSONDecodeError:
# Gemini 输出被截断时,截到最后一个完整对象
last_brace = text.rfind("},")
if last_brace == -1:
last_brace = text.rfind("}")
if last_brace != -1:
truncated = text[: last_brace + 1].rstrip(",") + "\n]"
return json.loads(truncated)
raise