Friday, September 18, 2009

Goodbye MP3s!

The story - skip to the end if you only want the scripts and could spare my self-righteous opinion.

I've used MP3 as my music compression format for years, as I'm sure most people have, without thinking too much about it. But my relatively recent ascension into Free Software has changed my perspective on many things related to software. Especially, it has given me the awareness that, in general, as a user I am the one in charge and I have the power to choose what I want to use and how I use it - or at least I should.

MP3 entered the equation when I learned that Apple iPods do not play any other music format except WAV, MP3 and ACC related codecs (such as M4A). The reason for this is simple: these are patented, proprietary compression formats that Apple supports in order to introduce DRM (copy protection), using iTunes as a centralizing vehicle. While I can't deny their right to do so, I do believe this sort of policies go against the realization I exposed in the previous paragraph.

I don't own an iPod. I have this small, very practical, albeit ugly, 2GB iBit music player that I bought at Sam's Club for under $80 that will play MP3, M4A, FLAC, OGG and pretty much any format I've thrown at it. Why would this small company support OGG while Apple does not? Would Apple lose business if they did?

So, exercising my right of decision, I've abandoned the MP3 format in favor of free alternatives: OGG for standard music compression and FLAC if I ever need lossless compression. Converting all my music to OGG is no easy task, however, mainly due to the size of my music collection. In Windows, I'd probably have to find a small utility that can do the batch conversion, and it probably would cost me something.

Fortunately, Linux is rooted at the most fundamental level on the basic premise that the power belongs to the user (as opposed to Apple's diametrically opposed mindset), and it provides countless tools that allow you to perform complex tasks with a minimum effort. So I took this as an opportunity to learn a bit about bash scripting and wrote a small script to handle the conversion for me.

The script

Here's the link to the source code. Copy/paste to your favorite text editor, save and set execute permission (chmod +x mpeg2ogg.sh).

mpeg2ogg.sh

The script assumes you have two programs installed in your system: mpg321 and oggenc. The former handles MP3 decoding and the latter compression to OGG. If you don't have them, you can most certainly get them through your favorite package manager (in Debian/Ubuntu, all you need to do is a sudo apt-get install mpg321 vorbis-tools).

Using the script is simple. Navigate to the root directory of your music library (in my case this was /home/meithan/Music), copy the script there (or put it in a dir in your path), and run it . It will find all MP3s in the directory tree rooted in the current directory, and convert them all to OGG. The script builds a list of all the files that it finds and uses it as a queue: every time a file is converted, it removes that entry from the list. Thus, you can stop the script whenever you want, and just run it again at a later time and it will resume where it had stopped (reading from the file list.txt).

A note: since I still don't trust my bashing abilities enough, the script won't delete your original MP3 files. You should first try the OGG files first, and then, if you're confident the conversion was successful, manually delete your old MP3s. Now, since "manually" means something completely different in Linux, here's a minimal script that will batch remove all MP3s in the current directory and its subdirectories:

mp3del.sh

Hope my ranting and/or bash hacking are useful to someone.

No comments: