發表文章

目前顯示的是 4月, 2025的文章

[AI] Whisper 語音轉文字完整教學:支援中文翻譯、SRT 字幕、自動切段與 MPS 加速

圖片
這篇教學將帶你一步步完成 Whisper 語音轉錄系統的建置,支援多語言轉換、字幕輸出、自動分段,並支援 Apple M1/M2 裝置使用 MPS 加速,適合 macOS 用戶。 🛠️ 前置準備 1. 安裝 Python 環境(建議 Python 3.10 以上) 建議使用 pyenv 管理多版本 Python: brew install pyenv pyenv install 3.10.13 pyenv global 3.10.13 或確認你系統已具備適當版本: python3 --version 2. 建立虛擬環境與安裝套件 python3 -m venv whisper-env source whisper-env/bin/activate pip install -U pip setuptools wheel pip install torch torchaudio --index-url https://download.pytorch.org/whl/cpu pip install transformers pip install ffmpeg-python macOS M1/M2 用戶將自動啟用 MPS 加速(使用 Metal) 3. 安裝 ffmpeg brew install ffmpeg ▶️ 執行方式 將完整程式碼儲存為 run_whisper.py 後,在終端機執行: python run_whisper.py 執行流程會引導你選擇語音處理模式與音訊檔案,並於處理完成後輸出: xxx_transcribed.txt (純文字稿) xxx.srt (字幕檔) 📄 完整程式碼 請將下方完整 Python 程式碼貼入這裡: #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import subprocess import torch import time from datetime import timedelta from difflib import SequenceMatcher from concurrent.futures import ThreadPoolExecutor, as_c...

[AI] PowerShell 腳本筆記: Whisper 模型快取檔案

圖片
PowerShell 腳本筆記:清除 Whisper 模型快取檔案 Whisper / WhisperX 模型會快取在 C:\Users\你的帳號\.cache\huggingface 中,可能佔用大量磁碟空間。 以下腳本可自動掃描快取目錄中與 Whisper 相關的模型,列出清單並可手動選擇刪除,有助於釋放磁碟空間。 📜 腳本內容 # PowerShell script to find and optionally delete cached Whisper model directories from Hugging Face $cacheDir = Join-Path $Env:USERPROFILE '.cache\huggingface\hub' if (-not (Test-Path $cacheDir)) { Write-Host "Hugging Face cache directory not found." return } # Get all model cache directories under the hub that contain "whisper" $whisperDirs = Get-ChildItem -Path $cacheDir -Directory | Where-Object { $_.Name -like '*whisper*' } | Sort-Object Name if ($whisperDirs.Count -eq 0) { Write-Host "No Whisper model caches found." return } # List found model caches with their sizes Write-Host "Whisper model caches found:" $index = 1 foreach ($dir in $whisperDirs) { # Extract model name from directory (everything after the second ...