Birch Street Computing -

about me

John M is a Linux fan in Lowell, MA.

I work at at a local company writing software. I fool around with Free and Open Source software for fun.

I am an avid Last.fm user.

Python Taglib Tutorial

Technology
Technology

I constantly get hit by people googling for a python taglib tutorial. I'm not sure why, I had not written one (until now). So, I'm decided to actually write the tutorial, after all, I have used python taglib and it works pretty well.

Introduction

Taglib is a C++ library written by Scott Wheeler designed to provide an easy to use API for accessing tag metadata from ogg, mp3 and other similar music formats. Python-taglib, written by the namingmuse team, takes the C++ based library and makes it accessible to the python language. One of the "warts" of python-taglib is that it's API is very C++ish and not very pythonic. This detail can be hidden away if you write a simple wrapper module, like I did with my RagTag module.

Installing

Depending on your distribution, you'll want to install both the taglib library and its development headers. On debian the packages you need to apt-get are libtag1c2a, libtag1-dev and swig.

Go to the namingmuse project's download page and grab a copy of python-taglib-1.3.3.tar.gz [http://prdownload.berlios.de/namingmuse/python-taglib-1.3.3.tar.gz]. Untar this file and enter the directory it creates. Normally, you would run ./configure, make and make install. This worked for me, but it installed to the python 2.3 packages. To get it to install to python 2.4 I had to apply this patch I created to force it to check for python 2.4 first. You may or may not wish to do this.

To check that everything is installed type python at your shell prompt, enter the python interpreter and type import TagLib. If you don't get an import error, you should be good to go.

Using The Module

In case I do anything wrong I like to make a copy of a song to expiriment with, in this case I'll copy one of my favorite tracks Melvins - Revolve.ogg out of my music folder and into my home directory. I will use this file in my examples.

The main class that allows you to read a file's tags is the TagLib.FileRef class. FileRef's take a string argument, this string is the path to the track we want to access.

Python 2.4.2 (#2, Nov 20 2005, 17:02:17)
[GCC zzzzzzzzzzzzzzz (zzzzzzzzzz) (zzzzzzzzzzzzzz)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import TagLib
>>> track = TagLib.FileRef( "Melvins - Revolve.ogg" )
>>> track
<TagLib.FileRef; proxy of C++ TagLib::FileRef instance at _1019c088_p_TagLib__FileRef>

The two most interesting methods of a FileRef object are the tag() function and the audioProperties() function. Both return objects with methods of their own (see, I told you the API was very C++ish).

The object returned by tag() has a number of methods like artist, title, album, track, etc. You can see the entire list by either using the dir() function in python, or checking out Scott Wheeler's API documentation. These functions are getters and they have setter counterparts so a function foo would have indicate that the tag object also has setFoo(...).

I'll mess around with my Melvin's track as an example.

>>> track.tag().title()
'Revolve'
>>> track.tag().artist()
'Melvins'
>>> track.tag().album()
'Stoner Witch'
>>> track.tag().comment()
''
>>> track.tag().setComment( "An awesome track" )
>>> track.tag().comment()
'An awesome track'

While the comment field appears to have been changed, the change is not permanent. If I exit out of the python interpereter, go back in and examine the comment tag for that track, I'll find out that It hasn't changed. To permanently write changes to a track I need to call the FileRef object's save() method.

>>> track.save()
True

If the save function returns True, we've sucessfully written our changes. If it returns false, something fouled up and our changes didn't get made. One possible culprit could be that you don't have write permissions to the file, this is typical if you copied the file off a shared folder or network mount.

The other interesting component of a TagLib FileRef object is the audioProperties() method. This gives you access to another group of member functions like bitrate(), channels(), length(), and sampleRate(). Unlike the tags these properties are inherent to the ogg or mp3 file and can't be changed (therfore, no setFoo(...) methods).

With these functions in hand, you're well on your way to tagging fun.

Plugging RagTag

Because TagLib isn't a very pythonic library, I wrote RagTag for use in my own projects. RagTag provides the TagFile class which is very much like a FileRef class, however instead of a series of function calls it implements a dictionary style interface to both the tag and audio properties. Currently, RagTag provides read-only access to tags, but that just means I have something to do down the road.

>>> import RagTag
>>> track = RagTag.TagFile( "Melvins - Revolve.ogg" )
>>> track['title']
'Revolve'
>>> track['artist']
'Melvins'
>>> track['bitrate']
112

Conclusion

Taglib and python make a great combination. Using the two together lets you, the programmer, easily access track metadata from a powerful, high-level programming language.

Any questions, comments, or mistakes please email me at phlogistonjohn+writings@gSPAM.com. Change the word SPAM to gmail.

Every blog page or article on this site is available under the CC-BY-SA license unless otherwise noted.