Anything can go here, in any language... except my native language Sinhala. Be cool... anybody is warmly welcomed! :)

MP3 Batch Transcoding with Linux


Well, hello there; after some time... this one is about some small work I've done, that I think usful for other Linux users.

 I'm huge fan of music -- especially hard rock and thrash metal. I feel almost dead if I don't get a chance at least once a day, to listen to them. So I wanted to put my huge collection into my mobile phone's memory chip so I can carry them anywhere I go. But, my poor memory chip is just 1 GB. It's not enough at all! So, the only way to pack as much as I can is, reduce the bitrate of the MP3 files.

The next problem... I don't use Windows, nor Windows based software. But, if occurs a situation where I don't have any chance with Linux, I have installed Windows inside a virtual machine for use. But I rarely use that. Of course I can use JetAudio or any other audio conver to do this easily, but, as a professional and permanent Linux user, that's not my style.

So I asked our smart genie... Google to find out a solution. Here's what I've got:
#!/bin/bash
for file in `ls`
do
file=`echo $file | sed s/\ /_/g`
echo $file
lame --decode $file
lame -b 128 $file.wav
rm $file
rm $file.wav
mv $file.wav.mp3 $file
done

http://www.linuxquestions.org/questions/linux-software-2/reduce-mp3-bitrate-336791/

It's a shell script for batch processing. It will process all the MP3 files inside a given directory. Yes, that's more than what I wanted, but, what would happen if filenames contain whitespaces? The solution is not much practical, because most MP3s we get contains at least one whitespace in filename. I don't have time to rename each and every file. So I wsa thinking about a suitable solution.

It is based on lame encoder, if you don't have it, please install it first.
# apt-get install lame

Yes, the find command! Got that!!! Here's the code I've wrote:
Copy this into a text file (mp3enc.sh).

if [ $# -eq 2 ]
then
mkdir $1/output
cd $1
find . -maxdepth 1 -exec lame -h -b $2 {} ./output/{}  \;
else
echo "Invalid arguements, please refer http://portal.shaakunthala.com/2009/10/mp3-batch-transcoding-with-linux.html"
fi

Then execute,
$ chmod +x mp3enc.sh

Now run it,
$ ./mp3enc.sh <mp3_files_directory> <target_bitrate_kbps>

Eg:
$ ./mp3enc.sh /home/ubuntu/Music/MyMusic 128

It will make a directory called output inside your music directory, and put the encoded files in.

It's simple as that! I think it's easier than JetAudio, isn't it? ;)

Comments (2)

Dappi Sira

October 26, 2009 at 11:02 PM

I don't know, shell scripting and regexp are still too complicated for me.

Perhaps, I will try it someday. Thanks, nice job as usual

ශාකුන්තල

October 27, 2009 at 2:33 PM

@Dappi Sira,
Thank you for your comment. At the moment, egexp is somewhat complicated for me too, but, shell scripting is not. It's almost like writing .bat files in Windows. I conduct a shell scripting tutorial on diGIT (http://digit.lk) online Magazine in Sinhala. You can refer it if you are interested :)

Followers

Subscription Options

 Subscribe in a reader

or

Enter your email address:

and
to inscrease

and make me happy. :)

Other Blogs by the Author

The Author

My photo
Ambalangoda, Southern Province, Sri Lanka
Shaakunthala works as a Systems Support Engineer at a World's leading IT solutions company. A wannabe hacker, FOSS enthusiast, cat lover and an insane motorcyclist. And he comes from Sri Lanka!! :)

www.shaakunthala.com
www.flickr.com
ශාකුන්තල | Shaakunthala's items Go to ශාකුන්තල | Shaakunthala's photostream
Related Posts with Thumbnails