Last active
August 11, 2025 01:56
-
-
Save ericoporto/86e0c9392c979e46dda03a69aeef07d2 to your computer and use it in GitHub Desktop.
Helpful scripts made to help to condition mp3 files to my old car mp3 player
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for f in *.mp3; do | |
ffmpeg -hide_banner -loglevel error -i "$f" -vn -ar 44100 -ac 2 -sample_fmt s16 -b:a 320k -f mp3 "reencoded_$f" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from pathlib import Path | |
def fix_double_prefix(directory="."): | |
path = Path(directory).resolve() | |
renamed = [] | |
# Regex: match filenames like 12_34_SONGNAME.mp3 → keep 12 and SONGNAME | |
pattern = re.compile(r"^(\d{2})_(\d{2})_([a-zA-Z0-9_]+)\.mp3$") | |
for file in path.iterdir(): | |
if file.is_file() and file.suffix.lower() == ".mp3": | |
match = pattern.match(file.name) | |
if match: | |
first_prefix, _, name_part = match.groups() | |
new_name = f"{first_prefix}_{name_part}.mp3" | |
new_path = file.with_name(new_name) | |
if new_path.exists(): | |
print(f"Error: Cannot rename '{file.name}' → '{new_name}' (target exists)") | |
continue | |
file.rename(new_path) | |
renamed.append((file.name, new_name)) | |
print(f"\nRenamed {len(renamed)} file(s):") | |
for old, new in renamed: | |
print(f" {old} → {new}") | |
if __name__ == "__main__": | |
fix_double_prefix() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import unicodedata | |
import re | |
from pathlib import Path | |
def sanitize_filename(filename): | |
# Transliterate to closest ASCII equivalent | |
normalized = unicodedata.normalize('NFKD', filename) | |
ascii_str = normalized.encode('ascii', 'ignore').decode('ascii') | |
# Replace non-alphanumeric characters with underscores | |
safe_str = re.sub(r'[^a-zA-Z0-9_]', '_', ascii_str) | |
# Avoid repeated underscores and trim leading/trailing | |
safe_str = re.sub(r'_+', '_', safe_str).strip('_') | |
return safe_str or "file" # fallback to 'file' if empty | |
def make_unique(name, existing_names, suffix=""): | |
candidate = name + suffix | |
count = 1 | |
while candidate in existing_names: | |
candidate = f"{name}_{count}" | |
count += 1 | |
existing_names.add(candidate) | |
return candidate | |
def rename_files_to_ascii(directory="."): | |
path = Path(directory).resolve() | |
if not path.is_dir(): | |
print(f"Error: '{directory}' is not a valid directory.") | |
return | |
renamed = [] | |
existing_names = set() | |
for file in path.iterdir(): | |
if not file.is_file(): | |
continue | |
original_name = file.name | |
name_no_ext = file.stem | |
ext = file.suffix | |
sanitized_base = sanitize_filename(name_no_ext) | |
sanitized_name = make_unique(sanitized_base, existing_names) + ext | |
if sanitized_name != original_name: | |
new_path = path / sanitized_name | |
file.rename(new_path) | |
renamed.append((original_name, sanitized_name)) | |
print(f"\nRenamed {len(renamed)} files:") | |
for old, new in renamed: | |
print(f" {old} → {new}") | |
if __name__ == "__main__": | |
rename_files_to_ascii() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from pathlib import Path | |
def strip_prefix(filename): | |
"""Remove NNN_ prefix from filename if it exists.""" | |
return re.sub(r'^\d{3,}_', '', filename) | |
def rename_mp3s_by_playlist(playlist_filename="__playlist.m3u"): | |
playlist_path = Path(playlist_filename).resolve() | |
base_dir = playlist_path.parent | |
if not playlist_path.exists(): | |
print(f"Playlist file '{playlist_filename}' not found.") | |
return | |
# Try UTF-8 first, fallback to Latin-1 | |
try: | |
with open(playlist_path, "r", encoding="utf-8") as f: | |
lines = [line.strip() for line in f if line.strip() and not line.startswith("#")] | |
except UnicodeDecodeError: | |
with open(playlist_path, "r", encoding="latin1") as f: | |
lines = [line.strip() for line in f if line.strip() and not line.startswith("#")] | |
mp3_names_in_playlist = [Path(line).name for line in lines if line.lower().endswith(".mp3")] | |
num_digits = len(str(len(mp3_names_in_playlist))) | |
renamed = [] | |
for i, name_in_playlist in enumerate(mp3_names_in_playlist, 1): | |
# Strip prefix from playlist entry (in case it's already renamed) | |
target_base_name = strip_prefix(name_in_playlist) | |
# Find actual file with or without prefix | |
candidates = list(base_dir.glob(f"*{target_base_name}")) | |
if not candidates: | |
print(f"Warning: File matching '{target_base_name}' not found. Skipping.") | |
continue | |
# Use the first matching candidate | |
original_path = candidates[0] | |
new_prefix = f"{i:0{num_digits}d}_" | |
new_name = new_prefix + target_base_name | |
new_path = base_dir / new_name | |
if original_path.name == new_path.name: | |
print(f"Skipping: {original_path.name} (already correct)") | |
continue | |
if new_path.exists(): | |
print(f"Error: '{new_path.name}' already exists. Skipping rename of '{original_path.name}'.") | |
continue | |
original_path.rename(new_path) | |
renamed.append((original_path.name, new_name)) | |
print(f"\nRenamed {len(renamed)} files:") | |
for old, new in renamed: | |
print(f" {old} → {new}") | |
if __name__ == "__main__": | |
rename_mp3s_by_playlist() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment