关于 whisper
Whisper 是 OpenAI 的多语言语音识别模型,可转录音频并将语音翻译成英文。本指南涵盖模型选择、时间戳、批量处理及命令行用法,适用于转录工作流。
主要功能
- 01多语言音频转录
- 02语音翻译为英文
- 03词级时间戳
- 04批量转录音频文件
- 05TXT 与 SRT 输出
适用场景
- 01转录播客和录音
- 02根据音频生成会议记录
- 03为视频生成字幕
- 04将语音内容翻译成英文
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ kalarislabs/ research-agent-skills --skill whisper安装后在 Claude Code 中输入/whisper确认已加载。
附带文件references/languages.md
详细介绍
Whisper - 稳健的语音识别
OpenAI 的多语言语音识别模型。
何时使用 Whisper
适用场景:
- 语音转文字(99 种语言)
- 播客/视频转录
- 自动生成会议纪要
- 翻译成英语
- 嘈杂音频转录
- 多语言音频处理
指标:
- 支持 99 种语言
- 使用 680,000 小时音频进行训练
- MIT License
可改用以下替代方案:
- AssemblyAI:托管 API,支持说话人分离
- Deepgram:实时流式 ASR
- Google Speech-to-Text:基于云的服务
快速入门
安装
# 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
基础转录
import whisper
# Load model
model = whisper.load_model("base")
# Transcribe
result = model.transcribe("audio.mp3")
# Print text
print(result["text"])
# Access segments
for segment in result["segments"]:
print(f"[{segment['start']:.2f}s - {segment['end']:.2f}s] {segment['text']}")
模型大小
# Available models
models = ["tiny", "base", "small", "medium", "large", "turbo"]
# Load specific model
model = whisper.load_model("turbo") # Fastest, good quality
| 模型 | 参数量 | 仅限英语 | 多语言 | 速度 | VRAM |
|---|---|---|---|---|---|
| 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 进行原型开发
转录选项
指定语言
# Auto-detect language
result = model.transcribe("audio.mp3")
# Specify language (faster)
result = model.transcribe("audio.mp3", language="en")
# Supported: en, es, fr, de, it, pt, ru, ja, ko, zh, and 89 more
选择任务
# Transcription (default)
result = model.transcribe("audio.mp3", task="transcribe")
# Translation to English
result = model.transcribe("spanish.mp3", task="translate")
# Input: Spanish audio → Output: English text
初始提示词
# Improve accuracy with context
result = model.transcribe(
"audio.mp3",
initial_prompt="This is a technical podcast about machine learning and AI."
)
# Helps with:
# - Technical terms
# - Proper nouns
# - Domain-specific vocabulary
时间戳
# Word-level timestamps
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)")
温度回退
# Retry with different temperatures if confidence low
result = model.transcribe(
"audio.mp3",
temperature=(0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
)
命令行用法
# Basic transcription
whisper audio.mp3
# Specify model
whisper audio.mp3 --model turbo
# Output formats
whisper audio.mp3 --output_format txt # Plain text
whisper audio.mp3 --output_format srt # Subtitles
whisper audio.mp3 --output_format vtt # WebVTT
whisper audio.mp3 --output_format json # JSON with timestamps
# Language
whisper audio.mp3 --language Spanish
# Translation
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)
# Save to file
output_file = audio_file.replace(".mp3", ".txt")
with open(output_file, "w") as f:
f.write(result["text"])
实时转录
# For streaming audio, use faster-whisper
# pip install faster-whisper
from faster_whisper import WhisperModel
model = WhisperModel("base", device="cuda", compute_type="float16")
# Transcribe with streaming
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
# Automatically uses GPU if available
model = whisper.load_model("turbo")
# Force CPU
model = whisper.load_model("turbo", device="cpu")
# Force GPU
model = whisper.load_model("turbo", device="cuda")
# 10-20× faster on GPU
与其他工具集成
字幕生成
# Generate SRT subtitles
whisper video.mp4 --output_format srt --language English
# Output: video.srt
与 LangChain 配合使用
from langchain.document_loaders import WhisperTranscriptionLoader
loader = WhisperTranscriptionLoader(file_path="audio.mp3")
docs = loader.load()
# Use transcription in RAG
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
vectorstore = Chroma.from_documents(docs, OpenAIEmbeddings())
从视频中提取音频
# Use ffmpeg to extract audio
ffmpeg -i video.mp4 -vn -acodec pcm_s16le audio.wav
# Then transcribe
whisper audio.wav
最佳实践
- 使用 turbo 模型 - 英语场景下速度和质量最佳
- 指定语言 - 比自动检测更快
- 添加初始提示词 - 提升技术术语的识别效果
- 使用 GPU - 速度提升 10-20×
- 批量处理 - 效率更高
- 转换为 WAV - 兼容性更好
- 拆分长音频 - 每段 <30 分钟
- 检查语言支持 - 质量因语言而异
- 使用 faster-whisper - 比 openai-whisper 快 4×
- 监控 VRAM - 根据硬件调整模型大小
性能
| 模型 | 实时系数(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 分钟时准确度会下降
- 说话人识别 - 不支持说话人分离
- 口音 - 质量因口音而异
- 背景噪声 - 可能影响准确度
- 实时延迟 - 不适用于实时字幕
资源
- 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
Agent 操作流程
- 检查环境。 确认 GPU 内存、模型检查点及许可证、框架版本和输入格式。
- 明确输入。 根据数据或用户确认格式、标识符和参数。任何会影响结果的值都应先询问,不要猜测。
- 先运行小规模任务。 先以低分辨率或较短长度对单个样本运行推理。
- 使用上述说明和参考资料执行完整任务。
- 验证结果。 通过定性检查和任务指标检查输出;检查生成内容的许可证限制。
- 报告。 说明运行了什么(版本、命令、参数)、检查了什么,以及还有哪些不确定之处。
| 如果出现这种情况 | 请这样做 |
|---|---|
| 大型输入导致内存不足 | 降低分辨率或序列长度、使用半精度,或对输入进行分块。 |
| 这些说明中的函数、标志或端点在已安装版本中不存在 | 查阅已安装版本自身的文档(help()、--help、官方文档),进行调整,并告知用户。切勿编造 API。 |
| 必需的输入、标识符或参数不明确 | 询问用户,或在运行前明确说明所采用的假设。 |
诚信规则
- 切勿捏造结果、参数、标识符、引用或统计数据。如果无法运行或验证某项内容,请如实说明。
- 在商业用途或发布内容之前,检查模型和数据集的许可证。
- 此处与版本相关的细节可能已经过时:请对照已安装版本的官方文档确认。
- 对于会产生费用、占用共享 GPU 或云配额、涉及个人或患者数据,或无法撤销的操作,请先征求同意。
相关技能
transformers:Hugging Face Transformers,用于加载 Hub 模型、运行流水线推理、生成文本,以及在 NLP、视觉、音频…领域进行 Trainer 微调
评论
支持 Markdown