用途
Whisper 是 OpenAI 的多语言语音识别模型,用于将音频转换为文本。根据资料,它适用于以下场景:语音转文字转录(支持 99 种语言)、播客/视频转录、会议记录自动化、翻译为英文、噪声环境下的音频转录、多语言音频处理。
资料同时指出,若需要说话人分离(diarization)可考虑 AssemblyAI,需要实时流式 ASR 可考虑 Deepgram,需要云端方案可考虑 Google Speech-to-Text。
主要能力
- 语音转文字转录,支持 99 种语言。
- 翻译任务:可将非英语音频翻译为英文文本(task="translate")。
- 语言识别:可自动检测音频语言,也可手动指定语言以加快速度。
- 时间戳:支持段落级时间戳,开启 word_timestamps 后支持词级时间戳。
- 初始提示(initial_prompt):可传入上下文文本,帮助提升技术术语、专有名词和领域词汇的识别准确率。
- 温度回退(temperature fallback):置信度低时可用不同温度重试。
- 输出格式:命令行支持 txt、srt、vtt、json 等格式。
- GPU 加速:自动使用 GPU(若可用),也可强制指定 CPU 或 CUDA,资料称 GPU 上可快 10–20 倍。
模型规格
资料列出六种模型:
| 模型 | 参数量 | 仅英文 | 多语言 | 速度 | 显存 |
|---|---|---|---|---|---|
| tiny | 39M | ✓ | ✓ | ~32x | ~1 GB |
| base | 74M | ✓ | ✓ | ~16x | ~1 GB |
| small | 244M | ✓ | ✓ | ~6x | ~2 GB |
| medium | 769M | ✓ | ✓ | ~2x | ~5 GB |
| large | 1550M | ✗ | ✓ | 1x | ~10 GB |
| turbo | 809M | ✗ | ✓ | ~8x | ~6 GB |
资料建议:追求速度与质量平衡可用 turbo,原型开发可用 base。
使用方式
安装:
# Requires Python 3.8-3.11
pip install -U openai-whisper
# Requires ffmpeg
# macOS: brew install ffmpeg
# Ubuntu: sudo apt install ffmpeg
# Windows: choco install ffmpeg
Python 基本转录:
import whisper
model = whisper.load_model("base")
result = model.transcribe("audio.mp3")
print(result["text"])
for segment in result["segments"]:
print(f"[{segment['start']:.2f}s - {segment['end']:.2f}s] {segment['text']}")
指定语言与任务:
result = model.transcribe("audio.mp3", language="en")
result = model.transcribe("audio.mp3", task="transcribe")
result = model.transcribe("spanish.mp3", task="translate")
初始提示:
result = model.transcribe(
"audio.mp3",
initial_prompt="This is a technical podcast about machine learning and AI."
)
词级时间戳:
result = model.transcribe("audio.mp3", word_timestamps=True)
for segment in result["segments"]:
for word in segment["words"]:
print(f"{word['word']} ({word['start']:.2f}s - {word['end']:.2f}s)")
温度回退:
result = model.transcribe(
"audio.mp3",
temperature=(0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
)
命令行用法:
whisper audio.mp3
whisper audio.mp3 --model turbo
whisper audio.mp3 --output_format txt
whisper audio.mp3 --output_format srt
whisper audio.mp3 --output_format vtt
whisper audio.mp3 --output_format json
whisper audio.mp3 --language Spanish
whisper spanish.mp3 --task translate
批量处理示例:
import os
audio_files = ["file1.mp3", "file2.mp3", "file3.mp3"]
for audio_file in audio_files:
print(f"Transcribing {audio_file}...")
result = model.transcribe(audio_file)
output_file = audio_file.replace(".mp3", ".txt")
with open(output_file, "w") as f:
f.write(result["text"])
实时转录(使用 faster-whisper):
# pip install faster-whisper
from faster_whisper import WhisperModel
model = WhisperModel("base", device="cuda", compute_type="float16")
segments, info = model.transcribe("audio.mp3", beam_size=5)
for segment in segments:
print(f"[{segment.start:.2f}s -> {segment.end:.2f}s] {segment.text}")
GPU 加速:
import whisper
model = whisper.load_model("turbo")
model = whisper.load_model("turbo", device="cpu")
model = whisper.load_model("turbo", device="cuda")
与其他工具集成:
生成 SRT 字幕:
whisper video.mp4 --output_format srt --language English
与 LangChain 配合:
from langchain.document_loaders import WhisperTranscriptionLoader
loader = WhisperTranscriptionLoader(file_path="audio.mp3")
docs = loader.load()
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
vectorstore = Chroma.from_documents(docs, OpenAIEmbeddings())
从视频提取音频:
ffmpeg -i video.mp4 -vn -acodec pcm_s16le audio.wav
whisper audio.wav
适用场景
- 播客、视频的批量转录与字幕生成(srt/vtt)。
- 会议记录自动化。
- 多语言音频处理与语言识别。
- 将非英语音频翻译为英文文本。
- 噪声环境下的音频转录。
- 将转录结果接入 RAG 等下游流程。
最佳实践(资料建议)
- 使用 turbo 模型,英文场景速度与质量较均衡。
- 明确指定语言,比自动检测更快。
- 添加 initial_prompt 以改善技术术语识别。
- 使用 GPU,可快 10–20 倍。
- 批量处理更高效。
- 转换为 WAV 兼容性更好。
- 长音频切分为小于 30 分钟的片段。
- 检查语言支持情况,不同语言质量有差异。
- 使用 faster-whisper,资料称比 openai-whisper 快 4 倍。
- 关注显存,按硬件选择模型规格。
性能
资料给出的实时因子(real-time factor):
| 模型 | CPU 实时因子 | GPU 实时因子 |
|---|---|---|
| tiny | ~0.32 | ~0.01 |
| base | ~0.16 | ~0.01 |
| turbo | ~0.08 | ~0.01 |
| large | ~1.0 | ~0.05 |
实时因子 0.1 表示比实时快 10 倍。
语言支持
资料列出的主要支持语言包括:英语(en)、西班牙语(es)、法语(fr)、德语(de)、意大利语(it)、葡萄牙语(pt)、俄语(ru)、日语(ja)、韩语(ko)、中文(zh),总计 99 种语言。
限制
- 幻觉:可能重复或编造文本。
- 长音频准确率:超过 30 分钟会下降。
- 无说话人识别(无 diarization)。
- 口音:质量因口音而异。
- 背景噪声:可能影响准确率。
- 实时延迟:不适合实时字幕。
资源
- GitHub:https://github.com/openai/whisper (72,900+ 星标)
- 论文:https://arxiv.org/abs/2212.04356
- 模型卡:https://github.com/openai/whisper/blob/main/model-card.md
- Colab:仓库中提供
- 许可证:MIT
指标
- GitHub 星标:72,900+
- 支持语言:99 种
- 训练音频:680,000 小时
- 许可证:MIT