music-library-tools/misc/fix_singles.py
2025-12-20 12:02:36 +02:00

63 lines
2 KiB
Python

import os
import shutil
from mutagen import File
# Path where your music files are
MUSIC_DIR = "/home/fucksophie/OrpheusDL/downloads"
# First, collect all .lrc files
lrc_files = {}
for filename in os.listdir(MUSIC_DIR):
if filename.lower().endswith(".lrc"):
key = os.path.splitext(filename)[0] # filename without extension
lrc_files[key] = os.path.join(MUSIC_DIR, filename)
# Loop through all items in the directory
for filename in os.listdir(MUSIC_DIR):
filepath = os.path.join(MUSIC_DIR, filename)
# Only process files, ignore directories
if not os.path.isfile(filepath):
continue
# Only process .mp3 and .flac files
if not (filename.lower().endswith(".mp3") or filename.lower().endswith(".flac")):
continue
# Load file with mutagen
audio = File(filepath, easy=True)
if not audio:
print(f"Skipping {filename}, can't read metadata")
continue
# Try to get the album artist tag; fallback to artist if missing
artist = None
if "albumartist" in audio:
artist = audio["albumartist"][0]
elif "artist" in audio:
artist = audio["artist"][0]
else:
print(f"No artist tag found for {filename}, skipping")
continue
# Clean up artist name for folder creation
safe_artist = artist.replace("/", "_").replace("\\", "_")
folder_name = f"{safe_artist} (singles)"
target_folder = os.path.join(MUSIC_DIR, folder_name)
# Create folder if it doesn't exist
os.makedirs(target_folder, exist_ok=True)
# Move file into folder
target_path = os.path.join(target_folder, filename)
print(f"Moving '{filename}' to '{target_folder}'")
shutil.move(filepath, target_path)
# Also move corresponding .lrc file if it exists
base_name = os.path.splitext(filename)[0]
if base_name in lrc_files:
lrc_target = os.path.join(target_folder, os.path.basename(lrc_files[base_name]))
print(f"Moving lyrics '{lrc_files[base_name]}' to '{target_folder}'")
shutil.move(lrc_files[base_name], lrc_target)
print("Done!")