Skip to content

Instantly share code, notes, and snippets.

@LaptopDev
Last active August 31, 2023 14:50
Show Gist options
  • Save LaptopDev/2f9e76825034b6a20394af11f4fc0381 to your computer and use it in GitHub Desktop.
Save LaptopDev/2f9e76825034b6a20394af11f4fc0381 to your computer and use it in GitHub Desktop.
linux transcribe.sh (bg script, hotkey adaptable) for whisper.cpp - Backup voice memo and transcription, and copy transcription to clipboard
#!/bin/bash
# place in whisper.cpp/
# assign this scfript to a hotkey to start a recording
# assign ffmpeg kill command to another hotkey to stop it
# if desired,
# modify the whisper.cpp/main command to add an argument
# for using a specific model other than whisper.cpp's base model
cd /home/user/whisper.cpp
# Function checks if ffmpeg is still running; transcribes if not
check_ffmpeg_running() {
local filepath=$1
while pgrep -x "ffmpeg" > /dev/null; do
sleep 1 # Waiting for ffmpeg to finish
done
# Transcribe the audio if the file is available
if [ -f "$filepath" ]; then
TRANSCRIPTION=$(/home/user/whisper.cpp/main -f "$filepath" 2>/dev/null | grep -o '\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9][0-9][0-9] --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9]\.[0-9][0-9][0-9]\] .*' | awk -F'] ' '{print $2}' | tr '\n' ' ')
TRANSCRIPTION=$(echo -n "$TRANSCRIPTION" | sed 's/ $//')
echo -n "$TRANSCRIPTION" | wl-copy
echo -n "$TRANSCRIPTION"
echo "$TRANSCRIPTION" > "${filepath%.wav}.txt"
notify-send "Transcription copied to clipboard"
else
notify-send "Recording failed"
fi
}
# Set the directory where the recordings and transcripts will be saved
DIR="/home/user/.transcription"
# Create directory if it doesn't exist
mkdir -p "$DIR"
# Generate filenames for new recordings and transcripts
# using current date/time
FILENAME="$(date '+%Y-%m-%d_%H-%M-%S').wav"
FILEPATH="$DIR/$FILENAME"
# Start recording with ffmpeg
ffmpeg -f alsa -i default -ar 16000 "$FILEPATH" &
# Show notification for "Recording started"
notify-send "Recording started" "Recording is currently in progress. Terminate the ffmpeg process to stop."
# Start the background function to check ffmpeg status
check_ffmpeg_running "$FILEPATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment