Skip to content

Commit 8605a4e

Browse files
committed
Fix encoding not passed to function
1 parent 52728cc commit 8605a4e

File tree

11 files changed

+98
-58
lines changed

11 files changed

+98
-58
lines changed

assets/subtitle.srt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ and what stays off.
1616
4
1717
00:00:10,144 --> 00:00:13,000
1818
This was a demo showing that we successfully
19-
converted srt into WebVTT compatible subtitles
19+
converted srt into WebVTT compatible subtitles
20+
21+

dist/main.es.js

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
var c = Object.defineProperty;
2-
var d = (n, t, e) => t in n ? c(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3-
var s = (n, t, e) => (d(n, typeof t != "symbol" ? t + "" : t, e), e);
4-
class o {
5-
constructor(t, e, r, i) {
2+
var o = (e, t, n) => t in e ? c(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3+
var s = (e, t, n) => (o(e, typeof t != "symbol" ? t + "" : t, n), n);
4+
class d {
5+
constructor(t, n, r, a) {
66
s(this, "number");
77
s(this, "startTime");
88
s(this, "endTime");
99
s(this, "text");
10-
this.number = t, this.startTime = e, this.endTime = r, this.text = i;
10+
this.number = t, this.startTime = n, this.endTime = r, this.text = a;
1111
}
1212
}
13-
function l(n) {
14-
const t = {
15-
number: parseInt(n.match(/^\d+/g)[0]),
16-
timing: {
17-
start: n.match(/(\d+:){2}\d+,\d+/g)[0].replace(",", "."),
18-
end: n.match(/(\d+:){2}\d+,\d+/g)[1].replace(",", ".")
19-
},
20-
text: n.split(/\r?\n/g).slice(2, n.split(/\r?\n/g).length).join(`
13+
function l(e) {
14+
try {
15+
if (!e || e === " ")
16+
return !1;
17+
const t = {
18+
number: parseInt(e.match(/^\d+/g)[0]),
19+
timing: {
20+
start: e.match(/(\d+:){2}\d+,\d+/g)[0].replace(",", "."),
21+
end: e.match(/(\d+:){2}\d+,\d+/g)[1].replace(",", ".")
22+
},
23+
text: e.split(/\r?\n/g).slice(2, e.split(/\r?\n/g).length).join(`
2124
`)
22-
};
23-
return new o(t.number, a(t.timing.start), a(t.timing.end), t.text);
25+
};
26+
return new d(t.number, i(t.timing.start), i(t.timing.end), t.text);
27+
} catch {
28+
return !1;
29+
}
2430
}
25-
function a(n) {
26-
let t = n.split(":"), e = 0, r = 1;
31+
function i(e) {
32+
let t = e.split(":"), n = 0, r = 1;
2733
for (; t.length > 0; )
28-
e += r * parseFloat(t.pop(), 10), r *= 60;
29-
return e;
34+
n += r * parseFloat(t.pop(), 10), r *= 60;
35+
return n;
3036
}
31-
async function h(n, t = "utf-8") {
32-
return fetch(n).then((e) => e.arrayBuffer()).then((e) => new TextDecoder(t).decode(e));
37+
async function h(e, t = "utf-8") {
38+
return (!t || t === "") && (t = "utf-8"), fetch(e).then((n) => n.arrayBuffer()).then((n) => new TextDecoder(t).decode(n));
3339
}
34-
function f(n) {
40+
function u(e) {
3541
return `WEBVTT
3642
37-
` + n.split(/\n/g).map((t) => t.replace(/((\d+:){0,2}\d+),(\d+)/g, "$1.$3")).join(`
43+
` + e.split(/\n/g).map((t) => t.replace(/((\d+:){0,2}\d+),(\d+)/g, "$1.$3")).join(`
3844
`);
3945
}
40-
class m {
46+
class f {
4147
constructor(t) {
4248
s(this, "src");
4349
s(this, "encoding");
@@ -48,26 +54,26 @@ class m {
4854
s(this, "body");
4955
s(this, "needsTransform");
5056
s(this, "cues", []);
51-
this.src = t.src, this.encoding = t.dataset.encoding, this.lang = t.srclang, this.kind = t.kind, this.label = t.label, this.default = t.default, this.needsTransform = !this.src.endsWith(".vtt");
57+
this.src = t.src, this.encoding = t.dataset.encoding, this.lang = t.srclang, this.kind = t.kind, this.label = t.label, this.default = t.default, this.needsTransform = !this.src.toLowerCase().endsWith(".vtt");
5258
}
5359
async parse() {
54-
this.body = await h(this.src), this.cues = this.body.split(/\r?\n\r?\n/g).map(l);
60+
this.body = await h(this.src, this.encoding), this.cues = this.body.split(/\r?\n\r?\n/g).map(l).filter(Boolean);
5561
}
5662
}
57-
async function g(n) {
58-
const t = [...n.querySelectorAll("track")].map((e) => new m(e));
59-
for (const e of t) {
60-
if (!e.needsTransform)
63+
async function T(e) {
64+
const t = [...e.querySelectorAll("track")].map((n) => new f(n));
65+
for (const n of t) {
66+
if (!n.needsTransform)
6167
continue;
62-
await e.parse();
63-
const r = n.addTextTrack(e.kind, e.label, e.lang);
64-
e.cues.forEach((i) => r.addCue(new VTTCue(i.startTime, i.endTime, i.text))), e.default && (r.mode = "showing");
68+
await n.parse();
69+
const r = e.addTextTrack(n.kind, n.label, n.lang);
70+
n.cues.forEach((a) => r.addCue(new VTTCue(a.startTime, a.endTime, a.text))), n.default && (r.mode = "showing");
6571
}
6672
}
6773
export {
6874
h as fetchTrack,
69-
a as hmsToSeconds,
70-
f as srt2vtt,
75+
i as hmsToSeconds,
76+
u as srt2vtt,
7177
l as toVttCue,
72-
g as transformSrtTracks
78+
T as transformSrtTracks
7379
};

dist/main.iife.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/helpers.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @param {string} srtCue
2525
* @returns Cue
2626
*/
27-
export function toVttCue(srtCue: string): Cue;
27+
export function toVttCue(srtCue: string): false | Cue;
2828
/**
2929
* Converts a VTT or SRT timing `string`
3030
* to a `number` in seconds + milliseconds

dist/src/helpers.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<body>
99
<div id="app">
1010
<video src="assets/pexels-cottonbro-5532773.mp4" width="100%" controls autoplay muted>
11-
<track label="English" kind="subtitles" srclang="en" data-encoding="UTF-8" src="assets/subtitle.srt" default />
11+
<track label="English" kind="subtitles" srclang="en" src="assets/subtitle.srt" default />
1212
</video>
1313
</div>
1414
<script type="module" src="main.js"></script>

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "srt-support-for-html5-videos",
3-
"version": "2.2.8",
3+
"version": "2.3.8",
44
"repository": "https://github.com/codeit-ninja/SRT-Support-for-HTML5-videos",
55
"type": "module",
66
"main": "main.js",
@@ -12,15 +12,15 @@
1212
},
1313
"scripts": {
1414
"dev": "vite",
15-
"build": "npm run build:iife && npm run build:esm && npm run build:source && tsc",
16-
"build:source": "vite --config vite.config.js build",
15+
"build": "npm run build:iife && npm run build:esm && tsc",
1716
"build:iife": "vite --config vite.config.iife.js build",
1817
"build:esm": "vite --config vite.config.esm.js build",
1918
"preview": "vite preview",
2019
"publish": "npm run build"
2120
},
2221
"devDependencies": {
2322
"rollup-plugin-cleanup": "^3.2.1",
23+
"typescript": "^4.9.4",
2424
"vite": "^3.0.0"
2525
}
2626
}

src/helpers.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,24 @@ import Cue from "./cue.js";
2727
* @returns Cue
2828
*/
2929
export function toVttCue(srtCue) {
30-
const convertedCue = {
31-
number: parseInt(srtCue.match(/^\d+/g)[0]),
32-
timing: {
33-
start: srtCue.match(/(\d+:){2}\d+,\d+/g)[0].replace(',', '.'),
34-
end: srtCue.match(/(\d+:){2}\d+,\d+/g)[1].replace(',', '.')
35-
},
36-
text: srtCue.split(/\r?\n/g).slice(2, srtCue.split(/\r?\n/g).length).join('\n')
30+
try {
31+
if( ! srtCue || srtCue === ' ' ) {
32+
return false;
33+
}
34+
35+
const convertedCue = {
36+
number: parseInt(srtCue.match(/^\d+/g)[0]),
37+
timing: {
38+
start: srtCue.match(/(\d+:){2}\d+,\d+/g)[0].replace(',', '.'),
39+
end: srtCue.match(/(\d+:){2}\d+,\d+/g)[1].replace(',', '.')
40+
},
41+
text: srtCue.split(/\r?\n/g).slice(2, srtCue.split(/\r?\n/g).length).join('\n')
42+
}
43+
44+
return new Cue(convertedCue.number, hmsToSeconds(convertedCue.timing.start), hmsToSeconds(convertedCue.timing.end), convertedCue.text);
45+
} catch(e) {
46+
return false;
3747
}
38-
39-
return new Cue(convertedCue.number, hmsToSeconds(convertedCue.timing.start), hmsToSeconds(convertedCue.timing.end), convertedCue.text);
4048
}
4149
/**
4250
* Converts a VTT or SRT timing `string`
@@ -74,6 +82,10 @@ export function hmsToSeconds(str) {
7482
* @returns Promise<string>
7583
*/
7684
export async function fetchTrack(src, encoding = 'utf-8') {
85+
if( ! encoding || encoding === '' ) {
86+
encoding = 'utf-8';
87+
}
88+
7789
return fetch(src).then(r => r.arrayBuffer()).then(r => new TextDecoder(encoding).decode(r));
7890
}
7991
/**

src/track.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ export default class Track {
7171
this.kind = track.kind;
7272
this.label = track.label;
7373
this.default = track.default;
74-
this.needsTransform = !this.src.endsWith('.vtt');
74+
this.needsTransform = !this.src.toLowerCase().endsWith('.vtt');
7575
}
7676

7777
async parse() {
78-
this.body = await fetchTrack(this.src);
79-
this.cues = this.body.split(/\r?\n\r?\n/g).map(toVttCue);
78+
this.body = await fetchTrack(this.src, this.encoding);
79+
this.cues = this.body.split(/\r?\n\r?\n/g).map(toVttCue).filter(Boolean);
8080
}
8181
}

0 commit comments

Comments
 (0)