Skip to main content

I much prefer lossless audio when listen to music. When listening at home through my PC or home theater I find MP3 leaves something to be desired, unfortunately I do not have the storage space on my cell phone to bring my FLAC collection with me.

While I could rip all my CD’s as both MP3 and FLAC, that leaves downloaded FLAC files to convert, and is considerably time consuming.

The below script is what I use to capture all the music I have, loop through copying non-FLAC audio if they exist, and converting to MP3 if they are FLAC. The mp3 folder synchronizes with my cell phone through Syncthing to bring my music with me for offline enjoyment.

#!/usr/bin/env bash

mp3dir=$HOME/SyncMusic

cd $1
find . -type d -exec mkdir -p -- $mp3dir/{} \\;
rsync -avzh --exclude "*.flac" . "$mp3dir/"
for a in ./*/*/*.flac; do
  if [[ ! -f "$mp3dir/${a[@]/%flac/mp3}" ]]
  then
    < /dev/null ffmpeg -i "$a" -qscale:a 0 "$mp3dir/${a[@]/%flac/mp3}"
  else
    echo $mp3dir/${a[@]/%flac/mp3} exists!
  fi
done
echo -e "done."
exit $?

The only applications you need for this script are bash, rsync, and ffmpeg, which should already be installed on your Linux computer.

  • save the script to ./flactomp3.sh
  • Set the mp3dir (i.e mp3dir=$HOME/SyncMusic)
  • Save
  • chmod +x ./flactomp3.sh
  • ./flactomp3.sh $HOME/Music

If you want to convert your music in batches, put them in $HOME/convert and do ./flactomp3.sh $HOME/convert

Per song takes only a few seconds. If you have an exceptionally large music collection this could save you hundreds of hours doing conversions.

If you found this useful please comment below, or float me an email.

graphic that says Adam and shows an exclamation point

Adam Anderson

Systems Administrator by day; ronin illustrator by night. I promote learning the tools in your computer system to complete your work in a more effective way, meanwhile agonizing for hours over an A3 sheet of bristol paper with a 0.03mm pen.