Skip to content

Instantly share code, notes, and snippets.

@peterc
Created February 6, 2026 12:48
Show Gist options
  • Select an option

  • Save peterc/68d82d15ad9721a7e58c104d74f41da5 to your computer and use it in GitHub Desktop.

Select an option

Save peterc/68d82d15ad9721a7e58c104d74f41da5 to your computer and use it in GitHub Desktop.
Transcribe audio files to plain text using OpenAI Whisper API. Compresses via ffmpeg (mono, 22kHz, 64kbps) and trims if over 25MB.
#!/usr/bin/env bash
set -euo pipefail
if [ -z "${OPENAI_API_KEY:-}" ]; then
echo "Error: OPENAI_API_KEY is not set" >&2
exit 1
fi
if [ $# -ne 1 ] || [ ! -f "$1" ]; then
echo "Usage: transcribe <audio-file>" >&2
exit 1
fi
input="$1"
tmpfile=$(mktemp /tmp/transcribe-XXXXXX.mp3)
trap 'rm -f "$tmpfile"' EXIT
max_bytes=$((25 * 1024 * 1024))
echo "Compressing audio (mono, 22kHz, 64kbps)..." >&2
ffmpeg -y -i "$input" -ac 1 -ar 22050 -b:a 64k "$tmpfile" -loglevel error >&2
filesize=$(stat -f%z "$tmpfile")
echo "Compressed to $(($filesize / 1024 / 1024))MB." >&2
if [ "$filesize" -ge "$max_bytes" ]; then
duration=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$tmpfile")
bytes_per_sec=$(echo "$filesize $duration" | awk '{printf "%.0f", $1 / $2}')
max_dur=$(echo "$bytes_per_sec" | awk '{printf "%.0f", (24 * 1024 * 1024) / $1}')
echo "Still too large, trimming to ${max_dur}s..." >&2
ffmpeg -y -i "$input" -ac 1 -ar 22050 -b:a 64k -t "$max_dur" "$tmpfile" -loglevel error >&2
filesize=$(stat -f%z "$tmpfile")
echo "Trimmed to $(($filesize / 1024 / 1024))MB." >&2
fi
echo "Sending to Whisper API..." >&2
curl -s https://api.openai.com/v1/audio/transcriptions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F file="@$tmpfile" \
-F model="whisper-1" \
-F response_format="text"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment