From a019daabed91485722ded004d0c39e473c80dfe9 Mon Sep 17 00:00:00 2001 From: eroncero Date: Fri, 23 May 2025 12:00:33 +0200 Subject: [PATCH] Add dynamic BPM-based folder creation --- src/app.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/app.py b/src/app.py index 29095ea..4baae95 100644 --- a/src/app.py +++ b/src/app.py @@ -1,38 +1,53 @@ import os import shutil -from mutagen.mp3 import MP3 +from collections import defaultdict from mutagen.id3 import ID3, TBPM - from pathlib import Path -# Define paths +# Config 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") +bin_size = 5 # BPM range size -# Ensure dir B exists +# Ensure target directory exists dir_b.mkdir(parents=True, exist_ok=True) +# Function to get BPM from file def get_bpm(file_path): try: tags = ID3(file_path) bpm_tag = tags.get('TBPM') if bpm_tag: bpm_str = str(bpm_tag.text[0]) - return int(float(bpm_str)) # Some BPMs are float strings + return int(float(bpm_str)) except Exception as e: print(f"Could not read BPM from {file_path}: {e}") return None +# Collect BPMs +bpm_to_files = defaultdict(list) + for file in dir_a.glob("*.mp3"): bpm = get_bpm(file) - if bpm is not None: - lower_bound = (bpm // 5) * 5 - upper_bound = lower_bound + 5 - folder_name = f"{lower_bound}-to-{upper_bound}" - 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}") + if bpm: + bpm_to_files[bpm].append(file) else: - print(f"No BPM found for {file.name}, skipping.") + print(f"Skipping {file.name} (no BPM)") + +# Group into dynamic 5-BPM bins +bins = defaultdict(list) + +for bpm, files in bpm_to_files.items(): + bin_start = (bpm // bin_size) * bin_size + bin_end = bin_start + bin_size + bin_label = f"{bin_start}-to-{bin_end}" + bins[bin_label].extend(files) + +# Copy files to corresponding bins +for bin_label, files in bins.items(): + target_folder = dir_b / bin_label + target_folder.mkdir(exist_ok=True) + for file in files: + shutil.copy(file, target_folder / file.name) + print(f"Copied {file.name} to {bin_label}")