Wednesday, July 22, 2009

Batch MP3 encoding with lame

I recently bought the new album from Chimaira and liked listen ot it on my iPod. Since I like the mp3 format, I did not want to import it with iTunes using the AAC format I used the open and free mp3 encoder Lame. I ripped the CD tracks using Winamp and configured it to export the tracks to the format "Artist - Album - TrackNumber - SongTitle".wav. After that, I can encode the wav's to mp3's. But encoding each wav file manually is quite time consuming, especially, when all id3 tag information had to be entered manually. So I wrote a little Windows batch file that encodes all the wav's in a batch and automatically sets the ID3 tags.

The source for the script is:

@echo off

if "%1"=="" (
echo Usage: %0 [files] {genre} {year}
goto :eof
)

rem %2=genre
rem %3=year
if "%2"=="" (set GENRE=) else (set GENRE=--tg "%2")
if "%3"=="" (set YEAR=) else (set YEAR=--ty "%3")

for /R %%f in (%1) do (
echo Converting "%%~nxf"

rem extract from format "Artist - Album - trackNo - Title"
for /F "tokens=1,2,3,4 delims=-" %%A in ("%%~nf") DO (
call :encode "%%~nxf" "%%A" "%%B" "%%C" "%%D" "%GENRE%" "%YEAR%"
)
echo.
)

goto :eof

:encode
SETLOCAL

rem strip quotes
set _artist=%~2
set _album=%~3
set _trackNo=%~4
set _title=%~5

rem trim spaces
set artist=%_artist:~0,-1%
set album=%_album:~1,-1%
set trackNo=%_trackNo:~1,-1%
set title=%_title:~1%

echo Artist: "%artist%"
echo Album: "%album%"
echo TrackNO: "%trackNo%"
echo Title: "%title%"
if NOT "%genre%"=="" echo Genre: "%genre:~6,-1%"
if NOT "%year%"=="" echo Year: "%year:~6,-1%"

lame.exe -h -V 6 "%~1" "%~n1.mp3" --add-id3v2 --tt "%title%" --ta "%artist%" --tl "%album%" --tn "%trackNo%" %genre% %year%
ENDLOCAL

goto :eof

:eof

With the help of the scripts I could easily encode the files of an entire directory using:

myScipt *.wav 137 2009

Note, that 137 refers to the Genre list of the lame encoder. Use "lame --genre-list" to print an alphabetically sorted ID3 genre list.