Skip to content

Commit 158759a

Browse files
committed
Fixes #14
1 parent 536a730 commit 158759a

16 files changed

+211
-84
lines changed

.github/workflows/publish.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
name: "Automatic Releaser"
22

3-
on:
3+
on:
44
push:
5-
branches: [ master ]
5+
tags:
6+
- 'v2.*'
67

78
jobs:
89
build:

.github/workflows/release.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Releases
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v2.*'
7+
8+
jobs:
9+
10+
build:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: ncipollo/release-action@v1

bun.lockb

3.71 KB
Binary file not shown.

dist/main.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { hmsToSeconds } from "./src/helpers.js";
2-
import { toVttCue } from "./src/helpers.js";
3-
import { transformSrtTracks } from "./src/transformer.js";
4-
import { srt2vtt } from "./src/helpers.js";
5-
import { fetchTrack } from "./src/helpers.js";
1+
import { hmsToSeconds } from './src/helpers.js';
2+
import { toVttCue } from './src/helpers.js';
3+
import { transformSrtTracks } from './src/transformer.js';
4+
import { srt2vtt } from './src/helpers.js';
5+
import { fetchTrack } from './src/helpers.js';
66
export { hmsToSeconds, toVttCue, transformSrtTracks, srt2vtt, fetchTrack };
77
//# sourceMappingURL=main.d.ts.map

dist/main.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.

dist/main.es.js

Lines changed: 101 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,148 @@
11
var c = Object.defineProperty;
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) {
2+
var o = (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) => (o(n, typeof t != "symbol" ? t + "" : t, e), e);
4+
class l {
5+
/**
6+
*
7+
* @param {number} number
8+
* @param {number} startTime
9+
* @param {number} endTime
10+
* @param {string} text
11+
*/
12+
constructor(t, e, r, i) {
13+
/**
14+
* @readonly
15+
* @type {number}
16+
*/
617
s(this, "number");
18+
/**
19+
* @readonly
20+
* @type {number}
21+
*/
722
s(this, "startTime");
23+
/**
24+
* @readonly
25+
* @type {number}
26+
*/
827
s(this, "endTime");
28+
/**
29+
* @readonly
30+
* @type {string}
31+
*/
932
s(this, "text");
10-
this.number = t, this.startTime = n, this.endTime = r, this.text = a;
33+
this.number = t, this.startTime = e, this.endTime = r, this.text = i;
1134
}
1235
}
13-
function l(e) {
36+
function d(n) {
1437
try {
15-
if (!e || e === " ")
38+
if (!n || n === " ")
1639
return !1;
1740
const t = {
18-
number: parseInt(e.match(/^\d+/g)[0]),
41+
number: parseInt(n.match(/^\d+/g)[0]),
1942
timing: {
20-
start: e.match(/(\d+:){2}\d+,\d+/g)[0].replace(",", "."),
21-
end: e.match(/(\d+:){2}\d+,\d+/g)[1].replace(",", ".")
43+
start: n.match(/(\d+:){2}\d+,\d+/g)[0].replace(",", "."),
44+
end: n.match(/(\d+:){2}\d+,\d+/g)[1].replace(",", ".")
2245
},
23-
text: e.split(/\r?\n/g).slice(2, e.split(/\r?\n/g).length).join(`
46+
text: n.split(/\r?\n/g).slice(2, n.split(/\r?\n/g).length).join(`
2447
`)
2548
};
26-
return new d(t.number, i(t.timing.start), i(t.timing.end), t.text);
49+
return new l(t.number, a(t.timing.start), a(t.timing.end), t.text);
2750
} catch {
2851
return !1;
2952
}
3053
}
31-
function i(e) {
32-
let t = e.split(":"), n = 0, r = 1;
54+
function a(n) {
55+
let t = n.split(":"), e = 0, r = 1;
3356
for (; t.length > 0; )
34-
n += r * parseFloat(t.pop(), 10), r *= 60;
35-
return n;
57+
e += r * parseFloat(t.pop(), 10), r *= 60;
58+
return e;
3659
}
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));
60+
async function h(n, t = "utf-8") {
61+
return (!t || t === "") && (t = "utf-8"), fetch(n).then((e) => e.arrayBuffer()).then((e) => new TextDecoder(t).decode(e));
3962
}
40-
function u(e) {
63+
function u(n) {
4164
return `WEBVTT
4265
43-
` + e.split(/\n/g).map((t) => t.replace(/((\d+:){0,2}\d+),(\d+)/g, "$1.$3")).join(`
66+
` + n.split(/\n/g).map((t) => t.replace(/((\d+:){0,2}\d+),(\d+)/g, "$1.$3")).join(`
4467
`);
4568
}
46-
class f {
69+
class m {
70+
/**
71+
* Parses a `HTMLTrackElement`
72+
*
73+
* @param {HTMLTrackElement} track
74+
*/
4775
constructor(t) {
76+
/**
77+
* @readonly
78+
* @type {HTMLTrackElement}
79+
*/
80+
s(this, "element");
81+
/**
82+
* @readonly
83+
* @type {string}
84+
*/
4885
s(this, "src");
86+
/**
87+
* @readonly
88+
* @type {string}
89+
*/
4990
s(this, "encoding");
91+
/**
92+
* @readonly
93+
* @type {string}
94+
*/
5095
s(this, "lang");
96+
/**
97+
* @readonly
98+
* @type {string}
99+
*/
51100
s(this, "kind");
101+
/**
102+
* @readonly
103+
* @type {string}
104+
*/
52105
s(this, "label");
106+
/**
107+
* @readonly
108+
* @type {boolean}
109+
*/
53110
s(this, "default");
111+
/**
112+
* @readonly
113+
* @type {string}
114+
*/
54115
s(this, "body");
116+
/**
117+
* @readonly
118+
* @type {boolean}
119+
*/
55120
s(this, "needsTransform");
121+
/**
122+
* @readonly
123+
* @type {Cue[]}
124+
*/
56125
s(this, "cues", []);
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");
126+
this.element = t, 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");
58127
}
59128
async parse() {
60-
this.body = await h(this.src, this.encoding), this.cues = this.body.split(/\r?\n\r?\n/g).map(l).filter(Boolean);
129+
this.body = await h(this.src, this.encoding), this.cues = this.body.split(/\r?\n\r?\n/g).map(d).filter(Boolean);
61130
}
62131
}
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)
132+
async function T(n) {
133+
const t = [...n.querySelectorAll("track")].map((e) => new m(e));
134+
for (const e of t) {
135+
if (!e.needsTransform)
67136
continue;
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");
137+
await e.parse(), e.element.remove();
138+
const r = n.addTextTrack(e.kind, e.label, e.lang);
139+
e.cues.forEach((i) => r.addCue(new VTTCue(i.startTime, i.endTime, i.text))), e.default && (r.mode = "showing");
71140
}
72141
}
73142
export {
74143
h as fetchTrack,
75-
i as hmsToSeconds,
144+
a as hmsToSeconds,
76145
u as srt2vtt,
77-
l as toVttCue,
146+
d as toVttCue,
78147
T as transformSrtTracks
79148
};

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/source.es.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,22 @@ class Cue {
7777
* @returns Cue
7878
*/
7979
function toVttCue(srtCue) {
80-
const convertedCue = {
81-
number: parseInt(srtCue.match(/^\d+/g)[0]),
82-
timing: {
83-
start: srtCue.match(/(\d+:){2}\d+,\d+/g)[0].replace(',', '.'),
84-
end: srtCue.match(/(\d+:){2}\d+,\d+/g)[1].replace(',', '.')
85-
},
86-
text: srtCue.split(/\r?\n/g).slice(2, srtCue.split(/\r?\n/g).length).join('\n')
87-
};
88-
return new Cue(convertedCue.number, hmsToSeconds(convertedCue.timing.start), hmsToSeconds(convertedCue.timing.end), convertedCue.text);
80+
try {
81+
if( ! srtCue || srtCue === ' ' ) {
82+
return false;
83+
}
84+
const convertedCue = {
85+
number: parseInt(srtCue.match(/^\d+/g)[0]),
86+
timing: {
87+
start: srtCue.match(/(\d+:){2}\d+,\d+/g)[0].replace(',', '.'),
88+
end: srtCue.match(/(\d+:){2}\d+,\d+/g)[1].replace(',', '.')
89+
},
90+
text: srtCue.split(/\r?\n/g).slice(2, srtCue.split(/\r?\n/g).length).join('\n')
91+
};
92+
return new Cue(convertedCue.number, hmsToSeconds(convertedCue.timing.start), hmsToSeconds(convertedCue.timing.end), convertedCue.text);
93+
} catch(e) {
94+
return false;
95+
}
8996
}
9097
/**
9198
* Converts a VTT or SRT timing `string`
@@ -121,6 +128,9 @@ function hmsToSeconds(str) {
121128
* @returns Promise<string>
122129
*/
123130
async function fetchTrack(src, encoding = 'utf-8') {
131+
if( ! encoding || encoding === '' ) {
132+
encoding = 'utf-8';
133+
}
124134
return fetch(src).then(r => r.arrayBuffer()).then(r => new TextDecoder(encoding).decode(r));
125135
}
126136

@@ -136,6 +146,11 @@ async function fetchTrack(src, encoding = 'utf-8') {
136146
* @property {Cue[]} cues
137147
*/
138148
class Track {
149+
/**
150+
* @readonly
151+
* @type {HTMLTrackElement}
152+
*/
153+
element;
139154
/**
140155
* @readonly
141156
* @type {string}
@@ -187,17 +202,18 @@ class Track {
187202
* @param {HTMLTrackElement} track
188203
*/
189204
constructor(track) {
205+
this.element = track;
190206
this.src = track.src;
191207
this.encoding = track.dataset.encoding;
192208
this.lang = track.srclang;
193209
this.kind = track.kind;
194210
this.label = track.label;
195211
this.default = track.default;
196-
this.needsTransform = !this.src.endsWith('.vtt');
212+
this.needsTransform = !this.src.toLowerCase().endsWith('.vtt');
197213
}
198214
async parse() {
199-
this.body = await fetchTrack(this.src);
200-
this.cues = this.body.split(/\r?\n\r?\n/g).map(toVttCue);
215+
this.body = await fetchTrack(this.src, this.encoding);
216+
this.cues = this.body.split(/\r?\n\r?\n/g).map(toVttCue).filter(Boolean);
201217
}
202218
}
203219

@@ -215,6 +231,10 @@ async function transformSrtTracks(video) {
215231
* We need to do before we can use it.
216232
*/
217233
await track.parse();
234+
/**
235+
* Remove the original
236+
*/
237+
track.element.remove();
218238
/**
219239
* Add new TextTrack to video
220240
* We later fill this with the transformed data

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.

dist/src/track.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export default class Track {
1616
* @param {HTMLTrackElement} track
1717
*/
1818
constructor(track: HTMLTrackElement);
19+
/**
20+
* @readonly
21+
* @type {HTMLTrackElement}
22+
*/
23+
readonly element: HTMLTrackElement;
1924
/**
2025
* @readonly
2126
* @type {string}
@@ -63,5 +68,5 @@ export default class Track {
6368
readonly cues: Cue[];
6469
parse(): Promise<void>;
6570
}
66-
import Cue from "./cue.js";
71+
import Cue from './cue.js';
6772
//# sourceMappingURL=track.d.ts.map

0 commit comments

Comments
 (0)