How do you use eyed3 to create comments for MP3 files?

84 Views Asked by At

I made a script that changes some metadata of MP3 files to title case, while wiping all other fields. It does this by copying the existing metadata to a separate variable, then wiping the original data with tag.clear(), and finally adding back the metadata now in title case. This works for every tag I've used, except comments.

When there are existing comments, I can edit them with this:

import eyed3
mp3 = eyed3.load(FILE_PATH)
comments = mp3.tag.comments
comments_list = []
for comment in comments:
comments_list.append(comment.text.title()) #This creates an editable list for recall
for comment in comments: #As long as there were already comments, I can change them with this:
comment.text = ("anything")
mp3.tag.save()

However, as a result of clearing all metadata, comments only returns None, and the previous code won't work. How can I add comments? It looks like this: Comments blank, but everything else works

To clarify, I want to add comments to a blank MP3 file. I already know how to edit existing comments, as shown by the block of code above.

When I looked up how comments work in Eyed3, all I found was "the comments attribute is an instance of the CommentsAccessor class, and it doesn't have a direct text attribute". That's why I can't just get the comment in the same way I might get the title: title = audiofile.tag.title

It also means I can't set it the same way, and that's what I'm struggling with.

1

There are 1 best solutions below

8
Ratler On

As I commented, I'm not clear what your code is trying to achieve (e.g., if the file metadata is blank, what are you trying to append into the comments_list and why?).

But in any case, the following code should add a general comment to your file.

comment_to_add = "My new comment" # Change this to the comment you want

import eyed3

mp3 = eyed3.load(FILE_PATH)
mp3.tag.clear()

comments = mp3.tag.comments
comments.set(comment_to_add)

mp3.tag.save()

It's also unclear what the intention is with the For loop. It's possible to add comments under different keys/descriptions in the following format

comments.set(TEXT, DESCRIPTION)

in which case, the For loop would iterate over those. For example,

comment_to_add = "My new comment"

import eyed3

mp3 = eyed3.load(FILE_PATH)
mp3.tag.clear()

comments = mp3.tag.comments

comments.set(comment_to_add)
comments.set("Classical", "Genre")
comments.set("1824", "year")

mp3.tag.save()

"""The code below should return:
My new comment
Classical
1824
"""
for c in comments:
    print(c.text)

The changes should appear in the file (MediaInfo output below):

'''
Format                                   : MPEG Audio
File size                                : 153 KiB
Duration                                 : 8 s 620 ms
Overall bit rate mode                    : Constant
Overall bit rate                         : 128 kb/s
Genre                                    : Classical
Writing library                          : LAME3.90 (alpha)
Comment                                  : My new comment
year                                     : 1824
'''