UNPKG

@coursebuilder/core

Version:

Core package for Course Builder

133 lines (127 loc) 3.2 kB
import { __name } from "./chunk-VLQXSCFN.js"; // src/inngest/video-processing/utils.ts import SrtParser2 from "srt-parser-2"; async function mergeSrtWithScreenshots(srt, muxPlaybackId) { let parser = new SrtParser2(); let result = parser.fromSrt(srt); let timesWithWords = result.filter((x) => { for (let word of triggerWords) { if (x.text.toLowerCase().includes(word)) { return true; } } return false; }); let timesWithWordsScreenshots = []; let screenshotNumber = 1; for (let time of timesWithWords) { try { const startTime = time.startTime.split(",")[0]; const [hours, minutes, seconds] = startTime?.split(":").map(Number); const totalSeconds = hours * 3600 + minutes * 60 + seconds; const muxThumbnailUrl = `https://image.mux.com/${muxPlaybackId}/thumbnail.png?width=800&height=600`; timesWithWordsScreenshots.push({ ...time, screenshot: muxThumbnailUrl + `&time=${totalSeconds}` }); } catch (e) { console.log(e); } screenshotNumber++; } let resultWithScreenshots = result.map((x) => { let found = timesWithWordsScreenshots.find((y) => y.startTime === x.startTime); if (found) { return { ...x, screenshot: found.screenshot }; } return x; }); let withTimes = resultWithScreenshots.map((line) => { return { ...line, totalSeconds: line.endSeconds - line.startSeconds }; }); let timeLimitInSeconds = 20; let currentTimeInSeconds = 0; let transcribedSentencesCount = 0; let arrayByTimes = []; let tempArray = []; withTimes.forEach((x, i) => { if (currentTimeInSeconds + x.totalSeconds >= timeLimitInSeconds) { arrayByTimes.push(tempArray); tempArray = []; currentTimeInSeconds = 0; transcribedSentencesCount = 0; } if (currentTimeInSeconds === 0) { if (x.screenshot) { tempArray.push(`[${formatMdTimeString(x.startTime.split(",")[0])}] ${x.text} ![](${x.screenshot}) `); } else { tempArray.push(`[${formatMdTimeString(x.startTime.split(",")[0])}] ${x.text}`); } } else { if (x.screenshot) { tempArray.push(`${x.text} ![](${x.screenshot}) `); } else { tempArray.push(`${x.text}`); } } currentTimeInSeconds += x.totalSeconds; transcribedSentencesCount++; }); arrayByTimes.push(tempArray); let transcript = [ ...arrayByTimes.map((x) => x.join(" ")).flat().join("\n\n") ].join(""); return { transcriptWithScreenshots: transcript, resultWithScreenshots }; } __name(mergeSrtWithScreenshots, "mergeSrtWithScreenshots"); function formatMdTimeString(str) { let [h, m, s] = str.split(":"); if (h == "00") { return `${m}:${s}`; } return `${h}:${m}:${s}`; } __name(formatMdTimeString, "formatMdTimeString"); var triggerWords = [ "here", "have", "there", "this", "that", "see", "these", "look", "show", "watch", "notice", "line", "where", "say", "go", "do", "can", "hover", "click", "function", "component", "variable" ]; export { mergeSrtWithScreenshots }; //# sourceMappingURL=chunk-XLXVFTME.js.map