Migrate your MP3 library to Spotify

How to automatically identify your MP3s and upload them to a Spotify playlist using Python.

If, like me, you have a large library of MP3s that you have collected over the years, you might want to have them available on the music streaming service of your choice, so that you don’t have to lug them around everywhere with you. In this article, we are going to focus on Spotify, but it should be possible to do something similar with YouTube and Deezer.

We are going to use the music recognition service provided by ACRCloud and the Spotify Developer API. To begin with, you will need to register with these and obtain your credentials.

Get your credentials from ACRCloud

Sign up with ACRCloud and create a new Audio & Video Recognition Project.

It’s important to check the box to enable 3rd party ID integration. You should now have a set of values for Host, Access Key and Access Secret. You’ll be needing these later. As part of a free 14 day trial, you will able to identify 10,000 MP3s a day.

Get your credentials from Spotify

Head over to the Spotify for Developers Dashboard and create a new client ID. (If you don’t already have a Spotify account, you can easily obtain a free one.) You’ll have to provide some blah blah about what you are doing but this is not important, as long as you don’t intend to use it for commercial purposes. You’ll now have access to your Client ID and your Client Secret. However, it is also important to edit the settings and include a Redirect URI. As part of the authentication process, Spotify will call you back at this web address with a URL that contains a magic code. You can choose any website that allows you to grab this information (e.g., https://github.com) without being redirected to a 404 error page. Make a note of your Client ID, Client Secret and Redirect URI.

TL;DR

Now that you have all the pieces you need, you can run my notebook in Google Colab and just follow the steps.

Identify MP3s using Python

First, we have to import some libraries. It should be straightforward to install them if you have not done so already.

Now we define a couple of functions to handle POSTing requests to ACRCloud in the multi-part format which it expects. I copied this code from the ACRCloud Python SDK.

Now we can define a function which identifies an MP3 and returns all the available information about it.

Note that I have truncated the size of the sample to 5 Mb in order to avoid an error.

This is where you should fill in your ACRCloud API credentials:

We can try it out with an example like so:

We get the following results:

Not only has it correctly recognized the song, but we have information about how to identify it on Spotify, Deezer, YouTube and Music Story! Now it is a simple matter to iterate over a directory and compile a list of Spotify IDs.

If the track is not in the appropriate format, it is re-coded as an MP3 by Pydub. If the track is still not recognized by ACRCloud or if there is no Spotify ID associated with it, a message is displayed.

Add the tracks to a new playlist in Spotify

For this, we need to install and import the Spotipy (not a typo) library.

Fill in your previously obtained API credentials, as well as your Spotify username and the name of the playlist you’d like to create.

Unfortunately, there is a small bug in the current version of Spotipy (and it doesn’t look as though it will be corrected any time soon). To get around this, we have to make our own function to create a new playlist.

If you run the following code, you will be directed to another website (the one you provided as a callback) and be asked to copy paste the full URL in order to authenticate yourself with Spotify. This is necessary because we are using the API to create and modify your playlists.

The final step is to iterate over the Spotify IDs we obtained earlier and add them to the new playlist. The Spotify API limits us to only being able to add 100 tracks at a time.

Et voilà! You should now have a playlist in Spotify containing your library of MP3s.

The full code is available in my GitHub repository

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store