4

ID3. This is the Python 3 API, i'm not sure how to embed an image, so far I have this where I change the tags,

def addMetaData(url, title, artist, album, track):

    response = requests.get(url, stream=True)
    with open('img.jpg', 'wb') as out_file:
        shutil.copyfileobj(response.raw, out_file)
    del response

    audio = MP3(filename=title+".mp3", ID3 = EasyID3)
    audio['artist'] = artist
    audio['title'] = title
    audio['tracknumber'] = track
    audio['album'] = album

    audio.save()
Aju
  • 503
  • 1
  • 8
  • 26
Robert Jeers
  • 121
  • 1
  • 2
  • 9

2 Answers2

7

I don't think it is possible to embed albumart using EasyID3 but it is possible with ID3.

You can embed albumart using ID3 like below:

from mutagen.id3 import ID3, APIC

audio = ID3('music_file.mp3')

with open('img.jpg', 'rb') as albumart:
    audio['APIC'] = APIC(
                      encoding=3,
                      mime='image/jpeg',
                      type=3, desc=u'Cover',
                      data=albumart.read()
                    )

audio.save()

If performance is not your major concern, you can save textual metadata with EasyID3 and then load the music file again using ID3 to embed albumart. Otherwise, you can work completely using ID3.

So, your code in 1st case will be:

import requests
import shutil

# you can directly import EasyID3 and ID3
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC

def addMetaData(url, title, artist, album, track):

    response = requests.get(url, stream=True)
    with open('img.jpg', 'wb') as out_file:
        shutil.copyfileobj(response.raw, out_file)
    del response

    audio = EasyID3('music_file.mp3')
    audio['artist'] = artist
    audio['title'] = title
    audio['tracknumber'] = track
    audio['album'] = album
    audio.save()

    audio = ID3('music_file.mp3')
    with open('img.jpg', 'rb') as albumart:
        audio['APIC'] = APIC(
                          encoding=3,
                          mime='image/jpeg',
                          type=3, desc=u'Cover',
                          data=albumart.read()
                        )            
    audio.save()

In 2nd case:

import requests
import shutil

from mutagen.id3 import ID3, TPE1, TIT2, TRCK, TALB, APIC

def addMetaData(url, title, artist, album, track):

    response = requests.get(url, stream=True)
    with open('img.jpg', 'wb') as out_file:
        shutil.copyfileobj(response.raw, out_file)
    del response

    audio = ID3('music_file.mp3')
    audio['TPE1'] = TPE1(encoding=3, text=artist)
    audio['TIT2'] = TALB(encoding=3, text=title)
    audio['TRCK'] = TRCK(encoding=3, text=track)
    audio['TALB'] = TALB(encoding=3, text=album)

    with open('img.jpg', 'rb') as albumart:
        audio['APIC'] = APIC(
                          encoding=3,
                          mime='image/jpeg',
                          type=3, desc=u'Cover',
                          data=albumart.read()
                        )            
    audio.save()

Note: You can also directly embed the albumart using urllib2 without having to save it on disk first. Example:

import urllib2
from mutagen.id3 import ID3, APIC

audio = ID3('music_file.mp3')
albumart = urllib2.urlopen(url)

audio['APIC'] = APIC(
                  encoding=3,
                  mime='image/jpeg',
                  type=3,
                  desc=u'Cover',
                  data=albumart.read()
                )

albumart.close()
audio.save()
Nick Vee
  • 621
  • 2
  • 7
  • 17
ritiek
  • 2,477
  • 2
  • 18
  • 25
  • **Note**: `encoding=3` means `encoding=Encoding.UTF8` and `type=3` means `type=PictureType.COVER_FRONT` . Not forget `from mutagen.id3 import Encoding, PictureType` – Nick Vee Aug 08 '21 at 12:16
  • For the 2nd case: it should be audio['TIT2'] = TIT2(encoding=3, text=title) – Oguz Oct 07 '22 at 10:40
0

You need to change the shutil call to instead write to the file you opened:

...
response = requests.get(url, stream=True)
with open('img.jpg', 'wb') as out_file:
    out_file.write(response.raw)
...
thebjorn
  • 26,297
  • 11
  • 96
  • 138