TTML, SMPTE-TT, and IMSC1¶
The TTML module adds support for Timed Text Markup Language (TTML) and related profiles, including SMPTE Timed Text (SMPTE-TT) and Internet Media Subtitles and Captions 1 (IMSC1) text or image subtitles. Import the module before creating the player:
import "@castlabs/prestoplay/cl.ttml";
Supported features¶
Supported attributes:
tt:begintt:endtt:regiontt:stylettp:cellResolutionttp:frameRatettp:subFrameRatettp:frameRateMultiplierttp:tickRatetts:backgroundColortts:colortts:directiontts:displayAligntts:origintts:extenttts:fontFamilytts:fontSizetts:fontWeighttts:fontStyletts:lineHeighttts:opacitytts:paddingtts:textAligntts:textOutlinetts:textShadowtts:unicodeBiditts:writingModetts:zIndexsmpte:backgroundImage
SMPTE-TT and IMSC1 support covers bitmap images, such as PNG images encoded in Base64. Position the bitmap images by enclosing them in a region.
Support for legacy manifests¶
A well-designed TTML manifest defines a root container and vertical cell definitions, which let the layout adjust to different sizes, such as fullscreen mode. Some legacy manifests use pixels instead to define region or font size, for example:
<?xml version="1.0" encoding="utf-8"?>
<tt
xmlns="http://www.w3.org/ns/ttml"
xmlns:tts="http://www.w3.org/ns/ttml#styling" xml:lang="en">
<head>
<styling>
<style xml:id="s1"
tts:fontSize="16px"
tts:textAlign="center"/>
</styling>
<layout>
<region xml:id="r1"
style="s1"
tts:displayAlign="center"
tts:extent="512px 48px"
tts:origin="64px 384px"/>
</layout>
</head>
<body>
<div xml:lang="en">
<p begin="00:00:00.000000" end="00:00:10.000000" region="r1">
Lorem ipsum
</p>
</div>
</body>
</tt>
For manifests like this, provide hints to the parser:
const playerConfig = {
// ...
ttml: {
containerWidth: 640,
containerHeight: 480,
fontSizeRadix: 16,
},
};
containerWidth and containerHeight convert a region’s size and position
into percentages of the container. In this example, the r1 region’s width
becomes 80% and its height becomes 10%; its x-position becomes 10% and
its y-position becomes 80%, so the region is centered horizontally near the
bottom of the container.
fontSizeRadix converts a font size into em. In this example, the cue font
size becomes 1em. The HTML displayer then adjusts the parent font size
based on the video size. See TextStyleConfiguration for details. Cues sized this way are responsive.
SMPTE-TT over HLS¶
HTTP Live Streaming (HLS) doesn’t support SMPTE-TT natively. Harmonic proposed an extension to the HLS specification to work around this, described in SMPTE-TT Embedded in ID3 for HTTP Live Streaming. The HlsSmpte plugin implements this extension. It extends the TTML parser and works exclusively on Safari with native HLS playback — it has no effect on the MSE-based player.
To use it:
Import the TTML module:
import "@castlabs/prestoplay/cl.ttml";
Import the HLS-SMPTE module:
import "@castlabs/prestoplay/cl.hlssmpte";
Enable the
config.hlssmpte.patchMetadataTrackflag if recent Safari versions fail to create the metadata track on load. As a workaround, the plugin briefly switches to another audio track and back to force Safari to create it. This flag is opt-in rather than default because the workaround requires at least two audio tracks and causes an audible track switch.Adjust
config.hlssmpte.timestampOffsetif cues are time-shifted.Provide placeholder text tracks using
config.remoteTextTracks. Each placeholder must definekind,language,mimeType, and an optionallabel. Omiturl— the HlsSmpte plugin injects cues from the metadata track. The language code must match the metadata track’s language codes, and the MIME type must betext/vtt.
In the example below, url is omitted from each placeholder because cues
are delivered through the metadata track; the placeholders are still needed
to expose the language options.
<link rel="stylesheet" href="./clpp.styles.css">
<video id="video" width="640" height="360" controls crossorigin="anonymous"></video>
<script type="module">
import { clpp } from "@castlabs/prestoplay";
import "@castlabs/prestoplay/cl.ttml";
import "@castlabs/prestoplay/cl.htmlcue";
import "@castlabs/prestoplay/cl.hlssmpte";
const player = new clpp.Player('video', { });
player.open({
source: '...',
hlssmpte: {
patchMetadataTrack: true
},
htmlcue: {
stretchSmpteImage: true
},
remoteTextTracks: [
{
kind: 'subtitles',
language: 'ara',
mimeType: 'text/vtt',
label: 'Arabic'
},
{
kind: 'subtitles',
language: 'eng',
mimeType: 'text/vtt',
label: 'English'
}
]
});
</script>