Dynamically generate BPM bins based on min/max BPM of all tracks before copying
This commit is contained in:
47
src/app.py
47
src/app.py
@@ -1,13 +1,9 @@
|
|||||||
import os
|
|
||||||
import shutil
|
import shutil
|
||||||
from mutagen.id3 import ID3, TBPM
|
from mutagen.id3 import ID3
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Define source (dir A) and destination (dir B) directories
|
|
||||||
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")
|
||||||
|
|
||||||
# Make sure destination base exists
|
|
||||||
dir_b.mkdir(parents=True, exist_ok=True)
|
dir_b.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
def get_bpm(file_path):
|
def get_bpm(file_path):
|
||||||
@@ -16,23 +12,44 @@ def get_bpm(file_path):
|
|||||||
bpm_tag = tags.get('TBPM')
|
bpm_tag = tags.get('TBPM')
|
||||||
if bpm_tag:
|
if bpm_tag:
|
||||||
bpm_str = str(bpm_tag.text[0])
|
bpm_str = str(bpm_tag.text[0])
|
||||||
return int(float(bpm_str)) # Some BPMs are floats or strings
|
return int(float(bpm_str))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
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
|
||||||
|
bpm_list = []
|
||||||
|
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:
|
||||||
# Calculate non-overlapping 5-BPM bin
|
bpm_list.append(bpm)
|
||||||
lower_bound = (bpm // 5) * 5
|
file_bpm_map[file] = bpm
|
||||||
upper_bound = lower_bound + 4 # Inclusive range: e.g., 130–134
|
|
||||||
folder_name = f"{lower_bound}-to-{upper_bound}"
|
|
||||||
target_folder = dir_b / folder_name
|
|
||||||
target_folder.mkdir(exist_ok=True)
|
|
||||||
target_file = target_folder / file.name
|
|
||||||
shutil.copy(file, target_file)
|
|
||||||
print(f"[+] Copied {file.name} to {folder_name}")
|
|
||||||
else:
|
else:
|
||||||
print(f"[-] No BPM found for {file.name}, skipping.")
|
print(f"[-] No BPM found for {file.name}, skipping.")
|
||||||
|
|
||||||
|
if not bpm_list:
|
||||||
|
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)
|
||||||
|
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}")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user