How to create the spectrogram of many audio files efficiently with Sox

audiosox

I have a bunch of audio files and I would like to create the spectrogram for each individual file using Sox. Usually, for a single file, I do this:

sox audiofile.flac -n spectrogram

However I don't know how to extend this method to more than one file. Ideally I would like my output .png file to have a filename associated to its respective audio file; for example audiofile1.png for audiofile1.flac, audiofile2.png for audiofile2.flac and so on.

Does anybody know how to do this?

Best Answer

Thanks to Joseph for his answer. Perhaps it worked at the time of his posting but I had to add -o just after spectrogram for sox to accept the command.

for file in *.flac;do
    outfile="${file%.*}.png"
    sox "$file" -n spectrogram -o "$outfile"
done

I'll be whacking them all in their own folder for archiving. It works!

You could even go a bit further by adding the title of the file above the spectrogram within the image, and making it a little wider to see more details. The default image is a bit small for me.

for file in *.flac;do
    outfile="${file%.*}.png"
    title_in_pic="${file%.*}"
    sox "$file" -n spectrogram -t "$title_in_pic" -o "$outfile" -x 2000
done
Related Question