Skip to content

Instantly share code, notes, and snippets.

@pradyumnac
Last active July 3, 2023 18:11
Show Gist options
  • Save pradyumnac/94c4aa68d602f1916f65ae7c63d53abe to your computer and use it in GitHub Desktop.
Save pradyumnac/94c4aa68d602f1916f65ae7c63d53abe to your computer and use it in GitHub Desktop.
Runs whisper cpp along with ffmpeg to record voice to transcribe. Tested on mac m2
#!/usr/bin/env bash
# This script runs the speech-to-text loop from the commandline. It records to given file till you press CTRL-C or 30 seconds has passs`ed
# and then it transcribes the recording and prints the result. It then starts recording again and so on.
# Install whisper
:'
git clone git@github.com:ggerganov/whisper.cpp.git
cd whisper.cpp
# Download whisper open source model base version
# Good enough for englisg
bash ./models/download-ggml-model.sh base.en # Download
make
'
# Usage: ./run_stt_loop.sh
# Dependencies: ffmpeg
# Install: sudo apt-get install ffmpeg
set -e
mkdir -p /tmp/stt
REC_LOCATION=/tmp/stt/rec
REC_ITERATION=0
REC_FILE=
log () {
ts=$(date +"%Y-%m-%d %H:%M:%S")
echo "$ts: $1"
}
# interrupt on space
trap 'transcribe' SIGINT
trap 'echo "Exiting after cleanup..."; exit 0' SIGTERM
transcribe() {
log "Transcribing recording.wav"
./main $REC_FILE -nt -otxt
log "Transcription complete:"
cat $REC_FILE.txt
rm $REC_FILE
log "Waiting 5 seconds before recording again"
sleep 5
run_loop
}
# List recording devices and let user choose device unless recoarding device id is given as first argument
re='^[0-9]$'
if ! [[ $1 =~ $re ]] ; then
device=$(ffmpeg -f avfoundation -list_devices true -i "" 2>&1 | grep -E "\[AVFoundation.*\] \["|sed -E 's,[AV.*] ,,'|fzf --reverse|grep -o '\[.\]'|sed 's,\[,,'|sed 's,\],,')
else
device=$1
fi
log "Device selected: $device"
# Start recording and transcribing loop
run_loop() {
# Record to file
REC_ITERATION=$((REC_ITERATION+1))
REC_FILE=$REC_LOCATION$REC_ITERATION.wav
log "Recording to file recording.wav. Press CTRL+C to transcribe or else this will transcribe in 30 seconds"
rm -f $REC_FILE
ffmpeg -f avfoundation -i ":$device" -t 30 -ar 16000 $REC_FILE -hide_banner -loglevel error
transcribe
}
run_loop
rm -f $REC_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment