← Back

record system audio on a timer

2026-01-20

#
# Record system audio on a timer
#
# Requirements:
# - blackhole audio router - brew install blackhole-2ch
# - switchaudio-osx - brew install switchaudio-osx
# - sox - brew install sox
#
# First, switch audio devices so we're outputting to and recording from
# BlackHole 2ch
switchaudiosource -t input -s "BlackHole 2ch"
switchaudiosource -t output -s "BlackHole 2ch"
# Then, record audio in .wav format into the Downloads dir,
# for the length of time specific by the Alfred query
INPUT_STRING={query}
TIME_MINUTES=$(echo $INPUT_STRING | cut -d ':' -f 1)
TIME_SECONDS=$(echo "$TIME_MINUTES * 60" | bc)
cd ~/Downloads && sox -t coreaudio "BlackHole 2ch" "$(date +"%Y-%m-%d-at-%H-%M")-recorded-audio.wav" --show-progress trim 0 $TIME_SECONDS
# Revert audio input and output devices
switchaudiosource -t input -s "MacBook Air Microphone"
switchaudiosource -t output -s "MacBook Air Speakers"

Also helpful to then compress the recorded audio into .mp3...

ffmpeg -i input.wav -qscale:a 0 output.mp3