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.
The songs were downloaded according to the following page. https://support.google.com/googleplaymusic/answer/1250232?hl=ja
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
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/
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)
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 used
to replace invalid characters with _
.
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 ()
.
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.
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