Web Dev Blog

Looking for help with WordPress, Apache, Vim, Web Security and more. Here are a few tips.

Converting FLAC to MP3 from the command line in Ubuntu

From time to time I have some flac files I want to convert to mp3 files so they will play on my car radio. This is simple enough to do,and with a Google search I found this command.

for f in *;do flac -cd $f |lame -b 128 - $f.mp3;done
 (http://www.upubuntu.com/2011/06/how-to-convert-flac-to-mp3-from-command.html)

The first problem with this is it doesn’t like spaces in the file names. This can be solved by adding quotation marks

for f in *;do flac -cd "$f" |lame -b 128 - "$f".mp3;done

The second problem is, it creates all the files with the name foo.flac.mp3. After a bit of research I found the rename command can quickly rename all of the files.

rename 's/\.flac.mp3$/.mp3/' *.mp3

You could probably add this to the end of the for loop with a && or something

Leave a Reply