set -euo pipefail
WHISPER_DIR="${WHISPER_DIR:-$HOME/.local/whisper.cpp}"
WHISPER_BIN="${WHISPER_BIN:-$WHISPER_DIR/build/bin/whisper-cli}"
WHISPER_MODEL="${WHISPER_MODEL:-$WHISPER_DIR/models/ggml-base.en.bin}"
missing=()
command -v ffmpeg >/dev/null 2>&1 || missing+=("ffmpeg (apt install ffmpeg)")
[ -x "$WHISPER_BIN" ] || missing+=("whisper.cpp binary at $WHISPER_BIN")
[ -f "$WHISPER_MODEL" ] || missing+=("whisper model at $WHISPER_MODEL")
if [ "${#missing[@]}" -ne 0 ]; then
{
echo "transcribe: local transcription unavailable. Missing:"
for m in "${missing[@]}"; do echo " - $m"; done
echo "Install with: make setup-transcribe"
} >&2
exit 3
fi
if [ "$#" -lt 1 ]; then
echo "usage: transcribe <audio-or-video-file> [more...]" >&2
exit 2
fi
for f in "$@"; do
if [ ! -f "$f" ]; then
echo "transcribe: no such file: $f" >&2
exit 2
fi
wav="$(mktemp --suffix=.wav)"
trap 'rm -f "$wav"' EXIT
ffmpeg -nostdin -loglevel error -y -i "$f" -vn -ar 16000 -ac 1 -c:a pcm_s16le "$wav"
"$WHISPER_BIN" -m "$WHISPER_MODEL" -f "$wav" -nt 2>/dev/null
rm -f "$wav"
trap - EXIT
done