-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsrt2ass.py
111 lines (90 loc) · 3.15 KB
/
srt2ass.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
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
# -*- coding: utf-8 -*-
#
# python-srt2ass: https://github.com/ewwink/python-srt2ass
# by: ewwink
#
import sys
import os
import re
import codecs
def fileopen(input_file):
encodings = ["utf-32", "utf-16", "utf-8", "cp1252", "gb2312", "gbk", "big5"]
tmp = ''
for enc in encodings:
try:
with codecs.open(input_file, mode="r", encoding=enc) as fd:
tmp = fd.read()
break
except:
# print enc + ' failed'
continue
return [tmp, enc]
def srt2ass(input_file):
if '.ass' in input_file:
return input_file
if not os.path.isfile(input_file):
print(input_file + ' not exist')
return
src = fileopen(input_file)
tmp = src[0]
encoding = src[1]
src = ''
utf8bom = ''
if u'\ufeff' in tmp:
tmp = tmp.replace(u'\ufeff', '')
utf8bom = u'\ufeff'
tmp = tmp.replace("\r", "")
lines = [x.strip() for x in tmp.split("\n") if x.strip()]
subLines = ''
tmpLines = ''
lineCount = 0
output_file = '.'.join(input_file.split('.')[:-1])
output_file += '.ass'
for ln in range(len(lines)):
line = lines[ln]
if line.isdigit() 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')
tmpLines += 'Dialogue: 0,' + line + ',SubStyle,,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)
head_str = '''[Script Info]
; This is an Advanced Sub Station Alpha v4+ script.
Title:
ScriptType: v4.00+
Collisions: Normal
PlayDepth: 0
[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: SubStyle,Arial,20,&H0300FFFF,&H00FFFFFF,&H00000000,&H02000000,-1,0,0,0,100,100,0,0,3,2,0,2,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 open(output_file, 'wb') as output:
output.write(output_str)
output_file = output_file.replace('\\', '\\\\')
output_file = output_file.replace('/', '//')
return output_file
if len(sys.argv) > 1:
for name in sys.argv[1:]:
srt2ass(name)