Skip to content

Instantly share code, notes, and snippets.

@Nugjii
Last active September 9, 2022 01:31
Show Gist options
  • Save Nugjii/d404314f07103729059a76889ef21f30 to your computer and use it in GitHub Desktop.
Save Nugjii/d404314f07103729059a76889ef21f30 to your computer and use it in GitHub Desktop.
Getting artist name and song title from file name and set to mp3 file.
import eyed3
import os
import time
eyed3.log.setLevel("ERROR")
basepath = "E:\HIP HOP"
dirs = os.listdir(basepath)
total = len(dirs)
i = 0
for entry in dirs:
if os.path.isfile(os.path.join(basepath, entry)):
titleOriginal = entry[:-4] #slice .mp3
artist = ''
title = ''
replacables = ['Official_Music_Video', 'Official_music_video', 'Official_MV', 'Official_M_V', 'Official_mv', 'Official_Video', 'Official_video', 'Lyric_Video', 'Lyric_video', 'LYRICS', 'Lyrics', 'lyrics', 'Lyric', 'lyric', 'MV', 'Official_Audio', 'REMIX_Audio']
for rep in replacables:
titleOriginal = titleOriginal.replace(rep, '')
titleOriginal = titleOriginal.replace('_', ' ')
if titleOriginal.find("mp3cut.net") > 0:
titleOriginal = titleOriginal[:titleOriginal.find("mp3cut.net")]
if titleOriginal.rfind('-') > 0:
artist = titleOriginal[:titleOriginal.rfind('-')]
artist = artist.strip()
title = titleOriginal[titleOriginal.rfind('-') + 1:]
title = title.strip()
else:
title = titleOriginal.strip()
artist = 'None'
print('File name:', entry)
print('Title dirty:', titleOriginal)
print('Artist:', artist)
print('Title:', title)
audiofile = eyed3.load(os.path.join(basepath, entry))
time.sleep(0.01)
if audiofile != None:
if audiofile.tag is None:
audiofile.initTag()
audiofile.tag.artist = artist
audiofile.tag.title = title
audiofile.tag.save()
print('Tag applied')
else:
print('!!! Tag NOT applied')
i += 1
per = int(i * 100 / total)
str1 = '='
str0 = '-'
print(str1*int(per / 2) + str(per) + '%' + str0*(50-int(per / 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment