Compare commits
3 Commits
dfe237d3ea
...
1f83bb8517
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1f83bb8517 | ||
![]() |
76d25ebe5e | ||
![]() |
63953f45e8 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -180,3 +180,5 @@ cython_debug/
|
|||||||
.cursorignore
|
.cursorignore
|
||||||
.cursorindexingignore
|
.cursorindexingignore
|
||||||
|
|
||||||
|
# ed1337x
|
||||||
|
config.ini
|
||||||
|
198
src/app.py
198
src/app.py
@ -1,29 +1,61 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from mutagen.id3 import ID3
|
from mutagen.id3 import ID3, TBPM, ID3NoHeaderError
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
import configparser
|
||||||
|
import sys
|
||||||
|
import librosa
|
||||||
|
|
||||||
CD_SIZE = 695 * 1024 * 1024 # 695 MB
|
|
||||||
GROUP_SIZE = 5
|
GROUP_SIZE = 5
|
||||||
|
|
||||||
# Store the current date
|
def load_config():
|
||||||
run_date = datetime.now().strftime("%Y-%m-%d_%H%M%S")
|
config_path = Path("config.ini")
|
||||||
|
default_config_path = Path("def_config.ini")
|
||||||
|
|
||||||
# Ask for source path, strip quotes if pasted
|
if not config_path.exists():
|
||||||
source_input = input("Drag and drop your music folder here, then press Enter: ").strip().strip('"')
|
if default_config_path.exists():
|
||||||
source_media_path = Path(source_input)
|
shutil.copy(default_config_path, config_path)
|
||||||
|
print("config.ini created from def_config.ini with default settings.")
|
||||||
|
else:
|
||||||
|
print("Error: def_config.ini not found! Please create it and rerun.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
if not source_media_path.exists() or not source_media_path.is_dir():
|
default_config = configparser.ConfigParser()
|
||||||
print(f"Error: {source_media_path} is not a valid directory.")
|
default_config.read(default_config_path)
|
||||||
exit(1)
|
|
||||||
|
|
||||||
# Build destination path
|
user_config = configparser.ConfigParser()
|
||||||
parent = source_media_path.parent
|
user_config.read(config_path)
|
||||||
folder_name = source_media_path.name
|
|
||||||
dest_media_path = parent / f"[CDs-{run_date}]{folder_name}"
|
merged_config = configparser.ConfigParser()
|
||||||
dest_media_path.mkdir(parents=True, exist_ok=True)
|
merged_config.read_dict(default_config)
|
||||||
|
merged_config.read_dict(user_config)
|
||||||
|
|
||||||
|
return merged_config
|
||||||
|
|
||||||
|
def analyze_bpm_librosa(file_path):
|
||||||
|
try:
|
||||||
|
y, sr = librosa.load(file_path, mono=True)
|
||||||
|
tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
|
||||||
|
return int(round(float(tempo[0])))
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error analyzing BPM for {file_path.name}: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def write_bpm_tag(file_path, bpm):
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
tags = ID3(file_path)
|
||||||
|
except ID3NoHeaderError:
|
||||||
|
tags = ID3()
|
||||||
|
|
||||||
|
tags.delall("TBPM")
|
||||||
|
tags.add(TBPM(encoding=3, text=str(bpm)))
|
||||||
|
tags.save(file_path)
|
||||||
|
print(f"Written BPM={bpm} to {file_path.name}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to write BPM tag for {file_path.name}: {e}")
|
||||||
|
|
||||||
def get_bpm(file_path):
|
def get_bpm(file_path):
|
||||||
try:
|
try:
|
||||||
@ -31,68 +63,96 @@ def get_bpm(file_path):
|
|||||||
bpm_tag = tags.get("TBPM")
|
bpm_tag = tags.get("TBPM")
|
||||||
if bpm_tag:
|
if bpm_tag:
|
||||||
return int(float(bpm_tag.text[0]))
|
return int(float(bpm_tag.text[0]))
|
||||||
except Exception as e:
|
except Exception:
|
||||||
print(f"Skipping {file_path.name} (no BPM): {e}")
|
pass
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Collect all tracks recursively, grouping unknown BPM separately
|
|
||||||
all_tracks = []
|
|
||||||
for file in source_media_path.rglob("*.mp3"): # <-- recursive glob
|
|
||||||
bpm = get_bpm(file)
|
|
||||||
size = file.stat().st_size
|
|
||||||
if bpm is None:
|
|
||||||
bpm_range = "[Unknown BPM]"
|
|
||||||
else:
|
|
||||||
bpm_range = f"{(bpm // GROUP_SIZE) * GROUP_SIZE}-to-{((bpm // GROUP_SIZE) * GROUP_SIZE) + GROUP_SIZE - 1}"
|
|
||||||
all_tracks.append({"file": file, "size": size, "bpm_range": bpm_range})
|
|
||||||
|
|
||||||
if not all_tracks:
|
|
||||||
print("No MP3 files found.")
|
|
||||||
exit(0)
|
|
||||||
|
|
||||||
# Calculate total size and number of CDs needed
|
|
||||||
total_size = sum(t["size"] for t in all_tracks)
|
|
||||||
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")
|
|
||||||
|
|
||||||
# Group tracks by BPM range
|
|
||||||
bpm_groups = defaultdict(list)
|
|
||||||
for track in all_tracks:
|
|
||||||
bpm_groups[track["bpm_range"]].append(track)
|
|
||||||
|
|
||||||
# Sort each bpm group
|
|
||||||
for bpm_range in bpm_groups:
|
|
||||||
bpm_groups[bpm_range].sort(key=lambda x: x["file"].name)
|
|
||||||
|
|
||||||
# Split each BPM group evenly into num_cds parts
|
|
||||||
def split_evenly(lst, n):
|
def split_evenly(lst, n):
|
||||||
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)]
|
||||||
|
|
||||||
bpm_chunks = {}
|
def main():
|
||||||
for bpm_range, tracks in bpm_groups.items():
|
config = load_config()
|
||||||
bpm_chunks[bpm_range] = split_evenly(tracks, num_cds)
|
|
||||||
|
|
||||||
# Prepare CDs
|
CD_SIZE = config.getint("Settings", "SplitFolderMB") * 1024 * 1024
|
||||||
cd_contents = [[] for _ in range(num_cds)]
|
bWriteNonPresentBPM = config.getboolean("Settings", "bWriteNonPresentBPM")
|
||||||
|
bCheckAllTracksBPM = config.getboolean("Settings", "bCheckAllTracksBPM")
|
||||||
|
|
||||||
# Fill CDs with balanced BPM chunks
|
run_date = datetime.now().strftime("%Y-%m-%d_%H%M%S")
|
||||||
for bpm_range in bpm_chunks:
|
|
||||||
for i, chunk in enumerate(bpm_chunks[bpm_range]):
|
|
||||||
cd_contents[i].extend(chunk)
|
|
||||||
|
|
||||||
# Write CDs with BPM subfolders
|
source_input = input("Drag and drop your music folder here, then press Enter: ").strip().strip('"')
|
||||||
for i, tracks in enumerate(cd_contents, start=1):
|
source_media_path = Path(source_input)
|
||||||
cd_folder = dest_media_path / f"CD-{i:02}"
|
|
||||||
cd_folder.mkdir(parents=True, exist_ok=True)
|
|
||||||
size_accum = 0
|
|
||||||
for track in tracks:
|
|
||||||
bpm_subfolder = cd_folder / track["bpm_range"]
|
|
||||||
bpm_subfolder.mkdir(parents=True, exist_ok=True)
|
|
||||||
shutil.copy(track["file"], bpm_subfolder / track["file"].name)
|
|
||||||
size_accum += track["size"]
|
|
||||||
print(f"[WRITE] CD-{i:02}: {len(tracks)} tracks, approx {size_accum/1024**2:.2f} MB")
|
|
||||||
|
|
||||||
print("\n✅ Done!")
|
if not source_media_path.exists() or not source_media_path.is_dir():
|
||||||
|
print(f"Error: {source_media_path} is not a valid directory.")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
all_tracks = []
|
||||||
|
for file in source_media_path.rglob("*.mp3"):
|
||||||
|
bpm = get_bpm(file)
|
||||||
|
|
||||||
|
if bpm is None and bWriteNonPresentBPM:
|
||||||
|
bpm = analyze_bpm_librosa(file)
|
||||||
|
if bpm is not None:
|
||||||
|
write_bpm_tag(file, bpm)
|
||||||
|
|
||||||
|
elif bpm is not None and bCheckAllTracksBPM:
|
||||||
|
new_bpm = analyze_bpm_librosa(file)
|
||||||
|
if new_bpm is not None and new_bpm != bpm:
|
||||||
|
bpm = new_bpm
|
||||||
|
write_bpm_tag(file, bpm)
|
||||||
|
|
||||||
|
size = file.stat().st_size
|
||||||
|
if bpm is None:
|
||||||
|
bpm_range = "[Unknown BPM]"
|
||||||
|
else:
|
||||||
|
bpm_range = f"{(bpm // GROUP_SIZE) * GROUP_SIZE}-to-{((bpm // GROUP_SIZE) * GROUP_SIZE) + GROUP_SIZE - 1}"
|
||||||
|
all_tracks.append({"file": file, "size": size, "bpm_range": bpm_range})
|
||||||
|
|
||||||
|
if not all_tracks:
|
||||||
|
print("No MP3 files found.")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
total_size = sum(t["size"] for t in all_tracks)
|
||||||
|
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")
|
||||||
|
|
||||||
|
bpm_groups = defaultdict(list)
|
||||||
|
for track in all_tracks:
|
||||||
|
bpm_groups[track["bpm_range"]].append(track)
|
||||||
|
|
||||||
|
for bpm_range in bpm_groups:
|
||||||
|
bpm_groups[bpm_range].sort(key=lambda x: x["file"].name)
|
||||||
|
|
||||||
|
bpm_chunks = {}
|
||||||
|
for bpm_range, tracks in bpm_groups.items():
|
||||||
|
bpm_chunks[bpm_range] = split_evenly(tracks, num_cds)
|
||||||
|
|
||||||
|
cd_contents = [[] for _ in range(num_cds)]
|
||||||
|
|
||||||
|
for bpm_range in bpm_chunks:
|
||||||
|
for i, chunk in enumerate(bpm_chunks[bpm_range]):
|
||||||
|
cd_contents[i].extend(chunk)
|
||||||
|
|
||||||
|
for i, tracks in enumerate(cd_contents, start=1):
|
||||||
|
cd_folder = dest_media_path / f"CD-{i:02}"
|
||||||
|
cd_folder.mkdir(parents=True, exist_ok=True)
|
||||||
|
size_accum = 0
|
||||||
|
for track in tracks:
|
||||||
|
bpm_subfolder = cd_folder / track["bpm_range"]
|
||||||
|
bpm_subfolder.mkdir(parents=True, exist_ok=True)
|
||||||
|
shutil.copy(track["file"], bpm_subfolder / track["file"].name)
|
||||||
|
size_accum += track["size"]
|
||||||
|
print(f"[WRITE] CD-{i:02}: {len(tracks)} tracks, approx {size_accum / 1024**2:.2f} MB")
|
||||||
|
|
||||||
|
print("\n✓ Done!")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
4
src/def_config.ini
Normal file
4
src/def_config.ini
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[Settings]
|
||||||
|
bWriteNonPresentBPM = False
|
||||||
|
bCheckAllTracksBPM = False
|
||||||
|
SplitFolderMB = 695
|
Loading…
x
Reference in New Issue
Block a user