1
3

More than 3 years have passed since last update.

mutagenでmp3とflacの埋め込み画像を表示

Posted at

Python 3.7.3 で動作確認

import mutagen
from io import BytesIO
from PIL import Image

# ファイル名は適宜変更してください
file = r"example.mp3"
# ファイル読み込み
audio = mutagen.File(file)

# 画像のリストを取得
if 'audio/mp3' in audio.mime:
    images = [audio[i] for i in audio if "APIC" in i]
elif 'audio/flac' in audio.mime:
    images = audio.pictures

for imgb in images:
    # 画像を表示用に変換
    img = Image.open(BytesIO(imgb.data))
    img.show()

検索しても画像を新たに埋め込む記事ばかりで、埋め込み画像を表示する記事が見つけられなかったから書いてみました。

"APIC" == i ではなく "APIC" in i としたのは、
Mp3tagで埋め込んだ画像のキーが "APIC:" となっていたから。

テキトーに書いたから最適なコードではないかも。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3