-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlrcParse.py
34 lines (33 loc) · 1.17 KB
/
lrcParse.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
import re
def lrc2dict(lrc: str) -> dict:
lrc_dict = {}
def remove(x): return x.strip('[|]')
for line in lrc.split('\n'):
time_stamps = re.findall(r'\[[^\]]+\]', line)
if time_stamps:
# 截取歌词
lyric = line
for tplus in time_stamps:
lyric = lyric.replace(tplus, '')
# 如果歌词为空,跳过这一行
if not lyric.strip():
continue
# 解析时间
for tplus in time_stamps:
t = remove(tplus)
# 匹配时间戳修改时间防止报错
if t == '99:00.00':
t = '00:00.00'
if t == '00:99.99':
t = '00:00.00'
if t == '00:99.00':
t = '00:00.00'
tag_flag = t.split(':')[0]
# 跳过: [ar: 逃跑计划]
if not tag_flag.isdigit():
continue
# 时间累加
time_lrc = int(tag_flag) * 60
time_lrc += int(t.split(':')[1].split('.')[0])
lrc_dict[time_lrc] = lyric
return lrc_dict