nicfit / eyeD3

eyeD3 is a Python module and command line program for processing ID3 tags. Information about mp3 files (i.e bit rate, sample frequency, play time, etc.) is also provided. The formats supported are ID3v1 (1.0/1.1) and ID3v2 (2.3/2.4).
http://eyed3.nicfit.net/
GNU General Public License v3.0
532 stars 58 forks source link

Encoding error when saving file #569

Closed HeuristicPerson closed 2 years ago

HeuristicPerson commented 2 years ago

Hi. I've created a script to edit id3 tags of downloaded podcast episodes and I'm having an encoding issue with one of them. My configuration is:

  Python 3.8.10
  eyed3 0.9.6

The minimal version of the code that fails is:

o_audiofile = eyed3.load(po_file.u_path)
[...I edit other tags with no problem...]
o_audiofile.tag.title = self.u_mod_title
o_audiofile.tag.save()

And I'm getting this error:

File "foo/bar/dev/pod_dl/libs/podcasts.py", line 438, in fix_tags
  o_audiofile.tag.save()
File "/usr/local/lib/python3.8/dist-packages/eyed3/id3/tag.py", line 914, in save
  self._saveV2Tag(version, encoding, max_padding)
File "/usr/local/lib/python3.8/dist-packages/eyed3/id3/tag.py", line 1119, in _saveV2Tag
  rewrite_required, tag_data, padding = self._render(version,
File "/usr/local/lib/python3.8/dist-packages/eyed3/id3/tag.py", line 1038, in _render
  raw_frame = f.render()
File "/usr/local/lib/python3.8/dist-packages/eyed3/id3/frames.py", line 303, in render
  self.text.encode(id3EncodingToString(self.encoding)))
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2013' in position 21: ordinal not in range(256)

Below you can check the original title tag of the mp3 file and the new one I assign:

imagen imagen

It looks like an encoding problem but I don't know how to fix it. I've tried passing the new title encoded in utf-8 and also changing eyed3 default encoding (eyed3._DEFAULT_ENCODING = 'utf-8') with no luck.

HeuristicPerson commented 2 years ago

OK, my fault. The IDE (Pycharm) wasn't suggesting any parameter but I see at https://eyed3.readthedocs.io/en/latest/eyed3.id3.html?highlight=tag.save#eyed3.id3.tag.Tag.save that the encoding can be specified when saving the file which solves my issue:

BEFORE (not working):

o_audiofile.tag.save()

NOW (working):

o_audiofile.tag.save(encoding='utf-8')