-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml2ass.py
More file actions
executable file
·186 lines (166 loc) · 8.67 KB
/
Copy pathxml2ass.py
File metadata and controls
executable file
·186 lines (166 loc) · 8.67 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python
#http://www.tutorialspoint.com/python/python_xml_procestylesing.htm
import sys
import os.path
from xml.dom.minidom import parse
import xml.dom.minidom
if not os.path.isfile(sys.argv[1]):
print "file not found"
exit(1)
filename= sys.argv[1]
(base, ext) = os.path.splitext(filename)
outname = base + ".ass"
# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse(filename)
source = DOMTree.documentElement
tt_styles = source.getElementsByTagName("tt:style")
tt_regions = source.getElementsByTagName("tt:region")
tt_divs = source.getElementsByTagName("tt:div")
styles = {}
defaultStyle = "none"
for s in tt_styles:
if s.hasAttribute("xml:id"):
#print s.getAttribute("xml:id")
currentStyle = s.getAttribute("xml:id")
styles[currentStyle] = {}
if s.hasAttribute("tts:fontFamily"):
#print s.getAttribute("tts:fontFamily")
styles[currentStyle]["fontFamily"] = s.getAttribute("tts:fontFamily")
defaultStyleName = currentStyle
if s.hasAttribute("tts:fontSize"):
#print s.getAttribute("tts:fontSize")
styles[currentStyle]["fontSize"] = s.getAttribute("tts:fontSize")
if s.hasAttribute("tts:textAlign"):
#print s.getAttribute("tts:textAlign")
styles[currentStyle]["textAlign"] = s.getAttribute("tts:textAlign")
if s.getAttribute("tts:textAlign") == "left":
styleLeft = currentStyle
elif s.getAttribute("tts:textAlign") == "right":
styleRight = currentStyle
elif s.getAttribute("tts:textAlign") == "center":
styleCenter = currentStyle
if s.hasAttribute("tts:lineHeight"):
#print s.getAttribute("tts:lineHeight")
styles[currentStyle]["lineHeight"] = s.getAttribute("tts:lineHeight")
if s.hasAttribute("tts:color"):
#print s.getAttribute("tts:color")
styles[currentStyle]["color"] = s.getAttribute("tts:color")
if defaultStyle == "none":
defaultStyle = currentStyle
if s.hasAttribute("tts:backgroundColor"):
#print s.getAttribute("tts:backgroundColor")
styles[currentStyle]["backgroundColor"] = s.getAttribute("tts:backgroundColor")
regions = {}
styleTop = "top"
styleBottom = "bottom"
for r in tt_regions:
if r.hasAttribute("xml:id"):
#print r.getAttribute("xml:id")
currentRegion = r.getAttribute("xml:id")
regions[currentRegion] = {}
if r.hasAttribute("tts:displayAlign"):
#print r.getAttribute("tts:displayAlign")
regions[currentRegion]["displayAlign"] = r.getAttribute("tts:displayAlign")
if r.getAttribute("tts:displayAlign") == "after":
styleBottom = currentRegion
elif r.getAttribute("tts:displayAlign") == "top":
styleTop = currentRegion
if r.hasAttribute("tts:origin"):
#print r.getAttribute("tts:origin")
regions[currentRegion]["origin"] = r.getAttribute("tts:origin")
if r.hasAttribute("tts:extent"):
#print r.getAttribute("tts:extent")
regions[currentRegion]["extent"] = r.getAttribute("tts:extent")
f = open(outname, "w")
out = "[Script Info]\n; Script generated by xml2ass\nTitle: Infile\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n"
f.write(out.encode("utf-8"))
for s in styles:
if s != defaultStyleName and s != styleLeft and s != styleCenter and s != styleRight:
out = "Style: %s%s%s,%s,20,&H00%s,&H000000FF,&H%s,&H00000000,0,0,0,0,100,100,0,0,3,2,2,7,10,10,10,1\n" % (s, styleTop, styleLeft, styles[defaultStyleName]["fontFamily"].replace(",",";"), (styles[s]["color"][5:7]+styles[s]["color"][3:5]+styles[s]["color"][1:3]).upper(), (styles[s]["backgroundColor"][7:9]+styles[s]["backgroundColor"][5:7]+styles[s]["backgroundColor"][3:5]+styles[s]["backgroundColor"][1:3]).upper())
f.write(out.encode("utf-8"))
out = "Style: %s%s%s,%s,20,&H00%s,&H000000FF,&H%s,&H00000000,0,0,0,0,100,100,0,0,3,2,2,8,10,10,10,1\n" % (s, styleTop, styleCenter, styles[defaultStyleName]["fontFamily"].replace(",",";"), (styles[s]["color"][5:7]+styles[s]["color"][3:5]+styles[s]["color"][1:3]).upper(), (styles[s]["backgroundColor"][7:9]+styles[s]["backgroundColor"][5:7]+styles[s]["backgroundColor"][3:5]+styles[s]["backgroundColor"][1:3]).upper())
f.write(out.encode("utf-8"))
out = "Style: %s%s%s,%s,20,&H00%s,&H000000FF,&H%s,&H00000000,0,0,0,0,100,100,0,0,3,2,2,9,10,10,10,1\n" % (s, styleTop, styleRight, styles[defaultStyleName]["fontFamily"].replace(",",";"), (styles[s]["color"][5:7]+styles[s]["color"][3:5]+styles[s]["color"][1:3]).upper(), (styles[s]["backgroundColor"][7:9]+styles[s]["backgroundColor"][5:7]+styles[s]["backgroundColor"][3:5]+styles[s]["backgroundColor"][1:3]).upper())
f.write(out.encode("utf-8"))
out = "Style: %s%s%s,%s,20,&H00%s,&H000000FF,&H%s,&H00000000,0,0,0,0,100,100,0,0,3,2,2,1,10,10,10,1\n" % (s, styleBottom, styleLeft, styles[defaultStyleName]["fontFamily"].replace(",",";"), (styles[s]["color"][5:7]+styles[s]["color"][3:5]+styles[s]["color"][1:3]).upper(), (styles[s]["backgroundColor"][7:9]+styles[s]["backgroundColor"][5:7]+styles[s]["backgroundColor"][3:5]+styles[s]["backgroundColor"][1:3]).upper())
f.write(out.encode("utf-8"))
out = "Style: %s%s%s,%s,20,&H00%s,&H000000FF,&H%s,&H00000000,0,0,0,0,100,100,0,0,3,2,2,2,10,10,10,1\n" % (s, styleBottom, styleCenter, styles[defaultStyleName]["fontFamily"].replace(",",";"), (styles[s]["color"][5:7]+styles[s]["color"][3:5]+styles[s]["color"][1:3]).upper(), (styles[s]["backgroundColor"][7:9]+styles[s]["backgroundColor"][5:7]+styles[s]["backgroundColor"][3:5]+styles[s]["backgroundColor"][1:3]).upper())
f.write(out.encode("utf-8"))
out = "Style: %s%s%s,%s,20,&H00%s,&H000000FF,&H%s,&H00000000,0,0,0,0,100,100,0,0,3,2,2,3,10,10,10,1\n" % (s, styleBottom, styleRight, styles[defaultStyleName]["fontFamily"].replace(",",";"), (styles[s]["color"][5:7]+styles[s]["color"][3:5]+styles[s]["color"][1:3]).upper(), (styles[s]["backgroundColor"][7:9]+styles[s]["backgroundColor"][5:7]+styles[s]["backgroundColor"][3:5]+styles[s]["backgroundColor"][1:3]).upper())
f.write(out.encode("utf-8"))
f.write("\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n".encode("utf-8"))
lines = []
currentDivStyle = ""
for d in tt_divs:
if d.hasAttribute("style"):
currentDivStyle = d.getAttribute("style")
else:
currentDivStyle = defaultStyleName
ps = d.getElementsByTagName("tt:p")
for p in ps:
line = {}
if p.hasAttribute("xml:id"):
line["id"] = p.getAttribute("xml:id")
if p.hasAttribute("style"):
line["style"] = p.getAttribute("style")
if p.hasAttribute("region"):
line["region"] = p.getAttribute("region")
if p.hasAttribute("begin"):
line["begin"] = p.getAttribute("begin")
if p.hasAttribute("end"):
line["end"] = p.getAttribute("end")
text = []
for i in range(0, p.childNodes.length):
if p.childNodes[i].nodeName == "tt:span":
s = p.childNodes[i]
currentString = {}
if s.hasAttribute("style"):
currentString["style"] = s.getAttribute("style")
currentString["text"] = s.childNodes[0].data
text.append(currentString)
if p.childNodes[i].nodeName == "tt:br":
currentString = {}
currentString["text"] = "\\N"
text.append(currentString)
line["text"] = text
line[defaultStyleName] = currentDivStyle
lines.append(line)
for line in lines:
#nur eine Farbe?
single = True
style = line["text"][0]["style"]
if len(line["text"]) > 1:
for l in line["text"]:
if l.has_key("style"):
if l["style"] != style:
single = False
break
if "style" in line:
lineStyle = line["style"]
else:
lineStyle = defaultStyle
ass = "Dialogue: 0,0%s,0%s,%s,,0,0,0,," % (line["begin"][1:11], line["end"][1:11], style + line["region"] + lineStyle)
if single:
for l in line["text"]:
ass = ass + l["text"]
else:
for l in line["text"]:
if l.has_key("style"):
ll = "{\\1c&H%s&}{\\3c&H%s&}%s" % ((styles[l["style"]]["color"][5:7]+styles[l["style"]]["color"][3:5]+styles[l["style"]]["color"][1:3]).upper(), (styles[l["style"]]["backgroundColor"][7:9]+styles[l["style"]]["backgroundColor"][5:7]+styles[l["style"]]["backgroundColor"][3:5]+styles[l["style"]]["backgroundColor"][1:3]).upper(), l["text"])
ass = ass + ll
else:
ass = ass + l["text"]
f.write(ass.encode("utf-8"))
f.write('\n')
f.close()
# strings = p.getElementsByTagName("tt:span")
# i = 0
# text = []
# for s in strings:
# currentString = {}
# if s.hasAttribute("style"):
# currentString["style"] = s.getAttribute("style")
# currentString["text"] = s.childNodes[0].data
# text.append(currentString)
# line["text"] = text
# lines.append(line)