/
vtt_text_parser.js
816 lines (746 loc) · 24 KB
/
vtt_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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.text.VttTextParser');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.text.Cue');
goog.require('shaka.text.CueRegion');
goog.require('shaka.text.TextEngine');
goog.require('shaka.util.Error');
goog.require('shaka.util.StringUtils');
goog.require('shaka.util.TextParser');
goog.require('shaka.util.XmlUtils');
/**
* @implements {shaka.extern.TextParser}
* @export
*/
shaka.text.VttTextParser = class {
/** Constructs a VTT parser. */
constructor() {
/** @private {boolean} */
this.sequenceMode_ = false;
}
/**
* @override
* @export
*/
parseInit(data) {
goog.asserts.assert(false, 'VTT does not have init segments');
}
/**
* @override
* @export
*/
setSequenceMode(sequenceMode) {
this.sequenceMode_ = sequenceMode;
}
/**
* @override
* @export
*/
parseMedia(data, time) {
const VttTextParser = shaka.text.VttTextParser;
// Get the input as a string. Normalize newlines to \n.
let str = shaka.util.StringUtils.fromUTF8(data);
str = str.replace(/\r\n|\r(?=[^\n]|$)/gm, '\n');
const blocks = str.split(/\n{2,}/m);
if (!/^WEBVTT($|[ \t\n])/m.test(blocks[0])) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.TEXT,
shaka.util.Error.Code.INVALID_TEXT_HEADER);
}
// Depending on "segmentRelativeVttTiming" configuration,
// "vttOffset" will correspond to either "periodStart" (default)
// or "segmentStart", for segmented VTT where timings are relative
// to the beginning of each segment.
// NOTE: "periodStart" is the timestamp offset applied via TextEngine.
// It is no longer closely tied to periods, but the name stuck around.
// NOTE: This offset and the flag choosing its meaning have no effect on
// HLS content, which should use X-TIMESTAMP-MAP and periodStart instead.
let offset = time.vttOffset;
// Only use 'X-TIMESTAMP-MAP' in sequence mode, as that is currently
// shorthand for HLS. Note that an offset based on the first video
// timestamp has already been extracted, and appears in periodStart.
// The relative offset from X-TIMESTAMP-MAP will be added to that for HLS.
if (blocks[0].includes('X-TIMESTAMP-MAP') && this.sequenceMode_) {
// https://bit.ly/2K92l7y
// The 'X-TIMESTAMP-MAP' header is used in HLS to align text with
// the rest of the media.
// The header format is 'X-TIMESTAMP-MAP=MPEGTS:n,LOCAL:m'
// (the attributes can go in any order)
// where n is MPEG-2 time and m is cue time it maps to.
// For example 'X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000'
// means an offset of 10 seconds
// 900000/MPEG_TIMESCALE - cue time.
const cueTimeMatch =
blocks[0].match(/LOCAL:((?:(\d{1,}):)?(\d{2}):(\d{2})\.(\d{3}))/m);
const mpegTimeMatch = blocks[0].match(/MPEGTS:(\d+)/m);
if (cueTimeMatch && mpegTimeMatch) {
const parser = new shaka.util.TextParser(cueTimeMatch[1]);
const cueTime = shaka.text.VttTextParser.parseTime_(parser);
if (cueTime == null) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.TEXT,
shaka.util.Error.Code.INVALID_TEXT_HEADER);
}
let mpegTime = Number(mpegTimeMatch[1]);
const mpegTimescale = shaka.text.VttTextParser.MPEG_TIMESCALE_;
const rolloverSeconds =
shaka.text.VttTextParser.TS_ROLLOVER_ / mpegTimescale;
let segmentStart = time.segmentStart;
while (segmentStart >= rolloverSeconds) {
segmentStart -= rolloverSeconds;
mpegTime += shaka.text.VttTextParser.TS_ROLLOVER_;
}
offset = time.periodStart + mpegTime / mpegTimescale - cueTime;
}
}
// Parse VTT regions.
/* !Array.<!shaka.extern.CueRegion> */
const regions = [];
for (const line of blocks[0].split('\n')) {
if (/^Region:/.test(line)) {
const region = VttTextParser.parseRegion_(line);
regions.push(region);
}
}
/** @type {!Map.<string, shaka.text.Cue>} */
const styles = new Map();
VttTextParser.addDefaultTextColor_(styles);
// Parse cues.
const ret = [];
for (const block of blocks.slice(1)) {
const lines = block.split('\n');
VttTextParser.parseStyle_(lines, styles);
const cue = VttTextParser.parseCue_(lines, offset, regions, styles);
if (cue) {
ret.push(cue);
}
}
return ret;
}
/**
* Add default color
*
* @param {!Map.<string, shaka.text.Cue>} styles
* @private
*/
static addDefaultTextColor_(styles) {
const textColor = shaka.text.Cue.defaultTextColor;
for (const [key, value] of Object.entries(textColor)) {
const cue = new shaka.text.Cue(0, 0, '');
cue.color = value;
styles.set(key, cue);
}
const bgColor = shaka.text.Cue.defaultTextBackgroundColor;
for (const [key, value] of Object.entries(bgColor)) {
const cue = new shaka.text.Cue(0, 0, '');
cue.backgroundColor = value;
styles.set(key, cue);
}
}
/**
* Parses a string into a Region object.
*
* @param {string} text
* @return {!shaka.extern.CueRegion}
* @private
*/
static parseRegion_(text) {
const VttTextParser = shaka.text.VttTextParser;
const parser = new shaka.util.TextParser(text);
// The region string looks like this:
// Region: id=fred width=50% lines=3 regionanchor=0%,100%
// viewportanchor=10%,90% scroll=up
const region = new shaka.text.CueRegion();
// Skip 'Region:'
parser.readWord();
parser.skipWhitespace();
let word = parser.readWord();
while (word) {
if (!VttTextParser.parseRegionSetting_(region, word)) {
shaka.log.warning(
'VTT parser encountered an invalid VTTRegion setting: ', word,
' The setting will be ignored.');
}
parser.skipWhitespace();
word = parser.readWord();
}
return region;
}
/**
* Parses a style block into a Cue object.
*
* @param {!Array.<string>} text
* @param {!Map.<string, shaka.text.Cue>} styles
* @private
*/
static parseStyle_(text, styles) {
// Skip empty blocks.
if (text.length == 1 && !text[0]) {
return;
}
// Skip comment blocks.
if (/^NOTE($|[ \t])/.test(text[0])) {
return;
}
// Only style block are allowed.
if (text[0] != 'STYLE') {
return;
}
if (!text[1].includes('::cue')) {
return;
}
let styleSelector = 'global';
// Look for what is within parentisesis. For example:
// <code>:: cue (b) {</code>, what we are looking for is <code>b</code>
const selector = text[1].match(/\((.*)\)/);
if (selector) {
styleSelector = selector.pop();
}
// We start at 2 to avoid '::cue' and end earlier to avoid '}'
let propertyLines = text.slice(2, -1);
if (text[1].includes('}')) {
const payload = /\{(.*?)\}/.exec(text[1]);
if (payload) {
propertyLines = payload[1].split(';');
}
}
const cue = new shaka.text.Cue(0, 0, '');
let validStyle = false;
for (let i = 0; i < propertyLines.length; i++) {
// We look for CSS properties. As a general rule they are separated by
// <code>:</code>. Eg: <code>color: red;</code>
const lineParts = /^\s*([^:]+):\s*(.*)/.exec(propertyLines[i]);
if (lineParts) {
const name = lineParts[1].trim();
const value = lineParts[2].trim().replace(';', '');
switch (name) {
case 'background-color':
validStyle = true;
cue.backgroundColor = value;
break;
case 'color':
validStyle = true;
cue.color = value;
break;
case 'font-family':
validStyle = true;
cue.fontFamily = value;
break;
case 'font-size':
validStyle = true;
cue.fontSize = value;
break;
case 'font-weight':
if (parseInt(value, 10) >= 700) {
validStyle = true;
cue.fontWeight = shaka.text.Cue.fontWeight.BOLD;
}
break;
case 'font-style':
switch (value) {
case 'normal':
validStyle = true;
cue.fontStyle = shaka.text.Cue.fontStyle.NORMAL;
break;
case 'italic':
validStyle = true;
cue.fontStyle = shaka.text.Cue.fontStyle.ITALIC;
break;
case 'oblique':
validStyle = true;
cue.fontStyle = shaka.text.Cue.fontStyle.OBLIQUE;
break;
}
break;
case 'opacity':
validStyle = true;
cue.opacity = parseFloat(value);
break;
case 'white-space':
validStyle = true;
cue.wrapLine = value != 'noWrap';
break;
default:
shaka.log.warning('VTT parser encountered an unsupported style: ',
lineParts);
break;
}
}
}
if (validStyle) {
styles.set(styleSelector, cue);
}
}
/**
* Parses a text block into a Cue object.
*
* @param {!Array.<string>} text
* @param {number} timeOffset
* @param {!Array.<!shaka.extern.CueRegion>} regions
* @param {!Map.<string, shaka.text.Cue>} styles
* @return {shaka.text.Cue}
* @private
*/
static parseCue_(text, timeOffset, regions, styles) {
const VttTextParser = shaka.text.VttTextParser;
// Skip empty blocks.
if (text.length == 1 && !text[0]) {
return null;
}
// Skip comment blocks.
if (/^NOTE($|[ \t])/.test(text[0])) {
return null;
}
// Skip style blocks.
if (text[0] == 'STYLE') {
return null;
}
let id = null;
if (!text[0].includes('-->')) {
id = text[0];
text.splice(0, 1);
}
// Parse the times.
const parser = new shaka.util.TextParser(text[0]);
let start = VttTextParser.parseTime_(parser);
const expect = parser.readRegex(/[ \t]+-->[ \t]+/g);
let end = VttTextParser.parseTime_(parser);
if (start == null || expect == null || end == null) {
throw new shaka.util.Error(
shaka.util.Error.Severity.CRITICAL,
shaka.util.Error.Category.TEXT,
shaka.util.Error.Code.INVALID_TEXT_CUE,
'Could not parse cue time range in WebVTT');
}
start += timeOffset;
end += timeOffset;
// Get the payload.
const payload = text.slice(1).join('\n').trim();
let cue = null;
if (styles.has('global')) {
cue = styles.get('global').clone();
cue.startTime = start;
cue.endTime = end;
cue.payload = '';
} else {
cue = new shaka.text.Cue(start, end, '');
}
VttTextParser.parseCueStyles(payload, cue, styles);
// Parse optional settings.
parser.skipWhitespace();
let word = parser.readWord();
while (word) {
if (!VttTextParser.parseCueSetting(cue, word, regions)) {
shaka.log.warning('VTT parser encountered an invalid VTT setting: ',
word,
' The setting will be ignored.');
}
parser.skipWhitespace();
word = parser.readWord();
}
if (id != null) {
cue.id = id;
}
return cue;
}
/**
* Parses a WebVTT styles from the given payload.
*
* @param {string} payload
* @param {!shaka.text.Cue} rootCue
* @param {!Map.<string, shaka.text.Cue>} styles
*/
static parseCueStyles(payload, rootCue, styles) {
const VttTextParser = shaka.text.VttTextParser;
if (styles.size === 0) {
VttTextParser.addDefaultTextColor_(styles);
}
payload = VttTextParser.replaceColorPayload_(payload);
const xmlPayload = '<span>' + payload + '</span>';
const element = shaka.util.XmlUtils.parseXmlString(xmlPayload, 'span');
if (element) {
/** @type {!Array.<!shaka.extern.Cue>} */
const cues = [];
const childNodes = element.childNodes;
if (childNodes.length == 1) {
const childNode = childNodes[0];
if (childNode.nodeType == Node.TEXT_NODE ||
childNode.nodeType == Node.CDATA_SECTION_NODE) {
rootCue.payload = payload;
return;
}
}
for (const childNode of childNodes) {
VttTextParser.generateCueFromElement_(
childNode, rootCue, cues, styles);
}
rootCue.nestedCues = cues;
} else {
shaka.log.warning('The cue\'s markup could not be parsed: ', payload);
rootCue.payload = payload;
}
}
/**
* Converts color end tag to be valid for xml parsing
* For example,
* input: <c.yellow.bg_blue>Yellow text on blue bg</c>
* output: <c.yellow.bg_blue>Yellow text on blue bg</c.yellow.bg_blue>
*
* Returns original payload if invalid tag is found.
* Invalid tag example: <c.yellow><b>Example</c></b>
*
* @param {string} payload
* @return {string} processed payload
* @private
*/
static replaceColorPayload_(payload) {
const names = [];
let nameStart = -1;
let newPayload = '';
for (let i = 0; i < payload.length; i++) {
if (payload[i] === '/') {
const end = payload.indexOf('>', i);
if (end <= i) {
return payload;
}
const tagEnd = payload.substring(i + 1, end);
const tagStart = names.pop();
if (!tagEnd || !tagStart) {
return payload;
} else if (tagStart === tagEnd) {
newPayload += '/' + tagEnd + '>';
i += tagEnd.length + 1;
} else {
if (!tagStart.startsWith('c.') || tagEnd !== 'c') {
return payload;
}
newPayload += '/' + tagStart + '>';
i += tagEnd.length + 1;
}
} else {
if (payload[i] === '<') {
nameStart = i + 1;
} else if (payload[i] === '>') {
if (nameStart > 0) {
names.push(payload.substr(nameStart, i - nameStart));
nameStart = -1;
}
}
newPayload += payload[i];
}
}
return newPayload;
}
/**
* @param {string} value
* @param {string} defaultValue
* @private
*/
static getOrDefault_(value, defaultValue) {
if (value && value.length > 0) {
return value;
}
return defaultValue;
}
/**
* Merges values created in parseStyle_
* @param {!shaka.extern.Cue} cue
* @param {shaka.extern.Cue} refCue
* @private
*/
static mergeStyle_(cue, refCue) {
if (!refCue) {
return;
}
const VttTextParser = shaka.text.VttTextParser;
// Overwrites if new value string length > 0
cue.backgroundColor = VttTextParser.getOrDefault_(
refCue.backgroundColor, cue.backgroundColor);
cue.color = VttTextParser.getOrDefault_(
refCue.color, cue.color);
cue.fontFamily = VttTextParser.getOrDefault_(
refCue.fontFamily, cue.fontFamily);
cue.fontSize = VttTextParser.getOrDefault_(
refCue.fontSize, cue.fontSize);
// Overwrite with new values as unable to determine
// if new value is set or not
cue.fontWeight = refCue.fontWeight;
cue.fontStyle = refCue.fontStyle;
cue.opacity = refCue.opacity;
cue.wrapLine = refCue.wrapLine;
}
/**
* @param {!Node} element
* @param {!shaka.text.Cue} rootCue
* @param {Array.<!shaka.extern.Cue>} cues
* @param {!Map.<string, shaka.text.Cue>} styles
* @private
*/
static generateCueFromElement_(element, rootCue, cues, styles) {
const VttTextParser = shaka.text.VttTextParser;
const nestedCue = rootCue.clone();
if (element.nodeType === Node.ELEMENT_NODE && element.nodeName) {
const bold = shaka.text.Cue.fontWeight.BOLD;
const italic = shaka.text.Cue.fontStyle.ITALIC;
const underline = shaka.text.Cue.textDecoration.UNDERLINE;
const tags = element.nodeName.split(/[ .]+/);
for (const tag of tags) {
if (styles.has(tag)) {
VttTextParser.mergeStyle_(nestedCue, styles.get(tag));
}
switch (tag) {
case 'b':
nestedCue.fontWeight = bold;
break;
case 'i':
nestedCue.fontStyle = italic;
break;
case 'u':
nestedCue.textDecoration.push(underline);
break;
default:
break;
}
}
}
const isTextNode = shaka.util.XmlUtils.isText(element);
if (isTextNode) {
// Trailing line breaks may lost when convert cue to HTML tag
// Need to insert line break cue to preserve line breaks
const textArr = element.textContent.split('\n');
let isFirst = true;
for (const text of textArr) {
if (!isFirst) {
const lineBreakCue = rootCue.clone();
lineBreakCue.lineBreak = true;
cues.push(lineBreakCue);
}
if (text.length > 0) {
const textCue = nestedCue.clone();
textCue.payload = text;
cues.push(textCue);
}
isFirst = false;
}
} else {
for (const childNode of element.childNodes) {
VttTextParser.generateCueFromElement_(
childNode, nestedCue, cues, styles);
}
}
}
/**
* Parses a WebVTT setting from the given word.
*
* @param {!shaka.text.Cue} cue
* @param {string} word
* @param {!Array.<!shaka.text.CueRegion>} regions
* @return {boolean} True on success.
*/
static parseCueSetting(cue, word, regions) {
const VttTextParser = shaka.text.VttTextParser;
let results = null;
if ((results = /^align:(start|middle|center|end|left|right)$/.exec(word))) {
VttTextParser.setTextAlign_(cue, results[1]);
} else if ((results = /^vertical:(lr|rl)$/.exec(word))) {
VttTextParser.setVerticalWritingMode_(cue, results[1]);
} else if ((results = /^size:([\d.]+)%$/.exec(word))) {
cue.size = Number(results[1]);
} else if ((results =
/^position:([\d.]+)%(?:,(line-left|line-right|center|start|end))?$/
.exec(word))) {
cue.position = Number(results[1]);
if (results[2]) {
VttTextParser.setPositionAlign_(cue, results[2]);
}
} else if ((results = /^region:(.*)$/.exec(word))) {
const region = VttTextParser.getRegionById_(regions, results[1]);
if (region) {
cue.region = region;
}
} else {
return VttTextParser.parsedLineValueAndInterpretation_(cue, word);
}
return true;
}
/**
*
* @param {!Array.<!shaka.text.CueRegion>} regions
* @param {string} id
* @return {?shaka.text.CueRegion}
* @private
*/
static getRegionById_(regions, id) {
const regionsWithId = regions.filter((region) => {
return region.id == id;
});
if (!regionsWithId.length) {
shaka.log.warning('VTT parser could not find a region with id: ',
id,
' The region will be ignored.');
return null;
}
goog.asserts.assert(regionsWithId.length == 1,
'VTTRegion ids should be unique!');
return regionsWithId[0];
}
/**
* Parses a WebVTTRegion setting from the given word.
*
* @param {!shaka.text.CueRegion} region
* @param {string} word
* @return {boolean} True on success.
* @private
*/
static parseRegionSetting_(region, word) {
let results = null;
if ((results = /^id=(.*)$/.exec(word))) {
region.id = results[1];
} else if ((results = /^width=(\d{1,2}|100)%$/.exec(word))) {
region.width = Number(results[1]);
} else if ((results = /^lines=(\d+)$/.exec(word))) {
region.height = Number(results[1]);
region.heightUnits = shaka.text.CueRegion.units.LINES;
} else if ((results = /^regionanchor=(\d{1,2}|100)%,(\d{1,2}|100)%$/
.exec(word))) {
region.regionAnchorX = Number(results[1]);
region.regionAnchorY = Number(results[2]);
} else if ((results = /^viewportanchor=(\d{1,2}|100)%,(\d{1,2}|100)%$/
.exec(word))) {
region.viewportAnchorX = Number(results[1]);
region.viewportAnchorY = Number(results[2]);
} else if ((results = /^scroll=up$/.exec(word))) {
region.scroll = shaka.text.CueRegion.scrollMode.UP;
} else {
return false;
}
return true;
}
/**
* @param {!shaka.text.Cue} cue
* @param {string} align
* @private
*/
static setTextAlign_(cue, align) {
const Cue = shaka.text.Cue;
if (align == 'middle') {
cue.textAlign = Cue.textAlign.CENTER;
} else {
goog.asserts.assert(align.toUpperCase() in Cue.textAlign,
align.toUpperCase() +
' Should be in Cue.textAlign values!');
cue.textAlign = Cue.textAlign[align.toUpperCase()];
}
}
/**
* @param {!shaka.text.Cue} cue
* @param {string} align
* @private
*/
static setPositionAlign_(cue, align) {
const Cue = shaka.text.Cue;
if (align == 'line-left' || align == 'start') {
cue.positionAlign = Cue.positionAlign.LEFT;
} else if (align == 'line-right' || align == 'end') {
cue.positionAlign = Cue.positionAlign.RIGHT;
} else {
cue.positionAlign = Cue.positionAlign.CENTER;
}
}
/**
* @param {!shaka.text.Cue} cue
* @param {string} value
* @private
*/
static setVerticalWritingMode_(cue, value) {
const Cue = shaka.text.Cue;
if (value == 'lr') {
cue.writingMode = Cue.writingMode.VERTICAL_LEFT_TO_RIGHT;
} else {
cue.writingMode = Cue.writingMode.VERTICAL_RIGHT_TO_LEFT;
}
}
/**
* @param {!shaka.text.Cue} cue
* @param {string} word
* @return {boolean}
* @private
*/
static parsedLineValueAndInterpretation_(cue, word) {
const Cue = shaka.text.Cue;
let results = null;
if ((results = /^line:([\d.]+)%(?:,(start|end|center))?$/.exec(word))) {
cue.lineInterpretation = Cue.lineInterpretation.PERCENTAGE;
cue.line = Number(results[1]);
if (results[2]) {
goog.asserts.assert(
results[2].toUpperCase() in Cue.lineAlign,
results[2].toUpperCase() + ' Should be in Cue.lineAlign values!');
cue.lineAlign = Cue.lineAlign[results[2].toUpperCase()];
}
} else if ((results =
/^line:(-?\d+)(?:,(start|end|center))?$/.exec(word))) {
cue.lineInterpretation = Cue.lineInterpretation.LINE_NUMBER;
cue.line = Number(results[1]);
if (results[2]) {
goog.asserts.assert(
results[2].toUpperCase() in Cue.lineAlign,
results[2].toUpperCase() + ' Should be in Cue.lineAlign values!');
cue.lineAlign = Cue.lineAlign[results[2].toUpperCase()];
}
} else {
return false;
}
return true;
}
/**
* Parses a WebVTT time from the given parser.
*
* @param {!shaka.util.TextParser} parser
* @return {?number}
* @private
*/
static parseTime_(parser) {
// 00:00.000 or 00:00:00.000 or 0:00:00.000 or
// 00:00.00 or 00:00:00.00 or 0:00:00.00
const regexExpresion = /(?:(\d{1,}):)?(\d{2}):(\d{2})\.(\d{2,3})/g;
const results = parser.readRegex(regexExpresion);
if (results == null) {
return null;
}
// This capture is optional, but will still be in the array as undefined,
// in which case it is 0.
const hours = Number(results[1]) || 0;
const minutes = Number(results[2]);
const seconds = Number(results[3]);
const milliseconds = Number(results[4]);
if (minutes > 59 || seconds > 59) {
return null;
}
return (milliseconds / 1000) + seconds + (minutes * 60) + (hours * 3600);
}
};
/**
* @const {number}
* @private
*/
shaka.text.VttTextParser.MPEG_TIMESCALE_ = 90000;
/**
* At this value, timestamps roll over in TS content.
* @const {number}
* @private
*/
shaka.text.VttTextParser.TS_ROLLOVER_ = 0x200000000;
shaka.text.TextEngine.registerParser(
'text/vtt', () => new shaka.text.VttTextParser());
shaka.text.TextEngine.registerParser(
'text/vtt; codecs="vtt"', () => new shaka.text.VttTextParser());
shaka.text.TextEngine.registerParser(
'text/vtt; codecs="wvtt"', () => new shaka.text.VttTextParser());