
Whisper 是一款通用語音辨識模型的工具程式,透過 AI 精準捕捉每一句話,可以取得逐字稿,把 YouTube / Podcast 影片/聲音轉換為文字檔,可以省下慢慢觀看或收聽的時間,以提升學習效率。
Whisper 安裝方法
以下都是官方的教學,參考:
https://github.com/openai/whisper/
安裝 python
我所安裝的版本是 3.11.9, 目前建議的版本有 3.10.11, 或 3.12.6
安裝 Whisaper
你如果已經安裝過,請先進行移除Whisper,使用以下命令移除 Whisper:
pip install -U openai-whisper
全新的安裝可以使用以下命令:
pip install git+https://github.com/openai/whisper.git
曾經安裝過,無腦進行升級,請執行指令:
pip install --upgrade --no-deps --force-reinstall git+https://github.com/openai/whisper.git
安裝 ffmpeg
此外,還需要在您的系統上安裝命令列工具 ffmpeg,比較簡單的作法是連到 ffmpeg 的 github 進行下載:
https://github.com/BtbN/FFmpeg-Builds/releases
我自己是手動下載執行檔, 並放置到系統的 path 路徑下, 教學如下:
Windows 11 設定環境變數, 以加入 ffmpeg 指令為例
https://max-everyday.com/2023/01/windows-11-environment-variables/
ffmpeg工具可透過第三方的軟體套件管理器取得:
在 Ubuntu 或 Debian 系統上
sudo apt update && sudo apt install ffmpeg
在 Arch Linux 系統上
sudo pacman -S ffmpeg
在 macOS 系統上使用 Homebrew (https://brew.sh/)
brew install ffmpeg
在 Windows 系統上使用 Chocolatey (https://chocolatey.org/)
choco install ffmpeg
在 Windows 上使用 Scoop (https://scoop.sh/)
scoop install ffmpeg
要安裝 Rust套件
執行:
pip install setuptools-rust
Whisper 可用模型和語言
共有六種模型大小。
模型 / 檔案大小 / 所需顯存 / 相對速度
- tiny 39 MB ~1 GB ~10 倍
- base 74 MB ~1 GB ~7 倍
- small 244 MB ~2 GB ~4 倍
- medium 769 MB ~5 GB ~2 倍
- large 1550 MB ~10 GB 1 倍
- turbo 809 MB ~6 GB ~8 倍
turbo 型號是 large-v3 的最佳化版本,在保證準確性的前提下,可提供更快的轉錄速度。
不趕時間的話,建議使用 large, 慢慢去辦識。
不建議使用 base 與 tiny,雖然功能是可以用,都要花時間辦識了,還要花更多時間去校對。
這些模型,不需要我們自己手動下載,whisper 會自動幫我們下載在 C:\Users\{account}\.cache 目錄下。如果確定這台電腦未來用不到 whisper, 不想浪費儲存空間放用不到的檔案, 可以到這個資料夾來清除快取的檔案。
Whisper 命令列用法
以下命令將使用 Turbo 模型轉錄音訊檔案中的語音:
whisper audio.mp3 --language Chinese --model turbo
預設設定(選擇 Turbo 模型)適用於轉錄英文。但是,Turbo 模型並非為翻譯任務而訓練。如果您需要將非英語語音翻譯成英語,請使用多語言模型(tiny、base、small、medium、large)之一,而不是 Turbo。
執行以下命令查看所有可用選項:
whisper --help
如果遇到問題:
Skipping audio.mp3 due to ValueError: Expected parameter logits (Tensor of shape (5, 51866)) of distribution Categorical(logits: torch.Size([5, 51866])) to satisfy the constraint IndependentConstraint(Real(), 1), but found invalid values:
tensor([[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan],
[nan, nan, nan, ..., nan, nan, nan]], device='cuda:0')
請改用指令:
whisper audio.mp3 --language Chinese --device cpu --model turbo
下載 YouTube 影片指令:
yt-dlp --extractor-args "youtube:player_client=default" URL
下載 YouTube 影片為 mp3 指令:
yt-dlp --audio-format mp3 --extractor-args "youtube:player_js_version=actual" URL
影片或音檔轉為單聲道:
ffmpeg -i input.mp3 -ac 1 output_mono.mp3