@vestra-player/sdk - v1.11.3
    Preparing search index...

    Interface Subtitles

    plugin.subtitles: tracks, uploads and style. Changes read the current selection first.

    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; refresh reads the subtitle state for its font list, and setStyle sends this plugin's style layer naming one of those fonts.

    const bytes = await readFile(path);
    await plugin.subtitles.load(bytes, { title: "English", select: "secondary" });
    const { fonts } = await plugin.subtitles.refresh();
    if (!fonts.length) return;
    await plugin.subtitles.setStyle({
    fontId: fonts[0]!.id,
    fontSize: 40, // at the 1080p reference height
    color: "white",
    outline: 2,
    marginPercent: 5,
    offsetMs: 0,
    });

    The subtitle tracks of the current source, the primary and secondary selection, uploaded files and the style. state is the SDK's snapshot of the player's subtitle state (null until first read). While an onChange listener exists the SDK reads the state again when the source, its phase or the backend changes; it takes each subtitles.changed a 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 after refresh(). A change without ifCurrent reads the state first and applies to it; pass ifCurrent to make it fail with stale when the selection changed since that state. Each change is one operation the player confirms; select, remove and setStyle resolve with the new state, load with the new track.

    1.0.0

    interface Subtitles {
        state: SubtitlesState | null;
        onChange(listener: (state: SubtitlesState) => void): Disposable;
        refresh(): Promise<SubtitlesState>;
        select(
            track: SubtitleTrack | null,
            options?: SubtitleConditional & { role?: "primary" | "secondary" },
        ): Promise<SubtitlesState>;
        load(
            bytes: Uint8Array,
            options?: SubtitleConditional & {
                format?: "srt" | "ass";
                title?: string | Text;
                select?: "primary" | "secondary" | "none";
                signal?: AbortSignal;
            },
        ): Promise<SubtitleTrack>;
        remove(
            track: SubtitleTrack,
            options?: SubtitleConditional,
        ): Promise<SubtitlesState>;
        setStyle(
            style: SubtitleStyle | null,
            options?: SubtitleConditional,
        ): Promise<SubtitlesState>;
    }
    Index
    • The subtitle state changed.

      Parameters

      Returns Disposable

      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"]);
      });

      1.0.0

    • Read the state from the player now.

      Returns Promise<SubtitlesState>

      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]);

      disabled (rejects) The plugin is stopping.

      1.0.0

    • 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.

      Parameters

      Returns Promise<SubtitlesState>

      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.

      missing (rejects) The track is not in the current source.

      unavailable (rejects) Nothing loaded, or the source is still loading or not yet confirmed.

      busy (rejects) Too many subtitle operations at once.

      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
      });

      1.0.0

    • 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.

      Parameters

      Returns Promise<SubtitleTrack>

      invalid (rejects) A bad format or title, or a file over the limits.

      stale (rejects) With ifCurrent: the source changed since that state.

      unavailable (rejects) Nothing loaded, or the source is still loading.

      aborted (rejects) signal aborted before the commit (an AbortError); the upload is discarded.

      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]);

      1.0.0

    • Remove a track this plugin loaded.

      Parameters

      Returns Promise<SubtitlesState>

      invalid (rejects) The track was not loaded by this plugin.

      stale (rejects) With ifCurrent: the source changed since that state.

      unavailable (rejects) Nothing is loaded, or the source is still loading.

      busy (rejects) Too many subtitle operations at once.

      The plugin's downloaded subtitle disappears from the track list. A plugin can remove only tracks it loaded itself; a new source removes them anyway.

      await plugin.subtitles.remove(loaded);
      

      1.0.0

    • Set 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.

      Parameters

      Returns Promise<SubtitlesState>

      invalid (rejects) A field out of range.

      unsupported (rejects) A font that is not in state.fonts, or a primary subtitle that is a bitmap track.

      stale (rejects) With ifCurrent: the subtitle state changed since that state.

      unavailable (rejects) Nothing is loaded, or the source is still loading.

      busy (rejects) Too many subtitle operations at once.

      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);
      

      1.0.0

    state: SubtitlesState | null

    null until first read.