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








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.
That code is at the very last part of the very last line.
Thank you! You solved my problem!
This converts only the first file, alphabetically.
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
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.
also, if your mp3 player accepts ogg format it would be far better quality as a transcode than to make a lower bitrate mp3
seo