@scarlett-player/captions 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +195 -0
- package/dist/index.d.cts +73 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +170 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Hackney Enterprises Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createCaptionsPlugin: () => createCaptionsPlugin,
|
|
24
|
+
default: () => index_default
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
function createCaptionsPlugin(config = {}) {
|
|
28
|
+
let api = null;
|
|
29
|
+
let video = null;
|
|
30
|
+
let addedTrackElements = [];
|
|
31
|
+
let hlsSubtitleHandler = null;
|
|
32
|
+
const extractFromHLS = config.extractFromHLS !== false;
|
|
33
|
+
const autoSelect = config.autoSelect ?? false;
|
|
34
|
+
const defaultLanguage = config.defaultLanguage ?? "en";
|
|
35
|
+
const getVideo = () => {
|
|
36
|
+
if (video) return video;
|
|
37
|
+
video = api?.container.querySelector("video") ?? null;
|
|
38
|
+
return video;
|
|
39
|
+
};
|
|
40
|
+
const cleanupTracks = () => {
|
|
41
|
+
for (const trackEl of addedTrackElements) {
|
|
42
|
+
trackEl.parentNode?.removeChild(trackEl);
|
|
43
|
+
}
|
|
44
|
+
addedTrackElements = [];
|
|
45
|
+
api?.setState("textTracks", []);
|
|
46
|
+
api?.setState("currentTextTrack", null);
|
|
47
|
+
};
|
|
48
|
+
const addTrackElement = (source) => {
|
|
49
|
+
const videoEl = getVideo();
|
|
50
|
+
if (!videoEl) throw new Error("No video element");
|
|
51
|
+
const trackEl = document.createElement("track");
|
|
52
|
+
trackEl.kind = source.kind || "subtitles";
|
|
53
|
+
trackEl.label = source.label;
|
|
54
|
+
trackEl.srclang = source.language;
|
|
55
|
+
trackEl.src = source.src;
|
|
56
|
+
trackEl.default = false;
|
|
57
|
+
videoEl.appendChild(trackEl);
|
|
58
|
+
addedTrackElements.push(trackEl);
|
|
59
|
+
if (trackEl.track) {
|
|
60
|
+
trackEl.track.mode = "disabled";
|
|
61
|
+
}
|
|
62
|
+
return trackEl;
|
|
63
|
+
};
|
|
64
|
+
const syncTracksToState = () => {
|
|
65
|
+
const videoEl = getVideo();
|
|
66
|
+
if (!videoEl) return;
|
|
67
|
+
const tracks = [];
|
|
68
|
+
let currentTrack = null;
|
|
69
|
+
for (let i = 0; i < videoEl.textTracks.length; i++) {
|
|
70
|
+
const track = videoEl.textTracks[i];
|
|
71
|
+
if (track.kind !== "subtitles" && track.kind !== "captions") continue;
|
|
72
|
+
const scarlettTrack = {
|
|
73
|
+
id: `track-${i}`,
|
|
74
|
+
label: track.label || `Track ${i + 1}`,
|
|
75
|
+
language: track.language || "",
|
|
76
|
+
kind: track.kind,
|
|
77
|
+
active: track.mode === "showing"
|
|
78
|
+
};
|
|
79
|
+
tracks.push(scarlettTrack);
|
|
80
|
+
if (track.mode === "showing") {
|
|
81
|
+
currentTrack = scarlettTrack;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
api?.setState("textTracks", tracks);
|
|
85
|
+
api?.setState("currentTextTrack", currentTrack);
|
|
86
|
+
};
|
|
87
|
+
const selectTrack = (trackId) => {
|
|
88
|
+
const videoEl = getVideo();
|
|
89
|
+
if (!videoEl) return;
|
|
90
|
+
for (let i = 0; i < videoEl.textTracks.length; i++) {
|
|
91
|
+
const track = videoEl.textTracks[i];
|
|
92
|
+
if (track.kind !== "subtitles" && track.kind !== "captions") continue;
|
|
93
|
+
const id = `track-${i}`;
|
|
94
|
+
if (trackId && id === trackId) {
|
|
95
|
+
track.mode = "showing";
|
|
96
|
+
} else {
|
|
97
|
+
track.mode = "disabled";
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
syncTracksToState();
|
|
101
|
+
};
|
|
102
|
+
const extractHlsSubtitles = () => {
|
|
103
|
+
if (!extractFromHLS || !api) return;
|
|
104
|
+
const hlsPlugin = api.getPlugin("hls-provider");
|
|
105
|
+
if (!hlsPlugin || hlsPlugin.isNativeHLS()) return;
|
|
106
|
+
const hlsInstance = hlsPlugin.getHlsInstance();
|
|
107
|
+
if (!hlsInstance?.subtitleTracks?.length) return;
|
|
108
|
+
api.logger.debug("Extracting HLS subtitle tracks", {
|
|
109
|
+
count: hlsInstance.subtitleTracks.length
|
|
110
|
+
});
|
|
111
|
+
for (const hlsTrack of hlsInstance.subtitleTracks) {
|
|
112
|
+
addTrackElement({
|
|
113
|
+
language: hlsTrack.lang || "unknown",
|
|
114
|
+
label: hlsTrack.name || `Subtitle ${hlsTrack.id}`,
|
|
115
|
+
src: hlsTrack.url,
|
|
116
|
+
kind: "subtitles"
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
syncTracksToState();
|
|
120
|
+
if (autoSelect) {
|
|
121
|
+
autoSelectTrack();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
const autoSelectTrack = () => {
|
|
125
|
+
const tracks = api?.getState("textTracks") || [];
|
|
126
|
+
const match = tracks.find((t) => t.language === defaultLanguage);
|
|
127
|
+
if (match) {
|
|
128
|
+
selectTrack(match.id);
|
|
129
|
+
api?.logger.debug("Auto-selected caption track", { language: defaultLanguage, id: match.id });
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
const initSources = () => {
|
|
133
|
+
if (!config.sources?.length) return;
|
|
134
|
+
for (const source of config.sources) {
|
|
135
|
+
addTrackElement(source);
|
|
136
|
+
}
|
|
137
|
+
syncTracksToState();
|
|
138
|
+
if (autoSelect) {
|
|
139
|
+
autoSelectTrack();
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
return {
|
|
143
|
+
id: "captions",
|
|
144
|
+
name: "Captions",
|
|
145
|
+
version: "1.0.0",
|
|
146
|
+
type: "feature",
|
|
147
|
+
description: "WebVTT subtitles and closed captions with HLS extraction",
|
|
148
|
+
init(pluginApi) {
|
|
149
|
+
api = pluginApi;
|
|
150
|
+
api.logger.debug("Captions plugin initialized");
|
|
151
|
+
api.setState("textTracks", []);
|
|
152
|
+
api.setState("currentTextTrack", null);
|
|
153
|
+
const unsubTrackText = api.on("track:text", ({ trackId }) => {
|
|
154
|
+
selectTrack(trackId);
|
|
155
|
+
});
|
|
156
|
+
const unsubLoaded = api.on("media:loaded", () => {
|
|
157
|
+
video = null;
|
|
158
|
+
cleanupTracks();
|
|
159
|
+
initSources();
|
|
160
|
+
if (extractFromHLS) {
|
|
161
|
+
setTimeout(extractHlsSubtitles, 500);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
const unsubLoadRequest = api.on("media:load-request", () => {
|
|
165
|
+
video = null;
|
|
166
|
+
cleanupTracks();
|
|
167
|
+
});
|
|
168
|
+
api.onDestroy(() => {
|
|
169
|
+
unsubTrackText();
|
|
170
|
+
unsubLoaded();
|
|
171
|
+
unsubLoadRequest();
|
|
172
|
+
cleanupTracks();
|
|
173
|
+
if (hlsSubtitleHandler) {
|
|
174
|
+
const hlsPlugin = api?.getPlugin("hls-provider");
|
|
175
|
+
const hlsInstance = hlsPlugin?.getHlsInstance();
|
|
176
|
+
if (hlsInstance) {
|
|
177
|
+
hlsInstance.off("SUBTITLE_TRACKS_UPDATED", hlsSubtitleHandler);
|
|
178
|
+
}
|
|
179
|
+
hlsSubtitleHandler = null;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
destroy() {
|
|
184
|
+
api?.logger.debug("Captions plugin destroyed");
|
|
185
|
+
cleanupTracks();
|
|
186
|
+
video = null;
|
|
187
|
+
api = null;
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
var index_default = createCaptionsPlugin;
|
|
192
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
193
|
+
0 && (module.exports = {
|
|
194
|
+
createCaptionsPlugin
|
|
195
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Plugin } from '@scarlett-player/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Captions Plugin Types
|
|
5
|
+
*/
|
|
6
|
+
interface CaptionSource {
|
|
7
|
+
/** BCP 47 language code (e.g., 'en', 'es') */
|
|
8
|
+
language: string;
|
|
9
|
+
/** Human-readable label (e.g., 'English', 'Spanish') */
|
|
10
|
+
label: string;
|
|
11
|
+
/** WebVTT file URL */
|
|
12
|
+
src: string;
|
|
13
|
+
/** Track kind — default: 'subtitles' */
|
|
14
|
+
kind?: 'subtitles' | 'captions';
|
|
15
|
+
/** Whether this track should be selected by default */
|
|
16
|
+
default?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface CaptionsPluginConfig {
|
|
19
|
+
/** External caption sources (WebVTT URLs) */
|
|
20
|
+
sources?: CaptionSource[];
|
|
21
|
+
/** Auto-extract subtitle tracks from HLS manifests — default: true */
|
|
22
|
+
extractFromHLS?: boolean;
|
|
23
|
+
/** Auto-select a track on load — default: false */
|
|
24
|
+
autoSelect?: boolean;
|
|
25
|
+
/** Preferred language for auto-select (BCP 47 code) — default: 'en' */
|
|
26
|
+
defaultLanguage?: string;
|
|
27
|
+
/** Index signature for PluginConfig compatibility */
|
|
28
|
+
[key: string]: unknown;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Captions Plugin for Scarlett Player
|
|
33
|
+
*
|
|
34
|
+
* Provides WebVTT subtitle/caption support with:
|
|
35
|
+
* - External WebVTT file loading via <track> elements
|
|
36
|
+
* - HLS.js subtitle track extraction
|
|
37
|
+
* - Browser-native rendering (no custom VTT parser)
|
|
38
|
+
* - State sync with core textTracks/currentTextTrack
|
|
39
|
+
* - Automatic cleanup on source change
|
|
40
|
+
*
|
|
41
|
+
* Works with existing UI controls:
|
|
42
|
+
* - CaptionsButton (toggle on/off)
|
|
43
|
+
* - SettingsMenu captions submenu
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create a Captions Plugin instance.
|
|
48
|
+
*
|
|
49
|
+
* @param config - Plugin configuration
|
|
50
|
+
* @returns Captions Plugin instance
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { createCaptionsPlugin } from '@scarlett-player/captions';
|
|
55
|
+
*
|
|
56
|
+
* const player = new ScarlettPlayer({
|
|
57
|
+
* container: '#player',
|
|
58
|
+
* plugins: [
|
|
59
|
+
* createCaptionsPlugin({
|
|
60
|
+
* sources: [
|
|
61
|
+
* { language: 'en', label: 'English', src: '/subs/en.vtt' },
|
|
62
|
+
* { language: 'es', label: 'Spanish', src: '/subs/es.vtt' },
|
|
63
|
+
* ],
|
|
64
|
+
* autoSelect: true,
|
|
65
|
+
* defaultLanguage: 'en',
|
|
66
|
+
* }),
|
|
67
|
+
* ],
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function createCaptionsPlugin(config?: CaptionsPluginConfig): Plugin;
|
|
72
|
+
|
|
73
|
+
export { type CaptionSource, type CaptionsPluginConfig, createCaptionsPlugin, createCaptionsPlugin as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Plugin } from '@scarlett-player/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Captions Plugin Types
|
|
5
|
+
*/
|
|
6
|
+
interface CaptionSource {
|
|
7
|
+
/** BCP 47 language code (e.g., 'en', 'es') */
|
|
8
|
+
language: string;
|
|
9
|
+
/** Human-readable label (e.g., 'English', 'Spanish') */
|
|
10
|
+
label: string;
|
|
11
|
+
/** WebVTT file URL */
|
|
12
|
+
src: string;
|
|
13
|
+
/** Track kind — default: 'subtitles' */
|
|
14
|
+
kind?: 'subtitles' | 'captions';
|
|
15
|
+
/** Whether this track should be selected by default */
|
|
16
|
+
default?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface CaptionsPluginConfig {
|
|
19
|
+
/** External caption sources (WebVTT URLs) */
|
|
20
|
+
sources?: CaptionSource[];
|
|
21
|
+
/** Auto-extract subtitle tracks from HLS manifests — default: true */
|
|
22
|
+
extractFromHLS?: boolean;
|
|
23
|
+
/** Auto-select a track on load — default: false */
|
|
24
|
+
autoSelect?: boolean;
|
|
25
|
+
/** Preferred language for auto-select (BCP 47 code) — default: 'en' */
|
|
26
|
+
defaultLanguage?: string;
|
|
27
|
+
/** Index signature for PluginConfig compatibility */
|
|
28
|
+
[key: string]: unknown;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Captions Plugin for Scarlett Player
|
|
33
|
+
*
|
|
34
|
+
* Provides WebVTT subtitle/caption support with:
|
|
35
|
+
* - External WebVTT file loading via <track> elements
|
|
36
|
+
* - HLS.js subtitle track extraction
|
|
37
|
+
* - Browser-native rendering (no custom VTT parser)
|
|
38
|
+
* - State sync with core textTracks/currentTextTrack
|
|
39
|
+
* - Automatic cleanup on source change
|
|
40
|
+
*
|
|
41
|
+
* Works with existing UI controls:
|
|
42
|
+
* - CaptionsButton (toggle on/off)
|
|
43
|
+
* - SettingsMenu captions submenu
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create a Captions Plugin instance.
|
|
48
|
+
*
|
|
49
|
+
* @param config - Plugin configuration
|
|
50
|
+
* @returns Captions Plugin instance
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { createCaptionsPlugin } from '@scarlett-player/captions';
|
|
55
|
+
*
|
|
56
|
+
* const player = new ScarlettPlayer({
|
|
57
|
+
* container: '#player',
|
|
58
|
+
* plugins: [
|
|
59
|
+
* createCaptionsPlugin({
|
|
60
|
+
* sources: [
|
|
61
|
+
* { language: 'en', label: 'English', src: '/subs/en.vtt' },
|
|
62
|
+
* { language: 'es', label: 'Spanish', src: '/subs/es.vtt' },
|
|
63
|
+
* ],
|
|
64
|
+
* autoSelect: true,
|
|
65
|
+
* defaultLanguage: 'en',
|
|
66
|
+
* }),
|
|
67
|
+
* ],
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function createCaptionsPlugin(config?: CaptionsPluginConfig): Plugin;
|
|
72
|
+
|
|
73
|
+
export { type CaptionSource, type CaptionsPluginConfig, createCaptionsPlugin, createCaptionsPlugin as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function createCaptionsPlugin(config = {}) {
|
|
3
|
+
let api = null;
|
|
4
|
+
let video = null;
|
|
5
|
+
let addedTrackElements = [];
|
|
6
|
+
let hlsSubtitleHandler = null;
|
|
7
|
+
const extractFromHLS = config.extractFromHLS !== false;
|
|
8
|
+
const autoSelect = config.autoSelect ?? false;
|
|
9
|
+
const defaultLanguage = config.defaultLanguage ?? "en";
|
|
10
|
+
const getVideo = () => {
|
|
11
|
+
if (video) return video;
|
|
12
|
+
video = api?.container.querySelector("video") ?? null;
|
|
13
|
+
return video;
|
|
14
|
+
};
|
|
15
|
+
const cleanupTracks = () => {
|
|
16
|
+
for (const trackEl of addedTrackElements) {
|
|
17
|
+
trackEl.parentNode?.removeChild(trackEl);
|
|
18
|
+
}
|
|
19
|
+
addedTrackElements = [];
|
|
20
|
+
api?.setState("textTracks", []);
|
|
21
|
+
api?.setState("currentTextTrack", null);
|
|
22
|
+
};
|
|
23
|
+
const addTrackElement = (source) => {
|
|
24
|
+
const videoEl = getVideo();
|
|
25
|
+
if (!videoEl) throw new Error("No video element");
|
|
26
|
+
const trackEl = document.createElement("track");
|
|
27
|
+
trackEl.kind = source.kind || "subtitles";
|
|
28
|
+
trackEl.label = source.label;
|
|
29
|
+
trackEl.srclang = source.language;
|
|
30
|
+
trackEl.src = source.src;
|
|
31
|
+
trackEl.default = false;
|
|
32
|
+
videoEl.appendChild(trackEl);
|
|
33
|
+
addedTrackElements.push(trackEl);
|
|
34
|
+
if (trackEl.track) {
|
|
35
|
+
trackEl.track.mode = "disabled";
|
|
36
|
+
}
|
|
37
|
+
return trackEl;
|
|
38
|
+
};
|
|
39
|
+
const syncTracksToState = () => {
|
|
40
|
+
const videoEl = getVideo();
|
|
41
|
+
if (!videoEl) return;
|
|
42
|
+
const tracks = [];
|
|
43
|
+
let currentTrack = null;
|
|
44
|
+
for (let i = 0; i < videoEl.textTracks.length; i++) {
|
|
45
|
+
const track = videoEl.textTracks[i];
|
|
46
|
+
if (track.kind !== "subtitles" && track.kind !== "captions") continue;
|
|
47
|
+
const scarlettTrack = {
|
|
48
|
+
id: `track-${i}`,
|
|
49
|
+
label: track.label || `Track ${i + 1}`,
|
|
50
|
+
language: track.language || "",
|
|
51
|
+
kind: track.kind,
|
|
52
|
+
active: track.mode === "showing"
|
|
53
|
+
};
|
|
54
|
+
tracks.push(scarlettTrack);
|
|
55
|
+
if (track.mode === "showing") {
|
|
56
|
+
currentTrack = scarlettTrack;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
api?.setState("textTracks", tracks);
|
|
60
|
+
api?.setState("currentTextTrack", currentTrack);
|
|
61
|
+
};
|
|
62
|
+
const selectTrack = (trackId) => {
|
|
63
|
+
const videoEl = getVideo();
|
|
64
|
+
if (!videoEl) return;
|
|
65
|
+
for (let i = 0; i < videoEl.textTracks.length; i++) {
|
|
66
|
+
const track = videoEl.textTracks[i];
|
|
67
|
+
if (track.kind !== "subtitles" && track.kind !== "captions") continue;
|
|
68
|
+
const id = `track-${i}`;
|
|
69
|
+
if (trackId && id === trackId) {
|
|
70
|
+
track.mode = "showing";
|
|
71
|
+
} else {
|
|
72
|
+
track.mode = "disabled";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
syncTracksToState();
|
|
76
|
+
};
|
|
77
|
+
const extractHlsSubtitles = () => {
|
|
78
|
+
if (!extractFromHLS || !api) return;
|
|
79
|
+
const hlsPlugin = api.getPlugin("hls-provider");
|
|
80
|
+
if (!hlsPlugin || hlsPlugin.isNativeHLS()) return;
|
|
81
|
+
const hlsInstance = hlsPlugin.getHlsInstance();
|
|
82
|
+
if (!hlsInstance?.subtitleTracks?.length) return;
|
|
83
|
+
api.logger.debug("Extracting HLS subtitle tracks", {
|
|
84
|
+
count: hlsInstance.subtitleTracks.length
|
|
85
|
+
});
|
|
86
|
+
for (const hlsTrack of hlsInstance.subtitleTracks) {
|
|
87
|
+
addTrackElement({
|
|
88
|
+
language: hlsTrack.lang || "unknown",
|
|
89
|
+
label: hlsTrack.name || `Subtitle ${hlsTrack.id}`,
|
|
90
|
+
src: hlsTrack.url,
|
|
91
|
+
kind: "subtitles"
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
syncTracksToState();
|
|
95
|
+
if (autoSelect) {
|
|
96
|
+
autoSelectTrack();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const autoSelectTrack = () => {
|
|
100
|
+
const tracks = api?.getState("textTracks") || [];
|
|
101
|
+
const match = tracks.find((t) => t.language === defaultLanguage);
|
|
102
|
+
if (match) {
|
|
103
|
+
selectTrack(match.id);
|
|
104
|
+
api?.logger.debug("Auto-selected caption track", { language: defaultLanguage, id: match.id });
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const initSources = () => {
|
|
108
|
+
if (!config.sources?.length) return;
|
|
109
|
+
for (const source of config.sources) {
|
|
110
|
+
addTrackElement(source);
|
|
111
|
+
}
|
|
112
|
+
syncTracksToState();
|
|
113
|
+
if (autoSelect) {
|
|
114
|
+
autoSelectTrack();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
id: "captions",
|
|
119
|
+
name: "Captions",
|
|
120
|
+
version: "1.0.0",
|
|
121
|
+
type: "feature",
|
|
122
|
+
description: "WebVTT subtitles and closed captions with HLS extraction",
|
|
123
|
+
init(pluginApi) {
|
|
124
|
+
api = pluginApi;
|
|
125
|
+
api.logger.debug("Captions plugin initialized");
|
|
126
|
+
api.setState("textTracks", []);
|
|
127
|
+
api.setState("currentTextTrack", null);
|
|
128
|
+
const unsubTrackText = api.on("track:text", ({ trackId }) => {
|
|
129
|
+
selectTrack(trackId);
|
|
130
|
+
});
|
|
131
|
+
const unsubLoaded = api.on("media:loaded", () => {
|
|
132
|
+
video = null;
|
|
133
|
+
cleanupTracks();
|
|
134
|
+
initSources();
|
|
135
|
+
if (extractFromHLS) {
|
|
136
|
+
setTimeout(extractHlsSubtitles, 500);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
const unsubLoadRequest = api.on("media:load-request", () => {
|
|
140
|
+
video = null;
|
|
141
|
+
cleanupTracks();
|
|
142
|
+
});
|
|
143
|
+
api.onDestroy(() => {
|
|
144
|
+
unsubTrackText();
|
|
145
|
+
unsubLoaded();
|
|
146
|
+
unsubLoadRequest();
|
|
147
|
+
cleanupTracks();
|
|
148
|
+
if (hlsSubtitleHandler) {
|
|
149
|
+
const hlsPlugin = api?.getPlugin("hls-provider");
|
|
150
|
+
const hlsInstance = hlsPlugin?.getHlsInstance();
|
|
151
|
+
if (hlsInstance) {
|
|
152
|
+
hlsInstance.off("SUBTITLE_TRACKS_UPDATED", hlsSubtitleHandler);
|
|
153
|
+
}
|
|
154
|
+
hlsSubtitleHandler = null;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
destroy() {
|
|
159
|
+
api?.logger.debug("Captions plugin destroyed");
|
|
160
|
+
cleanupTracks();
|
|
161
|
+
video = null;
|
|
162
|
+
api = null;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
var index_default = createCaptionsPlugin;
|
|
167
|
+
export {
|
|
168
|
+
createCaptionsPlugin,
|
|
169
|
+
index_default as default
|
|
170
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@scarlett-player/captions",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Captions Plugin for Scarlett Player - WebVTT subtitles and closed captions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@scarlett-player/core": "^1.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@vitest/coverage-v8": "^1.6.0",
|
|
29
|
+
"jsdom": "^24.0.0",
|
|
30
|
+
"tsup": "^8.5.1",
|
|
31
|
+
"typescript": "^5.3.0",
|
|
32
|
+
"vitest": "^1.6.0",
|
|
33
|
+
"@scarlett-player/core": "1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"video",
|
|
37
|
+
"player",
|
|
38
|
+
"captions",
|
|
39
|
+
"subtitles",
|
|
40
|
+
"webvtt",
|
|
41
|
+
"closed-captions",
|
|
42
|
+
"scarlett"
|
|
43
|
+
],
|
|
44
|
+
"author": "The Stream Platform",
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/Hackney-Enterprises-Inc/scarlett-player.git",
|
|
49
|
+
"directory": "packages/plugins/captions"
|
|
50
|
+
},
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/Hackney-Enterprises-Inc/scarlett-player/issues"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://scarlettplayer.com",
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
57
|
+
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
58
|
+
"test": "vitest --run",
|
|
59
|
+
"test:watch": "vitest",
|
|
60
|
+
"test:coverage": "vitest --coverage"
|
|
61
|
+
}
|
|
62
|
+
}
|