Convert wav to flac with tags

conversionflacwav

long story short: I need a script to convert wav files to flac, while taking the file name (minus extension) to place in the song title tag of the flac file.

I have about 1200 audio CDs that I decided to archive lossless on a terabyte drive. Since there was plenty of room, I used k3b to rip them as wav files, thinking that would save a lot of time by skipping the compression step. Well, it did, but I had already ripped quite a few before I caught the error: when I try to import them into a player like Rhythmbox, they don't have any tags (meta data) so it can't identify and sort them properly. I've switched to ripping the rest to flac to avoid that problem, but I'd like to fix the error without going back and remounting all of those CDs again. Any suggestions?

Best Answer

Install the flac command from the package of the same name and run

#!/bin/bash
find . -name '*.wav' |
while read file # eg stuff/artist/album/title.wav
do      file="$PWD/${file#./}" # make absolute to get more info
        album=${file%/*}    # stuff/artist/album
        artist=${album%/*}  # stuff/artist
        album=${album##*/}  # album
        artist=${artist##*/} # artist
        title=${file##*/}   # title.wav
        title=${title%.wav} # title
        flac -s --best --delete-input-file \
         --tag="TITLE=$title" \
         --tag="ALBUM=$album" \
         --tag="ARTIST=$artist" \
         "$file" # creates .flac removes .wav
done

The title is the basename of the file, minus the .wav suffix, album is the immediate directory above, and artist the directory above that. The --delete-input-file option removes the .wav. See Parameter Expansion in the bash man page for ${var%pattern} which removes the glob pattern (i.e. formed with * ? and [...]) at the end of the variable, or at the start (${var#pattern}); the %% and ## versions remove the longest matches.

Related Question