Skip to content

Instantly share code, notes, and snippets.

@c-bata
Last active February 17, 2022 10:27
Show Gist options
  • Select an option

  • Save c-bata/5e311c6685626c915aea275d5b4e56f8 to your computer and use it in GitHub Desktop.

Select an option

Save c-bata/5e311c6685626c915aea275d5b4e56f8 to your computer and use it in GitHub Desktop.
import os
import subprocess
from mutagen.id3 import TRCK, Encoding, TALB, TPE1
from mutagen.mp3 import MP3
original_dir = "mp3/Speaking for IELTS"
new_dir = original_dir + "_new"
title = 'Speaking for IELTS'
author = 'Karen Kovacs'
def set_id3(filename: str, track: str) -> None:
mp3 = MP3(os.path.join(new_dir, filename))
mp3['TRCK'] = TRCK(encoding=Encoding.LATIN1, text=track)
mp3['TALB'] = TALB(encoding=Encoding.UTF16, text=title)
mp3['TPE1'] = TPE1(encoding=Encoding.UTF16, text=author)
mp3.save()
def main() -> None:
if os.path.exists(new_dir):
subprocess.call(['rm', '-rf', new_dir])
subprocess.call(['cp', '-r', original_dir, new_dir])
files = [f for f in os.listdir(original_dir) if f.endswith('mp3')]
files.sort()
num_tracks = len(files)
for i, f in enumerate(files):
set_id3(f, f"{i+1}/{num_tracks}")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment