Use actual BPM values in each bin to name folders accurately
This commit is contained in:
35
src/app.py
35
src/app.py
@@ -1,6 +1,7 @@
|
|||||||
import shutil
|
import shutil
|
||||||
from mutagen.id3 import ID3
|
from mutagen.id3 import ID3
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
dir_a = Path(r"C:\Users\Edgar\Desktop\Peak Time BOF (ed1337x)\Already where in MP3")
|
dir_a = Path(r"C:\Users\Edgar\Desktop\Peak Time BOF (ed1337x)\Already where in MP3")
|
||||||
dir_b = Path(r"C:\Users\Edgar\Desktop\Peak Time BOF (ed1337x)\CD")
|
dir_b = Path(r"C:\Users\Edgar\Desktop\Peak Time BOF (ed1337x)\CD")
|
||||||
@@ -17,39 +18,31 @@ def get_bpm(file_path):
|
|||||||
print(f"[!] Error reading BPM from {file_path.name}: {e}")
|
print(f"[!] Error reading BPM from {file_path.name}: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Step 1: Collect all BPMs from files
|
# Step 1: Scan all files and collect BPMs
|
||||||
bpm_list = []
|
binned_files = defaultdict(list) # bin_index -> list of (file, bpm)
|
||||||
file_bpm_map = {} # Map file -> bpm to avoid re-reading later
|
|
||||||
|
|
||||||
for file in dir_a.glob("*.mp3"):
|
for file in dir_a.glob("*.mp3"):
|
||||||
bpm = get_bpm(file)
|
bpm = get_bpm(file)
|
||||||
if bpm is not None:
|
if bpm is not None:
|
||||||
bpm_list.append(bpm)
|
bin_index = bpm // 5
|
||||||
file_bpm_map[file] = bpm
|
binned_files[bin_index].append((file, bpm))
|
||||||
else:
|
else:
|
||||||
print(f"[-] No BPM found for {file.name}, skipping.")
|
print(f"[-] No BPM found for {file.name}, skipping.")
|
||||||
|
|
||||||
if not bpm_list:
|
if not binned_files:
|
||||||
print("No BPM data found in any files. Exiting.")
|
print("No BPM data found in any files. Exiting.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
min_bpm = min(bpm_list)
|
# Step 2: Process each bin, and use actual min/max BPM in the bin for folder name
|
||||||
max_bpm = max(bpm_list)
|
for bin_index, file_data in binned_files.items():
|
||||||
|
bpms_in_bin = [bpm for _, bpm in file_data]
|
||||||
print(f"Min BPM found: {min_bpm}")
|
actual_min = min(bpms_in_bin)
|
||||||
print(f"Max BPM found: {max_bpm}")
|
actual_max = max(bpms_in_bin)
|
||||||
|
folder_name = f"{actual_min}-to-{actual_max}"
|
||||||
# Step 2: Define bins dynamically based on min/max BPM
|
|
||||||
def bpm_to_bin(bpm):
|
|
||||||
lower_bound = (bpm // 5) * 5
|
|
||||||
upper_bound = lower_bound + 4
|
|
||||||
return f"{lower_bound}-to-{upper_bound}"
|
|
||||||
|
|
||||||
# Step 3: Copy files into proper bins
|
|
||||||
for file, bpm in file_bpm_map.items():
|
|
||||||
folder_name = bpm_to_bin(bpm)
|
|
||||||
target_folder = dir_b / folder_name
|
target_folder = dir_b / folder_name
|
||||||
target_folder.mkdir(exist_ok=True)
|
target_folder.mkdir(exist_ok=True)
|
||||||
|
|
||||||
|
for file, _ in file_data:
|
||||||
shutil.copy(file, target_folder / file.name)
|
shutil.copy(file, target_folder / file.name)
|
||||||
print(f"[+] Copied {file.name} to {folder_name}")
|
print(f"[+] Copied {file.name} to {folder_name}")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user