Lame, Bash script programming

By smnva

About 2 weeks ago I bought a San disk Sansa Clip[official, any thing but ipod], $ GB. sorry I forgot to tell you about that. Anyway, I want to listen to Podcasts and Audio books which I usually download Elllo, Grammar Girl, and Just Vocabulary. The problem was that these podcasts have a lot of small mp3 files, something in the order of 500. So keeping track of last file which you have listened to is really hard. I am not sure what is the solution in more advance players like ipod touch or Creative Zen, but the solution which I found for my player was to concatenate all files (or at least each 50 or 100 files) together. Because Sansa Clip has a feature that can resume playing podcasts and audio books so my solution was fine.

I search the internet and I found out that a way for concatenating the mp3 files is to use cat file in Linux like this:

cat *.mp3 > allCont.mp3

This command will help you if all the files which you are concatenating have similar structure. By Structure I mean sampling rate and number of channels. That is because when they are concatenating all of them will share a same header file. Still it doesn’t seem to be be big problem, if I can find a program which can change sampling rate and number of channels of mp3 file in batch mode then I will be fine.

I searched again, yes! There is a program called lame[official, wiki] which can does anything you want by mp3 files. Really neat program which is open source and can be compiled easily both in Linux and Windows. You can even do more things like changing raw PCM and wav file into mp3 files. It also supports variable bit rate (VBR) which means in the parts of file where there is more high frequencies it will use higher sampling rate to preserve the quality and in the silent parts it will use lower sampling rate to achieve higher compression. This was the best documentation I found about lame. So again a simple command will change the file:

lame -b 32 -mm FileName.mp3 FileName_DS_32.mp3

Which means FileName.mp3 will be downsampled (DS) to 32 kbps and will have just only one channel(mono instead of stereo). The output file will be FileName_DS_32.mp3 .

Every thing was fine up to this stage, I did all of them in 1-2 hours, searching and installing lame on both linux and Mac os. The problem started when I tried to find a bash script which can be used to do the same thing to all files in a folder. I am not going to be offensive but I think about 90% of people on the Internet who speak like “Linux Nerds” do not know anything about computers. They just copy a piece of code from one forum to another without understanding. I tried 3 or 4 different ones, but none of them worked. So I decided to learn bash programming by myself and then write it, I started about 1:30 am, crazy ha?! Anyway here is the result:

#!/bin/bash
for inp in *.mp3; do
    #inp  : input file name with extension (.mp3)
    #tmp1 : all blanks ' ' are replaced by '_'
    #tmp2 : without extention, just file name
    #out  : output file name, which is (inp_DS_32.mp3), you can modify it as you wish (DS: DownSample)

    #In order to see these varables add an statement like this
    #echo $tmp2
    tmp1=${inp//' '/'_'}
    tmp2=${tmp1%.mp3}
    out="${tmp2}_DS_32.mp3"

    lame -b 32 -mm "$inp" $out
done

mkdir downSample
mv *_DS_32.mp3 downSample
cd downSample
cat *.mp3 > concatenate.mp3

Main part of this code is the for loop which will be run one time for each file. Input file name in changed in order to get rid of blank spaces in it. For example if input name is “557 File Name.mp3″ then variable tmp1 will be “557_File_Name.mp3″. Then we should get rid of file extension (.mp3), because we want to add _DS_32 to the end of file name. This step is done by help of tmp2 variable which will be “557_File_Name” . Finally output file name will be in the out variable which in the case of our example will be “557_File_Name_DS_32.mp3″. At the last step lame is called by its appropriate arguments, input, and output file names. To write this bash script I used these sources Bash Guid for Beginners and Advance Bash Scripting Guide. I also found this document very useful, bash pitfalls.

After for loop, I simply make a new directory called downSample and then move (cut/past) all new files into that folder. Then as I said cat is used to find concatenate new files together. Just change the ID3 tags of this file (simply in iTunes) and then copy it into your Sansa Clip. Don’t forget to enjoy it.

Size of resulted files are really interesting in comparison of original ones. The only one which doesn’t have problems in the mp3 files was JustVocabulary, so I compare its sizes. I downloaded 258 files when I subscribed to this podcast through the iTunes store. All the files are recorded in two channels with 96 kbps bit rate. When I concatenate all of them together the result size is 1.29 GB, practically one third of my player capacity. When I change them to one channel and 32 kbps then size of concatenated file is 283 MB. Amazing, ha? And you know I haven’t lost any quality, because these file generally contain voice, and basically if there is any music I don’t care about it quality. I guess I deserve a thumbs up :D

Leave a Reply