Wednesday, September 23, 2009

Fixing unrecognized File Type extensions in Ubuntu

I've had a little 'bug' in my Ubuntu for the past month that was annoying me a bit. I use TeXmacs for creating/editing small LaTeX documents. The problem is that Nautilus was incorrectly detecting the file type of .tm files, thinking they were plain text documents (see image below). The consequence is that TeXmacs files would be opened in gedit (or the default text editor) when opening them through the Nautilus file browser.



The first solution attempt was right-clicking a .tm file, going to "Properties -> Open With" and changing the default application to GNU TeXmacs. This was a disaster, since now *all* plain text files would be opened in GNU TeXmacs, since Nautilus genuinely believed a .tm to be the same as a .txt and thus changed the default application for all text files.

My second attempt at a solution (after googling a bit) was checking the MIME file type associations, which is the way Ubuntu associates an extension to a file type. The info is all in the file /etc/mime.types. However, to my disappointment, this file already contained a mime type for TeXmacs documents, indicated by the entry
"text/texmacs tm ts". So even though the mime type was correctly recognized, it seems Nautilus wasn't picking it up. This could be further deduced from the fact that running the command file on a .tm document in the terminal returned the correct mime type.

After more googling, I finally discovered that this is in fact a bug and found a fix for it:

http://savannah.gnu.org/bugs/?25938#discussion

Essentially, you manually create an xml file describing the TeXmacs mime type to /usr/share/mime/packages and then manually update the mime database of the system (setting an icon for the file type is optional, but may be useful).

NOTE:
the original poster of the above bug fix made a small typo: an extra semicolon (;) at the end of the line containing "mime-info xmlns=" Remove it before trying to update the mime database.

After this is done, logout / re-log (or simply kill / restart nautilus from the terminal) and the new file extension should now be recognized:



The nice thing about this is that it should conceivably work for any other file type that has a correct mime type in /etc/mime.types but is not being recognized by Nautilus.

Hope this was helpful ;).

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.