LoginSignup
13
15

More than 5 years have passed since last update.

Python - MP3ファイルにタグづけ

Last updated at Posted at 2013-02-22
$ pip install mutagen
from mutagen.id3 import ID3, TIT2, TALB, TPE1, TRCK, APIC, TDRC, TCON
from mutagen.mp3 import MP3
import mutagen.id3

def sample(filename, album, artist, title, track, genre, date, description, savepath):
    """
    MP3ファイルにタグをつける
    """
    m = MP3(filename, ID3=ID3)

    try:
        m.add_tags(ID3=ID3)
        #print("Added tags to %s" % filename)
    except mutagen.id3.error:
        #print("%s already had tags" % filename)
        pass

    m["TPE1"] = TPE1(encoding=3, text=artist)           # アーティスト
    m["TIT2"] = TIT2(encoding=3, text=title)            # タイトル
    m["TALB"] = TALB(encoding=3, text=album)            # アルバム名
    m['TCON'] = TCON(encoding=3, text=genre)            # ジャンル
    m['TDRC'] = TDRC(encoding=3, text=date)             # リリース年
    m["TRCK"] = TRCK(encoding=3, text=[str(track + 1)]) # トラックナンバー

    # ジャケット画像
    if savepath:
        m.tags.add(
            APIC(
                encoding=3,        # 3 is for utf-8
                mime='image/jpeg', # image/jpeg or image/png
                type=3,            # 3 is for the cover image
                desc=u'Cover',
                data=open(savepath).read()
            )
        )
    m.save()
13
15
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
13
15