Skip to content

Instantly share code, notes, and snippets.

@bibigon812
Created February 7, 2020 07:33
Show Gist options
  • Save bibigon812/a1abd6fe25b73694b74e5666884cac23 to your computer and use it in GitHub Desktop.
Save bibigon812/a1abd6fe25b73694b74e5666884cac23 to your computer and use it in GitHub Desktop.
Fix mp3 id3 tags
import os
import mutagen.id3
tryencodings = ['cp1251']
path = './'
def findMP3s(path):
for child in os.listdir(path):
child = os.path.join(path, child)
if child.lower().endswith(".mp3"):
yield child
for path in findMP3s(path):
id3 = mutagen.id3.ID3(path)
for key, value in id3.items():
if value.encoding != 3 and hasattr(value, 'text') and isinstance(getattr(value, 'text', [None])[0], str):
if value.encoding==0:
bytes= '\n'.join(value.text).encode('iso-8859-1')
for encoding in tryencodings:
try:
bytes.decode(encoding)
except UnicodeError:
pass
else:
break
else:
raise ValueError('None of the tryencodings work for %r key %r' % (path, key))
for i in range(len(value.text)):
value.text[i]= value.text[i].encode('iso-8859-1').decode(encoding)
value.encoding = 3
id3.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment