Files
shaka-player/lib/text/web_vtt_generator.js
T
Joey Parrish ac5acc80cb feat!: Remove deprecated features, update upgrade guides (#4089)
Below are the changelog entries for each deprecated feature removed by this commit.

-----

feat(config)!: `manifest.dash.defaultPresentationDelay` has been replaced by `manifest.defaultPresentationDelay` (deprecated in v3.0.0)

feat(config)!: Configuration of factories should be plain factory functions, not constructors; these will not be invoked with `new` (deprecated in v3.1.0)

feat(player)!: `shaka.Player.prototype.addTextTrack()` has been replaced by `addTextTrackAsync()`, which returns a `Promise` (deprecated in v3.1.0)

feat(ui)!: `shaka.ui.TrackLabelFormat` has been renamed to `shaka.ui.Overlay.TrackLabelFormat` (deprecated in v3.1.0)

feat(ui)!: `shaka.ui.FailReasonCode` has been renamed to `shaka.ui.Overlay.FailReasonCode` (deprecated in v3.1.0)

feat(offline)!: `shaka.offline.Storage.prototype.store()` returns `AbortableOperation` instead of `Promise` (deprecated in v3.0.0)

feat(offline)!: `shaka.offline.Storage.prototype.getStoreInProgress()` has been removed; concurrent operations are supported, so callers don't need to check this (deprecated in v3.0.0)

feat!: `shaka.util.Uint8ArrayUtils.equal` has been replaced by `shaka.util.BufferUtils.equal`, which can handle multiple types of buffers (deprecated in v3.0.0)

feat(manifest)!: `shaka.media.SegmentIndex.prototype.destroy()` has been replaced by `release()`, which is synchronous (deprecated in v3.0.0)

feat(manifest)!: `shaka.media.SegmentIterator.prototype.seek()`, which mutates the iterator, has been replaced by `shaka.media.SegmentIndex.getIteratorForTime()` (deprecated in v3.1.0)

feat(manifest)!: `shaka.media.SegmentIndex.prototype.merge()` has become private; use `mergeAndEvict()` instead (deprecated in v3.2.0)

feat(plugin)!: `AbrManager` plugins must implement the `playbackRateChanged()` method (deprecated in v3.0.0)

feat(plugin)!: `shaka.extern.Cue.prototype.spacer` has been replaced by the more clearly-named `lineBreak` (deprecated in v3.1.0)

feat(plugin)!: `IUIElement` plugins must have a `release()` method (not `destroy()`) (deprecated in v3.0.0)
2022-04-11 17:11:40 -07:00

138 lines
4.3 KiB
JavaScript

/*! @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.
const flattenedCues = cues.map((cue) => {
if (cue.nestedCues.length) {
const flatCue = cue.clone();
flatCue.nestedCues = [];
flatCue.payload = flattenPayload(cue);
return flatCue;
} else {
return cue;
}
});
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;
}
};