pythonmp3flacmutagenalbumart

Mutagen: How can I "correctly" embed album art into mp3 file so that I can see the pitcure display as file icon


I am trying to convert flac files to mp3 format, using pydub for conversion and mutagen for tags and album art copy.

Convert a flac file to a 320Kbps mp3:

from pydub import AudioSegment

path_flac  = 'mc_test/from/01 Lapislazuli.flac'
path_mp3   = 'mc_test/to/01 Lapislazuli.mp3'
flac_audio = AudioSegment.from_file(path_flac, format="flac")
flac_audio.export(path_mp3, format="mp3", bitrate='320K')

Load album art image from flac file and embed it into mp3 file (follow this question):

from mutagen.flac import FLAC
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC

file = FLAC(path_flac)
art = file.pictures[0].data

audio = MP3(path_mp3, ID3=ID3)    
audio.tags.add(
    APIC(
        encoding=3, # 3 is for utf-8
        mime='image/png', # image/jpeg or image/png
        type=3, # 3 is for the cover image
        desc=u'Cover',
        data=art
    )
)
audio.save()

I successfully embed the album art into the mp3 file, and the picture showed in players such as foobar and MPC, but didn't correctly showed in file icon. If I convert the file via foobar, it correctly showed, but didn't work with mutagen.

File icon (image preview) in different situation

Does anyone knows how to make the album art correctly showed as icon?


Solution

  • Thanks for suggestion from @diggusbickus , I found and compared differences between mp3 file generated from foobar and pydub. The difference is encoding.

    In pydub-converted file, which tags and album art were added by mutagen:

    path_mp3 = 'mc_test/to/01 Lapislazuli.mp3'
    file_mutagen = File(path_mp3)
    file_mutagen.tags['APIC:'].encoding
    

    It shows <Encoding.UTF8: 3>, which probably came from audio.tags.add(APIC(encoding=3)) above.

    In foobar-converted file:

    path_mp3_foobar = 'mc_test/foobar/01 Lapislazuli.mp3'
    file_foobar = File(path_mp3_foobar)
    file_foobar.tags['APIC:'].encoding
    

    shows <Encoding.LATIN1: 0>

    So I change my setting to audio.tags.add(APIC(encoding=0)) while embeding image, and it works, now I can see album art as a icon preview image. Also I do a little survey to check if other encoding number works, album art would correctly showed with encoding=0, 1 and 2.

    enter image description here