环境准备
先在本地装个 Python 3.8+。Windows 用户记得勾选 "Add Python to PATH",不然后面命令跑不起来。装完打开终端输 python --version 看看版本号。
安装 Whisper 核心库
直接用 pip 装 openai-whisper。国内网络慢的话可以加清华镜像源。
pip install openai-whisper -i https://pypi.tuna.tsinghua.edu.cn/simple
注意:Whisper 处理音频强依赖 FFmpeg。Windows 用户得去官网下载并配置到环境变量里,否则转写时容易报找不到 ffmpeg 的错误。
模型下载与管理
默认情况下,第一次运行会自动下载模型文件到缓存目录。如果想提前准备好,特别是用大模型 large-v3,建议手动指定。
pip install "openai-whisper[large-v3]"
模型通常缓存在 ~/.cache/whisper/ 下,Windows 路径类似 C:\Users\用户名\.cache\whisper\。
命令行快速转写
最简单的用法就是直接在终端跑命令。
whisper D:\Net_Program\test\whisper-test.wav --model large-v3 --language Chinese
参数说明一下:--model 选模型大小,越大越准但吃资源;--language 强制指定中文能避免自动检测翻车;--format 决定输出 txt 还是 srt。
Python 脚本集成
如果要在项目里调用,封装成函数更灵活。下面这个脚本做了些额外处理,比如检查 FFmpeg 是否就绪,以及把结果里的繁体字转成简体。
import whisper
import os
import pathlib
import subprocess
from zhconv import convert
def check_ffmpeg():
"""检查 FFmpeg 是否安装并配置正确"""
try:
subprocess.run(
["ffmpeg", "-version"],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
return True
except FileNotFoundError:
print("错误:未找到 FFmpeg 工具,请先安装并配置环境变量")
Exception e:
()
():
check_ffmpeg():
audio_path = (pathlib.Path(audio_path).resolve())
os.path.exists(audio_path):
()
os.path.isfile(audio_path):
()
:
()
model = whisper.load_model(model_name, device=)
()
result = model.transcribe(
audio=audio_path,
language=,
verbose=,
fp16=,
initial_prompt=
)
simplified_text = convert(result[], )
output_dir =
os.makedirs(output_dir, exist_ok=)
audio_name = os.path.splitext(os.path.basename(audio_path))[]
output_path = os.path.join(output_dir, )
(output_path, , encoding=) f:
f.write(simplified_text)
()
simplified_text
Exception e:
()
__name__ == :
:
zhconv
ImportError:
()
subprocess.run([, , ], check=)
zhconv
audio_file =
transcribe_audio(audio_file)

