OpenAI Whisper:97.2k Stars 通用语音识别完全指南
posts posts 2026-04-06T22:50:00+08:00全面介绍97.2k Stars的OpenAI Whisper语音识别模型,详解Transformer seq2seq架构、6种模型规模、命令行/Python API使用、多语言翻译、Faster Whisper加速、LangChain集成和微调训练。技术笔记Whisper, 语音识别, OpenAI, ASR, 语音翻译, 多语言, Transformer, Python目录
学习目标
通过本文,你将全面掌握以下核心能力:
- 深入理解 Whisper 的技术原理和架构设计
- 学会安装配置 Whisper 环境和依赖
- 掌握不同模型规模的选择和性能对比
- 学会使用命令行和 Python 进行语音识别
- 理解 Whisper 的多语言翻译和语言识别功能
- 掌握性能优化和微调技巧
- 了解 Whisper 的生态集成和应用场景
1. 项目概述
1.1 是什么
Whisper 是 OpenAI 发布的通用语音识别模型。它在大规模多样化音频数据集上训练,是一个多任务模型,可以执行多语言语音识别、语音翻译和语言识别。
1.2 核心数据
| 指标 | 数值 |
|---|---|
| GitHub Stars | 97.2k |
| GitHub Forks | 12k |
| Watchers | 721 |
| Contributors | 82+ |
| Branches | 15 |
| Tags | 13 |
| License | MIT |
| 语言 | Python 100% |
1.3 技术架构
Whisper 基于 Transformer 序列到序列(sequence-to-sequence)模型架构,训练完成多种语音处理任务:
| 任务 | 说明 |
|---|---|
| 多语言语音识别 | 将语音转写为文字 |
| 语音翻译 | 将非英语语音翻译为英语 |
| 语言识别 | 识别音频所属语言 |
| 语音活动检测 | 检测是否有人说话 |
这些任务被统一表示为 token 序列,由 decoder 预测,实现单一模型替代传统语音处理管道的多个阶段。
1.4 技术特点
| 特点 | 说明 |
|---|---|
| 端到端 | 无需传统语音识别的音素识别、发音词典等组件 |
| 多语言 | 支持 100+ 语言 |
| 鲁棒性 | 在多样化音频上训练,抗噪声能力强 |
| 多功能 | 识别 + 翻译 + 语言识别 |
2. 环境配置
2.1 系统依赖
Whisper 依赖 ffmpeg,需要提前安装:
# Ubuntu / Debian
sudo apt update && sudo apt install ffmpeg
# Arch Linux
sudo pacman -S ffmpeg
# macOS (Homebrew)
brew install ffmpeg
# Windows (Chocolatey)
choco install ffmpeg
# Windows (Scoop)
scoop install ffmpeg2.2 Python 环境
Whisper 使用 Python 3.9.9 和 PyTorch 1.10.1 训练,但兼容 Python 3.8-3.11 和最新 PyTorch 版本:
# 创建虚拟环境
python -m venv whisper-env
source whisper-env/bin/activate # Linux/macOS
# 或
whisper-env\Scripts\activate # Windows
# 安装 PyTorch(建议 CPU 版本)
pip install torch torchaudio
# 安装 Whisper
pip install -U openai-whisper
# 或从源码安装最新版本
pip install git+https://github.com/openai/whisper.git2.3 可选依赖
# tiktoken(快速分词器,Rust 实现)
pip install tiktoken
# Rust 编译工具链(如 tiktoken 安装失败)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
export PATH="$HOME/.cargo/bin:$PATH"
# setuptools-rust(如需要)
pip install setuptools-rust3. 模型选择
3.1 模型规模对比
Whisper 提供 6 种模型规模,以及英语专用版本:
| 规模 | 参数量 | 英语模型 | 多语言模型 | 显存需求 | 相对速度 |
|---|---|---|---|---|---|
| tiny | 39M | tiny.en | tiny | ~1GB | ~10x |
| base | 74M | base.en | base | ~1GB | ~7x |
| small | 244M | small.en | small | ~2GB | ~4x |
| medium | 769M | medium.en | medium | ~5GB | ~2x |
| large | 1550M | N/A | large | ~10GB | 1x |
| turbo | 809M | N/A | turbo | ~6GB | ~8x |
3.2 模型选择建议
| 场景 | 推荐模型 | 说明 |
|---|---|---|
| 资源极度受限 | tiny / base | CPU 可运行,速度快 |
| 日常使用 | small / medium | 平衡速度和质量 |
| 高质量需求 | large / turbo | 最佳识别质量 |
| 英语场景 | .en 系列 | 英语专用模型效果更好 |
| 翻译任务 | medium / large | turbo 不支持翻译 |
注意:turbo 模型是 large-v3 的优化版本,推理速度更快但不支持翻译任务。
3.3 下载和使用
模型会在首次使用时自动下载。也可手动下载:
import whisper
# 首次使用时自动下载
model = whisper.load_model("base")
# 或手动指定路径
model = whisper.load_model("base", download_root="./models")4. 命令行使用
4.1 基础识别
# 使用默认模型(turbo)转写音频
whisper audio.flac audio.mp3 audio.wav
# 指定语言
whisper japanese.wav --language Japanese
# 指定模型
whisper audio.wav --model small
# 翻译为英语(需使用多语言模型)
whisper japanese.wav --model medium --language Japanese --task translate
# 输出所有信息(包含语言识别结果)
whisper audio.wav --model medium --task translate --verbose True
# 纯音频(无说话人分离)
whisper audio.wav4.2 高级选项
# 指定输出格式
whisper audio.wav --model medium --format json # JSON
whisper audio.wav --model medium --format srt # SRT字幕
whisper audio.wav --model medium --format vtt # VTT字幕
whisper audio.wav --model medium --format txt # 纯文本
# 温度采样(创造性)
whisper audio.wav --model medium --temperature 0.0 # 确定性强
whisper audio.wav --model medium --temperature 1.0 # 创造性强
# 条件分割(用于长音频)
whisper audio.wav --model medium --condition_on_previous_text True
# 忽略无语音片段
whisper audio.wav --model medium --word_timestamps True
# 查看帮助
whisper --help4.3 批量处理
# 处理多个文件
whisper audio1.wav audio2.mp3 audio3.flac --model small
# 使用通配符
whisper "*.wav" --model small
# 指定输出目录
whisper audio.wav --model small --output_dir ./transcripts5. Python API 使用
5.1 基础转写
import whisper
import torch
# 检测 GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
# 加载模型
model = whisper.load_model("base")
model = model.to(device)
# 转写音频
result = model.transcribe("audio.mp3")
print(result["text"])5.2 详细结果
import whisper
model = whisper.load_model("medium")
# 获取详细结果
result = model.transcribe(
"audio.mp3",
verbose=True, # 打印进度
task="transcribe", # 或 "translate"
language="zh", # 指定语言
temperature=0.0, # 采样温度
condition_on_previous_text=True, # 条件生成
initial_prompt="...", # 初始提示
word_timestamps=True, # 词级时间戳
)
# 完整返回结构
print(result.keys())
# dict_keys(['text', 'segments', 'language'])
# 文本内容
print(result["text"])
# 段落级别信息
for segment in result["segments"]:
print(f"[{segment['start']:.2f}s - {segment['end']:.2f}s] {segment['text']}")
print(f" 语言: {segment.get('language', 'N/A')}")
print(f" 置信度: {segment.get('avg_logprob', 'N/A'):.2f}")
# 词级时间戳
for segment in result["segments"]:
for word in segment.get("words", []):
print(f"{word['word']}: {word['start']:.2f}s - {word['end']:.2f}s")5.3 语言检测
import whisper
model = whisper.load_model("base")
# 加载音频
audio = whisper.load_audio("audio.mp3")
audio = whisper.pad_or_trim(audio)
# 生成梅尔频谱
mel = whisper.log_mel_spectrogram(audio, n_mels=model.dims.n_mels).to(model.device)
# 检测语言
_, probs = model.detect_language(mel)
detected_lang = max(probs, key=probs.get)
print(f"检测到语言: {detected_lang}")
print(f"语言概率: {probs[detected_lang]:.2%}")5.4 低级 API
import whisper
model = whisper.load_model("turbo")
# 加载并处理音频
audio = whisper.load_audio("audio.mp3")
audio = whisper.pad_or_trim(audio)
# 生成梅尔频谱
mel = whisper.log_mel_spectrogram(audio, n_mels=model.dims.n_mels).to(model.device)
# 解码
options = whisper.DecodingOptions(
task="transcribe",
language="zh",
temperature=0.0,
)
result = whisper.decode(model, mel, options)
print(f"文本: {result.text}")
print(f"语言: {result.language}")
print(f"是否完成: {result.no_speech_prob:.2f}")6. 性能优化
6.1 GPU 加速
import torch
import whisper
# 检查 CUDA
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"GPU: {torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'None'}")
# 使用 GPU
model = whisper.load_model("medium")
model = model.to("cuda")
# 转写
result = model.transcribe("audio.mp3")6.2 批量处理
import whisper
from concurrent.futures import ThreadPoolExecutor
model = whisper.load_model("small")
audio_files = ["audio1.mp3", "audio2.mp3", "audio3.mp3"]
def transcribe(file):
result = model.transcribe(file)
return file, result["text"]
# 多线程处理
with ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(transcribe, audio_files))
for file, text in results:
print(f"{file}: {text}")6.3 长音频处理
import whisper
model = whisper.load_model("medium")
# 处理长音频(自动分chunk)
result = model.transcribe(
"long_audio.mp3",
chunk_length=30, # chunk 长度(秒)
stride_length=5, # 相邻 chunk 重叠(秒)
condition_on_previous_text=True, # 使用前一个 chunk 的文本
)
print(result["text"])6.4 内存优化
import whisper
import torch
# CPU 内存优化:使用小模型
model = whisper.load_model("tiny")
# GPU 内存优化:半精度
model = whisper.load_model("medium").to("cuda").half()
# 批处理大小
result = model.transcribe(
"audio.mp3",
batch_size=8, # 降低内存占用
)7. 微调训练
7.1 训练数据准备
# 准备微调数据(Whisper 格式)
from whisper import loadTokenizer
tokenizer = whisper.load_tokenizer()
# 你的训练数据格式:
# {
# "audio": "/path/to/audio.wav",
# "text": "转写文本内容"
# }7.2 微调命令
# 使用 Hugging Face Transformers 微调
pip install transformers datasets
python train.py \
--model_name_or_path openai/whisper-small \
--dataset_name mozilla-foundation/common_voice_11_zh-CN \
--language zh \
--output_dir ./whisper-finetuned \
--num_train_epochs 3 \
--per_device_train_batch_size 8 \
--learning_rate 1e-57.3 微调注意事项
| 注意事项 | 说明 |
|---|---|
| 数据质量 | 微调数据质量直接影响模型效果 |
| 语言适配 | 使用目标语言的音频和文本 |
| 时长 | 建议总时长 > 100 小时 |
| 预处理 | 音频需重采样为 16kHz |
8. 生态集成
8.1 Hugging Face Transformers
from transformers import pipeline
# 使用 Hugging Face pipeline
whisper = pipeline(
"automatic-speech-recognition",
model="openai/whisper-small"
)
result = whisper("audio.mp3")
print(result["text"])8.2 LangChain
from langchain_community.tools import WhisperTool
from langchain.agents import Agent
# 创建 Whisper 工具
whisper_tool = WhisperTool()
# 使用工具
result = whisper_tool.run("audio.mp3")8.3 Faster Whisper(加速版)
from faster_whisper import WhisperModel
# 加载加速版模型(CUDA)
model = WhisperModel(
"medium",
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}")8.4 WhisperX(带时间戳对齐)
import whisperx
# 加载模型和音频
model = whisperx.load_model("medium", device="cuda")
audio = whisperx.load_audio("audio.mp3")
# 转写
result = model.transcribe(audio, batch_size=8)
# 时间戳对齐
result = whisperx.align(
result["segments"],
model,
audio,
device="cuda"
)
# 获取带时间戳的词
for segment in result["segments"]:
for word in segment["words"]:
print(f"{word['word']}: {word['start']:.2f}s - {word['end']:.2f}s")9. 常见问题
9.1 安装问题
问题:No module named 'setuptools_rust'
解决:
pip install setuptools-rust问题:ffmpeg 找不到
解决:确保 ffmpeg 已安装并添加到 PATH
9.2 识别质量问题
问题:识别结果不准确
解决:
| 方法 | 说明 |
|---|---|
| 使用更大模型 | small → medium → large |
| 指定语言 | --language Chinese |
| 提供初始提示 | --initial_prompt "以下是一段中文对话" |
| 清除背景音 | 使用降噪工具预处理音频 |
9.3 翻译问题
问题:turbo 模型翻译效果差
解决:使用 medium 或 large 模型进行翻译,turbo 不适合翻译任务
9.4 速度问题
问题:推理速度慢
解决:
# 使用加速版
pip install faster-whisper
# 或使用更小的模型
whisper audio.wav --model tiny10. 应用场景
10.1 音频转写
| 场景 | 说明 |
|---|---|
| 会议记录 | 自动生成会议文字稿 |
| 播客字幕 | 为视频生成字幕 |
| 采访转写 | 将采访录音转为文字 |
| 有声书 | 将语音转为文本 |
10.2 语音翻译
| 场景 | 说明 |
|---|---|
| 实时翻译 | 将外语演讲实时翻译 |
| 内容本地化 | 将外语视频本地化 |
| 跨国会议 | 多语言会议翻译 |
10.3 语言识别
| 场景 | 说明 |
|---|---|
| 多语言处理 | 确定音频语言后选择对应模型 |
| 内容审核 | 检测音频语种分布 |
| 语音分割 | 用于说话人分离预处理 |
11. 总结
OpenAI Whisper 是目前最强大的开源语音识别模型之一:
| 维度 | 评价 |
|---|---|
| 识别准确率 | ⭐⭐⭐⭐⭐ WER 低至 4% |
| 多语言支持 | ⭐⭐⭐⭐⭐ 100+ 语言 |
| 鲁棒性 | ⭐⭐⭐⭐⭐ 嘈杂环境表现好 |
| 易用性 | ⭐⭐⭐⭐⭐ pip 一键安装 |
| 生态 | ⭐⭐⭐⭐ 多种集成可选 |
适用场景:
- 会议记录和字幕生成
- 多语言语音翻译
- 有声书转文字
- 语音分析和研究
- 无障碍辅助
官方资源:
- GitHub:https://github.com/openai/whisper
- Blog:https://openai.com/blog/whisper
- Paper:https://arxiv.org/abs/2212.04356
- Model Card:https://github.com/openai/whisper/blob/main/model-card.md
- Colab:https://colab.research.google.com/github/openai/whisper/blob/master/notebooks/LibriSpeech.ipynb