Created
June 10, 2017 13:39
-
-
Save brentvollebregt/ecd8d560ced1c7f80dd4cf7e70a7c245 to your computer and use it in GitHub Desktop.
Sorts a directory of mp3 and m4a files to folders by their tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Will sort a given direcotry of music (copy) | |
# Requires mutagen | |
import os | |
from mutagen.id3 import ID3 | |
from mutagen.easymp4 import EasyMP4 | |
import shutil | |
input_folder = input("What is the folder to get files from?") | |
output_folder = os.getcwd() + "/Output/" | |
def chechAndCreate(direcotry): | |
if not os.path.exists(direcotry): | |
os.makedirs(direcotry) | |
return | |
duplicates_file_created = False | |
for root, dirs, files in os.walk(input_folder, topdown=False): | |
for name in files: | |
file = os.path.join(root, name) | |
primary_tags_available = True | |
if file.endswith(".mp3"): | |
audio = ID3(file) | |
try: | |
Artist = audio['TPE1'].text[0] | |
Artist = "".join([i for i in Artist if i not in ['\\', '/', '?', ':', '*', '"', '>', '<', '|']]) | |
Title = audio['TIT2'].text[0] | |
Title = "".join([i for i in Title if i not in ['\\', '/', '?', ':', '*', '"', '>', '<', '|']]) | |
except: | |
primary_tags_available = False | |
elif file.endswith(".m4a"): | |
audio = EasyMP4(file) | |
try: | |
Artist = audio['artist'][0] | |
Artist = "".join([i for i in Artist if i not in ['\\', '/', '?', ':', '*', '"', '>', '<', '|']]) | |
Title = audio['title'][0] | |
Title = "".join([i for i in Title if i not in ['\\', '/', '?', ':', '*', '"', '>', '<', '|']]) | |
except: | |
primary_tags_available = False | |
else: | |
continue | |
if primary_tags_available: | |
new_file_base = Title + ".m4a" | |
new_file_directory = output_folder + Artist + "/" | |
else: | |
new_file_base = os.path.basename(file) | |
new_file_directory = output_folder + "[Unsorted]/" | |
chechAndCreate(new_file_directory) | |
not_allowed_characters = ['\\', '/', '?', ':', '*', '"', '>', '<', '|'] | |
for i in not_allowed_characters: | |
if i in new_file_base: | |
new_file_base = new_file_base.replace(i, "") | |
if not os.path.isfile(new_file_directory + new_file_base): | |
shutil.copyfile(file, new_file_directory + new_file_base) | |
print ("Coppied: " + new_file_directory + new_file_base) | |
else: | |
duplicates_file_created = True | |
chechAndCreate(output_folder + "[Duplicates]/") | |
f = open(output_folder + "[Duplicates]/Note.txt" , 'w') | |
f.write("These files have appeared in here because they have the same artist and title tags\nThe 'original' file will be able to be found in its proper direcotry while all 'duplicates' (may not be) will be found in this direcotry") | |
f.close() | |
new_file_to_save_to_original = output_folder + "[Duplicates]/" + new_file_base | |
new_file_to_save_to = new_file_to_save_to_original | |
loop = 0 | |
while True: | |
if loop > 0: | |
new_file_to_save_to = new_file_to_save_to_original[:-4] + "(" + str(loop) + ")" + new_file_to_save_to_original[-4:] | |
if not os.path.isfile(new_file_to_save_to): | |
shutil.copyfile(file, new_file_to_save_to) | |
break | |
else: | |
loop += 1 | |
print ("Coppied: " + new_file_to_save_to) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment