batch re-encode mp3s using lame

5 05 2010

Being a frugal type of guy, I purchased an mp3 player that only holds 4GB(really just 3.7GB of music). Well a large portion of my mp3s are encoded at 192kbps or higher so I really fit that many on it. Solution was simple, re-encode them down to 128kbps or lower using lame and id3lib. Most distros have them. Some scripts I looked at take all the bitrate info and write it to a text file then compare it to find out if it needs to be re-encoded, I really don’t think that is necessary to accomplish the task.

In this script I use file and pipe it through cut a few times to come up with my bitrate. This works pretty well unless you have a corrupt mp3. Well guess what, if it is too corrupt lame isn’t going to process it anyways. Sorry Charlie.

I use id3info(found in id3lib package) to get the existing ID3 tags. My mp3 player only supports artist, album and song title tags, so that’s what I use. If yours supports more by all means go ahead and check the man pages for lame and id3info for all that they have support for.

———————-

#!/bin/bash

## Description: Let’s batch reencode any mp3s that’s bitrate is higher than 128 for our mp3 player.
## Depends: lame -
## id3lib -

IFS=$’\t\n’

reset

for i in $(find . -iname ‘*.mp3′)
do

bit=$(file “$i” | cut -d”:” -f3 | cut -d”,” -f4 | cut -c1-4)
title=$(id3info “$i” | grep TIT2 | cut -d”:” -f2)
artist=$(id3info “$i” | grep TPE1 | cut -d”:” -f2)
album=$(id3info “$i” | grep TALB | cut -d”:” -f2)
target_bit=” 129″
out=temp.mp3

if [ "$bit" -gt "$target_bit" ]; then
echo “$bit $i ” >> reencode # prints a list of what was re-encoded. Can be commented out
nice -n 19 lame –cbr -b 128 –id3v2-only –tt $title –ta $artist –tl $album “$i” “$out”
mv “$out” “$i”
else
echo “$bit $i ” >> untouched # prints a list of what wasn’t re-encoded. Can be commented out along with above line.
fi

done


Actions

Information

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s




Follow

Get every new post delivered to your Inbox.