Skip to content

Commit 2a8965c

Browse files
committed
Fix some minor bugs
1 parent 14a92a0 commit 2a8965c

File tree

11 files changed

+363
-76
lines changed

11 files changed

+363
-76
lines changed

README.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Officially only VTT files are supported by the HTML5 track element.
44
This package will convert your SRT subtitles on the fly to VTT subtitles.
55

66
### 👉 What's new in v2?
7-
7+
- Updated code base to modern JavaScript
88
- Added [TextDecoder](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder?retiredLocale=nl) to support all kinds of encodings
99
- SRT can be encoded in [multiple formats](https://en.wikipedia.org/wiki/SubRip#Text_encoding), while WebVTT should only be [encoded in UTF-8](https://www.w3.org/TR/webvtt1/#file-structure) format.
1010
- Does not break the TextTrack selection in video elements
1111
- The previous version broke the TextTrack selection in the captions' menu. So when you had multiple captions you could not switch between them.
1212
- Is available as a package (ES module) or as a ready to use script
13+
- No external dependencies
14+
- More functionality included
1315

1416
### Installation
1517

@@ -19,13 +21,13 @@ As a NPM package, works both in the browser and NodeJS
1921
npm install srt-support-for-html5-videos
2022
```
2123

22-
CDN
24+
**CDN**
2325
- Ready to use script, just include it, and it will parse all video elements to convert the tracks
2426
```text
2527
https://cdn.jsdelivr.net/gh/codeit-ninja/SRT-Support-for-HTML5-videos@master/dist/main.iife.js
2628
```
2729

28-
ES Module
30+
**ES Module**
2931
- Use as ES Module, see usage section
3032
```text
3133
https://cdn.jsdelivr.net/gh/codeit-ninja/SRT-Support-for-HTML5-videos@master/dist/main.es.js
@@ -38,7 +40,7 @@ If you installed it as a package using NPM or as an ES Module using the CDN, you
3840
// If using NPM
3941
import { transformSrtTracks } from 'srt-support-for-html5-videos';
4042
// If using CDN
41-
import { transformSrtTracks } from 'https://cdn.jsdelivr.net/gh/codeit-ninja/SRT-Support-for-HTML5-videos/dist/main.es.js';
43+
import { transformSrtTracks } from 'https://cdn.jsdelivr.net/gh/codeit-ninja/srt-support-for-html5-videos/dist/main.es.js';
4244

4345
// Single video element
4446
const video = document.getElementById('video');
@@ -60,8 +62,12 @@ If your SRT file is encoded in a different format than *UTF-8* you must specify
6062
</video>
6163
```
6264

65+
### Other functions
6366
Some more functions are exported, these are just helper functions, you can import and use them if you have any use for them.
6467

68+
#### toVttCue
69+
Function is internally used to covert a srt cue into a WebVTT compatible cue.
70+
6571
```javascript
6672
/**
6773
* Converts a SRT cue into a VTT compatible cue
@@ -84,14 +90,41 @@ Some more functions are exported, these are just helper functions, you can impor
8490
*
8591
*/
8692
import { toVttCue } from 'srt-support-for-html5-videos';
93+
```
94+
#### hmsToSeconds
95+
Converts a `HH:MM:SS.MS` string into a *number in seconds*
96+
97+
```javascript
8798
/**
8899
* Converts a VTT or SRT timing `string`
89100
* to a `number` in seconds + milliseconds
90-
*
91-
* *Example*
92-
* const seconds = hmsToSeconds('00:00:02.827'); // 2.827
93101
*/
94102
import { hmsToSeconds } from 'srt-support-for-html5-videos';
103+
104+
const seconds = hmsToSeconds('00:00:02.827'); // 2.827
105+
```
106+
#### fetchTrack
107+
Fetches the contents of a track and returns the body.
108+
109+
```javascript
110+
/**
111+
* Fetches the contents of a track source
112+
*/
113+
import { fetchTrack } from 'srt-support-for-html5-videos';
114+
115+
const content = await fetchTrack('https://example.com/path/my-subtitle.srt');
116+
```
117+
118+
#### srt2vtt
119+
Convert SRT to VTT
120+
121+
```javascript
122+
/**
123+
* Converts SRT formatted string into a WebVTT formated string
124+
*/
125+
import { srt2vtt } from 'srt-support-for-html5-videos';
126+
127+
const vtt = srt2vtt(srt);
95128
```
96129

97130
### Support me

dist/main.es.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
var o = Object.defineProperty;
2-
var c = (n, e, t) => e in n ? o(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3-
var s = (n, e, t) => (c(n, typeof e != "symbol" ? e + "" : e, t), t);
4-
class d {
5-
constructor(e, t, i, r) {
1+
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) {
66
s(this, "number");
77
s(this, "startTime");
88
s(this, "endTime");
99
s(this, "text");
10-
this.number = e, this.startTime = t, this.endTime = i, this.text = r;
10+
this.number = t, this.startTime = e, this.endTime = r, this.text = i;
1111
}
1212
}
1313
function l(n) {
14-
const e = {
14+
const t = {
1515
number: parseInt(n.match(/^\d+/g)[0]),
1616
timing: {
1717
start: n.match(/(\d+:){2}\d+,\d+/g)[0].replace(",", "."),
@@ -20,42 +20,54 @@ function l(n) {
2020
text: n.split(/\r?\n/g).slice(2, n.split(/\r?\n/g).length).join(`
2121
`)
2222
};
23-
return new d(e.number, a(e.timing.start), a(e.timing.end), e.text);
23+
return new o(t.number, a(t.timing.start), a(t.timing.end), t.text);
2424
}
2525
function a(n) {
26-
for (var e = n.split(":"), t = 0, i = 1; e.length > 0; )
27-
t += i * parseFloat(e.pop(), 10), i *= 60;
28-
return t;
26+
let t = n.split(":"), e = 0, r = 1;
27+
for (; t.length > 0; )
28+
e += r * parseFloat(t.pop(), 10), r *= 60;
29+
return e;
2930
}
30-
class h {
31-
constructor(e) {
31+
async function h(n, t = "utf-8") {
32+
return fetch(n).then((e) => e.arrayBuffer()).then((e) => new TextDecoder(t).decode(e));
33+
}
34+
function f(n) {
35+
return `WEBVTT
36+
37+
` + n.split(/\n/g).map((t) => t.replace(/((\d+:){0,2}\d+),(\d+)/g, "$1.$3")).join(`
38+
`);
39+
}
40+
class m {
41+
constructor(t) {
3242
s(this, "src");
3343
s(this, "encoding");
3444
s(this, "lang");
3545
s(this, "kind");
3646
s(this, "label");
3747
s(this, "default");
38-
s(this, "content");
48+
s(this, "body");
3949
s(this, "needsTransform");
4050
s(this, "cues", []);
41-
this.src = e.src, this.encoding = e.dataset.encoding, this.lang = e.srclang, this.kind = e.kind, this.label = e.label, this.default = e.default, this.needsTransform = !0, this.src.endsWith(".vtt") && (this.needsTransform = !1);
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");
4252
}
4353
async parse() {
44-
const e = new TextDecoder(this.encoding), i = await (await fetch(this.src)).arrayBuffer();
45-
this.content = e.decode(i), this.cues = this.content.split(/\r?\n\r?\n/g).map(l);
54+
this.body = await h(this.src), this.cues = this.body.split(/\r?\n\r?\n/g).map(l);
4655
}
4756
}
48-
async function f(n) {
49-
[...n.querySelectorAll("track")].map((t) => new h(t)).forEach(async (t) => {
50-
if (!t.needsTransform)
51-
return;
52-
await t.parse();
53-
const i = n.addTextTrack(t.kind, t.label, t.lang);
54-
t.cues.forEach((r) => i.addCue(new VTTCue(r.startTime, r.endTime, r.text))), t.default && (i.mode = "showing"), console.log(t);
55-
});
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)
61+
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");
65+
}
5666
}
5767
export {
68+
h as fetchTrack,
5869
a as hmsToSeconds,
70+
f as srt2vtt,
5971
l as toVttCue,
60-
f as transformSrtTracks
72+
g as transformSrtTracks
6173
};

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.

main.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
* _/ |
99
* |__/
1010
*/
11-
import { hmsToSeconds, toVttCue } from './src/helpers.js';
11+
import { hmsToSeconds, toVttCue, srt2vtt, fetchTrack } from './src/helpers.js';
1212
import { transformSrtTracks } from './src/transformer.js';
1313

1414
export {
1515
hmsToSeconds,
1616
toVttCue,
17-
transformSrtTracks
18-
}
17+
transformSrtTracks,
18+
srt2vtt,
19+
fetchTrack
20+
}

0 commit comments

Comments
 (0)