Adding cover art to my daughter's MuPiBox

Updated September 24, 2024

My daughter loves her MuPiBox media server, which is full of her favorite audiobooks and music. I added cover art to each album so she can recognize them while browsing.

Tools

The script is Python. It uses os for filesystem paths, glob for matching file names, and mutagen to read the audio metadata and pull out the embedded cover art. It walks the MuPiBox library, extracts the cover from the MP3s, and saves it as cover.jpg per album.

Step by step

Step 1: setting up the script

First I installed mutagen with pip:

pip install mutagen

Then, I created a Python script named add_cover_art.py:

import os
import glob
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC

def extract_and_save_cover_art(mp3_file, output_file):
    audio = MP3(mp3_file, ID3=ID3)
    
    # Check if the MP3 file contains an image
    for tag in audio.tags.values():
        if isinstance(tag, APIC):
            with open(output_file, 'wb') as img:
                img.write(tag.data)
            print(f'Cover artwork successfully saved to {output_file}.')
            return
    
    print('No cover artwork found in the MP3 file.')

def delete_existing_images(album_dir):
    image_files = glob.glob(os.path.join(album_dir, "*.[jJpP][pPnNgG]*"))
    for image_file in image_files:
        os.remove(image_file)
        print(f'{image_file} has been deleted.')

def process_artist_directory(artist_dir):
    for album_dir in os.listdir(artist_dir):
        full_album_path = os.path.join(artist_dir, album_dir)
        
        if os.path.isdir(full_album_path):
            cover_file = os.path.join(full_album_path, "cover.jpg")
            
            if os.path.exists(cover_file):
                print(f'Skipping {full_album_path}, cover.jpg already exists.')
                continue
            
            mp3_files = sorted(glob.glob(os.path.join(full_album_path, "*.mp3")))
            
            if mp3_files:
                first_mp3_file = mp3_files[0]
                delete_existing_images(full_album_path)
                extract_and_save_cover_art(first_mp3_file, cover_file)

def process_music_library(base_dir):
    for artist_dir in os.listdir(base_dir):
        if not "Pippi" in artist_dir:
            continue
        full_artist_path = os.path.join(base_dir, artist_dir)
        
        if os.path.isdir(full_artist_path):
            process_artist_directory(full_artist_path)

# Example call
music_library_path = '/Volumes/media/music/Hörbücher'
process_music_library(music_library_path)

Step 2: running the script

I ran it against the base directory of the MuPiBox media library. It walked the directories, found the MP3 files, and wrote each album’s cover art to cover.jpg in its folder.

Step 3: checking the results

Afterwards I browsed the MuPiBox server and the cover.jpg files were sitting next to each album, so the covers now show up in her library.

Result

If your media server’s MP3s already have embedded covers, this script saves you from adding them by hand.

How did you like this article?