Skip to content

Instantly share code, notes, and snippets.

@benwu232
Created May 20, 2014 11:51
Show Gist options
  • Save benwu232/0a3a07328376a18cf4b7 to your computer and use it in GitHub Desktop.
Save benwu232/0a3a07328376a18cf4b7 to your computer and use it in GitHub Desktop.
rename and modify tags of audios of harry potter to play in Android phone in order
# This script renames Harry Potter audio file names to play on Android in order
import os
import re
import eyed3
def sub_func1(m):
strm = m.group()
digit_len = len(strm)-1
prefix = ''
k = 0
while k < 4 - digit_len:
prefix += '0'
k += 1
return prefix + strm
def sub_func2(m):
strm = m.group()
digit_len = len(strm)-1
prefix = ''
k = 0
while k < 4 - digit_len:
prefix += '0'
k += 1
return '-' + prefix + strm[1:]
def hp_rename(folder):
file_list = os.listdir(folder)
#complete digits
for f in file_list:
path = os.path.join(folder,f)
if os.path.isdir(path):
continue
f1 = re.sub(r'^\d+', '', f)
p = re.compile(r'(\d+-)')
f2 = p.sub(sub_func1, f1)
p = re.compile(r'(-\d+)')
f3 = p.sub(sub_func2, f2)
old_file_path = folder + '\\' + f
new_file_path = folder + '\\' + f3
#Set the tags of mp3 files
meta = eyed3.load(new_file_path)
meta.tag._setTitle(f3[0:-4].decode('utf-8'))
meta.tag.save()
if f is not f3:
os.rename(old_file_path, new_file_path)
if __name__ == '__main__':
hp_rename("C:\\tmp\\harry potter\\hp3")
#hp_rename("C:\\tmp\\justing")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment