2

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?

5

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.

  • 1
    Nice. I think you want ${file##*/} to handle a/b/c.wav – jthill Oct 6 '15 at 14:32
  • Thanks! Would you mind a few refinements? Is there a way to put the directory name in album title tag? Or even the directory one up in the artist tag? Can flac or the script also delete the wav file when it's done? – Bunny Oct 6 '15 at 23:32
  • I added your refinements. Good luck! – meuh Oct 7 '15 at 7:10
  • @meuh, I didn't quite understand your comments. Anyway, that script apparently did not work. Rhythmbox sees the title but not artist and album. I added some substring operators and a few echos to see what I'm getting: – Bunny Oct 7 '15 at 20:04
  • @meuh, I didn't quite understand your comments. Anyway, that script apparently did not work. Rhythmbox sees the title but not artist and album. I added some substring operators and a few echos to see what I'm getting:<code> track=${title:0:2} # track number title=${title:5} # strip off track number echo $track echo $title ... --tag="TRACK=$track" \ echo $album echo $artist</code>The $album and $artist echo nothing – Bunny Oct 7 '15 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.