Convert all files recursively from one format to another (ie .flac to .mp3) using Linux

HI All,

A little post for a cool Linux bash script I created with some help from the net that will convert all the .flac files in a directory and all sub-directories to .mp3 format.  This script can easily be modified to convert other audio files to any other format that FFMPEG supports and also convert between all the different video files FFMPEG supports.

*** Stupid WordPress messes up all the quotes ” please make sure your final script has only regular quotes throughout and not forward and backward quotes.  Also the single quotes are mess up too…  Make sure they are regular sigle quotes ‘. Sorry for the inconvenience.   ***

First install ffmpeg:

Go here for how-to install ffmpeg with mp3 and other file support…

Otherwise a regular sudo apt-get install ffmpeg will install a limited version of ffmpeg

Now onto the script…

Use nano to create the script

$ nano flac2mp3

Copy and paste the following script:

# convert .flac to .mp3 recursively
# By Glen Hewlett
if [ -z $1 ];then echo Give target directory; exit 0;fi
find “$1” -depth -name ‘*’ | while read file ; do
directory=$(dirname “$file”)
oldfilename=$(basename “$file”)
newfilename=$(basename “${file%.[Ff][Ll][Aa][Cc]}”)
if [ “$oldfilename” != “$newfilename” ]; then
ffmpeg -i “$directory/$oldfilename” -ab 320k “$directory/$newfilename.mp3” </dev/null
#rm “$directory/$oldfilename”
CTRL-X and save the output Then make it executable

$ chmod +x flac2mp3

Use the command flac2mp3 with the directory name you want it to look for files to convert. Example: convert all the files in the current directory and all subdirectories.

$./flac2mp3 ./

Example: convert all the files in the “/temp” directory and all subdirectories.

$./flac2mp3 /temp

You can remove the “#” at the beginning of the line rm “$directory/$oldfilename” and the script will delete the original file. Be careful to make sure you still have a copy of the original files if you do remove the “#”.

If you want this script to convert all the .wav files to .aac you would change the line:

newfilename=$(basename “${file%.[Ff][Ll][Aa][Cc]}”)


newfilename=$(basename “${file%.[Ww][Aa][Vv]}”)

Also modify the ffmpeg output line from:

ffmpeg -i “$directory/$oldfilename” -ab 320k “$directory/$newfilename.mp3” </dev/null
ffmpeg -i “$directory/$oldfilename” -ab 320k “$directory/$newfilename.aac” </dev/null

Of course you can always tweak the ffmpeg command line to a lower bitrate if you want depending on your needs (-ab 320k). If you do plan on using this for video conversion then you will have to play with the ffmpeg settings to get the desired output. There’s tons of info all over the net for using ffmpeg to convert audio and video formats. Just search for ffmpeg and the format you want to end up with and you’ll surely find tons of example ffmpeg commands.

To get ffmpeg to list all the file types it can convert use the command:

$ffmpeg -formats

Have fun… Glen.

This entry was posted in Uncategorized. Bookmark the permalink.

14 Responses to Convert all files recursively from one format to another (ie .flac to .mp3) using Linux

  1. Ady Ngom says:

    Very neat and helpful my friend maybe an improvement would be to capture all possible formats in an array and pass two parameters to the function specifying the conversion type, thus making it a universal conversion tool. Thanks again for posting this.

  2. jcampos says:

    Thank you very much, you script guided to me and now I will be able to convert recursively my video files.

  3. lovely… works just fine. i modified it to convert all my WMA files.

  4. Manuel Parra says:

    I changed

    find “$1″ -depth -name ‘*’ | while read file ; do


    find “$1″ -depth -name ‘*.flac’ | while read file ; do

    It kept showing error messages with files beginning with “._” period underscores.

  5. Joakim says:

    I run the script as instructed and I get this error
    find: `“./″’: No such file or directory

    and even when I run it with ./flactomp3 dir dir it wont work

    • nowhereman999 says:

      When copying and pasting from the wordpress blog often the qutoes get changed. I suggest going over every quote in the code that you copied and change them to a regular quote “, the script has worked fine for me without problems and the others who have commented seem to be working too.

  6. I removed the -depth which lets me run through deeper directories .. ie artist/album/song.flac
    also added *.flac to the find as all my files are lower case and it speeds the process up.

    One other change, /dev/null to supress all off the ffmpeg output.

  7. Like says:

    Thank you very much!

  8. Dc says:

    mmm – this doesn’t seem to pick up the files on os x –

    echo $file
    echo $newfilename

    gives directories –


  9. Thanks, I tweaked this as well for wma to mp3.

    Just change:
    newfilename=$(basename “${file%.[Ff][Ll][Aa][Cc]}”)


    newfilename=$(basename “${file%.[Ww][Mm][Aa]}”)

    Also for meta data, this has changed slightly to “-map_metadata 0:0” instead of “-map_meta_data 0:0”.

    There’s a note when executing that ffmpeg is being depreciated and replaced with avconv, but as of this writing the conversion worked fine.

  10. DrTebi says:

    Thanks, that saved me a few minutes writing it myself 😉

  11. shelleyfrank says:

    Thanks for this script. It works for me. I modified it to convert wav to flac files.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s