Using TagLib with pytaglib in Python

I tend to write a lot of background to paint a picture why I’m doing things, so I’ll try to keep it short for to move on to the code quickly.

I have a digital music collection that was sorted by the first letter of the artist (A, B, C etc.) and then the artist and underneath that the albums. While that is good to find things, it’s not optimal for listening in my car (via USB stick). Sometimes I find myself wanting to listen to all of Melodic Death Metal on shuffle play. My car doesn’t support this like iTunes, with its internal music library, which is why I wanted to group artists and albums by genre. Since I didn’t plan to do this all manually, I opted to write some scripts in Python.

The code is available on GitHub. If you have suggestions for improvements, please comment or create a pull request. I’m not a Python pro, so I’m sure there’s some room to make it better.


This is for macOS only. On Windows… good luck, I guess. On Linux you very likely can use your package manager to install everything.

I refused the use of 3rdparty package managers forever, likely because I’m an idealist and don’t like bolted on solutions. To get going with pytaglib I had no other (convenient) choice other than using Homebrewor Macports. Based on this answeron StackOverflow I elected to use Homebrew for my needs.

So, let’s start. pytaglib is a Python wrapper around the great C++ TagLib library. It’s easy to install pytaglib, but it requires TagLib to be present on the system. On macOS it isn’t and there’s no Apple supplied package manager to help you. Hence, you compile it yourself and install it manually or you use something like Homebrew.

brew install taglib

Now install pytaglib using Python’s package manger.

pip install pytaglib



First of all, if you want to print all tags of a song, there’s a nice script bundled with pytaglib that already does that (I’m using it in one of my scripts). It’s called pyprinttagsand its usage and output looks like so:

As you can see, it’s prompting if it should remove unknown tags. You can forego this prompt by adding the -b/–batch command line option. Then it’ll just print the tags.

Now, to the real code.

First, you need to import the pytaglib library.

import taglib

It basically all starts with a file, so the first thing to do is load one.

song = taglib.File('Winters gate.m4a')

Next, you can access the song’s tags by using the tagsdictionary. The keys for the most important tags are as follows:

  • DATE

To read the artist’s name you can do something like this:

if song.tags['ARTIST']

Even if a tag has only one value, it is stored in a list. You should also always provide tags in a list, even if you add just one.

song.tags['ARTIST'] = ['Insomium']

When you’re done, save you changes and move on to the next track.

These are the basics that I used to consolidate my music library’s genres and then group them based on that.

One thought on “Using TagLib with pytaglib in Python

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.