The subtitle state changed.
When a new file starts, the OSD names the language of its primary
subtitle. The SDK reads the subtitle state again when the source or its
phase changes (there is no subtitle event, so a selection changed in the
player shows only after refresh()) and then calls the listener;
selected is null when subtitles are off.
plugin.subtitles.onChange((state) => {
// A track's language is null when the file does not say.
void plugin.osd.show("Subtitles: {}", [state.selected?.language ?? "off"]);
});
Read the state from the player now.
The plugin reads how many subtitle tracks the file has before building a
menu. refresh asks the player and resolves with the current state;
state holds it afterwards.
const { tracks } = await plugin.subtitles.refresh();
await plugin.osd.show("{} subtitle tracks", [tracks.length]);
role: "secondary" sets the secondary subtitle: a text track that is not
the primary one (invalid otherwise). A new source clears it.
track: null turns the role off.
Optionaloptions: SubtitleConditional & { role?: "primary" | "secondary" }stale (rejects) With ifCurrent: the source or the subtitle
selection changed since that state.
invalid (rejects) An unknown track; as secondary, a bitmap
track or the primary one; as primary, the current secondary.
The first English track becomes the primary subtitle, but only in the
file the plugin looked at. Embedded tracks are chosen by number, so
without ifCurrent a select after another file opened would pick that
file's track with the same number; with ifCurrent: state it fails with
stale instead. Without ifCurrent, select reads the newest state
itself and almost never fails with stale.
const state = await plugin.subtitles.refresh();
const english = state.tracks.find((track) => track.language === "en");
if (english)
await plugin.subtitles.select(english, { ifCurrent: state }).catch((error: PluginError) => {
if (error.code !== "stale") throw error; // another file or selection now
});
Upload a subtitle file's bytes (UTF-8 SRT, or ASS/SSA with format: "ass")
for the current source; select it unless select: "none". title (1–200
characters) names the track: data, or a Text the player translates
(text("{} · {}", [text("Simplified Chinese"), file])). Aborting signal
before the commit discards the upload (rejects with AbortError); once the
commit was sent it completes.
Limits: SRT ≤ 2 MiB and ≤ 10,000 cues; ASS ≤ 2 MiB with [Script Info]
and [Events] and 1–20,000 Dialogue: lines. Convert other formats
(VTT, SAMI) to SRT first. Per source, a plugin has at most 4 loaded
tracks at once and loads at most 16 files (removed ones count), and
every plugin together at most 16 tracks; see SUBTITLE_LIMITS in
@vestra-player/sdk/testing.
Optionaloptions: SubtitleConditional & {busy (rejects) Too many subtitle operations at once, or a
limit of SUBTITLE_LIMITS is reached (4 tracks of this plugin, 16
loads of this plugin or 16 tracks in the source, or another of its
limits); nothing is loaded.
A subtitle file the plugin downloaded becomes a track of the current file
and is selected. The plugin passes the file's bytes; the player parses
them, adds an external track for this source only and resolves with
it once the upload is committed.
const response = await fetch(url);
if (!response.ok) throw new Error(`subtitles: HTTP ${response.status}`);
const bytes = new Uint8Array(await response.arrayBuffer());
const track = await plugin.subtitles.load(bytes, { title: "Downloaded" }); // selected
await plugin.osd.show("Loaded {}", [track.title]);
Remove a track this plugin loaded.
Optionaloptions: SubtitleConditionalSet this plugin's style layer; null removes it. The newest call of
this plugin replaces its earlier layer. When several plugins have a
layer, the player uses the one whose manifest id sorts last, whatever
the order of the calls.
Optionaloptions: SubtitleConditionalunsupported (rejects) A font that is not in state.fonts, or a
primary subtitle that is a bitmap track.
The plugin removes its style layer, so the subtitles go back to the
player's default (or to another plugin's layer). null removes only this
plugin's layer; the effective layer is the one whose manifest id sorts
last among the plugins that still have one.
await plugin.subtitles.setStyle(null);
plugin.subtitles: tracks, uploads and style. Changes read the current selection first.Example: Load an SRT file as the secondary subtitle
An English SRT file shows as a second subtitle line next to the film's own subtitles, in white with an outline. The plugin reads the file and uploads its bytes with
load, which adds a track to the current source and selects it as the secondary subtitle;refreshreads the subtitle state for its font list, andsetStylesends this plugin's style layer naming one of those fonts.Remarks
The subtitle tracks of the current source, the primary and secondary selection, uploaded files and the style.
stateis the SDK's snapshot of the player's subtitle state (nulluntil first read). While anonChangelistener exists the SDK reads the state again when the source, its phase or the backend changes; it takes eachsubtitles.changeda Player.on listener hears; and it reads it after this plugin's own changes. Otherwise a selection the user or another plugin changed shows only afterrefresh(). A change withoutifCurrentreads the state first and applies to it; passifCurrentto make it fail withstalewhen the selection changed since that state. Each change is one operation the player confirms;select,removeandsetStyleresolve with the new state,loadwith the new track.See
Load subtitles from a plugin
Since
1.0.0