Linux – Batch Convert .wav to .flac

I ran into a weird issue the other day where SoundConverter, a GUI for converting audio files, was generating flac files that my audio player couldn’t see. I’m still not exactly sure what the problem was, but in trying to solve the problem, I went ahead and wrote a command to batch convert a folder of .wav (WAV) files to .flac (FLAC) files using FFMPEG. I figured I’d put it up here for me in the future and in case anyone else finds it useful.

First, navigate in a terminal/console to the folder where the audio files are you want to convert to flac. Then run the following command:

for i in *.wav; do ffmpeg -i "$i" -c:a flac "${i%.*}.flac"; done

Breaking this code down… The first part “for i in *.wav” starts the loop by telling the computer to loop through every file in that folder. The second part tells the computer what to do (“do”) with each of those files: load the ffmpeg software and for each file “$i” convert it to flac “-c:a flac”, renaming the file with the same name as before but with the flac file extension “”${i%.*}.flac””. (See here for what these characters do.) When that is complete, the loop is done.

Loading

Comments

4 responses to “Linux – Batch Convert .wav to .flac”

  1. Lorenzo Avatar
    Lorenzo

    Thansk a lot, smart and efficient.

  2. poddmo Avatar

    Worked for me too. This method also preserves embedded cover photos

  3. MusicFans Avatar
    MusicFans

    Thank you for your great share. I use Avdshare Audio Converter to convert WAV to FLAC, Apple Lossless M4A, MP3 etc

  4. Hannes Avatar

    Saved my day. Thx for sharing

Leave a Reply

Your email address will not be published. Required fields are marked *