Automatically organized mp3s downloaded from Google Play Music using Python

Start

The news that Google Play Music will end at the end of December has been announced. Purchased and uploaded songs can be transferred to YouTube music as they are, but I do not know when the service will end, so I decided to back it up to the local environment just in case. When I downloaded the songs, all the data was downloaded all at once in the same folder, so I decided to use Python to divide the songs into folders for each "artist name" and "album name". Even a programming amateur like me seems to be able to do this kind of thing.

download

The songs were downloaded according to the following page. https://support.google.com/googleplaymusic/answer/1250232?hl=ja

Folder hierarchy

The folder hierarchy is set as follows.

Root ├ Artist A │ └ Album name │ └ Music_1.mp3 │ └ Music_2.mp3 ├ Artist B ├ Artist C

I referred to here for how to fill in the directory configuration diagram. https://qiita.com/paty-fakename/items/c82ed27b4070feeceff6

Get mp3 tag information

Use ʻEasyID3` to get the tag information included in mp3 such as song name, artist name, album name. I referred to this site. https://note.nkmk.me/python-mutagen-mp3-id3/

Completed code

Suddenly, the completed code is as follows.

from mutagen.easyid3 import EasyID3
import os
import shutil
import re

def replace(string):
    return(re.sub(r'[\\/:*?"<>|]+', '-', string))

def arrange_data(mp3_path):
    tags = EasyID3(mp3_path)
    try:
        artistname = (tags['albumartist'])
    except KeyError:
        artistname = (tags['artist'])
    albumname = (tags['album'])
    artistname = replace(artistname[0]).strip()
    albumname = replace(albumname[0]).strip()
    title = os.path.basename(mp3_path)
    artist_dir = os.path.join(globalpath, artistname)
    album_dir = os.path.join(artist_dir, albumname)
    new_mp3_path = os.path.join(album_dir, title)
    if not artistname in artist_list:
        if not os.path.isdir(artist_dir):
            os.makedirs(artist_dir)
            artist_list.append(artistname)
    if not albumname in album_list:
        album_list.append(albumname)
        if not os.path.isdir(album_dir):
            os.makedirs(album_dir)
            album_list.append(albumname)
    try:
        shutil.copyfile(mp3_path, new_mp3_path)
        print(title + " is done.")
    except shutil.SameFileError:
        print(title + " is already exists.")


def data_check(file_list, path):
    for i in file_list:
        if os.path.isdir(os.path.join(path, i)):
            new_path = os.path.join(path, i)
            new_file_list = os.listdir(new_path)
            data_check(new_file_list, new_path)
        else:
            arrange_data(os.path.join(path, i))


globalpath = r"Folder containing songs"
file_list = os.listdir(globalpath)
album_list = []
artist_list = []

data_check(file_list, globalpath)

The point that got caught

KeyError: occurs

There are invalid characters \ /: *?" <> | Cannot be used as file names on Windows, but there were multiple artist names and album names that contained invalid characters. This isre.sub I usedto replace invalid characters with _.

There is a space in the directory path

If there is a space at the end of the artist name or album name, the actual folder name and folder path do not match, resulting in a read error. This was solved using .strip ().

feat. Problem

For songs whose artist name is something like "feat. 〇〇" and it is difficult to sort by artist name, I used the album artist name. Songs with an album artist name will be given priority, and songs without an album artist name will use the artist name.

in conclusion

I was able to organize the data safely. It would be convenient if you could use Python even a little. It seems that some songs do not work properly, but I am happy because I was able to process all the local data.

Thank you very much!

Recommended Posts

Automatically organized mp3s downloaded from Google Play Music using Python
Flatten using Python yield from
Play music from USB speakers using the ROS package (gx_sound_player)
Play with YouTube Data API v3 using Google API Python Client
Python beginners hit the unofficial API of Google Play Music to play music
Using Rstan from Python with PypeR
[Python3] Google translate google translate without using api
Notes on using MeCab from Python
Forcibly use Google Translate from python
Using Cloud Storage from Python3 (Introduction)
Run Ansible from Python using API
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
Use Google Analytics API from Python
Try using Amazon DynamoDB from Python
How to deal with OAuth2 error when using Google APIs from Python
Create a tool to automatically furigana with html using Mecab from Python3
From Python to using MeCab (and CaboCha)
Play audio files from Python with interrupts
How to update Google Sheets from Python
[Beginner] Python web scraping using Google Colaboratory
Try using Python with Google Cloud Functions
Use Google Cloud Vision API from Python
I tried using UnityCloudBuild API from Python
Creating Google Spreadsheet using Python / Google Data API
Automatically save images of your favorite characters from Google Image Search with Python
I tried using Google Translate from Python and it was just too easy