Bash Script : Batch convert mp3

So today I needed a bash script that will use lame to batch covert mp3 files to a lower bitrate so I can squeeze more onto my mp3 player.

After looking around on the net and taking bits of other peoples code (thanks NoStop and keefaz) I came up with this :

Use at your own risk !

Watch out for lame to generate errors. I had some damaged mp3 files and when lame tried to convert them it generated an error then stop processing.

#!/bin/sh    

#Be sure to edit as needed !
#Step 1 - Remove the spaces from the names
#example : Track 1.mp3 will be renamed to Track_1.mp3
#This is done because step 2 can't hand spaces in file names    

for f in *; do
file=$(echo $f | tr ' ' _)
[ ! -f $file ] && mv "$f" $file
done    

#Step 2
#Convert mp3 to a lower quality
#All mp3s are converted to 112 bit using "-b 112"
#All mp3s given names with _112 at the end
#example : Track_1.mp3 will be converted to Track_1_112.mp3
ls -1 *.mp3 | sed "s/(.*).mp3/1.mp3 1_112.mp3/" | xargs -n 2 lame -b 112
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Digg
  • StumbleUpon
  • Technorati
  • Netscape
  • Fark
  • Slashdot

8 Comments so far

  1. Patrick on August 9th, 2007

    is something missing from the coding, you said #All mp3s are converted to 112 bit using “-b 112″ but i don’t see that being used in the coding.

  2. admin on August 17th, 2007

    That code is at the very last part of the very last line.

  3. Filip on January 3rd, 2008

    Thank you! You solved my problem!

  4. slacker on May 16th, 2008

    This converts only the first file, alphabetically.

  5. i've been using this on June 9th, 2008

    for i in *.aif; do lame -h -b 256 “$i” “${i%.aif}.mp3″; done

    you could modify it to suit your purpose:

    for i in *.mp3; do lame -h -b 128 “$i” “${i%.mp3}_128.mp3″; done

  6. billynoah on June 9th, 2008

    for the record… your blog seems to have converted the quotes in my post to something other than regular quotes… they should just be normal quotes (the ones next to the return key)

    admin note : thanks for clearing that up and thanks for your comments. unfortunately wordpress can be a real pain.

  7. billynoah on June 9th, 2008

    also, if your mp3 player accepts ogg format it would be far better quality as a transcode than to make a lower bitrate mp3

  8. Kalyoncu on October 28th, 2008

    seo

Leave a reply