@pr0gramm/fluester
Version:
Node.js bindings for OpenAI's Whisper. Optimized for CPU.
31 lines (30 loc) • 1.02 kB
JavaScript
/**
* Creates a WebVTT string from a transcript which can be used for <track> elements of a <video> or <audio> tag.
*
* References:
* - https://en.wikipedia.org/wiki/WebVTT
* - https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API
*
* @throws If a timestamp has an invalid length
* @throws If start > end
*/
export function createWebVttString(transcript) {
const builder = ["WEBVTT", ""];
for (const { start, end, speech } of transcript) {
if (start > end) {
throw new Error("start > end");
}
if ("00:00:00.000".length !== start.length) {
throw new Error("00:00:00.000.length !== start.length");
}
if ("00:00:00.000".length !== end.length) {
throw new Error("00:00:00.000.length !== end.length");
}
builder.push(`${start} --> ${end}`);
for (const split of speech.split("\n")) {
builder.push(`${split}`);
builder.push("");
}
}
return builder.join("\n").trim();
}