Advertisement
vlatkovski

Mass music categorizing

Oct 5th, 2019
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. import os
  2. import eyed3
  3.  
  4. def fix_name(name):
  5.     """Removes illegal characters for file paths in Windows"""
  6.     forbidden = "<>:\"/\\|?*"
  7.     for character in forbidden:
  8.         name = name.replace(character, "")
  9.     return name
  10.  
  11. def move_all_to_root(rootdir, currdir):
  12.     """
  13.    Recursively moves all files to the root directory
  14.    """
  15.     for filename in os.listdir(currdir):
  16.         filepath = os.path.join(currdir, filename)
  17.         if os.path.isdir(filepath):
  18.             move_all_to_root(rootdir, filepath)
  19.         elif filename.endswith(".mp3"):
  20.             os.rename(filepath, os.path.join(rootdir, filename))
  21.  
  22. def delete_empty_folders(directory, removeThis=False):
  23.     """
  24.    Recursively deletes all empty folders in a directory
  25.    """
  26.     if not os.path.isdir(directory):
  27.         return
  28.     empty = True
  29.     for filename in os.listdir(directory):
  30.         filepath = os.path.join(directory, filename)
  31.         if os.path.isdir(filepath):
  32.             result = delete_empty_folders(filepath, True)
  33.             if result == False:
  34.                 empty = False
  35.         else:
  36.             empty = False
  37.     if empty and removeThis:
  38.         os.rmdir(directory)
  39.     return empty
  40.  
  41. def bad_tag(tag):
  42.     if tag is None:
  43.         return True
  44.     return False
  45.  
  46. def move_to_folders(directory):
  47.     """
  48.    Categorizes all mp3 files in a folder
  49.    This is done using the mp3 tags themselves
  50.    The format is:
  51.        {directory}/{artist} - {album}/{track number} {artist} - {title}
  52.    """
  53.     for filename in os.listdir(directory):
  54.         filepath = os.path.join(directory, filename)
  55.        
  56.         if not os.path.isfile(filepath) or not filename.endswith(".mp3"):
  57.             #os.rename(filepath, filepath + ".mp3")
  58.             continue
  59.  
  60.         audiofile = eyed3.load(filepath)
  61.  
  62.         if audiofile is None:
  63.             continue
  64.         if bad_tag(audiofile.tag.artist):
  65.             continue
  66.         if bad_tag(audiofile.tag.album):
  67.             continue
  68.         if bad_tag(audiofile.tag.title):
  69.             continue
  70.        
  71.         artist = fix_name(audiofile.tag.artist)
  72.         album = fix_name(audiofile.tag.album)
  73.         title = fix_name(audiofile.tag.title)
  74.  
  75.         audiofile.tag.album_artist = artist
  76.  
  77.         newfolder = artist + " - " + album
  78.         newfolderpath = os.path.join(directory, newfolder)
  79.         if not os.path.isdir(newfolderpath):
  80.             os.mkdir(newfolderpath)
  81.  
  82.         newname = ""
  83.         num = audiofile.tag.track_num[0]
  84.         if not num is None:
  85.             newname = newname + str(num).zfill(2) + " "
  86.         newname = newname + artist + " - " + title + ".mp3"
  87.        
  88.         newpath = os.path.join(newfolderpath, newname)
  89.         os.rename(filepath, newpath)
  90.  
  91. def main():
  92.     directory = "D:\\Vlatko\\Music\\"
  93.     move_all_to_root(directory, directory)
  94.     delete_empty_folders(directory)
  95.     move_to_folders(directory)
  96.  
  97. if __name__ == "__main__":
  98.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement