Skip to content

Instantly share code, notes, and snippets.

@bardware
Forked from pR0Ps/gen-flac-visuals
Created November 3, 2024 20:27
Show Gist options
  • Save bardware/dec34d91078a0abe017dcdde16ba7701 to your computer and use it in GitHub Desktop.
Save bardware/dec34d91078a0abe017dcdde16ba7701 to your computer and use it in GitHub Desktop.
Generates spectrogram and waveform images from flac files
#!/bin/sh
# Requires: audiowaveform (https://github.com/bbc/audiowaveform), metaflac, sox
zoom_start=60
zoom_duration=4
zoom_end=$((zoom_start + zoom_duration))
if [ $# -lt 1 ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
echo "Generates spectrogram and waveform images from flac files"
echo ""
echo "For each type, an image of the entire file will be made, as well as a"
echo "more detailed analysis of $zoom_duration seconds of audio starting $zoom_start seconds in."
echo ""
echo "The generated files will have the same name as the input files"
echo "with extra identifiers appended."
echo ""
echo "usage: `basename $0` file [file ..]"
echo ""
echo "positional arguments:"
echo " file The flac file(s) to process"
exit 0
fi
# Check deps
for cmd in 'audiowaveform' 'metaflac' 'sox'; do
if ! command -v "$cmd" > /dev/null ; then
echo "Missing dependency '$cmd'"
exit 1
fi
done
count=0
success=0
for x in "$@"; do
((count++))
echo "Processing file $count/$#: '$x'"
# Generate spectrograms
sox "$x" -n remix 1 spectrogram -X 500 -y 1025 -z 120 -w Kaiser -S "$zoom_start" -d "$zoom_duration" -o "${x}_spectral_zoom.png" || continue
sox "$x" -n remix 1 spectrogram -x 3000 -y 513 -z 120 -w Kaiser -o "${x}_spectral.png" || continue
# Get audio length and generate waveforms
len=`metaflac --show-total-samples --show-sample-rate "$x" | tr '\n' ' ' | awk '{print int($1/$2)}'` || continue
audiowaveform -i "$x" -o "${x}_waveform_zoom.png" -w 2000 -h 1024 -s "$zoom_start" -e "$zoom_end" > /dev/null || continue
audiowaveform -i "$x" -o "${x}_waveform.png" -w 3000 -h 512 -s 0 -e "$len" > /dev/null || continue
((success++))
done
echo ""
echo "Sucessfully generated images for $success/$count files"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment