-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebvtt.ts
38 lines (29 loc) · 895 Bytes
/
webvtt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// SPDX: 0BSD
import { subs } from "./utils.ts";
export function parseWebVTT(input: string): subs[] {
let parsed: subs[] = [];
// WebVTT files have content seperated by 2 newlines.
const r: string[] = input.split("\n\n");
r.shift();
for (let i = 0; i < r.length; i++) {
const subIndex: number = i + 1;
const startTime: string = r[i].split("\n")[0].split(" --> ")[0];
const endTime: string = r[i].split("\n")[0].split(" --> ")[1];
const subBody: string = r[i].split("\n").slice(1).join("\n");
const p: subs = {
index: subIndex,
start: startTime,
end: endTime,
content: subBody,
};
parsed.push(p);
}
return parsed;
}
export function toWebVTT(inp: subs[]) {
let p = "WEBVTT\n\n";
for (let i = 0; i < inp.length; i++) {
p += `${inp[i].start} --> ${inp[i].end}\n${inp[i].content}\n\n`;
}
return p.slice(0, -2);
}