File size: 2,154 Bytes
78dd807
 
 
 
 
 
 
 
 
 
 
 
 
 
db8f7af
78dd807
db8f7af
78dd807
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2976e81
 
78dd807
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
import os
import LRC as Parse

SRT = []
LRC = []

def safe_read(i, a):
    if i > len(a):
        return None
    else:
        return a[i]

def clear():
    global SRT
    global LRC
    SRT = []
    LRC = []

def convert_time(sec):
    t = ""
    t += str(int(sec//3600)).rjust(2, "0")
    t += ':'
    t += str(int(sec%3600//60)).rjust(2, "0")
    t += ':'
    t += str(int(sec%3600%60//1)).rjust(2, "0")
    t += ','
    t += str(round(sec%3600%60%1*1000)).rjust(3, "0")
    return t

def add_chunk(line, start, end, number):
    global SRT
    SRT.append(str(number))
    SRT.append(convert_time(float(start)) + " --> " + convert_time(float(end)))
    SRT.append(line)
    SRT.append("")

def load(input: str):
    global LRC
    LRC = input.split("\n")

def convert_line(number, chunk):
    m = chunk
    info = Parse.get_line_info(LRC[number])
    if number+1 < len(LRC):
        info1 = Parse.get_line_info(LRC[number+1])
    else:
        info1 = None
    print(info)
    tmp = ""
    i = 0
    if not info.get("wordbreaks"):
        add_chunk(info.get('line'),info.get('time'),info1.get('time'),m)
        return
    tmp = info.get('line')[0:info.get('wordbreaks')[0]-1]
    if tmp:
        add_chunk(tmp,info.get('time'),info.get('wordtimes')[0],m)
    while i <= len(info.get('wordtimes')) - 1:
        if tmp:
            m += 1

        if i+1 > len(info.get('wordtimes')):
            add_chunk(info.get('line'),info.get('wordtimes')[i],info1.get('time'),m)
        else:
            if i+1 < len(info.get('wordbreaks')):
                tmp = info.get('line')[0:info.get('wordbreaks')[i+1]-1]
                add_chunk(tmp,info.get('wordtimes')[i],info.get('wordtimes')[i+1],m)
        i += 1

def convert_to_srt(ly):
    load(Parse.remove_metadata(ly))
    i = 0
    e = 1
    while i < len(LRC):
        convert_line(i,e)
        i += 1
        e = int(SRT[len(SRT)-4])+1

if __name__ == "__main__":
    kk = open(".lrc", encoding="UTF8")
    
    convert_to_srt(kk.read())
    
    print('\n'.join(SRT))
    if os.path.exists("out.srt"):
        os.remove("out.srt")
    open("out.srt", mode='x', encoding='UTF8').write('\n'.join(SRT))