Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add transcript VTT cue text utilities

Richie McIlroy1ab10262831b
diff --git a/apps/web/lib/transcript-vtt.ts b/apps/web/lib/transcript-vtt.ts
new file mode 100644
index 000000000..994468995
--- /dev/null
+++ b/apps/web/lib/transcript-vtt.ts
@@ -0,0 +1,69 @@
+export function normalizeTranscriptCueText(text: string): string {
+ return text.replace(/\s+/g, " ").trim();
+}
+
+export function updateVttEntryText(
+ vttContent: string,
+ entryId: number,
+ newText: string,
+): { content: string; updated: boolean } {
+ const normalizedText = normalizeTranscriptCueText(newText);
+ const lines = vttContent.split(/\r?\n/);
+ const updatedLines: string[] = [];
+ let index = 0;
+ let updated = false;
+
+ while (index < lines.length) {
+ const line = lines[index] ?? "";
+ const trimmedLine = line.trim();
+
+ if (!/^\d+$/.test(trimmedLine)) {
+ updatedLines.push(line);
+ index++;
+ continue;
+ }
+
+ const cueId = parseInt(trimmedLine, 10);
+ const cueStart = index;
+ let cueEnd = cueStart + 1;
+
+ while (cueEnd < lines.length && (lines[cueEnd] ?? "").trim() !== "") {
+ cueEnd++;
+ }
+
+ if (cueId !== entryId) {
+ updatedLines.push(...lines.slice(cueStart, cueEnd));
+ if (cueEnd < lines.length) {
+ updatedLines.push(lines[cueEnd] ?? "");
+ }
+ index = cueEnd + 1;
+ continue;
+ }
+
+ const cueLines = lines.slice(cueStart, cueEnd);
+ const timingIndex = cueLines.findIndex((cueLine) =>
+ cueLine.includes("-->"),
+ );
+
+ if (timingIndex === -1) {
+ updatedLines.push(...cueLines);
+ if (cueEnd < lines.length) {
+ updatedLines.push(lines[cueEnd] ?? "");
+ }
+ index = cueEnd + 1;
+ continue;
+ }
+
+ updatedLines.push(...cueLines.slice(0, timingIndex + 1), normalizedText);
+ if (cueEnd < lines.length) {
+ updatedLines.push(lines[cueEnd] ?? "");
+ }
+ updated = true;
+ index = cueEnd + 1;
+ }
+
+ return {
+ content: updatedLines.join("\n"),
+ updated,
+ };
+}