File size: 5,100 Bytes
0aee47a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# type: ignore
# pylint: skip-file

import io
import os
import re
import sys
import getopt

DEBUG = False


def fileopen(input_file):
    encodings = ["utf-32", "utf-16", "utf-8", "cp1252", "gb2312", "gbk", "big5"]
    tmp = ""
    for enc in encodings:
        try:
            with io.open(input_file, mode="r", encoding=enc) as fd:
                tmp = fd.read()
                break
        except:
            if DEBUG:
                print(enc + " failed")
            continue
    return [tmp, enc]


def srt2ass(input_file, out_file, sub_type):
    if ".ass" in input_file:
        return input_file

    src = fileopen(input_file)
    tmp = src[0]
    encoding = src[1]
    src = ""
    utf8bom = ""

    if "\ufeff" in tmp:
        tmp = tmp.replace("\ufeff", "")
        utf8bom = "\ufeff"

    tmp = tmp.replace("\r", "")
    lines = [x.strip() for x in tmp.split("\n") if x.strip()]
    subLines = ""
    tmpLines = ""
    lineCount = 0
    output_file = out_file

    for ln in range(len(lines)):
        line = lines[ln]
        if (
            line.isdigit()
            and ln + 1 < len(lines)
            and re.match("-?\d\d:\d\d:\d\d", lines[(ln + 1)])
        ):
            if tmpLines:
                subLines += tmpLines + "\n"
            tmpLines = ""
            lineCount = 0
            continue
        else:
            if re.match("-?\d\d:\d\d:\d\d", line):
                line = line.replace("-0", "0").replace(",", ".")
                tmpLines += "Dialogue: 0," + line + ",Default,,0,0,0,,"
            else:
                if lineCount < 2:
                    tmpLines += line
                else:
                    tmpLines += "\\N" + line
            lineCount += 1
        ln += 1

    subLines += tmpLines + "\n"

    subLines = re.sub(r"\d(\d:\d{2}:\d{2}),(\d{2})\d", "\\1.\\2", subLines)
    subLines = re.sub(r"\s+-->\s+", ",", subLines)
    # replace style
    subLines = re.sub(r"<([ubi])>", "{\\\\\g<1>1}", subLines)
    subLines = re.sub(r"</([ubi])>", "{\\\\\g<1>0}", subLines)
    subLines = re.sub(
        r'<font\s+color="?#(\w{2})(\w{2})(\w{2})"?>', "{\\\\c&H\\3\\2\\1&}", subLines
    )
    subLines = re.sub(r"</font>", "", subLines)

    if sub_type == "movie" or sub_type == "serie":
        head_str = """[Script Info]

; This is an Advanced Sub Station Alpha v4+ script.

Title: Default Aegisub file

ScriptType: v4.00+

Collisions: Normal

PlayDepth: 0

PlayResX: 1920

PlayResY: 1080

[V4+ Styles]

Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding

Style: Default,Verdana,60,&H00FFFFFF,&H000000FF,&H00282828,&H00000000,-1,0,0,0,100,100,0,0,1,3.75,0,2,0,0,70,1

[Events]

Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text"""

    if sub_type == "anime":
        head_str = """[Script Info]

; This is an Advanced Sub Station Alpha v4+ script.

Title: Default Aegisub file

ScriptType: v4.00+

WrapStyle: 0

ScaledBorderAndShadow: yes

YCbCr Matrix: TV.709

PlayResX: 1920

PlayResY: 1080

[V4+ Styles]

Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding

Style: Default,Verdana,60,&H00FFFFFF,&H000000FF,&H00282828,&H00000000,-1,0,0,0,100.2,100,0,0,1,3.75,0,2,0,0,79,1

Style: BottomRight,Arial,30,&H00FFFFFF,&H000000FF,&H00282828,&H00000000,0,0,0,0,100,100,0,0,1,2,2,3,10,10,10,1

Style: TopLeft,Arial,30,&H00FFFFFF,&H000000FF,&H00282828,&H00000000,0,0,0,0,100,100,0,0,1,2,2,7,10,10,10,1

Style: TopRight,Arial,30,&H00FFFFFF,&H000000FF,&H00282828,&H00000000,0,0,0,0,100,100,0,0,1,2,2,9,10,10,10,1

[Events]

Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text"""
    output_str = utf8bom + head_str + "\n" + subLines
    #    output_str = output_str.encode(encoding)

    with io.open(output_file, "w", encoding="utf8") as output:
        output.write(output_str)

    output_file = output_file.replace("\\", "\\\\")
    output_file = output_file.replace("/", "//")
    return output_file


def print_helper():
    print("str2ass.py -t <type> -i <input> inputfile")
    print("Available types: anime, serie, movie")


if __name__ == "__main__":
    try:
        opts, args = getopt.getopt(sys.argv[1:], "t:i:", ["type=", "input="])
    except getopt.GetoptError:
        print_helper()
        sys.exit(2)

    sub_type = "anime"
    input_file = None
    for opt, arg in opts:
        if opt in ("-t", "--type"):
            sub_type = arg
        elif opt in ("-i", "--input"):
            input_file = arg

    if not input_file:
        print_helper()
        sys.exit(2)

    filenameOutput = srt2ass(input_file, sub_type)
    print("Output: " + filenameOutput)