-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubs.py
262 lines (211 loc) · 8.99 KB
/
subs.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import codecs
import os
import re
from collections import OrderedDict
from common import SushiError, format_time, format_srt_time
def _parse_ass_time(string):
hours, minutes, seconds = map(float, string.split(':'))
return hours*3600+minutes*60+seconds
class ScriptEventBase(object):
def __init__(self, start, end):
super(ScriptEventBase, self).__init__()
self._shift = 0
self._diff = 1
self.start = start
self.end = end
self._linked_event = None
self._start_shift = 0
self._end_shift = 0
@property
def shift(self):
return self._linked_event.shift if self.linked else self._shift
@property
def diff(self):
return self._linked_event.diff if self.linked else self._diff
@property
def duration(self):
return self.end - self.start
@property
def shifted_end(self):
return self.end + self.shift + self._end_shift
@property
def shifted_start(self):
return self.start + self.shift + self._start_shift
def apply_shift(self):
self.start = self.shifted_start
self.end = self.shifted_end
def set_shift(self, shift, audio_diff):
if self.linked:
raise Exception('Cannot set shift of a linked event. This is a bug')
self._shift = shift
self._diff = audio_diff
def adjust_additional_shifts(self, start_shift, end_shift):
if self.linked:
raise Exception('Cannot apply additional shifts to a linked event. This is a bug')
self._start_shift += start_shift
self._end_shift += end_shift
def get_link_chain_end(self):
return self._linked_event.get_link_chain_end() if self.linked else self
def link_event(self, other):
if other.get_link_chain_end() is self:
raise Exception('Circular link detected. This is a bug')
self._linked_event = other
def resolve_link(self):
if not self.linked:
raise Exception('Cannot resolve unlinked events. This is a bug')
self._shift = self._linked_event.shift
self._diff = self._linked_event.diff
self._linked_event = None
@property
def linked(self):
return self._linked_event is not None
def adjust_shift(self, value):
if self.linked:
raise Exception('Cannot adjust time of linked events. This is a bug')
self._shift += value
def __repr__(self):
return unicode(self)
class ScriptBase(object):
def sort_by_time(self):
self.events.sort(key=lambda x: x.start)
class SrtEvent(ScriptEventBase):
def __init__(self, text):
parse_time = lambda x: _parse_ass_time(x.replace(',', '.'))
lines = text.split('\n', 2)
times = lines[1].split('-->')
start = parse_time(times[0].rstrip())
end = parse_time(times[1].lstrip())
super(SrtEvent, self).__init__(start, end)
self.idx = int(lines[0])
self.text = lines[2]
self.style = None
self.is_comment = False
def __unicode__(self):
return u'{0}\n{1} --> {2}\n{3}'.format(self.idx, self._format_time(self.start),
self._format_time(self.end), self.text)
@staticmethod
def _format_time(seconds):
return format_srt_time(seconds)
class SrtScript(ScriptBase):
def __init__(self, events):
super(SrtScript, self).__init__()
self.events = events
@classmethod
def from_file(cls, path):
try:
with codecs.open(path, encoding='utf-8-sig') as script:
return cls([SrtEvent(x) for x in script.read().replace(os.linesep, '\n').split('\n\n') if x])
except IOError:
raise SushiError("Script {0} not found".format(path))
def save_to_file(self, path):
text = '\n\n'.join(map(unicode, self.events))
with codecs.open(path, encoding='utf-8', mode='w') as script:
script.write(text)
class AssEvent(ScriptEventBase):
def __init__(self, text):
self.source_index = 0
split = text.split(':', 1)
self.kind = split[0]
self.is_comment = self.kind.lower() == 'comment'
split = [x.strip() for x in split[1].split(',', 9)]
start = _parse_ass_time(split[1])
end = _parse_ass_time(split[2])
super(AssEvent, self).__init__(start, end)
self.layer = split[0]
self.style = split[3]
self.name = split[4]
self.margin_left = split[5]
self.margin_right = split[6]
self.margin_vertical = split[7]
self.effect = split[8]
self.text = split[9]
def __unicode__(self):
return u'{0}: {1},{2},{3},{4},{5},{6},{7},{8},{9},{10}'.format(self.kind, self.layer,
self._format_time(self.start),
self._format_time(self.end),
self.style, self.name,
self.margin_left, self.margin_right,
self.margin_vertical, self.effect,
self.text)
@staticmethod
def _format_time(seconds):
return format_time(seconds)
class AssScript(ScriptBase):
def __init__(self, script_info, styles, events, other):
super(AssScript, self).__init__()
self.script_info = script_info
self.styles = styles
self.events = events
self.other = other
@classmethod
def from_file(cls, path):
script_info, styles, events = [], [], []
other_sections = OrderedDict()
def parse_script_info_line(line):
if line.startswith(u'Format:'):
return
script_info.append(line)
def parse_styles_line(line):
if line.startswith(u'Format:'):
return
styles.append(line)
def parse_event_line(line):
if line.startswith(u'Format:'):
return
events.append(AssEvent(line))
def create_generic_parse(section_name):
other_sections[section_name] = []
return lambda x: other_sections[section_name].append(x)
parse_function = None
try:
with codecs.open(path, encoding='utf-8-sig') as script:
for line_idx, line in enumerate(script):
line = line.strip()
if not line:
continue
low = line.lower()
if low == u'[script info]':
parse_function = parse_script_info_line
elif low == u'[v4+ styles]':
parse_function = parse_styles_line
elif low == u'[events]':
parse_function = parse_event_line
elif re.match(r'\[.+?\]', low):
parse_function = create_generic_parse(line)
elif not parse_function:
raise SushiError("That's some invalid ASS script")
else:
try:
parse_function(line)
except Exception as e:
raise SushiError("That's some invalid ASS script: {0} [line {1}]".format(e.message, line_idx))
except IOError:
raise SushiError("Script {0} not found".format(path))
for idx, event in enumerate(events):
event.source_index = idx
return cls(script_info, styles, events, other_sections)
def save_to_file(self, path):
# if os.path.exists(path):
# raise RuntimeError('File %s already exists' % path)
lines = []
if self.script_info:
lines.append(u'[Script Info]')
lines.extend(self.script_info)
lines.append('')
if self.styles:
lines.append(u'[V4+ Styles]')
lines.append(u'Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding')
lines.extend(self.styles)
lines.append('')
if self.events:
events = sorted(self.events, key=lambda x: x.source_index)
lines.append(u'[Events]')
lines.append(u'Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text')
lines.extend(map(unicode, events))
if self.other:
for section_name, section_lines in self.other.iteritems():
lines.append('')
lines.append(section_name)
lines.extend(section_lines)
with codecs.open(path, encoding='utf-8-sig', mode='w') as script:
script.write(unicode(os.linesep).join(lines))