Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Group transcriptions into smaller amounts

Richie McIlroyac9b4104fa5d
diff --git a/apps/web/app/api/transcribe/route.ts b/apps/web/app/api/transcribe/route.ts
index a9a754cdc..9a4b1f59a 100644
--- a/apps/web/app/api/transcribe/route.ts
+++ b/apps/web/app/api/transcribe/route.ts
@@ -212,13 +212,36 @@ export async function GET(request: NextRequest) {
function formatToWebVTT(result: any): string {
let output = "WEBVTT\n\n";
-
- result.results.utterances.forEach((utterance: any, index: number) => {
- const start = formatTimestamp(utterance.start);
- const end = formatTimestamp(utterance.end);
- const text = utterance.transcript;
-
- output += `${index + 1}\n${start} --> ${end}\n${text}\n\n`;
+ let captionIndex = 1;
+
+ result.results.utterances.forEach((utterance: any) => {
+ let words = utterance.words;
+ let group = [];
+ let start = formatTimestamp(words[0].start);
+ let wordCount = 0;
+
+ for (let i = 0; i < words.length; i++) {
+ let word = words[i];
+ group.push(word.word);
+ wordCount++;
+
+ if (
+ word.punctuated_word.endsWith(",") ||
+ word.punctuated_word.endsWith(".") ||
+ (words[i + 1] && words[i + 1].start - word.end > 0.5) ||
+ wordCount === 8
+ ) {
+ let end = formatTimestamp(word.end);
+ let groupText = group.join(" ");
+
+ output += `${captionIndex}\n${start} --> ${end}\n${groupText}\n\n`;
+ captionIndex++;
+
+ group = [];
+ start = words[i + 1] ? formatTimestamp(words[i + 1].start) : start;
+ wordCount = 0; // Reset the counter for the next group
+ }
+ }
});
return output;