style: format static/player/subtitles/vtt.ts
This commit is contained in:
@@ -1,17 +1,23 @@
|
|||||||
// parses VTT timestamp (mm:ss.ms or hh:mm:ss.ms) to seconds
|
// parses VTT timestamp (mm:ss.ms or hh:mm:ss.ms) to seconds
|
||||||
export const parseVttTime = (raw: string): number => {
|
export const parseVttTime = (raw: string): number => {
|
||||||
const parts = raw.trim().split(":");
|
const parts = raw.trim().split(":");
|
||||||
if (parts.length < 2) return 0;
|
if (parts.length < 2) {
|
||||||
const secPart = parts[parts.length - 1];
|
return 0;
|
||||||
const minPart = parts[parts.length - 2];
|
}
|
||||||
const hourPart = parts.length > 2 ? parts[parts.length - 3] : "0";
|
const secPart = parts.at(-1);
|
||||||
if (!secPart || !minPart) return 0;
|
const minPart = parts.at(-2);
|
||||||
|
const hourPart = parts.length > 2 ? parts.at(-3) : "0";
|
||||||
|
if (!secPart || !minPart) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return Number(hourPart) * 3600 + Number(minPart) * 60 + Number(secPart.replace(",", "."));
|
return Number(hourPart) * 3600 + Number(minPart) * 60 + Number(secPart.replace(",", "."));
|
||||||
};
|
};
|
||||||
|
|
||||||
// parses a single VTT cue: timestamp line + text lines
|
// parses a single VTT cue: timestamp line + text lines
|
||||||
const parseVttCue = (line: string, lines: string[], i: number) => {
|
const parseVttCue = (line: string, lines: string[], i: number) => {
|
||||||
if (!line.includes("-->")) return null;
|
if (!line.includes("-->")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
const [startRaw, endRaw] = line.split("-->");
|
const [startRaw, endRaw] = line.split("-->");
|
||||||
const payload: string[] = [];
|
const payload: string[] = [];
|
||||||
let j = i + 1;
|
let j = i + 1;
|
||||||
@@ -23,31 +29,39 @@ const parseVttCue = (line: string, lines: string[], i: number) => {
|
|||||||
// strip tags, join lines
|
// strip tags, join lines
|
||||||
const text = payload
|
const text = payload
|
||||||
.join("\n")
|
.join("\n")
|
||||||
.replace(/<[^>]+>/g, "")
|
.replaceAll(/<[^>]+>/g, "")
|
||||||
.trim();
|
.trim();
|
||||||
if (!text) return null;
|
if (!text) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return { start: parseVttTime(startRaw), end: parseVttTime(endRaw), text };
|
return { start: parseVttTime(startRaw), end: parseVttTime(endRaw), text };
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses full VTT file into cue array.
|
* Parses full VTT file into cue array. Handles both compact (timestamp on separate line) and
|
||||||
* Handles both compact (timestamp on separate line) and standard formats.
|
* standard formats.
|
||||||
*/
|
*/
|
||||||
export const parseVtt = (text: string) => {
|
export const parseVtt = (text: string) => {
|
||||||
const lines = text.replace(/\r/g, "").split("\n");
|
const lines = text.replaceAll(/\r/g, "").split("\n");
|
||||||
const cues = [];
|
const cues = [];
|
||||||
for (let i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
const line = lines[i].trim();
|
const line = lines[i].trim();
|
||||||
if (!line) continue;
|
if (!line) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
// compact: cue id on line i, timestamp on i+1
|
// compact: cue id on line i, timestamp on i+1
|
||||||
if (i + 1 < lines.length && !line.includes("-->") && lines[i + 1].includes("-->")) {
|
if (i + 1 < lines.length && !line.includes("-->") && lines[i + 1].includes("-->")) {
|
||||||
const cue = parseVttCue(lines[i + 1].trim(), lines, i + 1);
|
const cue = parseVttCue(lines[i + 1].trim(), lines, i + 1);
|
||||||
if (cue) cues.push(cue);
|
if (cue) {
|
||||||
|
cues.push(cue);
|
||||||
|
}
|
||||||
i++;
|
i++;
|
||||||
} else if (line.includes("-->")) {
|
} else if (line.includes("-->")) {
|
||||||
// standard: timestamp on same line
|
// standard: timestamp on same line
|
||||||
const cue = parseVttCue(line, lines, i);
|
const cue = parseVttCue(line, lines, i);
|
||||||
if (cue) cues.push(cue);
|
if (cue) {
|
||||||
|
cues.push(cue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cues;
|
return cues;
|
||||||
|
|||||||
Reference in New Issue
Block a user