This repository has been archived on 2025-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
invidious-companion/src/lib/helpers/youtubeTranscriptsHandling.ts
syeopite a6e758cd64
Add support for Invidious captions endpoint (#55)
* Add basic skeletons for Invidious captions api

* Add logic to fetch and convert transcripts to vtt

* Use transcripts logic in captions route

* Format

* Fix lint
2025-03-08 18:11:58 +01:00

63 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Innertube } from "youtubei.js";
function createTemporalDuration(milliseconds) {
return new Temporal.Duration(
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
milliseconds,
);
}
const ESCAPE_SUBSTITUTIONS = {
"&": "&",
"<": "&lt;",
">": "&gt;",
"\u200E": "&lrm;",
"\u200F": "&rlm;",
"\u00A0": "&nbsp;",
};
export async function handleTranscripts(
innertubeClient: Innertube,
videoId: string,
selectedCaption,
) {
const lines: string[] = ["WEBVTT"];
const info = await innertubeClient.getInfo(videoId);
const transcriptInfo = await (await info.getTranscript()).selectLanguage(
selectedCaption.name.simpleText,
);
const rawTranscriptLines =
transcriptInfo.transcript.content.body.initial_segments;
rawTranscriptLines.forEach((line) => {
const timestampFormatOptions = {
style: "digital",
minutesDisplay: "always",
fractionalDigits: 3,
};
const start_ms = createTemporalDuration(line.start_ms).round({
largestUnit: "year",
}).toLocaleString(undefined, timestampFormatOptions);
const end_ms = createTemporalDuration(line.end_ms).round({
largestUnit: "year",
}).toLocaleString(undefined, timestampFormatOptions);
const timestamp = `${start_ms} --> ${end_ms}`;
const text = line.snippet.text.replace(
/[&<>\u200E\u200F\u00A0]/g,
(match) => ESCAPE_SUBSTITUTIONS[match],
);
lines.push(`${timestamp}\n${text}`);
});
return lines.join("\n\n");
}