forked from shaka-project/shaka-player
/
ssa_text_parser.js
330 lines (302 loc) · 8.94 KB
/
ssa_text_parser.js
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.text.SsaTextParser');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.text.Cue');
goog.require('shaka.text.TextEngine');
goog.require('shaka.util.StringUtils');
/**
* Documentation: http://moodub.free.fr/video/ass-specs.doc
* https://en.wikipedia.org/wiki/SubStation_Alpha
* @implements {shaka.extern.TextParser}
* @export
*/
shaka.text.SsaTextParser = class {
/**
* @override
* @export
*/
parseInit(data) {
goog.asserts.assert(false, 'SSA does not have init segments');
}
/**
* @override
* @export
*/
setSequenceMode(sequenceMode) {
// Unused.
}
/**
* @override
* @export
*/
parseMedia(data, time) {
const StringUtils = shaka.util.StringUtils;
const SsaTextParser = shaka.text.SsaTextParser;
// Get the input as a string.
const str = StringUtils.fromUTF8(data);
const section = {
styles: '',
events: '',
};
const parts = str.split(/\r?\n\s*\r?\n/);
for (const part of parts) {
// SSA content
const match = SsaTextParser.ssaContent_.exec(part);
if (match) {
const tag = match[1];
const lines = match[2];
if (tag == 'V4 Styles' || tag == 'V4+ Styles') {
section.styles = lines;
continue;
}
if (tag == 'Events') {
section.events = lines;
continue;
}
}
shaka.log.warning('SsaTextParser parser encountered an unknown part.',
part);
}
// Process styles
const styles = [];
// Used to be able to iterate over the style parameters.
let styleColumns = null;
const styleLines = section.styles.split(/\r?\n/);
for (const line of styleLines) {
if (/^\s*;/.test(line)) {
// Skip comment
continue;
}
const lineParts = SsaTextParser.lineParts_.exec(line);
if (lineParts) {
const name = lineParts[1].trim();
const value = lineParts[2].trim();
if (name == 'Format') {
styleColumns = value.split(SsaTextParser.valuesFormat_);
continue;
}
if (name == 'Style') {
const values = value.split(SsaTextParser.valuesFormat_);
const style = {};
for (let c = 0; c < styleColumns.length && c < values.length; c++) {
style[styleColumns[c]] = values[c];
}
styles.push(style);
continue;
}
}
}
// Process cues
/** @type {!Array.<!shaka.extern.Cue>} */
const cues = [];
// Used to be able to iterate over the event parameters.
let eventColumns = null;
const eventLines = section.events.split(/\r?\n/);
for (const line of eventLines) {
if (/^\s*;/.test(line)) {
// Skip comment
continue;
}
const lineParts = SsaTextParser.lineParts_.exec(line);
if (lineParts) {
const name = lineParts[1].trim();
const value = lineParts[2].trim();
if (name == 'Format') {
eventColumns = value.split(SsaTextParser.valuesFormat_);
continue;
}
if (name == 'Dialogue') {
const values = value.split(SsaTextParser.valuesFormat_);
const data = {};
for (let c = 0; c < eventColumns.length && c < values.length; c++) {
data[eventColumns[c]] = values[c];
}
const startTime = SsaTextParser.parseTime_(data['Start']);
const endTime = SsaTextParser.parseTime_(data['End']);
// Note: Normally, you should take the "Text" field, but if it
// has a comma, it fails.
const payload = values.slice(eventColumns.length - 1).join(',')
.replace(/\\N/g, '\n') // '\n' for new line
.replace(/\{[^}]+\}/g, ''); // {\pos(400,570)}
const cue = new shaka.text.Cue(startTime, endTime, payload);
const styleName = data['Style'];
const styleData = styles.find((s) => s['Name'] == styleName);
if (styleData) {
SsaTextParser.addStyle_(cue, styleData);
}
cues.push(cue);
continue;
}
}
}
return cues;
}
/**
* Adds applicable style properties to a cue.
*
* @param {shaka.extern.Cue} cue
* @param {Object} style
* @private
*/
static addStyle_(cue, style) {
const Cue = shaka.text.Cue;
const SsaTextParser = shaka.text.SsaTextParser;
const fontFamily = style['Fontname'];
if (fontFamily) {
cue.fontFamily = fontFamily;
}
const fontSize = style['Fontsize'];
if (fontSize) {
cue.fontSize = fontSize + 'px';
}
const color = style['PrimaryColour'];
if (color) {
const ccsColor = SsaTextParser.parseSsaColor_(color);
if (ccsColor) {
cue.color = ccsColor;
}
}
const backgroundColor = style['BackColour'];
if (backgroundColor) {
const cssBackgroundColor = SsaTextParser.parseSsaColor_(backgroundColor);
if (cssBackgroundColor) {
cue.backgroundColor = cssBackgroundColor;
}
}
const bold = style['Bold'];
if (bold) {
cue.fontWeight = Cue.fontWeight.BOLD;
}
const italic = style['Italic'];
if (italic) {
cue.fontStyle = Cue.fontStyle.ITALIC;
}
const underline = style['Underline'];
if (underline) {
cue.textDecoration.push(Cue.textDecoration.UNDERLINE);
}
const letterSpacing = style['Spacing'];
if (letterSpacing) {
cue.letterSpacing = letterSpacing + 'px';
}
const alignment = style['Alignment'];
if (alignment) {
const alignmentInt = parseInt(alignment, 10);
switch (alignmentInt) {
case 1:
cue.displayAlign = Cue.displayAlign.AFTER;
cue.textAlign = Cue.textAlign.START;
break;
case 2:
cue.displayAlign = Cue.displayAlign.AFTER;
cue.textAlign = Cue.textAlign.CENTER;
break;
case 3:
cue.displayAlign = Cue.displayAlign.AFTER;
cue.textAlign = Cue.textAlign.END;
break;
case 5:
cue.displayAlign = Cue.displayAlign.BEFORE;
cue.textAlign = Cue.textAlign.START;
break;
case 6:
cue.displayAlign = Cue.displayAlign.BEFORE;
cue.textAlign = Cue.textAlign.CENTER;
break;
case 7:
cue.displayAlign = Cue.displayAlign.BEFORE;
cue.textAlign = Cue.textAlign.END;
break;
case 9:
cue.displayAlign = Cue.displayAlign.CENTER;
cue.textAlign = Cue.textAlign.START;
break;
case 10:
cue.displayAlign = Cue.displayAlign.CENTER;
cue.textAlign = Cue.textAlign.CENTER;
break;
case 11:
cue.displayAlign = Cue.displayAlign.CENTER;
cue.textAlign = Cue.textAlign.END;
break;
}
}
const opacity = style['AlphaLevel'];
if (opacity) {
cue.opacity = parseFloat(opacity);
}
}
/**
* Parses a SSA color .
*
* @param {string} colorString
* @return {?string}
* @private
*/
static parseSsaColor_(colorString) {
// The SSA V4+ color can be represented in hex (&HAABBGGRR) or in decimal
// format (byte order AABBGGRR) and in both cases the alpha channel's
// value needs to be inverted as in case of SSA the 0xFF alpha value means
// transparent and 0x00 means opaque
/** @type {number} */
const abgr = parseInt(colorString.replace('&H', ''), 16);
if (abgr >= 0) {
const a = ((abgr >> 24) & 0xFF) ^ 0xFF; // Flip alpha.
const alpha = a / 255;
const b = (abgr >> 16) & 0xFF;
const g = (abgr >> 8) & 0xFF;
const r = abgr & 0xff;
return 'rgba(' + r + ',' + g + ',' + b + ',' + alpha + ')';
}
return null;
}
/**
* Parses a SSA time from the given parser.
*
* @param {string} string
* @return {number}
* @private
*/
static parseTime_(string) {
const SsaTextParser = shaka.text.SsaTextParser;
const match = SsaTextParser.timeFormat_.exec(string);
const hours = match[1] ? parseInt(match[1].replace(':', ''), 10) : 0;
const minutes = parseInt(match[2], 10);
const seconds = parseFloat(match[3]);
return hours * 3600 + minutes * 60 + seconds;
}
};
/**
* @const
* @private {!RegExp}
* @example [V4 Styles]\nFormat: Name\nStyle: DefaultVCD
*/
shaka.text.SsaTextParser.ssaContent_ =
/^\s*\[([^\]]+)\]\r?\n([\s\S]*)/;
/**
* @const
* @private {!RegExp}
* @example Style: DefaultVCD,...
*/
shaka.text.SsaTextParser.lineParts_ =
/^\s*([^:]+):\s*(.*)/;
/**
* @const
* @private {!RegExp}
* @example Style: DefaultVCD,...
*/
shaka.text.SsaTextParser.valuesFormat_ = /\s*,\s*/;
/**
* @const
* @private {!RegExp}
* @example 0:00:01.1 or 0:00:01.18 or 0:00:01.180
*/
shaka.text.SsaTextParser.timeFormat_ =
/^(\d+:)?(\d{1,2}):(\d{1,2}(?:[.]\d{1,3})?)?$/;
shaka.text.TextEngine.registerParser(
'text/x-ssa', () => new shaka.text.SsaTextParser());