Python MusicBrainz Library

Python MusicBrainz Library

In this post, we'll look at how we can pull music data and cover art images from MusicBrainz using python.

Setup

Python

  1. First step is to install python - see the beginners guide on the python website if you need help with this step.
  2. Once you have python installed, I would recommend creating a new virtual environment where we will install the libraries that we need. I suggest checking out Conda if you need a virtual environment manager.

MusicBrainzNgs Library

  1. Lucky for us, there is already a python library that works with the MusicBrainz API. Run the following in the environment you created above:
pip install musicbrainzngs
  1. Now create a new .py file or, my personal preference when testing out new libraries, an .ipynb file using Jupyter.

  2. In your new .ipynb file we just import the library

import musicbrainzngs as mbz
  1. Set the user agent
mbz.set_useragent('TheRecordIndustry.io', '0.1')

And we are ready to start sifting through the data.

Browse Data

The data schema gives a bit more information about the types of data that those functions return. We are quickly going to look at 3 objects:

  • Artist - An artist is generally a musician, a group of musicians, or another music professional (composer, engineer, illustrator, producer, etc.)
  • Release - Real-world release object you can buy in your music store. It has release date and country, list of catalog number and label pairs, packaging type and release status. Example:
    • 1984 US release of "The Wall" by "Pink Floyd", release on label "Columbia Records" with catalog number "C2K 36183" and UPC "074643618328", it's an official release and comes with two CDs in jewel case.
  • Cover Art - Actual images from the release

Search Artists

We are going to start by searching for an artist. Most of the library's functions return a dictionary object, so in this first line of code we are going to pull the 'artist-list' from the results, and then we'll use the first result in that list as our artist.

artist_list = mbz.search_artists(query='Red Sovine')['artist-list']
red_sovine = artist_list[0]
red_sovine

This will show the full data of what has been captured for this artist:
artist_list

Browse Releases

Now that we have Red Sovine's ID, we can use that to browse related objects like his releases, recordings, works, etc. In this next step, we'll look at releases.

release_list = mbz.browse_releases(artist=red_sovine['id'])['release-list']
release_list

This will return a list of releases.
release_list

Fetch Cover Art

Now that we have selected a release, let's pull the cover art for it.

phantom_309 = release_list[4]
cover_art_b = mbz.get_image(phantom_309['id'], coverid='front', size='500')

We'll need to import a couple more libraries to convert the binary data into a viewable image.

from PIL import Image
import io

image = Image.open(io.BytesIO(cover_art_b))
image.show()

Conclusion

We have successfully gone from an artist name, to browsing through their releases, to getting the cover art for that release. Hopefully this was a good overview of how to use the MuzicBrainzNgs library, but there is a lot more that you are capable of finding using the library.

Check out: