Musical Janitor

Updated

I have an extensive collection of music, collected over many years. This collection has grown at a faster rate since discovering bandcamp.com from where I've bought a lot of (great) music.

But sometimes this music is in some silly high sample rate (which is snakeoil), and it rarely contains the replaygain tags that I'd like.

So I typically hack a simple script together to go through the collection, transcode down to good old 44.1KHz/16bit and add replaygain tags if necessary. This is my third or forth iteration, as I never ever save these scripts anywhere before moving between systems, operating systems or distributions.

This should be 'portable', given that you install the dependencies (FLAC, GNU Parallel). It only checks the samplerate (and ignores the bitrate) but it's Good Enoughâ„¢. It will run on all your available cores for the heavy tasks1, not bad for a script thrown together just before bedtime eh?

  #!/usr/bin/env bash

  declare -r DEPENDENCIES="flac parallel"

  for dependency in DEPENDENCIES
  do
      type -P $t_cmd >> /dev/null && : || {
              echo -e "$t_cmd not found in PATH ." >&2
              exit 1
          }
  done

  function cleanup {
      rm /tmp/replaygain-folders /tmp/transcode-candidates /tmp/all-flac-files
  }

  trap "cleanup" EXIT SIGQUIT SIGKILL SIGTERM

  find /mnt/music -name "*.flac" > /tmp/all-flac-files

  cat /tmp/all-flac-files | parallel '
      if ! $(metaflac --show-sample-rate {} | grep -qi 44100); then
         echo {} >> /tmp/transcode-candidates
      fi

      if ! $(metaflac --list {}|grep -qi replaygain); then
         echo {//} >> /tmp/replaygain-folders
      fi
  '

  cat /tmp/transcode-candidates | parallel '
      transcoded=`mktemp -d`
      if (ffmpeg -i {} -ar 44100 -sample_fmt s16 $transcoded/{/}) 2>/dev/null; then
         cp $transcoded/{/} {//}
         rm -rf "${transcoded}"
      fi
  '
  cat /tmp/replaygain-folders|sort|uniq | parallel '
      pushd {}
      metaflac --add-replay-gain *.flac
      popd
  '

1

And dear GNU Parallel author, I will cite you if I ever use Parallels in an academic context. I promise.