clipwright/dashboard/src/remotion/lib/captions.ts
juancarlos.cavero 9c9342c499 feat: Add Remotion integration for animated subtitles, hooks and effects
Replace static FFmpeg post-processing with Remotion-powered browser rendering:
- Animated word-by-word subtitles (pop, karaoke, glow styles) with real-time preview
- Hook overlays with entrance animations (spring, fade, slide-up)
- Video effects layer (zoom, brightness, contrast, saturate)
- Browser-side rendering via @remotion/web-renderer (WebCodecs)
- Editable transcript text in subtitle modal
- Layers accumulate across operations (subtitles + hook rendered together)
- Gemini EffectsConfig endpoint for structured JSON instead of fragile FFmpeg strings
- Render service (Node.js) as alternative server-side renderer
- New API endpoints: /api/clip/transcript, /api/effects/generate, /api/render

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 14:00:11 +02:00

83 lines
1.9 KiB
TypeScript

import type { CaptionWord } from "./types";
export interface CaptionBlock {
words: CaptionWord[];
startMs: number;
endMs: number;
text: string;
}
/**
* Groups word-level captions into display blocks.
* Same logic as OpenShorts' generate_srt: max chars per block, max duration per block.
*/
export function groupCaptionsIntoBlocks(
captions: CaptionWord[],
maxChars = 20,
maxDurationMs = 2000
): CaptionBlock[] {
const blocks: CaptionBlock[] = [];
let currentWords: CaptionWord[] = [];
let blockStartMs = 0;
for (const word of captions) {
if (currentWords.length === 0) {
currentWords.push(word);
blockStartMs = word.startMs;
continue;
}
const currentTextLen = currentWords.reduce(
(sum, w) => sum + w.text.length + 1,
0
);
const duration = word.endMs - blockStartMs;
if (
currentTextLen + word.text.length > maxChars ||
duration > maxDurationMs
) {
// Finalize current block
const lastWord = currentWords[currentWords.length - 1];
blocks.push({
words: [...currentWords],
startMs: blockStartMs,
endMs: lastWord.endMs,
text: currentWords.map((w) => w.text).join(" "),
});
currentWords = [word];
blockStartMs = word.startMs;
} else {
currentWords.push(word);
}
}
// Final block
if (currentWords.length > 0) {
const lastWord = currentWords[currentWords.length - 1];
blocks.push({
words: [...currentWords],
startMs: blockStartMs,
endMs: lastWord.endMs,
text: currentWords.map((w) => w.text).join(" "),
});
}
return blocks;
}
/**
* Find the active word at a given time in milliseconds.
*/
export function getActiveWordIndex(
words: CaptionWord[],
timeMs: number
): number {
for (let i = 0; i < words.length; i++) {
if (timeMs >= words[i].startMs && timeMs < words[i].endMs) {
return i;
}
}
return -1;
}