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 = { top: { top: "12%", bottom: "auto" }, middle: { top: "45%", bottom: "auto" }, bottom: { bottom: "10%", top: "auto" }, }; export const Subtitles: React.FC = ({ config }) => { const { fps } = useVideoConfig(); const blocks = groupCaptionsIntoBlocks(config.captions); return ( {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 ( ); })} ); }; interface SubtitleBlockProps { block: ReturnType[number]; config: SubtitleConfig; blockStartMs: number; } const SubtitleBlock: React.FC = ({ 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 (
{block.words.map((word, i) => ( ))}
); }; 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 = ({ 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 ( {word} ); };