Python help with Google Music takeout

in STEMGeeks2 years ago

Sometime last year Google decided to do away with their Google Music offering, replacing it instead with Youtube Music.


(image courtesy of Pixabay)

I'm not a terribly big audiophile, but I do enjoy listening to music when I'm driving or cycling. I had everything set up just right with Google Music and it responded very well to my voice commands when driving. What I enjoyed about Google Music was that it was geared toward listening to the music I already had, and had uploaded to it - all 90GB of it.

Last year Google informed that they were going to sunset Google Music and replace it with Youtube Music. Dutifully, I allowed them to migrate my music over and began to use the new system. I gave it the old college try. Ultimately, however, I decided that YT Music is geared more toward browsing genre's that listening to very specific songs and albums, which is my main use case. So, something had to be done, knowing that I can't go back to Google Play Music.

Google does offer a "takeout" feature, which allows you to download your own info and data from their systems, so I downloaded all my music. It was received in a series of zip files which, when unzipped, were all unzipped into the same directory. Yes, the same, single directory. All 90GB of my music, now in one directory. Granted they were all named nicely, but browsing this was impossible. I can imagine the average user being really frustrated with this.

Thankfully (for me) I happen to know a little bit of python and was able to write up a small script to help me out.

Screenshot at Jan 03 094213.png

This script had a very simple function: iterate through all the mp3 files in the music directory, separate by artist and album, and move each into appropriate directories. To do this I used the python eyed3 modules, which is geared toward reading the metadata of mp3 files.

If anyone is interested, here is the script. It is rather straightforward with comments where appropriate. There was no need to make it any more complex, though undoubtedly there are improvements that can be made to it. What I really enjoy about python, however, is it's simplicity and usefulness for small projects just like this one I was facing. If I had to productise this script I'd add a configuration file that allowed the user to configure the output directory (instead of my own "Downloads" directory, which I routinely use as a cheap "/tmp" directory) and would log the output instead of printing to stdout.


import eyed3
import sys,os
import glob, re

songdict = {}

def main():
    os.chdir("Takeout/Google Play Music/Tracks")
    songs = glob.glob("*mp3")
    for filename in songs:
        audiofile = eyed3.load(filename)
        # I need the artist and album. The rest are just here for future use.
        artist = audiofile.tag.artist
        album = audiofile.tag.album

        #song_title = audiofile.tag.title
        #track_number = audiofile.tag.track_num
        #album_artist = audiofile.tag.album_artist

        # We may have no data in some of the songs.
        # Let's do some error checking and assign "Unknown" if necessary.
        # I also want to remove special characters for directory and file names

        if artist is None:
            artist = "Unknown"
        artist = re.sub('\W+',' ', artist )
        if album is None:
            album = "Unknown"
        album = re.sub('\W+',' ', album )

        if artist not in songdict:
            # If this holds true, then songdict[artist] does not exist as a dictionary item. We need to create it.
            songdict[artist] = {}

        if album not in songdict[artist]:
            # IF this holds true, then songdict[artist][album] does not exist as a list. We want this to be a list, not a dictionary item.
            songdict[artist][album] = []

        # Now we can add the filename to the songdict[artist][album] list


    for artist in songdict:
        ## I want to create a directory for this artist.
        # first check to see if it exists. If not, create it.
        artist_directory = "/mnt/c/Users/Victor Wiebe/Downloads/" + artist
        if os.path.isdir(artist_directory) == 0:
            # If we are here then my artist directory does not exist. Let's create it.
            print ("%s : Creating new Artist directory: %s" % (artist,artist_directory) )

        for album in songdict[artist]:
            print (album)
            # Now let's do the same for the album as a subdirectory of the artist directory
            album_directory = "/mnt/c/Users/Victor Wiebe/Downloads/" + artist + "/" + album
            if os.path.isdir(album_directory) == 0:
                print ("%s : Creating new Album directory: %s" % (artist,album_directory))

            for song in songdict[artist][album]:
                new_file = album_directory + "/" + song
                print ("%s : Moving song %s to %s" % (artist,song,new_file))

if __name__ == "__main__":

If you've made it to this point you've probably at least browsed the code and wondered about a couple parts of it. This was written for python3 on a Windows 10 desktop using Ubuntu on Windows Subsystem for Linux (WSL). Two years ago I never would have thought that the Windows operating syste would have any place on any of my computers, but WSL on Win 10 has really given me some food for thought.

(c) All images and photographs, unless otherwise specified, are created and owned by me.
(c) Victor Wiebe

About Me

Amateur photographer. Wannabe author. Game designer. Nerd. 
General all around problem-solver and creative type.

My Favourite Tags




Thanks, Victor!

Gracias @wwwiebe
Creo que es un gran trabajo para un amante de la musica.
Espero la APP

Well I made it part way thru your code (grin)! I'll need to sit down and take some time to better understand it! I'm continually impressed with the power of Python3!

I've been using python for years; it's my goto whenever I need to script something up. There's even a Node-Red function that allows for python scripting!

D*rn... now not only the Internet is difficult for the average user, even the music services getting too difficult for the mass {LOL} Google Music: I didn't follow this one at all, but am not surprised they stopped it so quickly after launch. Google is not a media house at all, and entered the game way too late. Super you created a script to get the files a little better organised :)

Thanks for sharing