-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic_files.py
130 lines (107 loc) · 4 KB
/
music_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
############## IMPORTS ##############
import os
import re
import sys
import mutagen
from mutagen.mp3 import MP3
from mutagen.flac import FLAC
############# VARIABLES #############
red = "\033[1;31m"
green = "\033[1;32m"
yellow = "\033[1;33m"
no_color = "\033[0m"
############# FUNCTIONS #############
def lowercase_match(match):
"""Lowercase the whole regular expression match group."""
return match.group().lower()
def title(value):
"""Make a string title, without some common mistakes."""
titled = value.title()
titled = re.sub(r"([a-z])'([A-Z])", lowercase_match, titled) # Fix Don'T
titled = re.sub(r"\d([A-Z])", lowercase_match, titled) # Fix 1St and 2Nd
return titled
def prettify(str):
"""Get a string and return it stripped and in title format."""
str = str.strip()
str = title(str)
return str
def rename(path, current_name, new_name):
"""Rename a file."""
current_file = path + "\\" + current_name
new_file = path + "\\" + new_name
print(f"Renaming '{yellow}{current_name}{no_color}' to '{yellow}{new_name}{no_color}'...")
os.rename(current_file, new_file)
def handle_mp3(path, track, file):
"""Handle MP3 files."""
# Artist
if ("TPE1" in track.keys()):
artist = str(track.get("TPE1"))
artist = prettify(artist)
# Track Title
if ("TIT2" in track.keys()):
title = str(track.get("TIT2"))
title = prettify(title)
if artist and title:
new_name = artist + " - " + title + ".mp3"
rename(path, file, new_name)
def handle_flac(path, track, file):
"""Handle FLAC files."""
# Artist
if ("artist" in track.keys()):
artist = str(track.get("artist")[0])
artist = prettify(artist)
# Track Title
if ("title" in track.keys()):
title = str(track.get("title")[0])
title = prettify(title)
if artist and title:
new_name = artist + " - " + title + ".flac"
rename(path, file, new_name)
def main(folder_path, sub_dirs):
for file_name in os.listdir(folder_path):
file_path = folder_path + "\\" + file_name
if os.path.isdir(file_path) and sub_dirs:
# Recursive Call
main(file_path, True)
elif os.path.isfile(file_path):
file = mutagen.File(file_path)
file_type = type(file)
if file_type == mutagen.mp3.MP3:
track = MP3(file_path)
handle_mp3(folder_path, track, file_name)
elif file_type == mutagen.flac.FLAC:
track = FLAC(file_path)
handle_flac(folder_path, track, file_name)
else:
print(f"{yellow}File type {type} is not supported yet...{no_color}")
def exit_program():
print(f"{green}BYE!!{no_color}")
sys.exit(0)
############### CODE ################
print("#######################################################################################")
while True:
ui_1 = input(f"Enter the {green}file path{no_color} that contains the files to be renamed or enter {red}exit{no_color} to exit: ")
if ui_1.lower() != "exit" and not os.path.isdir(ui_1):
print("Please enter a correct file path: ")
continue
else:
break
if ui_1.lower() == "exit":
exit_program()
else:
folder = ui_1
print(f"The directory is set to {yellow}'{folder}'{no_color}.")
while True:
ui_2 = input(f"Do you want to rename the music files inside all sub directories as well? ({green}y{no_color}/{red}n{no_color}) ")
if ui_2.lower() == "y" or ui_2.lower() == "n":
break
else:
print(f"Please answer {green}y{no_color} or {red}n{no_color}")
continue
print("#######################################################################################")
if ui_2.lower() == "exit":
exit_program()
elif ui_2.lower() == "n":
main(folder, False)
elif ui_2.lower() == "y":
main(folder, True)