-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
81 lines (66 loc) · 1.89 KB
/
main.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
#coding=utf-8
'''
假设lrc歌词文件中,翻译与原文都是在同一个时刻上(应该都是吧
author: Tmn07
'''
import re
f = open('test.lrc', 'r', encoding='utf-8')
data = f.read()
count = 0
K = 1 # 伪K轴,即是否进行奇偶定位
# 视频分辨率为1920*1080,在不同分辨率视频下可能需要调整。
# positions = ["{\pos(800,1030)}", "{\pos(80,900)}"]
positions = ["{\pos(80,900)}", "{\pos(1840,1024)}"] # 奇偶位置
translate = 0 # 是否保留中文翻译
result = {}
times = []
for line in data.split('\n'):
# 00:09.36
match_list = re.findall('\[\d*?:\d*?\.\d*?]', line)
if match_list:
for m in match_list:
# print(m)
tmp = result.get(m)
if tmp!=None:
if tmp == "" or line[len(line)-line[::-1].index(']'):] == "//": ## qq music中 翻译空白区 与 部分不翻译时用//来替代
continue
else:
# lrc
if translate:
result[m] = tmp + "\n" + line[len(line)-line[::-1].index(']'):]
else:
if K:
offset = positions[count]
count = 1 if count == 0 else 0
else:
offset = ''
result[m] = offset+line[line.index(']')+1:]
times.append(m)
else:
continue
f.close()
def maketime(start ,end):
# 00: 一小时以内的轴..
if start == 0:
ss = '00:00:00,000'
else:
ss = "00:"+start[1:-1].replace('.',',')+"0"
se = "00:"+end[1:-1].replace('.',',')+"0"
return ss + " --> " + se
times.sort()
with open("result.lrc", 'w', encoding='utf-8') as f:
with open("result.srt", 'w', encoding='utf-8') as f2:
pretime = times[0]
# for ind in range(1,len(times)):
for ind in range(1,len(times)+1):
f2.write(str(ind)+"\n")
# 用于处理最后一条歌词信息
if ind==len(times):
f2.write(maketime(pretime, pretime)+"\n")
else:
f2.write(maketime(pretime, times[ind])+"\n")
pretime = times[ind]
f2.write(result[times[ind-1]]+"\n\n")
# lrc
for t in times:
f.write(t+result[t]+"\n")