forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_vtt_generator.js
149 lines (139 loc) · 4.96 KB
/
web_vtt_generator.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.text.WebVttGenerator');
goog.require('shaka.text.Cue');
/**
* @summary Manage the conversion to WebVTT.
* @export
*/
shaka.text.WebVttGenerator = class {
/**
* @param {!Array.<!shaka.text.Cue>} cues
* @param {!Array.<!shaka.extern.AdCuePoint>} adCuePoints
* @return {string}
*/
static convert(cues, adCuePoints) {
// Flatten nested cue payloads recursively. If a cue has nested cues,
// their contents should be combined and replace the payload of the parent.
const flattenPayload = (cue) => {
// Handle styles (currently bold/italics/underline).
// TODO: add support for color rendering.
const openStyleTags = [];
const bold = cue.fontWeight >= shaka.text.Cue.fontWeight.BOLD;
const italics = cue.fontStyle == shaka.text.Cue.fontStyle.ITALIC;
const underline = cue.textDecoration.includes(
shaka.text.Cue.textDecoration.UNDERLINE);
if (bold) {
openStyleTags.push('b');
}
if (italics) {
openStyleTags.push('i');
}
if (underline) {
openStyleTags.push('u');
}
// Prefix opens tags, suffix closes tags in reverse order of opening.
const prefixStyleTags = openStyleTags.reduce((acc, tag) => {
return `${acc}<${tag}>`;
}, '');
const suffixStyleTags = openStyleTags.reduceRight((acc, tag) => {
return `${acc}</${tag}>`;
}, '');
if (cue.lineBreak) {
// This is a vertical lineBreak, so insert a newline.
return '\n';
} else if (cue.nestedCues.length) {
return cue.nestedCues.map(flattenPayload).join('');
} else {
// This is a real cue.
return prefixStyleTags + cue.payload + suffixStyleTags;
}
};
const webvttTimeString = (time) => {
let newTime = time;
for (const adCuePoint of adCuePoints) {
if (adCuePoint.end && adCuePoint.start < time) {
const offset = adCuePoint.end - adCuePoint.start;
newTime += offset;
}
}
const hours = Math.floor(newTime / 3600);
const minutes = Math.floor(newTime / 60 % 60);
const seconds = Math.floor(newTime % 60);
const milliseconds = Math.floor(newTime * 1000 % 1000);
return (hours < 10 ? '0' : '') + hours + ':' +
(minutes < 10 ? '0' : '') + minutes + ':' +
(seconds < 10 ? '0' : '') + seconds + '.' +
(milliseconds < 100 ? (milliseconds < 10 ? '00' : '0') : '') +
milliseconds;
};
// We don't want to modify the array or objects passed in, since we don't
// technically own them. So we build a new array and replace certain items
// in it if they need to be flattened.
// We also don't want to flatten the text payloads starting at a container
// element; otherwise, for containers encapsulating multiple caption lines,
// the lines would merge into a single cue. This is undesirable when a
// subset of the captions are outside of the append time window. To fix
// this, we only call flattenPayload() starting at elements marked as
// isContainer = false.
const getCuesToFlatten = (cues, result) => {
for (const cue of cues) {
if (cue.isContainer) {
// Recurse to find the actual text payload cues.
getCuesToFlatten(cue.nestedCues, result);
} else {
// Flatten the payload.
const flatCue = cue.clone();
flatCue.nestedCues = [];
flatCue.payload = flattenPayload(cue);
result.push(flatCue);
}
}
return result;
};
const flattenedCues = getCuesToFlatten(cues, []);
let webvttString = 'WEBVTT\n\n';
for (const cue of flattenedCues) {
const webvttSettings = (cue) => {
const settings = [];
const Cue = shaka.text.Cue;
switch (cue.textAlign) {
case Cue.textAlign.LEFT:
settings.push('align:left');
break;
case Cue.textAlign.RIGHT:
settings.push('align:right');
break;
case Cue.textAlign.CENTER:
settings.push('align:middle');
break;
case Cue.textAlign.START:
settings.push('align:start');
break;
case Cue.textAlign.END:
settings.push('align:end');
break;
}
switch (cue.writingMode) {
case Cue.writingMode.VERTICAL_LEFT_TO_RIGHT:
settings.push('vertical:lr');
break;
case Cue.writingMode.VERTICAL_RIGHT_TO_LEFT:
settings.push('vertical:rl');
break;
}
if (settings.length) {
return ' ' + settings.join(' ');
}
return '';
};
webvttString += webvttTimeString(cue.startTime) + ' --> ' +
webvttTimeString(cue.endTime) + webvttSettings(cue) + '\n';
webvttString += cue.payload + '\n\n';
}
return webvttString;
}
};