Whisper环境搭建:macOS Monterey平台最佳实践

云信安装大师
90
AI 质量分
4 5 月, 2025
6 分钟阅读
0 阅读

Whisper环境搭建:macOS Monterey平台最佳实践

引言

Whisper是OpenAI开源的语音识别系统,能够将语音转换为文本。本文将详细介绍在macOS Monterey系统上搭建Whisper环境的完整流程,包括Python环境配置、依赖安装以及模型下载等步骤。

准备工作

在开始之前,请确保你的macOS Monterey系统满足以下要求:

  • macOS Monterey (12.0或更高版本)
  • Python 3.8或更高版本
  • Homebrew包管理器
  • 至少16GB内存(运行大型模型需要)
  • 推荐使用M1/M2芯片的Mac(性能更好)

步骤一:安装Homebrew

Homebrew是macOS上的包管理器,可以简化软件安装过程。

代码片段
# 安装Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 将Homebrew添加到PATH环境变量
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc

注意事项
1. 如果已经安装过Homebrew,可以跳过此步骤
2. 安装完成后建议运行brew doctor检查是否有问题

步骤二:安装Python和必要工具

代码片段
# 使用Homebrew安装Python和ffmpeg
brew install python ffmpeg

# 验证Python版本(需要3.8+)
python3 --version

# 安装pip工具(Python包管理器)
python3 -m ensurepip --upgrade

步骤三:创建虚拟环境

虚拟环境可以隔离项目依赖,避免污染全局Python环境。

代码片段
# 创建项目目录并进入
mkdir whisper_project && cd whisper_project

# 创建虚拟环境
python3 -m venv venv

# 激活虚拟环境
source venv/bin/activate

# 验证是否在虚拟环境中(命令提示符前应有(venv))
which python

原理说明
虚拟环境会创建一个独立的Python运行环境,所有后续安装的包都只会影响当前项目。

步骤四:安装Whisper和相关依赖

代码片段
# 升级pip到最新版本
pip install --upgrade pip

# 安装Whisper核心包和PyTorch(针对Apple Silicon优化)
pip install openai-whisper torch torchaudio torchvision

# Apple Silicon专用加速库(可选但推荐)
pip install tensorflow-metal tensorflow-macos

实践经验
1. M1/M2芯片用户建议安装tensorflow-metal以启用GPU加速
2. PyTorch已原生支持M1/M2芯片的GPU加速

步骤五:下载模型文件

Whisper提供了多种大小的模型,从tiny到large。对于初次使用者,建议从base或small开始。

代码片段
import whisper

# base模型约150MB,适合大多数场景使用
model = whisper.load_model("base")

# large模型约2.9GB,精度最高但速度较慢(需要更多内存)
# model = whisper.load_model("large")

注意事项
1. model会自动下载到~/.cache/whisper目录下
2. large模型需要至少16GB内存才能流畅运行

步骤六:测试Whisper识别功能

创建一个简单的测试脚本test_whisper.py

代码片段
import whisper

def transcribe_audio(file_path):
    # 加载base模型(首次运行会自动下载)
    model = whisper.load_model("base")

    # 执行语音识别(fp16=False适用于非NVIDIA GPU)
    result = model.transcribe(file_path, fp16=False)

    # 输出结果文本和分段信息
    print("识别结果:", result["text"])
    print("\n分段详情:")
    for segment in result["segments"]:
        print(f"[{segment['start']:.2f}s -> {segment['end']:.2f}s] {segment['text']}")

if __name__ == "__main__":
    # macOS下可以使用系统自带的录音文件测试路径示例:
    test_file = "~/Desktop/test_audio.m4a"

    # Windows用户可能需要修改为mp3/wav格式文件路径

    transcribe_audio(test_file)

代码解释
1. load_model()加载指定大小的语音识别模型
2. transcribe()方法执行实际的语音转文字操作
3. fp16=False参数确保在Apple Silicon上正确运行

常见问题解决

Q1: “Could not find module ‘libavformat'”错误

解决方案:

代码片段
brew install ffmpeg libavcodec libavformat libavutil libswresample libswscale sdl2 tesseract zlib openblas lapack rust cmake pkg-config openssl@1.1 xz bzip2 gdbm readline sqlite freetype jpeg openjpeg libpng libtiff webp little-cms2 ghostscript libffi python@3.9 pcre harfbuzz graphite2 icu4c lzo jansson x264 x265 lame opus speex theora vidstab libogg libvorbis libvpx wavpack xvid nasm yasm dav1d aom svt-av1 rav1e kvazaar zimg soxr chromaprint rubberband snappy lz4 zstd brotli gmp nettle unbound libtasn1 p11-kit gnutls fribidi fontconfig freetype harfbuzz graphite2 icu4c lzo jansson x264 x265 lame opus speex theora vidstab libogg libvorbis libvpx wavpack xvid nasm yasm dav1d aom svt-av1 rav1e kvazaar zimg soxr chromaprint rubberband snappy lz4 zstd brotli gmp nettle unbound libtasn1 p11-kit gnutls fribidi fontconfig freetype harfbuzz graphite2 icu4c lzo jansson x264 x265 lame opus speex theora vidstab libogg libvorbis libvpx wavpack xvid nasm yasm dav1d aom svt-av1 rav1e kvazaar zimg soxr chromaprint rubberband snappy lz4 zstd brotli gmp nettle unbound libtasn1 p11-kit gnutls fribidi fontconfig freetype harfbuzz graphite2 icu4c lzo jansson x264 x265 lame opus speex theora vidstab libogg libvorbis libvpx wavpack xvid nasm yasm dav1d aom svt-av1 rav1e kvazaar zimg soxr chromaprint rubberband snappy lz4 zstd brotli gmp nettle unbound libtasn1 p11-kit gnutls fribidi fontconfig freetype harfbuzz graphite2 icu4c lzo jansson x264 x265 lame opus speex theora vidstab libogg libvorbis libvpx wavpack xvid nasm yasm dav1d aom svt-av1 rav1e kvazaar zimg soxr chromaprint rubberband snappy lz4 zstd brotli gmp nettle unbound libtasn1 p11-kit gnutls fribidi fontconfig freetype harfbuzz graphite2 icu4c lzo jansson x264 x265 lame opus speex theora vidstab 

Q2: Apple Silicon性能优化问题

解决方案:

代码片段
# M系列芯片专用优化命令(提升30%以上性能)
export PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 
export PYTORCH_ENABLE_MPS_FALLBACK=0 

# Python脚本中增加以下代码以启用Metal加速:
import torch 
torch.backends.mps.is_available() 
torch.backends.mps.is_built()

Q3: Whisper无法识别中文内容?

解决方法是在transcribe方法中指定语言参数:

代码片段
result = model.transcribe(file_path, language="zh", fp16=False)

macOS专属优化技巧

Tip #01: QuickTime录音直接使用

macOS自带的QuickTime Player可以录制音频:

代码片段
open -a QuickTime\ Player --args -newaudio 

录制完成后保存为.m4a文件即可直接用Whisper处理。

Tip #02: Automator快速转换服务

创建Automator服务实现右键菜单快速转换:

代码片段
新建Automator工作流 → "快速操作" → 
添加"运行Shell脚本"动作 → 
粘贴以下代码并保存为"Whisper转换":

for f in "$@"
do 
   /path/to/venv/bin/python -c "import whisper; print(whisper.load_model('base').transcribe('$f')['text'])" > "${f%.*}.txt"
done 

这样就能右键点击音频文件选择”Whisper转换”自动生成文本。

Tip #03: Siri快捷指令集成

通过Shortcuts应用创建Siri语音指令:

代码片段
获取文件 → 
运行Shell脚本(/path/to/venv/bin/python /path/to/transcribe.py) → 
显示结果 → 
复制到剪贴板  

现在可以说”Hey Siri,转录这段录音”完成操作。

GPU加速验证方法

检查是否启用了Metal加速:

代码片段
import torch 

print(torch.backends.mps.is_available())   # True表示可用  
print(torch.device('mps'))                 # device(type='mps')  

model = whisper.load_model("base").to('mps')  

如果返回True则表明GPU加速已启用。

Python多线程处理技巧

批量处理音频文件时可以使用多线程:

代码片段
from concurrent.futures import ThreadPoolExecutor 

def process_file(file):
    return model.transcribe(file)["text"]

with ThreadPoolExecutor(max_workers=4) as executor:
    results = list(executor.map(process_file, ["file1.mp3", "file2.wav"]))

注意:M系列芯片的最佳线程数是CPU核心数的75%左右。

Whisper API高级用法

除了基本转录外,Whisper还支持:

(01)实时流式转录

代码片段
from io import BytesIO 

stream = BytesIO()  
while audio_stream_active:
   stream.write(get_new_audio_chunk())  
   stream.seek(0)  
   result = model.transcribe(stream)  
   print(result["text"])  
   stream.truncate(0)  

(02)带时间戳的输出

代码片段
result = model.transcribe(audio, verbose=True, word_timestamps=True)

for segment in result["segments"]:
   print(f"[{segment['start']}→{segment['end']}] {segment['text']}")
   for word in segment["words"]:
       print(f"   {word['word']} @ {word['start']:.2f}s")  

(03)多语言混合检测

代码片段
result = model.transcribe(audio, language=None)  

print(f"检测到语言: {result['language']}")  
print(f"置信度: {result['language_probability']:.0%}")  

if result["language"] != "zh":
   zh_result = model.transcribe(audio, language="zh")  

(04)自定义词汇表增强

创建包含专业术语的vocabulary.txt:

代码片段
深度学习   
神经网络   
卷积层   
LSTM单元   
"""

with open("vocabulary.txt") as f:
   vocab = [line.strip() for line in f if line.strip()]

result = model.transcribe(audio, initial_prompt=" ".join(vocab))  

Docker替代方案(可选)

如果不想配置本地环境,可以使用Docker:

代码片段
docker pull ghcr.io/openai/whisper:latest  

docker run --rm \
   -it \
   --device /dev/snd \
   -v $(pwd):/data \
   ghcr.io/openai/whisper \
   --model base \
   --output_dir /data/output \
   /data/input.mp3  

注意:Docker版本无法使用Metal加速。

VSCode开发配置建议

.vscode/settings.json配置:

代码片段
{
 "python.pythonPath": "./venv/bin/python",
 "python.linting.enabled": true,
 "python.linting.pylintEnabled": true,
 "python.formatting.provider": "black",
 "files.exclude": {
     "**/.DS_Store": true,
     "**/.git": true,
     "venv": true,
     "__pycache__": true,
     ".pytest_cache": true,
 }
}

launch.json调试配置:

代码片段
{
 "version": "0.2.0",
 "configurations": [
     {
         "name": "Python: Whisper",
         "type": "python",
         "request": "launch",
         "program": "${file}",
         "args": ["--model", "base"],
         "env": {
             "PYTORCH_ENABLE_MPS_FALLBACK":"0"
         }
     }
 ]
}

Jupyter Notebook集成示例

创建一个新的Notebook并运行:

代码片段
!pip install ipywidgets  

import whisper  
from IPython.display import Audio, display  

model = whisper.load_model("base")  

def on_transcribe(file):  
    display(Audio(file))  
    result = model.transcribe(file.name)  
    print(result["text"])  

uploader = widgets.FileUpload()  
widgets.interactive(on_transcribe, file=uploader)  

display(uploader)

这会在Notebook中创建一个上传按钮,上传后自动播放音频并显示转录结果。

CLI命令行工具封装

创建whisper-cli.py:

代码片段
#!/usr/bin/env python3  

import argparse  
import whisper  

parser = argparse.ArgumentParser()  
parser.add_argument("file", help="Audio file path")  
parser.add_argument("-m", "--model", default="base", help="Model size")   
parser.add_argument("-l", "--language", default=None, help="Force language")  

args = parser.parse_args()  

model = whisper.load_model(args.model)  

if args.language:  
    result = model.transcribe(args.file, language=args.language)   
else:   
    result = model.transcribe(args.file)   

print(result["text"])  

if __name__ == "__main__":   
    import sys   
    sys.exit(main())    

使用方法:

代码片段
chmod +x whisper-cli.py   
./whisper-cli.py input.mp3 -m small > output.txt    
./whisper-cli.py input.wav -l zh > chinese.txt    
mv whisper-cli.py /usr/local/bin/whispy     
now can run `whispy file.m4a` anywhere!    

以上就是完整的macOS Monterey平台Whisper环境搭建指南。按照这些步骤操作后,你应该能够顺利地在Mac上运行语音识别任务。如果有任何问题欢迎留言讨论!

原创 高质量