mp3tag.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from datetime import date
  2. import os
  3. from mutagen.id3 import ID3, TDRC, TIT2, TALB, TPE1, TCON, APIC
  4. from config import ALBUM_NAME, GENRE
  5. if not os.path.isdir('covers'):
  6. os.mkdir('covers')
  7. def modified_id3(file_name: str, artist_name: str, title: str) -> None:
  8. id3 = ID3()
  9. id3.add(TIT2(encoding=3, text=title))
  10. id3.add(TALB(encoding=3, text=ALBUM_NAME))
  11. id3.add(TPE1(encoding=3, text=artist_name))
  12. id3.add(TDRC(encoding=3, text=str(date.today())))
  13. id3.add(TCON(encoding=3, text=GENRE))
  14. cover_path = f"covers/{artist_name}.jpg"
  15. if not os.path.isfile(cover_path):
  16. cover_path = "covers/cover.jpg"
  17. id3.add(
  18. APIC(encoding=3,
  19. mime=u'image/jpg',
  20. type=3,
  21. desc=u'Cover',
  22. data=open(cover_path, 'rb').read()))
  23. id3.save(file_name)
  24. if __name__ == '__main__':
  25. track_info = {
  26. 'song_name': 'mfghghfgh title',
  27. 'album_name': 'my albdfsadcxumb name',
  28. 'artist_name': 'Юга Аватара дас'
  29. }
  30. modified_id3('Брахмотсава. Юга Аватар дас. 27.05.22 [bpZDLB1dVB8].mp3', track_info['artist_name'], track_info['song_name'])