Add album art to an MP3 file in Python

For this specific task of assigning album art to a ‘.mp3’ file, I’ll be using the 'mutagen' module of Python(v: 3.7.4)

You can very easily install mutagen.

Installation of Required Module:

For Windows 10:

pip install mutagen

For Linux users:

python3 -m pip install mutagen

or,

pip3 install mutagen

How to Add album art to an MP3 file in Python

  1. import mutagen module and id3 from mutagen
    from mutagen.mp3 import MP3
    from mutagen.id3 import ID3, APIC, error
    import os
  2. you need to enter the paths to the audio file and the album art (correctly)
    def input_audio_path(): # input audio file path (.mp3)
        filepath = input('Enter the Path to the audio file: ')
        if os.path.isfile(filepath) and filepath.endswith('mp3'):
            return filepath
        else:
            print('Wrong Path Entered, TRY AGAIN!') # warning message
            filepath = input_audio_path()
            return filepath
    
    def input_cover_path(): # input cover picture path (.png) or (.jpg)
    
        cover_path = input('Enter the Path to the album art:')
        if os.path.isfile(cover_path) and (cover_path.endswith('png') or cover_path.endswith('jpg')):
            return cover_path
        else:
            print('Wrong Path Entered, TRY AGAIN!') # warning message
            cover_path = input_cover_path()
            return cover_path
    
  3. Add id3 tag if not present
    # adding ID3 tag if it is not present
        try:
            audio.add_tags()
        except error:
            pass
  4. Edit the ID3 tag to set the picture as the album art
    audio.tags.add(APIC(mime='image/jpeg',type=3,desc=u'Cover',data=open(picture_path,'rb').read()))
        # edit ID3 tags to open and read the picture from the path specified and assign it
  5. save the current changes
    audio.save()  # save the current changes

Now The final code will look like this:

from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error
import os
def input_audio_path(): # input audio file path (.mp3)
    filepath = input('Enter the Path to the audio file: ')
    if os.path.isfile(filepath) and filepath.endswith('mp3'):
        return filepath
    else:
        print('Wrong Path Entered, TRY AGAIN!') # warning message
        filepath = input_audio_path()
        return filepath

def input_cover_path(): # input cover picture path (.png) or (.jpg)

    cover_path = input('Enter the Path to the album art:')
    if os.path.isfile(cover_path) and (cover_path.endswith('png') or cover_path.endswith('jpg')):
        return cover_path
    else:
        print('Wrong Path Entered, TRY AGAIN!') # warning message
        cover_path = input_cover_path()
        return cover_path


if __name__ == '__main__':
    audio_path = input_audio_path()
    picture_path = input_cover_path()
    audio = MP3(audio_path, ID3=ID3)

    # adding ID3 tag if it is not present
    try:
        audio.add_tags()
    except error:
        pass

    audio.tags.add(APIC(mime='image/jpeg',type=3,desc=u'Cover',data=open(picture_path,'rb').read()))
    # edit ID3 tags to open and read the picture from the path specified and assign it

    audio.save()  # save the current changes

And the output:

Add album art to an MP3 file in Python

Also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *