Skip to content

Instantly share code, notes, and snippets.

@bkbilly
Last active October 3, 2024 09:57
Show Gist options
  • Save bkbilly/ada1455b5147c7a85d0cdb8e22822c5e to your computer and use it in GitHub Desktop.
Save bkbilly/ada1455b5147c7a85d0cdb8e22822c5e to your computer and use it in GitHub Desktop.
Useful for reading MP3 files with PLEX
import re
import os
import glob
import eyed3
def get_valid_filename(value):
value = value.replace("/", " ")
value = re.sub(r"[^-\w.\s^\]^\[^\(\^)]", "", value)
value = re.sub(r"[\s]+", " ", value).strip('-_.')
return value
input_path = "/home/bkbilly/Music/Input"
output_path = "/home/bkbilly/Music/Output"
songs = glob.glob(f"{input_path}/*.mp3", recursive=True)
for song in songs:
audio = eyed3.load(song).tag
audio.save(version=(1, 1, 0)) # Saves ID3v1.1 tag
artist_path = get_valid_filename(audio.album_artist)
album_path = get_valid_filename(audio.album)
new_dir = f"{output_path}/{artist_path}/{album_path}"
if not os.path.exists(new_dir):
os.makedirs(f"{new_dir}")
song_name = f"{audio.track_num[0]:>02} - {audio.title}.mp3"
song_name = get_valid_filename(song_name)
os.rename(song, f"{new_dir}/{song_name}")
# # Go Back 1
# songs = glob.glob(f"{output_path}/**/*.mp3", recursive=True)
# for song in songs:
# song_name = os.path.basename(song)[5:]
# os.rename(song, f"{input_path}/{song_name}")
# # Go Back 2
# songs = glob.glob(f"{output_path}/**/*", recursive=True)
# for song in songs:
# if os.path.isfile(song):
# print(song)
# song_name = os.path.basename(song)[5:]
# os.rename(song, f"{input_path}/{song_name}.mp3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment