Folders as Albuns #421
Unanswered
manoelcastillo
asked this question in
Q&A
Replies: 1 comment
-
I don't know if there is another way, but I solved it creating a script that set the album name based on folder in all music files in the folder (.mp3 and .flac). import os
import sys
from mutagen.mp3 import MP3
from mutagen.flac import FLAC
from mutagen.id3 import ID3, TIT2, TALB, TPE1, APIC, ID3NoHeaderError
from mutagen.flac import Picture
def find_album_cover(folder_path):
for file in os.listdir(folder_path):
if file.lower().endswith((".jpg", ".jpeg", ".png")):
return os.path.join(folder_path, file)
return None
def update_metadata(folder_path, artist_name):
album_name = os.path.basename(folder_path)
album_cover = find_album_cover(folder_path)
for file in os.listdir(folder_path):
file_path = os.path.join(folder_path, file)
if file.lower().endswith(".mp3"):
update_mp3_metadata(file_path, album_name, artist_name, album_cover)
elif file.lower().endswith(".flac"):
update_flac_metadata(file_path, album_name, artist_name, album_cover)
def update_mp3_metadata(file_path, album_name, artist_name, album_cover):
try:
audio = MP3(file_path, ID3=ID3)
try:
audio.add_tags()
except ID3NoHeaderError:
pass
audio.tags["TALB"] = TALB(encoding=3, text=album_name)
audio.tags["TPE1"] = TPE1(encoding=3, text=artist_name)
if album_cover:
with open(album_cover, "rb") as img:
audio.tags["APIC"] = APIC(
encoding=3, # UTF-8
mime="image/jpeg" if album_cover.endswith(".jpg") or album_cover.endswith(".jpeg") else "image/png",
type=3, # Cover front
desc="",
data=img.read()
)
audio.save()
print(f"Updated MP3: {file_path}")
except Exception as e:
print(f"Error updating {file_path}: {e}")
def update_flac_metadata(file_path, album_name, artist_name, album_cover):
try:
audio = FLAC(file_path)
audio["album"] = album_name # Set album name
audio["artist"] = artist_name # Set artist name
if album_cover:
with open(album_cover, "rb") as img:
picture = Picture()
picture.type = 3 # Cover front
picture.mime = "image/jpeg" if album_cover.endswith(".jpg") or album_cover.endswith(".jpeg") else "image/png"
picture.desc = ""
picture.data = img.read()
audio.clear_pictures()
audio.add_picture(picture)
audio.save()
print(f"Updated FLAC: {file_path}")
except Exception as e:
print(f"Error updating {file_path}: {e}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python update_metadata.py <album_folder_path> <artist_name>")
sys.exit(1)
album_folder = sys.argv[1]
artist_name = sys.argv[2]
if not os.path.isdir(album_folder):
print("Error: Provided path is not a directory.")
sys.exit(1)
update_metadata(album_folder, artist_name) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello all! Is there any way to handle folders as albums? If not, where I can start looking at in order to implement this feature? Many thanks. Great job BTW
Beta Was this translation helpful? Give feedback.
All reactions