Add run_date variable, prompt for source path, auto-generate destination folder with date-based name

This commit is contained in:
Roncero Blanco, Edgar
2025-05-26 13:35:48 +02:00
parent a83b7b0d11
commit e8cf98708b

View File

@@ -2,13 +2,28 @@ import os
import shutil import shutil
from pathlib import Path from pathlib import Path
from mutagen.id3 import ID3 from mutagen.id3 import ID3
from datetime import datetime
from collections import defaultdict
CD_SIZE = 700 * 1024 * 1024 # 700 MB CD_SIZE = 695 * 1024 * 1024 # 695 MB
GROUP_SIZE = 5 GROUP_SIZE = 5
dir_a = Path(r"C:\Users\Edgar\Desktop\Peak Time BOF (ed1337x)\Already where in MP3") # Store the current date
dir_b = Path(r"C:\Users\Edgar\Desktop\Peak Time BOF (ed1337x)\CD") run_date = datetime.now().strftime("%Y-%m-%d")
dir_b.mkdir(parents=True, exist_ok=True)
# Ask for source path
source_input = input("Enter the full path to your source MP3 folder: ").strip()
source_media_path = Path(source_input)
if not source_media_path.exists() or not source_media_path.is_dir():
print(f"Error: {source_media_path} is not a valid directory.")
exit(1)
# Build destination path
parent = source_media_path.parent
folder_name = source_media_path.name
dest_media_path = parent / f"[CDs-{run_date}]{folder_name}"
dest_media_path.mkdir(parents=True, exist_ok=True)
def get_bpm(file_path): def get_bpm(file_path):
try: try:
@@ -22,7 +37,7 @@ def get_bpm(file_path):
# Collect all tracks # Collect all tracks
all_tracks = [] all_tracks = []
for file in dir_a.glob("*.mp3"): for file in source_media_path.glob("*.mp3"):
bpm = get_bpm(file) bpm = get_bpm(file)
if bpm is None: if bpm is None:
continue continue
@@ -32,23 +47,21 @@ for file in dir_a.glob("*.mp3"):
# Calculate total size and number of CDs needed # Calculate total size and number of CDs needed
total_size = sum(t["size"] for t in all_tracks) total_size = sum(t["size"] for t in all_tracks)
num_cds = max(1, (total_size + CD_SIZE - 1) // CD_SIZE) # ceiling division num_cds = max(1, (total_size + CD_SIZE - 1) // CD_SIZE)
print(f"Total size: {total_size / 1024**2:.2f} MB, splitting into {num_cds} CDs") print(f"Total size: {total_size / 1024**2:.2f} MB, splitting into {num_cds} CDs")
# Group tracks by BPM range # Group tracks by BPM range
from collections import defaultdict
bpm_groups = defaultdict(list) bpm_groups = defaultdict(list)
for track in all_tracks: for track in all_tracks:
bpm_groups[track["bpm_range"]].append(track) bpm_groups[track["bpm_range"]].append(track)
# Sort each bpm group by filename (or size if you want) # Sort each bpm group
for bpm_range in bpm_groups: for bpm_range in bpm_groups:
bpm_groups[bpm_range].sort(key=lambda x: x["file"].name) bpm_groups[bpm_range].sort(key=lambda x: x["file"].name)
# Split each BPM group evenly into num_cds parts # Split each BPM group evenly into num_cds parts
def split_evenly(lst, n): def split_evenly(lst, n):
"""Split list lst into n chunks as evenly as possible by count."""
k, m = divmod(len(lst), n) k, m = divmod(len(lst), n)
return [lst[i*k + min(i, m):(i+1)*k + min(i+1, m)] for i in range(n)] return [lst[i*k + min(i, m):(i+1)*k + min(i+1, m)] for i in range(n)]
@@ -59,14 +72,14 @@ for bpm_range, tracks in bpm_groups.items():
# Prepare CDs # Prepare CDs
cd_contents = [[] for _ in range(num_cds)] cd_contents = [[] for _ in range(num_cds)]
# Fill CDs with chunks from each BPM range # Fill CDs with balanced BPM chunks
for bpm_range in bpm_chunks: for bpm_range in bpm_chunks:
for i, chunk in enumerate(bpm_chunks[bpm_range]): for i, chunk in enumerate(bpm_chunks[bpm_range]):
cd_contents[i].extend(chunk) cd_contents[i].extend(chunk)
# Write CDs with BPM subfolders # Write CDs with BPM subfolders
for i, tracks in enumerate(cd_contents, start=1): for i, tracks in enumerate(cd_contents, start=1):
cd_folder = dir_b / f"CD-{i:02}" cd_folder = dest_media_path / f"CD-{i:02}"
cd_folder.mkdir(parents=True, exist_ok=True) cd_folder.mkdir(parents=True, exist_ok=True)
size_accum = 0 size_accum = 0
for track in tracks: for track in tracks: