Use actual BPM values in each bin to name folders accurately

This commit is contained in:
2025-05-23 12:42:03 +02:00
parent cef1be9013
commit 95f319d7a7

View File

@@ -1,6 +1,7 @@
import shutil
from mutagen.id3 import ID3
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_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}")
return None
# Step 1: Collect all BPMs from files
bpm_list = []
file_bpm_map = {} # Map file -> bpm to avoid re-reading later
# Step 1: Scan all files and collect BPMs
binned_files = defaultdict(list) # bin_index -> list of (file, bpm)
for file in dir_a.glob("*.mp3"):
bpm = get_bpm(file)
if bpm is not None:
bpm_list.append(bpm)
file_bpm_map[file] = bpm
bin_index = bpm // 5
binned_files[bin_index].append((file, bpm))
else:
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.")
exit(1)
min_bpm = min(bpm_list)
max_bpm = max(bpm_list)
print(f"Min BPM found: {min_bpm}")
print(f"Max BPM found: {max_bpm}")
# 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)
# Step 2: Process each bin, and use actual min/max BPM in the bin for folder name
for bin_index, file_data in binned_files.items():
bpms_in_bin = [bpm for _, bpm in file_data]
actual_min = min(bpms_in_bin)
actual_max = max(bpms_in_bin)
folder_name = f"{actual_min}-to-{actual_max}"
target_folder = dir_b / folder_name
target_folder.mkdir(exist_ok=True)
shutil.copy(file, target_folder / file.name)
print(f"[+] Copied {file.name} to {folder_name}")
for file, _ in file_data:
shutil.copy(file, target_folder / file.name)
print(f"[+] Copied {file.name} to {folder_name}")