File size: 3,606 Bytes
83922b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class Subtitle:
    def __init__(self, ext="srt"):
        sub_dict = {
            "srt": {
                "coma": ",",
                "header": "",
                "format": self._srt_format,
            },
            "vtt": {
                "coma": ".",
                "header": "WebVTT\n\n",
                "format": self._vtt_format,
            },
            "txt": {
                "coma": "",
                "header": "",
                "format": self._txt_format,
            },
            "lrc": {
                "coma": "",
                "header": "",
                "format": self._lrc_format,
            },
        }

        self.ext = ext
        self.coma = sub_dict[ext]["coma"]
        self.header = sub_dict[ext]["header"]
        self.format_fn = sub_dict[ext]["format"]

    def timeformat(self, time):
        hours, remainder = divmod(time, 3600)
        minutes, seconds = divmod(remainder, 60)
        milliseconds = (time - int(time)) * 1000
        return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}{self.coma}{int(milliseconds):03d}"

    def seconds_to_lrc_timestamp(self, time):
        minutes = int(time // 60)
        secs = time % 60
        return f"[{minutes:02}:{secs:06.3f}]"
    
    def _srt_format(self, i, segment):
        start_time = self.timeformat(segment['timestamp'][0])
        end_time = self.timeformat(segment['timestamp'][1] if segment['timestamp'][1] else segment['timestamp'][0])
        return f"{i + 1}\n{start_time} --> {end_time}\n{segment['text']}\n\n"

    def _vtt_format(self, i, segment):
        start_time = self.timeformat(segment['timestamp'][0])
        end_time = self.timeformat(segment['timestamp'][1] if segment['timestamp'][1] else segment['timestamp'][0])
        return f"{start_time} --> {end_time}\n{segment['text']}\n\n"

    def _txt_format(self, i, segment):
        return f"{segment['text']}\n"

    def _lrc_format(self, i, segment):
        start_time = self.seconds_to_lrc_timestamp(segment['timestamp'][0])
        return f"{start_time}{segment['text']}\n"

    def get_subtitle(self, segments):
        output = self.header
        for i, segment in enumerate(segments):
            segment['text'] = segment['text'].lstrip()
            try:
                output += self.format_fn(i, segment)
            except Exception as e:
                print(e, segment)
        return output

    def write_subtitle(self, segments, output_file):
        output_file_with_ext = f"{output_file}.{self.ext}"
        subtitle = self.get_subtitle(segments)

        with open(output_file_with_ext, 'w', encoding='utf-8') as f:
            f.write(subtitle)

def write_file(output_file,subtitle):
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(subtitle)
        
def subtitle_output(inputs, chunks):
    file_name = inputs.split('/')[-1].split('.')[0]
    lrc_sub = Subtitle("lrc")
    srt_sub = Subtitle("srt")
    vtt_sub = Subtitle("vtt")
    txt_sub = Subtitle("txt")
    lrc = lrc_sub.get_subtitle(chunks)
    srt = srt_sub.get_subtitle(chunks)
    vtt = vtt_sub.get_subtitle(chunks)
    txt = txt_sub.get_subtitle(chunks)
    write_file(file_name+".lrc",lrc)
    write_file(file_name+".srt",srt)
    write_file(file_name+".vtt",vtt)
    write_file(file_name+".txt",txt)
    files_out = [file_name+".lrc", file_name+".srt", file_name+".vtt", file_name+".txt"]
    return lrc, files_out

def text_output(inputs, text):
    file_name = inputs.split('/')[-1].split('.')[0]
    write_file(file_name+".txt",text)
    files_out = [file_name+".txt"]
    return text, files_out