Skip to content

Instantly share code, notes, and snippets.

@ksqsf
Created May 29, 2018 08:58
Show Gist options
  • Save ksqsf/669bd0ba16286765063758ea7deb3d25 to your computer and use it in GitHub Desktop.
Save ksqsf/669bd0ba16286765063758ea7deb3d25 to your computer and use it in GitHub Desktop.
Move MP3 files to a specific directory with tags modified, which can save me tremendous amount of time!
#!/usr/bin/python3
# This is an interactive program to help myself rename and move a bunch of
# music files.
#
# Written by ksqsf <i@ksqsf.moe>
import os
import glob
from pathlib import Path
import shutil
from mutagen.mp3 import EasyMP3
def arab2roman(one_num):
num_list=[1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
str_list=["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
res=''
for i in range(len(num_list)):
while one_num>=num_list[i]:
one_num-=num_list[i]
res+=str_list[i]
return res
def get_files():
confirmed = False
files = []
pattern = "*.mp3"
while confirmed != True:
files = glob.glob(pattern)
files.sort()
for i, fn in enumerate(files):
print('%d. %s' % (i+1, fn))
if len(files) == 0:
print('No music is found!')
response = input("Does this look good to you? (Y/n/q) ")
if response in ['y', 'Y', '']:
confirmed = True
if len(files) == 0:
print('Bye!')
exit()
elif response == 'q':
exit()
else:
print('Pattern was: %s' % pattern)
new_pattern = input('Please enter another pattern: ')
if new_pattern != '':
pattern = new_pattern
return files
def get_info():
confirmed = False
artist = None
album = None
while confirmed != True:
artist = input('Artist: ')
album = input('Album: ')
response = input('Is this OK? (Y/n/q) ')
if response in ['y', 'Y', '']:
confirmed = True
elif response == 'q':
exit()
return (artist, album)
file2title = {}
def modify_tag(files, artist, album):
for (i,f) in enumerate(files):
title = input('Please enter title for %s: ' % f)
file2title[f] = "%s. %s" % (arab2roman(i+1), title)
title = file2title[f]
print('Writing tags for %s' % f)
audio = EasyMP3(f)
audio['title'] = title
audio['artist'] = artist
audio['album'] = album
audio['genre'] = 'Classical'
audio['tracknumber'] = ('%d'%(i+1)).rjust(2,'0')
audio.save()
def move_files(files, artist, album):
dest = '%s/音乐/%s/%s/' % (os.getenv('HOME'), artist, album)
Path(dest).mkdir(parents=True, exist_ok=True)
for f in files:
print('Moving %s to %s' % (f, "%s/%s.mp3" % (dest, file2title[f])))
shutil.move(f, "%s/%s.mp3" % (dest, file2title[f]))
def main():
# Get a list of mp3 files
files = get_files()
# Get artist and album name
artist, album = get_info()
# Modify id3tag (artist)
modify_tag(files, artist, album)
# Move them to the appropriate folder
move_files(files, artist, album)
# Done
print('Done! Saved you ~%d minutes' % len(files))
if __name__ == '__main__':
main()
else:
print("This is a standalone program.")
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment