Python --Tagging MP3 files

python


$ pip install mutagen

python


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):
    """
Tag MP3 files
    """
    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)           #Artist
    m["TIT2"] = TIT2(encoding=3, text=title)            #title
    m["TALB"] = TALB(encoding=3, text=album)            #Album name
    m['TCON'] = TCON(encoding=3, text=genre)            #Genre
    m['TDRC'] = TDRC(encoding=3, text=date)             #Release year
    m["TRCK"] = TRCK(encoding=3, text=[str(track + 1)]) #Track number

    #Jacket image
    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()

Recommended Posts

Python --Tagging MP3 files
[Python] Reading CSV files
Draw mp3 waveform in Python
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python
Integrate PDF files with Python
Reading .txt files with Python
Decompress multiple compressed files (Python)
Transpose CSV files in Python Part 1
[Python] Loading csv files using pandas
[Python] How to FFT mp3 data
Python
Recursively unzip zip files with python
Manipulating EAGLE .brd files with Python
Manipulate files and folders in Python
Read and use Python files from Python
Handling of JSON files in Python
Download Google Drive files in Python
Decrypt files encrypted with OpenSSL with Python 3
MP3 to WAV conversion with Python
Sort large text files in Python
Python os related files, subdirectory operations
Handle Excel CSV files with Python
Read files in parallel with Python
Export and output files in Python
How to play music (wav / mp3) files on Raspberry Pi python
Sorting files by Python naming convention
Extract strings from files in Python
[python] Move files that meet the conditions
[AWS] Using ini files with Lambda [Python]
Play audio files from Python with interrupts
Find files like find on linux in Python
Output tree structure of files in Python
Decrypt files encrypted with openssl from python with openssl
Referencing INI files in Python or Ruby
Automate jobs by manipulating files in Python
Reading and writing JSON files with Python
[Easy Python] Reading Excel files with openpyxl
Sample for handling eml files in Python
Easily download mp3 / mp4 with python and youtube-dl!
Download files in any format using Python
Combine multiple python files into one python file
[Python] Add comments to standard input files