Skip to content

Commit edd516e

Browse files
committed
better sampling, needs dir cleanup work
1 parent b41df00 commit edd516e

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

audiomanager.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ def bytestream_from_unknown_mp3(mp3_path):
77
base_name = os.path.basename(mp3_path)
88
base_name_no_ext = base_name[:base_name.rindex('.')]
99

10-
# full_song = AudioSegment.from_mp3(mp3_path)
1110
full_song = AudioSegment.from_file(mp3_path)
12-
first_5_seconds = full_song[10000:15000]
11+
#todo determine song length and cut into the middle? Luke Brian Song tagged incorrectly
12+
midsong_start_ms = full_song.duration_seconds * 1000 // 2
13+
midsong_end_ms = midsong_start_ms + 5000
14+
sample_5_seconds = full_song[midsong_start_ms:midsong_end_ms]
1315

1416
extra_params = ["-ar", "44100", "-ac", "1"]
15-
first_5_seconds.export(base_name_no_ext + '.raw', format='s16le', codec='pcm_s16le', parameters=extra_params)
17+
sample_5_seconds.export(base_name_no_ext + '.raw', format='s16le', codec='pcm_s16le', parameters=extra_params)
1618

1719
raw_file = open(base_name_no_ext + '.raw', 'rb')
1820
return base64.b64encode(raw_file.read())

filemanager.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -107,54 +107,56 @@ def cleanup_jpg_files(working_dir):
107107
os.remove(os.path.join(root, filename))
108108

109109

110-
def makedir_if_absent(base_path, path):
110+
def makedir_if_absent(path):
111111
if not os.path.exists(path):
112-
try:
113-
os.makedirs(path)
114-
except (NotADirectoryError, FileNotFoundError):
115-
new_path = base_path
116-
relative_path = os.path.relpath(path, base_path)
117-
relative_path_dirlist = relative_path.split(os.path.sep)
118-
for directory in relative_path_dirlist:
119-
new_path = os.path.join(new_path, re.sub(r'[^\w_. -]', '_', directory))
120-
if not os.path.exists(new_path):
121-
os.makedirs(new_path)
122-
123-
return path
112+
os.makedirs(path)
124113

125114

126115
def reorganize_files(path, quarantine_dir):
127116
for root, dirs, files in os.walk(path):
128117
if not root.endswith(quarantine_dir):
129118
for name in files:
130-
full_path = os.path.join(root, name)
131-
organize_song_by_tags(path, full_path)
119+
if os.path.splitext(name)[1] == '.mp3':
120+
full_path = os.path.join(root, name)
121+
organize_song_by_tags(path, full_path, quarantine_dir)
132122

133123

134-
def organize_song_by_tags(base_music_path, filepath):
124+
def organize_song_by_tags(base_music_path, filepath, quarantine_dir):
135125
file = music_tag.load_file(filepath)
136126
artist_name = str(file['artist'])
137127
album_name = str(file['album'])
128+
artist_name_sanitized = sanitize_name_for_directory(artist_name)
129+
album_name_sanitized = sanitize_name_for_directory(album_name)
130+
makedir_if_absent(os.path.join(base_music_path, artist_name_sanitized))
131+
makedir_if_absent(os.path.join(base_music_path, artist_name_sanitized, album_name_sanitized))
132+
133+
if not check_song_location(filepath, artist_name_sanitized, artist_name_sanitized):
134+
try:
135+
os.rename(filepath, os.path.join(base_music_path, artist_name_sanitized, album_name_sanitized,
136+
os.path.basename(filepath)))
137+
except FileExistsError:
138+
try:
139+
makedir_if_absent(os.path.join(quarantine_dir, 'DUPLICATE_FILES'))
140+
os.rename(filepath, os.path.join(quarantine_dir, 'DUPLICATE_FILES', os.path.basename(filepath)))
141+
except FileExistsError:
142+
return
138143

139-
base_name = os.path.basename(filepath)
140-
artist_dir = makedir_if_absent(base_music_path, os.path.join(base_music_path, artist_name))
141-
album_dir = makedir_if_absent(base_music_path, os.path.join(base_music_path, artist_name, album_name))
142-
directory_list = album_dir.split(os.sep)
143-
if not check_song_location(directory_list, artist_name, album_name):
144-
os.rename(filepath, os.path.join(album_dir, base_name))
145144

145+
def check_song_location(filepath, artist_name_sanitized, album_name_sanitized):
146+
directory_list = filepath.split(os.sep)
146147

147-
def check_song_location(directory_list, artist_name, album_name):
148148
if len(directory_list) < 2:
149149
return False
150150

151-
if directory_list[-1] != album_name:
151+
if directory_list[-1] != album_name_sanitized:
152152
return False
153153

154-
if directory_list[-2] != artist_name:
154+
if directory_list[-2] != artist_name_sanitized:
155155
return False
156156

157157
return True
158158

159159

160160
def sanitize_name_for_directory(name):
161+
#TODO this check is too aggressive, need to rework
162+
return re.sub(r'[^\w_. -]', '_', name)

main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
CLEANUP_DIRECTORY = "C:\\Users\\bennu\\OneDrive\\Desktop\\chelsea_phone_test"
1212
BAD_FILES_DIRNAME = "TuneTagger_BadFiles"
1313
bad_files_directory = os.path.join(CLEANUP_DIRECTORY, BAD_FILES_DIRNAME)
14-
makedir_if_absent(CLEANUP_DIRECTORY, bad_files_directory)
14+
makedir_if_absent(bad_files_directory)
1515

1616
sm = ShazamManager()
1717
for filepath in find_candidate_files(CLEANUP_DIRECTORY, BAD_FILES_DIRNAME):

0 commit comments

Comments
 (0)