34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
import re
|
|
import time
|
|
def lrcFormat(content: str) -> str:
|
|
lrcTuple = []
|
|
result = ""
|
|
def strTimeToFloat(content: str) -> float:
|
|
t = re.findall(r"\d+", content)
|
|
if len(t) == 3:
|
|
return int(t[0])*60 + int(t[1]) + float(t[2])/(10**(len(t[2])))
|
|
elif len(t) == 2:
|
|
return int(t[0])*60 + float(t[1])
|
|
def floatToStrTime(content: float) -> str:
|
|
return f"{int(content//60):02d}:{int(content%60):02d}.{int(content*1000%1000):03d}"
|
|
# 读取歌词
|
|
if content is None:
|
|
return "[0.0] 该歌曲暂无歌词\n"
|
|
for i in content.split('\n'):
|
|
a = re.findall(r"\[.*?\]",i)
|
|
timestamps = [strTimeToFloat(i[1:-1]) for i in a]
|
|
if len(timestamps) == 0:
|
|
continue
|
|
c = re.sub(r"\[.*?\]", "", i).strip()
|
|
for i in timestamps:
|
|
lrcTuple.append((i, c))
|
|
lrcTuple.sort(key=lambda x:x[0])
|
|
for i in lrcTuple:
|
|
result += "[" + str(i[0]) + "] " + i[1] + "\n"
|
|
return result
|
|
|
|
|
|
if __name__ == "__main__":
|
|
data = open("test.lrc", "r").read()
|
|
open("result.lrc", "w").write(lrcFormat(data))
|