Skip to content

Instantly share code, notes, and snippets.

@ziedbentahar
Last active December 31, 2023 11:43
Show Gist options
  • Save ziedbentahar/9ecc91f8f6c6dbea0cb443b93d54e8e8 to your computer and use it in GitHub Desktop.
Save ziedbentahar/9ecc91f8f6c6dbea0cb443b93d54e8e8 to your computer and use it in GitHub Desktop.
import { storeTranscript } from "adapters/transcript-repository";
import { YoutubeTranscript } from "youtube-transcript";
export const handler = async (event: {
youtubeVideoUrl: string;
requestId: string;
}) => {
const { youtubeVideoUrl, requestId } = event;
const transcript = await YoutubeTranscript.fetchTranscript(youtubeVideoUrl);
const sentences = Array.from(getSentencesFromYoutubeTranscript(transcript));
await storeTranscript(requestId, sentences.join("\n"));
};
function* getSentencesFromYoutubeTranscript(transcript: { text: string }[]) {
let currentSentence: string[] = [];
let i = 0;
do {
const { text } = transcript[i];
currentSentence.push(text);
if (text.endsWith(".")) {
yield currentSentence.join(" ").replaceAll("\n", " ");
currentSentence = [];
}
i++;
} while (i < transcript.length);
yield currentSentence.join(" ").replaceAll("\n", " ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment