Skip to content

Instantly share code, notes, and snippets.

@kgantsov
Created March 28, 2017 17:57
Show Gist options
  • Save kgantsov/d2bcf38eb5f35c379e6fa8f9183f3378 to your computer and use it in GitHub Desktop.
Save kgantsov/d2bcf38eb5f35c379e6fa8f9183f3378 to your computer and use it in GitHub Desktop.
Simple script that I wrote to fix encoding of id3 tags in my mp3 collection
import os
import click
from mutagen.easyid3 import EasyID3
from mutagen.id3._util import ID3NoHeaderError
@click.group()
def cli():
pass
@cli.command()
@click.option('-d', '--directory', help='Directory with mp3 files')
def encoding(directory):
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
try:
f = EasyID3(file_path)
except ID3NoHeaderError as e:
print('Error reading file:', file_path, e)
continue
for name, value in f.items():
try:
new_value = str(bytes(value[0], 'iso-8859-1'), 'cp1251')
print(
'Fix id3 tag "{tag}" value "{value}" with new value "{new_value}"'.format(
tag=name, value=value[0], new_value=new_value
)
)
except (ID3NoHeaderError, UnicodeEncodeError):
pass
except ValueError:
pass
else:
f[name] = new_value
f.save()
if __name__ == '__main__':
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment