clipwright/dashboard/src/remotion/compositions/Subtitles.tsx
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

227 lines
5.5 KiB
TypeScript

import React from "react";
import {
AbsoluteFill,
Sequence,
useCurrentFrame,
useVideoConfig,
spring,
interpolate,
} from "remotion";
import type { SubtitleConfig } from "../lib/types";
import { groupCaptionsIntoBlocks, getActiveWordIndex } from "../lib/captions";
import { getFontStack } from "../lib/fonts";
interface SubtitlesProps {
config: SubtitleConfig;
}
const POSITION_MAP: Record<string, React.CSSProperties> = {
top: { top: "12%", bottom: "auto" },
middle: { top: "45%", bottom: "auto" },
bottom: { bottom: "10%", top: "auto" },
};
export const Subtitles: React.FC<SubtitlesProps> = ({ config }) => {
const { fps } = useVideoConfig();
const blocks = groupCaptionsIntoBlocks(config.captions);
return (
<AbsoluteFill>
{blocks.map((block, i) => {
const startFrame = Math.round((block.startMs / 1000) * fps);
const durationFrames = Math.max(
1,
Math.round(((block.endMs - block.startMs) / 1000) * fps)
);
return (
<Sequence
key={i}
from={startFrame}
durationInFrames={durationFrames}
layout="none"
>
<SubtitleBlock
block={block}
config={config}
blockStartMs={block.startMs}
/>
</Sequence>
);
})}
</AbsoluteFill>
);
};
interface SubtitleBlockProps {
block: ReturnType<typeof groupCaptionsIntoBlocks>[number];
config: SubtitleConfig;
blockStartMs: number;
}
const SubtitleBlock: React.FC<SubtitleBlockProps> = ({
block,
config,
blockStartMs,
}) => {
const frame = useCurrentFrame();
const { fps } = useVideoConfig();
const { style, position } = config;
// Current time relative to composition start (sequence-relative frame)
const currentTimeMs = blockStartMs + (frame / fps) * 1000;
const activeIndex = getActiveWordIndex(block.words, currentTimeMs);
const positionStyle = POSITION_MAP[position] ?? POSITION_MAP.bottom;
const fontStack = getFontStack(style.fontFamily);
// Background box style
const hasBg = style.bgOpacity > 0;
const bgStyle: React.CSSProperties = hasBg
? {
backgroundColor: `${style.bgColor}${Math.round(style.bgOpacity * 255)
.toString(16)
.padStart(2, "0")}`,
borderRadius: 8,
padding: "8px 16px",
}
: {};
return (
<div
style={{
position: "absolute",
left: 0,
right: 0,
display: "flex",
justifyContent: "center",
...positionStyle,
}}
>
<div
style={{
display: "flex",
flexWrap: "wrap",
justifyContent: "center",
gap: "6px 8px",
maxWidth: "85%",
...bgStyle,
}}
>
{block.words.map((word, i) => (
<WordSpan
key={i}
word={word.text}
isActive={i === activeIndex}
style={style}
fontStack={fontStack}
animation={style.animation}
frame={frame}
fps={fps}
wordStartMs={word.startMs}
blockStartMs={blockStartMs}
/>
))}
</div>
</div>
);
};
interface WordSpanProps {
word: string;
isActive: boolean;
style: SubtitleConfig["style"];
fontStack: string;
animation: SubtitleConfig["style"]["animation"];
frame: number;
fps: number;
wordStartMs: number;
blockStartMs: number;
}
const WordSpan: React.FC<WordSpanProps> = ({
word,
isActive,
style,
fontStack,
animation,
frame,
fps,
wordStartMs,
blockStartMs,
}) => {
const wordStartFrame = Math.round(
((wordStartMs - blockStartMs) / 1000) * fps
);
let transform = "";
let color = style.fontColor;
let extraStyle: React.CSSProperties = {};
if (isActive) {
color = style.highlightColor;
switch (animation) {
case "pop": {
const scale = spring({
frame: frame - wordStartFrame,
fps,
config: { mass: 0.5, stiffness: 300, damping: 12 },
durationInFrames: 10,
});
const scaleValue = interpolate(scale, [0, 1], [1, 1.25]);
transform = `scale(${scaleValue})`;
break;
}
case "karaoke": {
extraStyle = {
backgroundColor: style.highlightColor,
color: style.bgColor || "#000000",
borderRadius: 4,
padding: "2px 6px",
};
break;
}
case "word-highlight": {
extraStyle = {
textShadow: `0 0 12px ${style.highlightColor}, 0 0 24px ${style.highlightColor}40`,
};
break;
}
default:
break;
}
}
// Text stroke via textShadow (CSS paint-order not reliable in Remotion)
const strokeShadow =
style.borderWidth > 0
? [
`${style.borderWidth}px 0 0 ${style.borderColor}`,
`-${style.borderWidth}px 0 0 ${style.borderColor}`,
`0 ${style.borderWidth}px 0 ${style.borderColor}`,
`0 -${style.borderWidth}px 0 ${style.borderColor}`,
].join(", ")
: "none";
return (
<span
style={{
fontFamily: fontStack,
fontSize: style.fontSize,
fontWeight: 700,
color: animation === "karaoke" && isActive ? undefined : color,
textShadow:
animation !== "karaoke"
? [strokeShadow, extraStyle.textShadow].filter(Boolean).join(", ")
: strokeShadow,
transform,
display: "inline-block",
transition: "none",
...extraStyle,
}}
>
{word}
</span>
);
};