-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbedLrc.py
85 lines (67 loc) · 2.27 KB
/
EmbedLrc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import re
import os.path
import sys
from mutagen.id3 import ID3, USLT
import chardet
version = '0.1.2'
print('EmbedLrc ', version, sep='')
supportAudioTypes = ['.mp3', '.flac', '.aac', '.wav']
supportLrcTypes = ['.lrc']
def ScanFiles(directory):
filelist = []
allObjects = [
os.path.normpath(os.path.join(directory.strip(), filename)) for filename in os.listdir(directory)]
for m in allObjects:
if os.path.isfile(m):
filelist.append(m)
elif os.path.isdir(m):
filelist.extend(ScanFiles(m))
else:
pass
return filelist
def GetMatchFiles(filelist, regexs):
matchedFiles = []
for regex in regexs:
pattern = re.compile(regex)
matchedFiles.extend(m for m in filelist if pattern.match(m))
return matchedFiles
def CreateMap(filelist):
tempMap = {}
for file in filelist:
tempMap[os.path.basename(file)[:-4]] = file
return tempMap
supportAudioTypes = ['.+\\' + m for m in supportAudioTypes]
supportLrcTypes = ['.+\\' + m for m in supportLrcTypes]
fileList = []
args = sys.argv[1:]
for arg in args:
if os.path.isdir(arg):
fileList.extend(ScanFiles(arg))
elif arg == 'EmbedLrc.py':
pass
else:
print('Error:{0} is not a directory!'.format(arg))
if not fileList:
print('No file input!')
sys.exit(1)
audioList = CreateMap(GetMatchFiles(fileList, supportAudioTypes))
lrcList = CreateMap(GetMatchFiles(fileList, supportLrcTypes))
pattern_deltimetags = re.compile(r'\[.*?\]')
pattern_dellinebreaks = re.compile(r'^\s$')
for audioName in audioList.keys():
if audioName in lrcList.keys():
print('Writting into {0} ...'.format(audioList[audioName]), end='')
lrctext = ''
with open(lrcList[audioName], 'rb') as lrc:
lrcRawText = lrc.read()
encoding = chardet.detect(lrcRawText)['encoding']
lrctext = lrcRawText.decode(encoding)
lrctext = pattern_deltimetags.subn('', lrctext)[0]
lrctext = pattern_dellinebreaks.subn('', lrctext)[0]
audio = ID3(audioList[audioName])
audio.delall('USLT')
audio.add(USLT(text=lrctext, encoding=3))
audio.save()
print('done!')
else:
print('Can\'t find lrc file for {0}'.format(audioName))