@banou/media-player 0.10.29 → 0.10.30

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 CHANGED
@@ -346,7 +346,9 @@ it has one.
346
346
 
347
347
  A media the player does not own reports picks the same way, by the option's label once the source's `select`
348
348
  resolves, and its host applies the preference to its own tracks: `preferredSelection(selection, preference)`, from
349
- `@banou/media-player/language`, names the option to select, and imports nothing else of the player.
349
+ `@banou/media-player/language`, names the option to select, and imports nothing else of the player. `languageTag(pick)`
350
+ from the same module gives a pick's BCP 47 tag (`jpn` gives `ja`, `Português (Brasil)` gives `pt-BR`), or undefined for
351
+ one naming no language, for code that reads tags.
350
352
 
351
353
  ## Layout
352
354
 
@@ -9,6 +9,12 @@ export type LanguageTrack = {
9
9
  * neither names a language.
10
10
  */
11
11
  export declare const languageOf: (track: LanguageTrack | undefined) => string | undefined;
12
+ /**
13
+ * The BCP 47 tag of the language `text` names, as a tag (`jpn`, `en_US`) or a name (`Japanese`, `Português (Brasil)`),
14
+ * with its region or script when it names one: `ja`, `en-US`, `pt-BR`. Undefined for a text naming no language (`und`,
15
+ * `Signs & Songs`), so a preference `languageOf` saved can be handed to code that reads tags.
16
+ */
17
+ export declare const languageTag: (text: string | undefined) => string | undefined;
12
18
  /**
13
19
  * The index of the track to play for a preferred language (a tag or a name, as `languageOf` gives it), else of the
14
20
  * track titled exactly as the preference, or -1 when there is neither. A full track wins over a signs, songs, forced
@@ -14,21 +14,25 @@ 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
14
14
  "zxx",
15
15
  "mul",
16
16
  "mis"
17
- ]), r = /sign|song|forced|commentary|karaoke|descri/i, i = /full|dialog/i, a = (e) => e.normalize("NFKC").toLowerCase().replace(/\s+/g, " ").trim(), o = new Intl.DisplayNames(["en"], {
17
+ ]), r = /sign|song|forced|commentary|karaoke|descri/i, i = /full|dialog/i, a = /* @__PURE__ */ new Set([
18
+ "CC",
19
+ "HI",
20
+ "AD"
21
+ ]), o = (e) => e.normalize("NFKC").toLowerCase().replace(/\s+/g, " ").trim(), s = new Intl.DisplayNames(["en"], {
18
22
  type: "language",
19
23
  fallback: "none"
20
- }), s = (e) => {
24
+ }), c = (e) => {
21
25
  if (/^[a-z]{2,3}(?:[-_][a-z\d]{2,8})*$/i.test(e)) try {
22
26
  let t = new Intl.Locale(e.replace(/_/g, "-"));
23
- return n.has(t.language) || !o.of(t.language) ? void 0 : {
27
+ return n.has(t.language) || !s.of(t.language) ? void 0 : {
24
28
  language: t.language,
25
29
  variant: t.region ?? t.script
26
30
  };
27
31
  } catch {
28
32
  return;
29
33
  }
30
- }, c, l = () => {
31
- if (c) return c;
34
+ }, l, u = () => {
35
+ if (l) return l;
32
36
  let n = /* @__PURE__ */ new Map(), r = [...new Set(["en", globalThis.navigator?.language].filter((e) => !!e))], i = (e, t, r) => {
33
37
  try {
34
38
  let i = new Intl.DisplayNames([t], {
@@ -36,40 +40,43 @@ 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
36
40
  languageDisplay: r,
37
41
  fallback: "none"
38
42
  }).of(e);
39
- i && !n.has(a(i)) && n.set(a(i), e);
43
+ i && !n.has(o(i)) && n.set(o(i), e);
40
44
  } catch {}
41
45
  };
42
46
  for (let e of t) for (let t of [...r, e]) for (let n of ["dialect", "standard"]) i(e, t, n);
43
47
  for (let t of e) for (let e of [...r, t]) i(t, e, "dialect");
44
- return c = n;
45
- }, u = (e) => {
48
+ return l = n;
49
+ }, d = (e) => {
46
50
  let t = e?.trim();
47
51
  if (!t) return;
48
- let n = l(), r = n.get(a(t));
49
- if (r) return s(r);
50
- let [, i, o] = /^([^([]+)(?:\(([^)]*)\))?/.exec(t.normalize("NFKC")) ?? [], c = i ? n.get(a(i)) : void 0;
51
- if (!c) return;
52
- let u = o?.trim();
53
- return (u && /^(?:[A-Z]{2}|\d{3})$/.test(u) ? s(`${c}-${u}`) : void 0) ?? s(c);
54
- }, d = (e) => {
52
+ let n = u(), r = n.get(o(t));
53
+ if (r) return c(r);
54
+ let [, i, s] = /^([^([]+)(?:\(([^)]*)\))?/.exec(t.normalize("NFKC")) ?? [], l = i ? n.get(o(i)) : void 0;
55
+ if (!l) return;
56
+ let d = s?.trim();
57
+ return (d && /^(?:[A-Z]{2}|\d{3})$/.test(d) && !a.has(d) ? c(`${l}-${d}`) : void 0) ?? c(l);
58
+ }, f = (e) => {
55
59
  let t = e?.trim();
56
- return t ? s(t) ?? u(t) : void 0;
57
- }, f = (e) => d(e?.language) ? e.language.trim() : u(e?.title) ? e.title.trim() : void 0, p = ({ language: e = "", title: t = "" }) => {
60
+ return t ? c(t) ?? d(t) : void 0;
61
+ }, p = (e) => f(e?.language) ? e.language.trim() : d(e?.title) ? e.title.trim() : void 0, m = (e) => {
62
+ let t = f(e);
63
+ return t && (t.variant ? `${t.language}-${t.variant}` : t.language);
64
+ }, h = ({ language: e = "", title: t = "" }) => {
58
65
  let n = `${e} ${t}`;
59
66
  return r.test(n) && !i.test(n);
60
- }, m = (e, t) => {
61
- let n = d(e), r = -1, i = 0;
62
- return n && t.forEach((t, o) => {
63
- let s = d(t.language) ?? u(t.title);
67
+ }, g = (e, t) => {
68
+ let n = f(e), r = -1, i = 0;
69
+ return n && t.forEach((t, a) => {
70
+ let s = f(t.language) ?? d(t.title);
64
71
  if (s?.language !== n.language) return;
65
- let c = f(t), l = 1 + +(s.variant === n.variant) + (c && a(c) === a(e) ? 2 : 0) + (p(t) ? 0 : 4);
66
- l > i && (r = o, i = l);
67
- }), r === -1 ? t.findIndex(({ title: t }) => !!t?.trim() && a(t) === a(e)) : r;
68
- }, h = (e, t) => {
72
+ let c = p(t), l = 1 + +(s.variant === n.variant) + (c && o(c) === o(e) ? 2 : 0) + (h(t) ? 0 : 4);
73
+ l > i && (r = a, i = l);
74
+ }), r === -1 ? t.findIndex(({ title: t }) => !!t?.trim() && o(t) === o(e)) : r;
75
+ }, _ = (e, t) => {
69
76
  if (t === void 0) return;
70
77
  if (t === null) return e.offLabel && e.selectedId !== null ? null : void 0;
71
- let n = e.options.filter((e) => !e.disabled), r = n[m(t, n.map(({ label: e }) => ({ title: e })))]?.id;
78
+ let n = e.options.filter((e) => !e.disabled), r = n[g(t, n.map(({ label: e }) => ({ title: e })))]?.id;
72
79
  return r !== void 0 && r !== e.selectedId ? r : void 0;
73
80
  };
74
81
  //#endregion
75
- export { f as languageOf, h as preferredSelection, m as preferredTrack };
82
+ export { p as languageOf, m as languageTag, _ as preferredSelection, g as preferredTrack };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@banou/media-player",
3
- "version": "0.10.29",
3
+ "version": "0.10.30",
4
4
  "description": "A video player for containers and codecs the browser cannot play natively, remuxed on the fly",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -20,6 +20,8 @@ const NOT_LANGUAGES = new Set(['und', 'zxx', 'mul', 'mis'])
20
20
  // sign and song tracks, forced subtitles, commentaries and described audio, unless the title says it is the full track
21
21
  const PARTIAL = /sign|song|forced|commentary|karaoke|descri/i
22
22
  const FULL = /full|dialog/i
23
+ // closed captions, hearing impaired and audio description in a title, which Intl.Locale reads as regions (CC, AD) or accepts
24
+ const TRACK_NOTES = new Set(['CC', 'HI', 'AD'])
23
25
 
24
26
  const key = (text: string) => text.normalize('NFKC').toLowerCase().replace(/\s+/g, ' ').trim()
25
27
 
@@ -65,7 +67,7 @@ const parseName = (text: string | undefined): Language | undefined => {
65
67
  if (!named) return undefined
66
68
  const region = qualifier?.trim()
67
69
  // through Intl.Locale, which reads the 'UK' of 'English (UK)' as GB
68
- return (region && /^(?:[A-Z]{2}|\d{3})$/.test(region) ? fromTag(`${named}-${region}`) : undefined) ?? fromTag(named)
70
+ return (region && /^(?:[A-Z]{2}|\d{3})$/.test(region) && !TRACK_NOTES.has(region) ? fromTag(`${named}-${region}`) : undefined) ?? fromTag(named)
69
71
  }
70
72
 
71
73
  const parseLanguage = (text: string | undefined) => {
@@ -80,6 +82,16 @@ const parseLanguage = (text: string | undefined) => {
80
82
  export const languageOf = (track: LanguageTrack | undefined): string | undefined =>
81
83
  parseLanguage(track?.language) ? track!.language!.trim() : parseName(track?.title) ? track!.title!.trim() : undefined
82
84
 
85
+ /**
86
+ * The BCP 47 tag of the language `text` names, as a tag (`jpn`, `en_US`) or a name (`Japanese`, `Português (Brasil)`),
87
+ * with its region or script when it names one: `ja`, `en-US`, `pt-BR`. Undefined for a text naming no language (`und`,
88
+ * `Signs & Songs`), so a preference `languageOf` saved can be handed to code that reads tags.
89
+ */
90
+ export const languageTag = (text: string | undefined): string | undefined => {
91
+ const found = parseLanguage(text)
92
+ return found && (found.variant ? `${found.language}-${found.variant}` : found.language)
93
+ }
94
+
83
95
  const isPartial = ({ language = '', title = '' }: LanguageTrack) => {
84
96
  const text = `${language} ${title}`
85
97
  return PARTIAL.test(text) && !FULL.test(text)