Skip to content Skip to sidebar Skip to footer

Mutagen: How To Detect And Embed Album Art In Mp3, Flac And Mp4

I'd like to be able to detect whether an audio file has embedded album art and, if not, add album art to that file. I'm using mutagen 1) Detecting album art. Is there a simpler me

Solution 1:

Embed flac:

from mutagen import File
from mutagen.flac import Picture, FLAC

def add_flac_cover(filename, albumart):
    audio = File(filename)
        
    image = Picture()
    image.type = 3
    if albumart.endswith('png'):
        mime = 'image/png'
    else:
        mime = 'image/jpeg'
    image.desc = 'front cover'
    with open(albumart, 'rb') as f: # better than open(albumart, 'rb').read() ?
        image.data = f.read()
    
    audio.add_picture(image)
    audio.save()

For completeness, detect picture

def pict_test(audio):
    try: 
        x = audio.pictures
        if x:
            return True
    except Exception:
        pass  
    if 'covr' in audio or 'APIC:' in audio:
        return True
    return False

Post a Comment for "Mutagen: How To Detect And Embed Album Art In Mp3, Flac And Mp4"