Whisper

部分內容由 LLM 生成,尚未經過人工驗證。

Whisper 是 OpenAI 開發的自動語音辨識(ASR)模型,可將音訊轉錄為文字並生成時間戳。

安裝

使用 pip 安裝

pip install -U openai-whisper

安裝 ffmpeg(必要依賴)

Whisper 需要 ffmpeg 來處理音訊檔案:

# 使用 Chocolatey
choco install ffmpeg

# 或使用 Scoop
scoop install ffmpeg
brew install ffmpeg
# Ubuntu/Debian
sudo apt update && sudo apt install ffmpeg

# Arch Linux
sudo pacman -S ffmpeg

可用模型

模型參數量僅英文模型多語言模型所需 VRAM相對速度
tiny39 Mtiny.entiny~1 GB~10x
base74 Mbase.enbase~1 GB~7x
small244 Msmall.ensmall~2 GB~4x
medium769 Mmedium.enmedium~5 GB~2x
large1550 MN/Alarge~10 GB1x
turbo809 MN/Aturbo~6 GB~8x
.en 模型僅支援英文,但在英文辨識上表現更好。多語言模型可自動偵測語言。

基本用法

命令列介面

whisper audio.mp3 --model base

常用參數

參數說明範例
--model指定模型--model medium
--language指定語言--language zh
--task任務類型(transcribe/translate)--task translate
--output_dir輸出目錄--output_dir ./output
--output_format輸出格式--output_format srt
--device指定設備--device cuda

輸出格式

格式說明
txt純文字(無時間戳)
vttWebVTT 字幕格式
srtSRT 字幕格式
tsvTab 分隔值(含時間戳)
jsonJSON 格式(含詳細資訊)
all輸出所有格式

常用指令範例

基本轉錄

# 使用預設模型轉錄
whisper audio.mp3

# 指定模型
whisper audio.mp3 --model medium

# 指定語言(加快處理速度)
whisper audio.mp3 --model medium --language zh

輸出字幕檔

# 輸出 SRT 字幕
whisper audio.mp3 --model base --output_format srt

# 輸出所有格式
whisper audio.mp3 --model base --output_format all --output_dir ./subtitles

翻譯為英文

# 將任何語言翻譯為英文
whisper audio.mp3 --model medium --task translate

使用 GPU 加速

# 使用 CUDA(NVIDIA GPU)
whisper audio.mp3 --model large --device cuda

# 指定 GPU 編號
whisper audio.mp3 --model large --device cuda:0

進階參數

參數說明預設值
--temperature取樣溫度0
--best_of候選數量5
--beam_sizeBeam search 大小5
--patienceBeam search patience1.0
--initial_prompt初始提示文字None
--condition_on_previous_text參考前文True
--word_timestamps詞級時間戳False

詞級時間戳

whisper audio.mp3 --model base --word_timestamps True

使用初始提示

# 提供領域術語或格式提示
whisper audio.mp3 --model medium --initial_prompt "這是一段關於機器學習的演講"

效能優化

記憶體不足時

# 使用較小模型
whisper audio.mp3 --model tiny

# 或使用 CPU(較慢但不需 GPU 記憶體)
whisper audio.mp3 --model medium --device cpu

加速處理

# 使用 turbo 模型(速度與品質平衡)
whisper audio.mp3 --model turbo

# 指定語言(跳過語言偵測)
whisper audio.mp3 --model base --language en

常見問題

支援的音訊格式

Whisper 透過 ffmpeg 支援多種格式:

  • MP3, WAV, FLAC, AAC, OGG, M4A
  • 影片檔案(自動提取音訊):MP4, MKV, AVI, MOV

最佳實踐

  1. 選擇適當模型:一般用途使用 basesmall,需要高準確度使用 mediumlarge
  2. 指定語言:已知語言時指定 --language 可加快處理
  3. 音訊品質:清晰的音訊可顯著提升辨識準確度
  4. GPU 加速:有 NVIDIA GPU 時使用 --device cuda

相關主題