@banou/media-player 0.10.35 → 0.10.36
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/README.md +18 -7
- package/dist/index.js +603 -599
- package/dist/language/index.d.ts +39 -5
- package/dist/language/index.js +53 -18
- package/dist/react/video-player.d.ts +18 -2
- package/package.json +1 -1
- package/src/lib/language/index.ts +94 -13
- package/src/lib/react/hooks/use-playback.ts +20 -19
- package/src/lib/react/video-player.tsx +26 -7
package/dist/language/index.d.ts
CHANGED
|
@@ -22,10 +22,44 @@ export declare const languageTag: (text: string | undefined) => string | undefin
|
|
|
22
22
|
* the first.
|
|
23
23
|
*/
|
|
24
24
|
export declare const preferredTrack: (preference: string, tracks: readonly LanguageTrack[]) => number;
|
|
25
|
+
/** The viewer's preferences, as `startingTracks` reads them. Each may be left out. */
|
|
26
|
+
export type TrackPreferences = {
|
|
27
|
+
/**
|
|
28
|
+
* The audio language, as `preferredTrack` reads it. A media without a full track in it, or with none given, plays its
|
|
29
|
+
* original: a commentary or described audio in it is no dub.
|
|
30
|
+
*/
|
|
31
|
+
audio?: string;
|
|
32
|
+
/**
|
|
33
|
+
* The language the media was made in, as its source knows it (`ja` for anime): its full track is the original.
|
|
34
|
+
* Without it, or in a file with no track in it, the original is the file's default.
|
|
35
|
+
*/
|
|
36
|
+
original?: string;
|
|
37
|
+
/** The subtitle language, as `preferredTrack` reads it, or null for none. A media without it shows its default. */
|
|
38
|
+
subtitles?: string | null;
|
|
39
|
+
/** False starts subtitles off when the audio plays in their language. True when left out. */
|
|
40
|
+
alwaysSubtitles?: boolean;
|
|
41
|
+
};
|
|
42
|
+
/** Whether `track` plays in the language `language` names, as a tag or a name, read by the track's tag, else its title. */
|
|
43
|
+
export declare const playsIn: (track: LanguageTrack | undefined, language: string) => boolean;
|
|
44
|
+
/**
|
|
45
|
+
* The tracks a media starts on, by index: the full audio in `preferences.audio`, else the original, else -1 for the
|
|
46
|
+
* file's default, its first track; and the subtitles in `preferences.subtitles`, else -1 for the file's default, or null
|
|
47
|
+
* for none, when they are off or when `alwaysSubtitles` is false and the audio plays in their language.
|
|
48
|
+
*/
|
|
49
|
+
export declare const startingTracks: (audio: readonly LanguageTrack[], subtitles: readonly LanguageTrack[], { audio: audioLanguage, original, subtitles: subtitleLanguage, alwaysSubtitles }: TrackPreferences) => {
|
|
50
|
+
audio: number;
|
|
51
|
+
subtitles: number | null;
|
|
52
|
+
};
|
|
53
|
+
type Selection = Omit<DelegatedSelection, 'select'>;
|
|
25
54
|
/**
|
|
26
|
-
* For a media the player does not own, whose host applies the viewer's
|
|
27
|
-
* to select
|
|
28
|
-
*
|
|
29
|
-
*
|
|
55
|
+
* For a media the player does not own, whose host applies the viewer's preferences itself: the option of
|
|
56
|
+
* `selections[kind]` to select as `startingTracks` starts it, null for the row that turns subtitles off, or undefined
|
|
57
|
+
* to leave the selection as it is: the source's own default, or already selected. The audio selected stands for the
|
|
58
|
+
* default, and the subtitles are chosen for it, so a host switching both asks for the subtitles once the audio has
|
|
59
|
+
* switched. Options are matched by their labels; disabled ones are skipped.
|
|
30
60
|
*/
|
|
31
|
-
export declare const preferredSelection: (
|
|
61
|
+
export declare const preferredSelection: (kind: 'audio' | 'subtitles', selections: {
|
|
62
|
+
audio?: Selection;
|
|
63
|
+
subtitles?: Selection;
|
|
64
|
+
}, preferences: TrackPreferences) => string | null | undefined;
|
|
65
|
+
export {};
|
package/dist/language/index.js
CHANGED
|
@@ -46,37 +46,72 @@ var e = "aa ab ae af ak am an ar as av ay az ba be bg bi bm bn bo br bs ca ce ch
|
|
|
46
46
|
for (let e of t) for (let t of [...r, e]) for (let n of ["dialect", "standard"]) i(e, t, n);
|
|
47
47
|
for (let t of e) for (let e of [...r, t]) i(t, e, "dialect");
|
|
48
48
|
return l = n;
|
|
49
|
-
}, d = (
|
|
49
|
+
}, d, f = () => {
|
|
50
|
+
if (d) return d;
|
|
51
|
+
let e = /* @__PURE__ */ new Map(), n = [
|
|
52
|
+
"en",
|
|
53
|
+
globalThis.navigator?.language,
|
|
54
|
+
...t
|
|
55
|
+
].filter((e) => !!e);
|
|
56
|
+
for (let r of new Set(t.map((e) => new Intl.Locale(e).region).filter((e) => !!e))) for (let t of n) try {
|
|
57
|
+
let n = new Intl.DisplayNames([t], {
|
|
58
|
+
type: "region",
|
|
59
|
+
fallback: "none"
|
|
60
|
+
}).of(r);
|
|
61
|
+
n && e.set(o(n), r);
|
|
62
|
+
} catch {}
|
|
63
|
+
return d = e;
|
|
64
|
+
}, p = (e) => {
|
|
50
65
|
let t = e?.trim();
|
|
51
66
|
if (!t) return;
|
|
52
67
|
let n = u(), r = n.get(o(t));
|
|
53
68
|
if (r) return c(r);
|
|
54
69
|
let [, i, s] = /^([^([]+)(?:\(([^)]*)\))?/.exec(t.normalize("NFKC")) ?? [], l = i ? n.get(o(i)) : void 0;
|
|
55
70
|
if (!l) return;
|
|
56
|
-
let d = s?.trim();
|
|
57
|
-
return (
|
|
58
|
-
},
|
|
71
|
+
let d = s?.trim(), p = !d || a.has(d) ? void 0 : /^(?:[A-Z]{2}|\d{3})$/.test(d) ? d : f().get(o(d));
|
|
72
|
+
return (p ? c(`${l}-${p}`) : void 0) ?? c(l);
|
|
73
|
+
}, m = (e) => {
|
|
59
74
|
let t = e?.trim();
|
|
60
|
-
return t ? c(t) ??
|
|
61
|
-
},
|
|
62
|
-
let t =
|
|
75
|
+
return t ? c(t) ?? p(t) : void 0;
|
|
76
|
+
}, h = (e) => m(e?.language) ? e.language.trim() : p(e?.title) ? e.title.trim() : void 0, g = (e) => {
|
|
77
|
+
let t = m(e);
|
|
63
78
|
return t && (t.variant ? `${t.language}-${t.variant}` : t.language);
|
|
64
|
-
},
|
|
79
|
+
}, _ = ({ language: e = "", title: t = "" }) => {
|
|
65
80
|
let n = `${e} ${t}`;
|
|
66
81
|
return r.test(n) && !i.test(n);
|
|
67
|
-
},
|
|
68
|
-
let n =
|
|
82
|
+
}, v = ({ language: e, title: t }) => {
|
|
83
|
+
let n = m(e), r = p(t);
|
|
84
|
+
return n && !n.variant && r?.variant && r.language === n.language ? r : n ?? r;
|
|
85
|
+
}, y = (e, t) => {
|
|
86
|
+
let n = m(e), r = -1, i = 0;
|
|
69
87
|
return n && t.forEach((t, a) => {
|
|
70
|
-
let s =
|
|
88
|
+
let s = v(t);
|
|
71
89
|
if (s?.language !== n.language) return;
|
|
72
|
-
let c =
|
|
90
|
+
let c = h(t), l = 1 + +(s.variant === n.variant) + (c && o(c) === o(e) ? 2 : 0) + (_(t) ? 0 : 4);
|
|
73
91
|
l > i && (r = a, i = l);
|
|
74
92
|
}), r === -1 ? t.findIndex(({ title: t }) => !!t?.trim() && o(t) === o(e)) : r;
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
93
|
+
}, b = (e, t) => {
|
|
94
|
+
let n = e && (m(e.language) ?? p(e.title));
|
|
95
|
+
return !!n && m(t)?.language === n.language;
|
|
96
|
+
}, x = (e, t, { audio: n, original: r, subtitles: i, alwaysSubtitles: a = !0 }) => {
|
|
97
|
+
let o = n === void 0 ? -1 : y(n, e), s = o !== -1 && !_(e[o]) ? o : -1, c = s !== -1 || r === void 0 ? s : y(r, e);
|
|
98
|
+
return i === null ? {
|
|
99
|
+
audio: c,
|
|
100
|
+
subtitles: null
|
|
101
|
+
} : i === void 0 ? {
|
|
102
|
+
audio: c,
|
|
103
|
+
subtitles: -1
|
|
104
|
+
} : {
|
|
105
|
+
audio: c,
|
|
106
|
+
subtitles: !a && b(e[Math.max(c, 0)], i) ? null : y(i, t)
|
|
107
|
+
};
|
|
108
|
+
}, S = (e, t, n) => {
|
|
109
|
+
let r = t[e];
|
|
110
|
+
if (!r) return;
|
|
111
|
+
let i = r.options.filter((e) => !e.disabled), a = i.map(({ label: e }) => ({ title: e })), o = t.audio?.options.find(({ id: e }) => e === t.audio.selectedId), s = e === "audio" ? x(a, [], n).audio : x(o ? [{ title: o.label }] : [], a, n).subtitles;
|
|
112
|
+
if (s === null) return r.offLabel && r.selectedId !== null ? null : void 0;
|
|
113
|
+
let c = i[s]?.id;
|
|
114
|
+
return c !== void 0 && c !== r.selectedId ? c : void 0;
|
|
80
115
|
};
|
|
81
116
|
//#endregion
|
|
82
|
-
export {
|
|
117
|
+
export { h as languageOf, g as languageTag, b as playsIn, S as preferredSelection, y as preferredTrack, x as startingTracks };
|
|
@@ -147,6 +147,11 @@ type CommonOptions = {
|
|
|
147
147
|
* with them on too.
|
|
148
148
|
*/
|
|
149
149
|
onPreferredSubtitleLanguageChange?: (language: string | null) => void;
|
|
150
|
+
/**
|
|
151
|
+
* True when the viewer picked subtitles in the language the audio plays in: what an app saves to pass back as
|
|
152
|
+
* `alwaysShowSubtitles`, so the next media in it starts with them on too.
|
|
153
|
+
*/
|
|
154
|
+
onAlwaysShowSubtitlesChange?: (always: boolean) => void;
|
|
150
155
|
};
|
|
151
156
|
/**
|
|
152
157
|
* Bytes in: the player owns the `<video>`, and libav feeds it a fragment at a time.
|
|
@@ -224,8 +229,8 @@ export type MediaPlayerLocalOptions = CommonOptions & MediaPlayerSource & {
|
|
|
224
229
|
onNormalizeVolumeChange?: (normalizeVolume: boolean) => void;
|
|
225
230
|
/**
|
|
226
231
|
* The language a file starts its audio in, as `onPreferredAudioLanguageChange` reported it or as any tag or name
|
|
227
|
-
* (`ja`, `jpn`, `Japanese`).
|
|
228
|
-
* track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
|
|
232
|
+
* (`ja`, `jpn`, `Japanese`). Only a full track counts, never a commentary or an audio description, and a file with
|
|
233
|
+
* none plays its original. A track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
|
|
229
234
|
*/
|
|
230
235
|
preferredAudioLanguage?: string;
|
|
231
236
|
/**
|
|
@@ -234,6 +239,17 @@ export type MediaPlayerLocalOptions = CommonOptions & MediaPlayerSource & {
|
|
|
234
239
|
* language starts on the track titled as it is.
|
|
235
240
|
*/
|
|
236
241
|
preferredSubtitleLanguage?: string | null;
|
|
242
|
+
/**
|
|
243
|
+
* The language the media was made in, as its source knows it (`ja` for anime): a file with no full track in
|
|
244
|
+
* `preferredAudioLanguage` plays its full track in this one, else its default, its first track. Read as the
|
|
245
|
+
* preferences are.
|
|
246
|
+
*/
|
|
247
|
+
originalLanguage?: string;
|
|
248
|
+
/**
|
|
249
|
+
* False starts a file with subtitles off when its audio plays in `preferredSubtitleLanguage`, as it does when the
|
|
250
|
+
* viewer's own language is both. True, the default, starts them as `preferredSubtitleLanguage` says.
|
|
251
|
+
*/
|
|
252
|
+
alwaysShowSubtitles?: boolean;
|
|
237
253
|
};
|
|
238
254
|
/**
|
|
239
255
|
* A media the caller owns and the player only drives.
|
package/package.json
CHANGED
|
@@ -55,6 +55,24 @@ const nameIndex = () => {
|
|
|
55
55
|
return names = index
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
let regions: Map<string, string> | undefined
|
|
59
|
+
// the regions FORMS name, by name in each of their languages: Crunchyroll's 'Español (América Latina)' is es-419 by the
|
|
60
|
+
// Portuguese name for 419, which Spanish's own CLDR names Latinoamérica
|
|
61
|
+
const regionIndex = () => {
|
|
62
|
+
if (regions) return regions
|
|
63
|
+
const index = new Map<string, string>()
|
|
64
|
+
const locales = ['en', globalThis.navigator?.language, ...FORMS].filter((locale): locale is string => !!locale)
|
|
65
|
+
for (const region of new Set(FORMS.map((tag) => new Intl.Locale(tag).region).filter((region): region is string => !!region))) {
|
|
66
|
+
for (const locale of locales) {
|
|
67
|
+
try {
|
|
68
|
+
const name = new Intl.DisplayNames([locale], { type: 'region', fallback: 'none' }).of(region)
|
|
69
|
+
if (name) index.set(key(name), region)
|
|
70
|
+
} catch {}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return regions = index
|
|
74
|
+
}
|
|
75
|
+
|
|
58
76
|
// a title or a label is read by name only: as a tag, 'SDH' would be Southern Kurdish and 'UK' Ukrainian
|
|
59
77
|
const parseName = (text: string | undefined): Language | undefined => {
|
|
60
78
|
const trimmed = text?.trim()
|
|
@@ -65,9 +83,11 @@ const parseName = (text: string | undefined): Language | undefined => {
|
|
|
65
83
|
const [, base, qualifier] = /^([^([]+)(?:\(([^)]*)\))?/.exec(trimmed.normalize('NFKC')) ?? []
|
|
66
84
|
const named = base ? index.get(key(base)) : undefined
|
|
67
85
|
if (!named) return undefined
|
|
68
|
-
const
|
|
69
|
-
// through Intl.Locale, which reads the 'UK' of 'English (UK)' as GB
|
|
70
|
-
|
|
86
|
+
const qualified = qualifier?.trim()
|
|
87
|
+
// a code goes through Intl.Locale, which reads the 'UK' of 'English (UK)' as GB
|
|
88
|
+
const region = !qualified || TRACK_NOTES.has(qualified) ? undefined
|
|
89
|
+
: /^(?:[A-Z]{2}|\d{3})$/.test(qualified) ? qualified : regionIndex().get(key(qualified))
|
|
90
|
+
return (region ? fromTag(`${named}-${region}`) : undefined) ?? fromTag(named)
|
|
71
91
|
}
|
|
72
92
|
|
|
73
93
|
const parseLanguage = (text: string | undefined) => {
|
|
@@ -97,6 +117,14 @@ const isPartial = ({ language = '', title = '' }: LanguageTrack) => {
|
|
|
97
117
|
return PARTIAL.test(text) && !FULL.test(text)
|
|
98
118
|
}
|
|
99
119
|
|
|
120
|
+
// a tag naming no region or script takes the one its title names in the same language, as MKV tags carry none:
|
|
121
|
+
// `chi` titled 'Chinese (Traditional)' is zh-Hant
|
|
122
|
+
const spokenIn = ({ language, title }: LanguageTrack) => {
|
|
123
|
+
const tagged = parseLanguage(language)
|
|
124
|
+
const named = parseName(title)
|
|
125
|
+
return tagged && !tagged.variant && named?.variant && named.language === tagged.language ? named : tagged ?? named
|
|
126
|
+
}
|
|
127
|
+
|
|
100
128
|
/**
|
|
101
129
|
* The index of the track to play for a preferred language (a tag or a name, as `languageOf` gives it), else of the
|
|
102
130
|
* track titled exactly as the preference, or -1 when there is neither. A full track wins over a signs, songs, forced
|
|
@@ -108,7 +136,7 @@ export const preferredTrack = (preference: string, tracks: readonly LanguageTrac
|
|
|
108
136
|
let best = -1
|
|
109
137
|
let bestScore = 0
|
|
110
138
|
if (wanted) tracks.forEach((track, index) => {
|
|
111
|
-
const found =
|
|
139
|
+
const found = spokenIn(track)
|
|
112
140
|
if (found?.language !== wanted.language) return
|
|
113
141
|
const named = languageOf(track)
|
|
114
142
|
const score = 1
|
|
@@ -123,19 +151,72 @@ export const preferredTrack = (preference: string, tracks: readonly LanguageTrac
|
|
|
123
151
|
return best !== -1 ? best : tracks.findIndex(({ title }) => !!title?.trim() && key(title) === key(preference))
|
|
124
152
|
}
|
|
125
153
|
|
|
154
|
+
/** The viewer's preferences, as `startingTracks` reads them. Each may be left out. */
|
|
155
|
+
export type TrackPreferences = {
|
|
156
|
+
/**
|
|
157
|
+
* The audio language, as `preferredTrack` reads it. A media without a full track in it, or with none given, plays its
|
|
158
|
+
* original: a commentary or described audio in it is no dub.
|
|
159
|
+
*/
|
|
160
|
+
audio?: string
|
|
161
|
+
/**
|
|
162
|
+
* The language the media was made in, as its source knows it (`ja` for anime): its full track is the original.
|
|
163
|
+
* Without it, or in a file with no track in it, the original is the file's default.
|
|
164
|
+
*/
|
|
165
|
+
original?: string
|
|
166
|
+
/** The subtitle language, as `preferredTrack` reads it, or null for none. A media without it shows its default. */
|
|
167
|
+
subtitles?: string | null
|
|
168
|
+
/** False starts subtitles off when the audio plays in their language. True when left out. */
|
|
169
|
+
alwaysSubtitles?: boolean
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Whether `track` plays in the language `language` names, as a tag or a name, read by the track's tag, else its title. */
|
|
173
|
+
export const playsIn = (track: LanguageTrack | undefined, language: string): boolean => {
|
|
174
|
+
const spoken = track && (parseLanguage(track.language) ?? parseName(track.title))
|
|
175
|
+
return !!spoken && parseLanguage(language)?.language === spoken.language
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The tracks a media starts on, by index: the full audio in `preferences.audio`, else the original, else -1 for the
|
|
180
|
+
* file's default, its first track; and the subtitles in `preferences.subtitles`, else -1 for the file's default, or null
|
|
181
|
+
* for none, when they are off or when `alwaysSubtitles` is false and the audio plays in their language.
|
|
182
|
+
*/
|
|
183
|
+
export const startingTracks = (
|
|
184
|
+
audio: readonly LanguageTrack[],
|
|
185
|
+
subtitles: readonly LanguageTrack[],
|
|
186
|
+
{ audio: audioLanguage, original, subtitles: subtitleLanguage, alwaysSubtitles = true }: TrackPreferences,
|
|
187
|
+
): { audio: number, subtitles: number | null } => {
|
|
188
|
+
const match = audioLanguage === undefined ? -1 : preferredTrack(audioLanguage, audio)
|
|
189
|
+
const wanted = match !== -1 && !isPartial(audio[match]!) ? match : -1
|
|
190
|
+
const audioIndex = wanted !== -1 || original === undefined ? wanted : preferredTrack(original, audio)
|
|
191
|
+
if (subtitleLanguage === null) return { audio: audioIndex, subtitles: null }
|
|
192
|
+
if (subtitleLanguage === undefined) return { audio: audioIndex, subtitles: -1 }
|
|
193
|
+
const hidden = !alwaysSubtitles && playsIn(audio[Math.max(audioIndex, 0)], subtitleLanguage)
|
|
194
|
+
return { audio: audioIndex, subtitles: hidden ? null : preferredTrack(subtitleLanguage, subtitles) }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
type Selection = Omit<DelegatedSelection, 'select'>
|
|
198
|
+
|
|
126
199
|
/**
|
|
127
|
-
* For a media the player does not own, whose host applies the viewer's
|
|
128
|
-
* to select
|
|
129
|
-
*
|
|
130
|
-
*
|
|
200
|
+
* For a media the player does not own, whose host applies the viewer's preferences itself: the option of
|
|
201
|
+
* `selections[kind]` to select as `startingTracks` starts it, null for the row that turns subtitles off, or undefined
|
|
202
|
+
* to leave the selection as it is: the source's own default, or already selected. The audio selected stands for the
|
|
203
|
+
* default, and the subtitles are chosen for it, so a host switching both asks for the subtitles once the audio has
|
|
204
|
+
* switched. Options are matched by their labels; disabled ones are skipped.
|
|
131
205
|
*/
|
|
132
206
|
export const preferredSelection = (
|
|
133
|
-
|
|
134
|
-
|
|
207
|
+
kind: 'audio' | 'subtitles',
|
|
208
|
+
selections: { audio?: Selection, subtitles?: Selection },
|
|
209
|
+
preferences: TrackPreferences,
|
|
135
210
|
): string | null | undefined => {
|
|
136
|
-
|
|
137
|
-
if (
|
|
211
|
+
const selection = selections[kind]
|
|
212
|
+
if (!selection) return undefined
|
|
138
213
|
const options = selection.options.filter((option) => !option.disabled)
|
|
139
|
-
const
|
|
214
|
+
const tracks = options.map(({ label }) => ({ title: label }))
|
|
215
|
+
const playing = selections.audio?.options.find(({ id }) => id === selections.audio!.selectedId)
|
|
216
|
+
const index = kind === 'audio'
|
|
217
|
+
? startingTracks(tracks, [], preferences).audio
|
|
218
|
+
: startingTracks(playing ? [{ title: playing.label }] : [], tracks, preferences).subtitles
|
|
219
|
+
if (index === null) return selection.offLabel && selection.selectedId !== null ? null : undefined
|
|
220
|
+
const id = options[index]?.id
|
|
140
221
|
return id !== undefined && id !== selection.selectedId ? id : undefined
|
|
141
222
|
}
|
|
@@ -8,7 +8,7 @@ import { isMediaElementError, startPlayback } from '../../engine'
|
|
|
8
8
|
import { usePlayer } from '../player'
|
|
9
9
|
import { useLabels } from '../labels'
|
|
10
10
|
import { toNamedTracks } from '../../utils/track-label'
|
|
11
|
-
import { languageOf,
|
|
11
|
+
import { languageOf, playsIn, startingTracks } from '../../language'
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* A wedged element is rebuilt, however many times it takes.
|
|
@@ -42,13 +42,6 @@ const SEEK_PREPARE_BUDGET_MS = 500
|
|
|
42
42
|
*/
|
|
43
43
|
const SEEK_DISPLAY_LIMIT_MS = 15_000
|
|
44
44
|
|
|
45
|
-
/** The subtitle stream a media starts on: the first in the preferred language, else the first; none when it is off. */
|
|
46
|
-
const preferredSubtitle = (preference: string | null | undefined, subtitles: SubtitleStream[]) => {
|
|
47
|
-
if (preference === null) return undefined
|
|
48
|
-
const index = preference === undefined ? 0 : preferredTrack(preference, subtitles)
|
|
49
|
-
return subtitles[Math.max(index, 0)]?.streamIndex
|
|
50
|
-
}
|
|
51
|
-
|
|
52
45
|
const messageOf = (error: unknown) =>
|
|
53
46
|
error instanceof Error ? error.message : String(error)
|
|
54
47
|
|
|
@@ -153,17 +146,24 @@ export const usePlayback = (
|
|
|
153
146
|
const sizeRef = useRef(size)
|
|
154
147
|
sizeRef.current = size
|
|
155
148
|
const streams = useRef<{ audio: AudioStream[], subtitles: SubtitleStream[] }>({ audio: [], subtitles: [] })
|
|
149
|
+
const playingAudio = useCallback(() => streams.current.audio.find((track) => track.streamIndex === player.selectedAudioTrack), [player])
|
|
156
150
|
|
|
157
151
|
const readRef = useRef(read)
|
|
158
152
|
readRef.current = read
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
153
|
+
const preferences = {
|
|
154
|
+
audio: options?.preferredAudioLanguage,
|
|
155
|
+
original: options?.originalLanguage,
|
|
156
|
+
subtitles: options?.preferredSubtitleLanguage,
|
|
157
|
+
alwaysSubtitles: options?.alwaysShowSubtitles,
|
|
158
|
+
}
|
|
159
|
+
const preferencesRef = useRef(preferences)
|
|
160
|
+
preferencesRef.current = preferences
|
|
163
161
|
const onAudioLanguageRef = useRef(options?.onPreferredAudioLanguageChange)
|
|
164
162
|
onAudioLanguageRef.current = options?.onPreferredAudioLanguageChange
|
|
165
163
|
const onSubtitleLanguageRef = useRef(options?.onPreferredSubtitleLanguageChange)
|
|
166
164
|
onSubtitleLanguageRef.current = options?.onPreferredSubtitleLanguageChange
|
|
165
|
+
const onAlwaysSubtitlesRef = useRef(options?.onAlwaysShowSubtitlesChange)
|
|
166
|
+
onAlwaysSubtitlesRef.current = options?.onAlwaysShowSubtitlesChange
|
|
167
167
|
const onSeekRef = useRef(options?.onSeek)
|
|
168
168
|
onSeekRef.current = options?.onSeek
|
|
169
169
|
const onPlaybackErrorRef = useRef(options?.onPlaybackError)
|
|
@@ -193,7 +193,8 @@ export const usePlayback = (
|
|
|
193
193
|
? null
|
|
194
194
|
: languageOf(stream) ?? (wasOff ? stream?.title?.trim() || 'und' : undefined)
|
|
195
195
|
if (language !== undefined) onSubtitleLanguageRef.current?.(language)
|
|
196
|
-
|
|
196
|
+
if (language && playsIn(playingAudio(), language)) onAlwaysSubtitlesRef.current?.(true)
|
|
197
|
+
}, [player, playingAudio])
|
|
197
198
|
|
|
198
199
|
/**
|
|
199
200
|
* Seek with the data already there, or after the deadline, whichever comes first.
|
|
@@ -382,10 +383,7 @@ export const usePlayback = (
|
|
|
382
383
|
cjkFontUrl,
|
|
383
384
|
bufferSize,
|
|
384
385
|
audioStreamIndex,
|
|
385
|
-
chooseAudioStream: (audio) =>
|
|
386
|
-
const preference = preferredAudioRef.current
|
|
387
|
-
return preference === undefined ? undefined : audio[preferredTrack(preference, audio)]?.streamIndex
|
|
388
|
-
},
|
|
386
|
+
chooseAudioStream: (audio) => audio[startingTracks(audio, [], preferencesRef.current).audio]?.streamIndex,
|
|
389
387
|
onReady: () => {
|
|
390
388
|
if (cancelled) return
|
|
391
389
|
player.setSourceState({ ready: true })
|
|
@@ -404,9 +402,12 @@ export const usePlayback = (
|
|
|
404
402
|
onSubtitleStreams: (subtitles) => {
|
|
405
403
|
if (cancelled) return
|
|
406
404
|
streams.current.subtitles = subtitles
|
|
405
|
+
// for the audio playing, which may be one the viewer picked
|
|
406
|
+
const playing = playingAudio()
|
|
407
|
+
const start = startingTracks(playing ? [playing] : [], subtitles, preferencesRef.current).subtitles
|
|
407
408
|
player.setSourceState({
|
|
408
409
|
subtitleTracks: toNamedTracks(subtitles, labelsRef.current),
|
|
409
|
-
...subtitleChoiceFor.current === size ? {} : { selectedSubtitleTrack:
|
|
410
|
+
...subtitleChoiceFor.current === size ? {} : { selectedSubtitleTrack: start === null ? undefined : subtitles[Math.max(start, 0)]?.streamIndex },
|
|
410
411
|
})
|
|
411
412
|
},
|
|
412
413
|
onAudioStreams: (audio, selected) => {
|
|
@@ -445,7 +446,7 @@ export const usePlayback = (
|
|
|
445
446
|
// identity. A streaming consumer passes a fresh closure on every state update, which is several
|
|
446
447
|
// times a second, and the restart loop reads as "Loading metadata" forever at a flat 0 B/s.
|
|
447
448
|
}, [
|
|
448
|
-
player, video, subtitles, size, publicPath, libavWorkerUrl, jassubWorkerUrl, jassubWasmUrl,
|
|
449
|
+
player, playingAudio, video, subtitles, size, publicPath, libavWorkerUrl, jassubWorkerUrl, jassubWasmUrl,
|
|
449
450
|
jassubLegacyWasmUrl, defaultFontUrl, cjkFontUrl, bufferSize, audioStreamIndex, autoplay, restartToken,
|
|
450
451
|
])
|
|
451
452
|
}
|
|
@@ -19,7 +19,7 @@ import { useVolumeNormalizer } from './hooks/use-volume-normalizer'
|
|
|
19
19
|
import { createPlaybackTap } from '../audio/playback-tap'
|
|
20
20
|
import Chrome from './components/chrome'
|
|
21
21
|
import { LabelsProvider } from './labels'
|
|
22
|
-
import { languageOf } from '../language'
|
|
22
|
+
import { languageOf, playsIn } from '../language'
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* The source, read a range at a time: the player never downloads the whole file. Both fields travel
|
|
@@ -169,6 +169,11 @@ type CommonOptions = {
|
|
|
169
169
|
* with them on too.
|
|
170
170
|
*/
|
|
171
171
|
onPreferredSubtitleLanguageChange?: (language: string | null) => void
|
|
172
|
+
/**
|
|
173
|
+
* True when the viewer picked subtitles in the language the audio plays in: what an app saves to pass back as
|
|
174
|
+
* `alwaysShowSubtitles`, so the next media in it starts with them on too.
|
|
175
|
+
*/
|
|
176
|
+
onAlwaysShowSubtitlesChange?: (always: boolean) => void
|
|
172
177
|
}
|
|
173
178
|
|
|
174
179
|
/**
|
|
@@ -254,8 +259,8 @@ export type MediaPlayerLocalOptions =
|
|
|
254
259
|
|
|
255
260
|
/**
|
|
256
261
|
* The language a file starts its audio in, as `onPreferredAudioLanguageChange` reported it or as any tag or name
|
|
257
|
-
* (`ja`, `jpn`, `Japanese`).
|
|
258
|
-
* track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
|
|
262
|
+
* (`ja`, `jpn`, `Japanese`). Only a full track counts, never a commentary or an audio description, and a file with
|
|
263
|
+
* none plays its original. A track the viewer picks wins for that file. Read when a file starts, so a change applies from the next one.
|
|
259
264
|
*/
|
|
260
265
|
preferredAudioLanguage?: string
|
|
261
266
|
/**
|
|
@@ -264,6 +269,17 @@ export type MediaPlayerLocalOptions =
|
|
|
264
269
|
* language starts on the track titled as it is.
|
|
265
270
|
*/
|
|
266
271
|
preferredSubtitleLanguage?: string | null
|
|
272
|
+
/**
|
|
273
|
+
* The language the media was made in, as its source knows it (`ja` for anime): a file with no full track in
|
|
274
|
+
* `preferredAudioLanguage` plays its full track in this one, else its default, its first track. Read as the
|
|
275
|
+
* preferences are.
|
|
276
|
+
*/
|
|
277
|
+
originalLanguage?: string
|
|
278
|
+
/**
|
|
279
|
+
* False starts a file with subtitles off when its audio plays in `preferredSubtitleLanguage`, as it does when the
|
|
280
|
+
* viewer's own language is both. True, the default, starts them as `preferredSubtitleLanguage` says.
|
|
281
|
+
*/
|
|
282
|
+
alwaysShowSubtitles?: boolean
|
|
267
283
|
}
|
|
268
284
|
|
|
269
285
|
/**
|
|
@@ -528,8 +544,8 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
528
544
|
const audio = remote?.audioTracks?.selection
|
|
529
545
|
// `select`'s promise reaches the menu, which is the only place that can hold the popover open, keep the tick off an
|
|
530
546
|
// unfinished switch, and show a failure, and a promise nothing awaits is an unhandled rejection.
|
|
531
|
-
const
|
|
532
|
-
|
|
547
|
+
const latest = useRef({ options, audio })
|
|
548
|
+
latest.current = { options, audio }
|
|
533
549
|
useEffect(() => {
|
|
534
550
|
if (!subtitles) return
|
|
535
551
|
setSourceState({
|
|
@@ -541,7 +557,10 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
541
557
|
const wasOff = subtitles.selectedId === null
|
|
542
558
|
return afterSwitch(subtitles.select(picked), () => {
|
|
543
559
|
const language = picked === null ? null : languageOf(track) ?? (wasOff ? track?.title : undefined)
|
|
544
|
-
|
|
560
|
+
const now = latest.current
|
|
561
|
+
if (language !== undefined) now.options.onPreferredSubtitleLanguageChange?.(language)
|
|
562
|
+
const playing = now.audio && trackOf(now.audio, now.audio.selectedId ?? '')
|
|
563
|
+
if (language && playsIn(playing, language)) now.options.onAlwaysShowSubtitlesChange?.(true)
|
|
545
564
|
})
|
|
546
565
|
},
|
|
547
566
|
subtitleOffLabel: subtitles.offLabel,
|
|
@@ -555,7 +574,7 @@ const PlayerRoot = ({ options, children }: { options: MediaPlayerOptions, childr
|
|
|
555
574
|
selectedAudioTrack: audio.selectedId ?? undefined,
|
|
556
575
|
selectAudioTrack: (id) => afterSwitch(audio.select(String(id)), () => {
|
|
557
576
|
const language = languageOf(trackOf(audio, String(id)))
|
|
558
|
-
if (language !== undefined)
|
|
577
|
+
if (language !== undefined) latest.current.options.onPreferredAudioLanguageChange?.(language)
|
|
559
578
|
}),
|
|
560
579
|
})
|
|
561
580
|
}, [setSourceState, audio])
|