backend/python

eyeD3: 파이썬 오디오 태그 넣기

seul chan 2019. 2. 5. 22:00

아이튠즈에 음악을 넣으면 태그 (앨범, 아티스트, 년도) 등이 있어야 제대로 구성을 해준다.

태그가 없는 앨범들을 넣다보니 귀찮아서 python 라이브러리를 찾아보니 eyeD3(https://pypi.org/project/eyeD3/)이 있어 이를 사용해 보았다.

설치

  • python 2.7 ~ 3.3까지 지원.

pip로 설치

pip install eyeD3

소스 아카이브에서 설치

여기에서 최신버전을 받은 뒤에 설치해준다.

$ tar xzf eyeD3-X.Y.Z.tar.gz
$ cd eyeD3-X.Y.Z
# This may require root access
$ python setup.py install

ImportError: failed to find libmagic. Check your installation

mac os에서 import eyeD3시 해당 에러가 발생하여 brew install libmagic하니 해결

사용법

import eyed3

audiofile = eyed3.load("song.mp3")
audiofile.tag.artist = u"Integrity"
audiofile.tag.album = u"Humanity Is The Devil"
audiofile.tag.album_artist = u"Integrity"
audiofile.tag.title = u"Hollow"
audiofile.tag.track_num = 2

audiofile.tag.save()
  • 다음은 예시
import os
import eyed3

for filename in os.listdir('.'):
    
    if filename.startswith('Queen') and filename.split('.')[-1] != 'png':
        print("ALBUM: " + filename + "================")
        year = filename.split()[1]
        album_name = u' '.join([x for x in filename.split()[2:]])
        os.chdir(filename)
        
        # 파일수정
        for f in os.listdir('.'):
            if f.split('.')[-1] == 'mp3':
                print('Before :' + f)
                title = ''
                track_num = 0
                try:
                    track_num = int(f.split()[0])
                except:
                    pass
                print('Track num: ' + str(track_num))
                if track_num:
                    title = f[5:]
                    save_title = title.split('.')[0]
                    os.rename(f, title)
                
                if title and track_num:
                    aud = eyed3.load(title)
                    aud.tag.artist = u"Queen"
                    aud.tag.album = album_name
                    aud.tag.album_artist = u"Queen"
                    aud.tag.title = unicode(save_title)
                    aud.tag.track_num = track_num
                    aud.tag.save()
                    print(title + ' saved')
        os.chdir('../')


'backend > python' 카테고리의 다른 글

Installing jupyterhub  (0) 2019.02.14
IPython (Jupyter) notebook에서 unittest 하기  (0) 2019.02.08
python: distributing package  (0) 2018.12.19
python: photo encrypt using python  (0) 2018.11.28
ipython: copy and paste without indentation  (0) 2018.10.17