Prateek Alat
Prateek Alat

Reputation: 216

Changing lyrics in an MP3 file via eyeD3

I am trying to create a program in Python which automatically retrieves lyrics for a particular folder of MP3s. [I get the lyrics from azlyrics.com ]

So far, I have succeeded in doing everything except for actually embedding the lyrics into the "lyrics" tag.

You answered a question regarding reading the lyrics from it's tag over here.

I was wondering if you could help me with setting the lyrics. Here's my code.

import urllib2 # For downloading webpage
import time # For pausing
import eyed3 # For MP3s
import re # For replacing characters
import os # For reading folders


path = raw_input('Please enter folder of music') # TODO Must make GUI PATH SELECTION


files = os.listdir(path) 
for x in files:
    # Must make the program stop for a while to minimize server load
    time.sleep(3)
    # Opening MP3
    mp3 = eyed3.load(path + '/' + x)
    # Setting Values
    artist = mp3.tag.artist.lower()
    raw_song = str(mp3.tag.title).lower()
    song = re.sub('[^0-9a-zA-Z]+', '', raw_song) #Stripping songs of anything other than alpha-numeric characters
    # Generating A-Z Lyrics URL
    url = "http://www.azlyrics.com/lyrics/" + artist + "/" + song + ".html"
    # Getting Source and extracting lyrics
    text = urllib2.urlopen(url).read()
    where_start = text.find('<!-- start of lyrics -->')
    start = where_start + 26
    where_end = text.find('<!-- end of lyrics -->')
    end = where_end - 2
    lyrics = unicode(text[start:end].replace('<br />', ''), "UTF8")
    # Setting Lyrics to the ID3 "lyrics" tag
    mp3.tag.lyrics = lyrics ### RUNNING INTO PROBLEMS HERE
    mp3.tag.save()

I am running into the following error after the 2nd-last line gets executed:-

Traceback (most recent call last):
File "<pyshell#62>", line 31, in <module>
mp3.tag.lyrics = lyrics
AttributeError: can't set attribute

I would also like you to know that I am a 15 year old who has been learning Python for about a year now. I searched everywhere and tried everything but I guess I need some help now.

Thanks in advance for all your help!

Upvotes: 5

Views: 4179

Answers (2)

ahmed Fathey
ahmed Fathey

Reputation: 21

chcp 65001

eyeD3 --encoding utf8 --add-lyrics "001-001.txt" 001-001.mp3

Upvotes: 2

mlissner
mlissner

Reputation: 18166

I don't pretend to understand why this is the way it is, but check out how lyrics are set in the handy example file:

from eyed3.id3 import Tag

t = Tag()
t.lyrics.set(u"""la la la""")

I believe this has to do with lyrics being placed into frames, but others may have to chime in with corrections on that. Note that this will fail unless you pass it unicode.

Upvotes: 2

Related Questions