|
| 1 | +# Local Voice Transcription with whisper.cpp |
| 2 | + |
| 3 | +This guide explains how to build and configure [whisper.cpp](https://github.com/ggerganov/whisper.cpp) for **offline** voice message transcription — no API keys or cloud services required. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +When `VOICE_PROVIDER=local` the bot transcribes Telegram voice messages entirely on your machine using: |
| 8 | + |
| 9 | +| Component | Purpose | |
| 10 | +|---|---| |
| 11 | +| **ffmpeg** | Converts Telegram OGG/Opus audio to 16 kHz mono WAV | |
| 12 | +| **whisper.cpp** | Runs OpenAI's Whisper model locally via optimised C/C++ | |
| 13 | +| **GGML model** | Quantised model weights (downloaded once) | |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- A C/C++ toolchain (`gcc`/`clang`, `cmake`, `make`) |
| 18 | +- `ffmpeg` installed and on PATH |
| 19 | +- ~400 MB disk space for the `base` model (~1.5 GB for `medium`) |
| 20 | + |
| 21 | +## 1. Install ffmpeg |
| 22 | + |
| 23 | +### Ubuntu / Debian |
| 24 | + |
| 25 | +```bash |
| 26 | +sudo apt update && sudo apt install -y ffmpeg |
| 27 | +``` |
| 28 | + |
| 29 | +### macOS (Homebrew) |
| 30 | + |
| 31 | +```bash |
| 32 | +brew install ffmpeg |
| 33 | +``` |
| 34 | + |
| 35 | +### Alpine |
| 36 | + |
| 37 | +```bash |
| 38 | +apk add ffmpeg |
| 39 | +``` |
| 40 | + |
| 41 | +Verify: |
| 42 | + |
| 43 | +```bash |
| 44 | +ffmpeg -version |
| 45 | +``` |
| 46 | + |
| 47 | +## 2. Build whisper.cpp from source |
| 48 | + |
| 49 | +```bash |
| 50 | +# Clone the repository |
| 51 | +git clone https://github.com/ggerganov/whisper.cpp.git |
| 52 | +cd whisper.cpp |
| 53 | + |
| 54 | +# Build with CMake (recommended) |
| 55 | +cmake -B build |
| 56 | +cmake --build build --config Release |
| 57 | + |
| 58 | +# The binary is at build/bin/whisper-cli (or build/bin/main on older versions) |
| 59 | +ls build/bin/whisper-cli |
| 60 | +``` |
| 61 | + |
| 62 | +> **Tip:** For GPU acceleration add `-DWHISPER_CUBLAS=ON` (NVIDIA) or `-DWHISPER_METAL=ON` (Apple Silicon) to the cmake configure step. |
| 63 | +
|
| 64 | +### Install system-wide (optional) |
| 65 | + |
| 66 | +```bash |
| 67 | +sudo cp build/bin/whisper-cli /usr/local/bin/whisper-cpp |
| 68 | +``` |
| 69 | + |
| 70 | +Or add the build directory to your `PATH`: |
| 71 | + |
| 72 | +```bash |
| 73 | +export PATH="$PWD/build/bin:$PATH" |
| 74 | +``` |
| 75 | + |
| 76 | +## 3. Download a GGML model |
| 77 | + |
| 78 | +Models are hosted on Hugging Face. Pick one based on your hardware: |
| 79 | + |
| 80 | +| Model | Size | RAM (approx.) | Quality | |
| 81 | +|---|---|---|---| |
| 82 | +| `tiny` | ~75 MB | ~400 MB | Fast but lower accuracy | |
| 83 | +| `base` | ~142 MB | ~500 MB | Good balance (default) | |
| 84 | +| `small` | ~466 MB | ~1 GB | Better accuracy | |
| 85 | +| `medium` | ~1.5 GB | ~2.5 GB | High accuracy | |
| 86 | +| `large-v3` | ~3 GB | ~5 GB | Best accuracy, slow on CPU | |
| 87 | + |
| 88 | +```bash |
| 89 | +# Create the model cache directory |
| 90 | +mkdir -p ~/.cache/whisper-cpp |
| 91 | + |
| 92 | +# Download the base model (recommended starting point) |
| 93 | +curl -L -o ~/.cache/whisper-cpp/ggml-base.bin \ |
| 94 | + https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.bin |
| 95 | + |
| 96 | +# Or download small for better accuracy |
| 97 | +curl -L -o ~/.cache/whisper-cpp/ggml-small.bin \ |
| 98 | + https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-small.bin |
| 99 | +``` |
| 100 | + |
| 101 | +## 4. Configure the bot |
| 102 | + |
| 103 | +Add the following to your `.env`: |
| 104 | + |
| 105 | +```bash |
| 106 | +# Enable voice transcription with local provider |
| 107 | +ENABLE_VOICE_MESSAGES=true |
| 108 | +VOICE_PROVIDER=local |
| 109 | + |
| 110 | +# Path to the whisper.cpp binary (omit if already on PATH as "whisper-cpp") |
| 111 | +WHISPER_CPP_BINARY_PATH=/usr/local/bin/whisper-cpp |
| 112 | + |
| 113 | +# Model: a name like "base", "small", "medium" or a full file path |
| 114 | +# Named models resolve to ~/.cache/whisper-cpp/ggml-{name}.bin |
| 115 | +WHISPER_CPP_MODEL_PATH=base |
| 116 | +``` |
| 117 | + |
| 118 | +### Minimal configuration |
| 119 | + |
| 120 | +If `whisper-cpp` is on your PATH and you downloaded the `base` model to the default location, you only need: |
| 121 | + |
| 122 | +```bash |
| 123 | +VOICE_PROVIDER=local |
| 124 | +``` |
| 125 | + |
| 126 | +## 5. Verify the setup |
| 127 | + |
| 128 | +```bash |
| 129 | +# Test ffmpeg conversion |
| 130 | +ffmpeg -f lavfi -i "sine=frequency=440:duration=2" -ar 16000 -ac 1 /tmp/test.wav -y |
| 131 | + |
| 132 | +# Test whisper.cpp |
| 133 | +whisper-cpp -m ~/.cache/whisper-cpp/ggml-base.bin -f /tmp/test.wav --no-timestamps |
| 134 | +``` |
| 135 | + |
| 136 | +You should see a transcription attempt (it will be empty or nonsensical for a sine wave, but the binary should run without errors). |
| 137 | + |
| 138 | +## Troubleshooting |
| 139 | + |
| 140 | +### `whisper.cpp binary not found on PATH` |
| 141 | + |
| 142 | +The bot could not locate the binary. Either: |
| 143 | +- Install it system-wide: `sudo cp build/bin/whisper-cli /usr/local/bin/whisper-cpp` |
| 144 | +- Or set the full path: `WHISPER_CPP_BINARY_PATH=/path/to/whisper-cli` |
| 145 | + |
| 146 | +### `whisper.cpp model not found` |
| 147 | + |
| 148 | +The model file does not exist at the expected path. Download it: |
| 149 | + |
| 150 | +```bash |
| 151 | +mkdir -p ~/.cache/whisper-cpp |
| 152 | +curl -L -o ~/.cache/whisper-cpp/ggml-base.bin \ |
| 153 | + https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-base.bin |
| 154 | +``` |
| 155 | + |
| 156 | +### `ffmpeg is required but was not found` |
| 157 | + |
| 158 | +Install ffmpeg for your platform (see step 1 above). |
| 159 | + |
| 160 | +### Poor transcription quality |
| 161 | + |
| 162 | +- Try a larger model (`small` or `medium` instead of `base`) |
| 163 | +- Ensure audio is not too short (< 1 second) or too noisy |
| 164 | +- whisper.cpp uses `--language auto` by default; this works well for most languages |
| 165 | + |
| 166 | +### High CPU usage / slow transcription |
| 167 | + |
| 168 | +- Use a smaller model (`tiny` or `base`) |
| 169 | +- Enable GPU acceleration when building whisper.cpp (CUDA / Metal) |
| 170 | +- Consider using the `mistral` or `openai` cloud providers for faster results on low-powered machines |
0 commit comments