import React, { useState, useEffect } from 'react'; import { Loader2 } from 'lucide-react'; import { apiFetch } from '../lib/api'; import RemotionPreview from './RemotionPreview'; import Modal from './ui/Modal'; import SegmentedControl from './ui/SegmentedControl'; const FONT_OPTIONS = [ { value: 'Anton', label: 'Anton' }, { value: 'Montserrat ExtraBold', label: 'Montserrat' }, { value: 'Verdana', label: 'Verdana' }, { value: 'Arial', label: 'Arial' }, { value: 'Impact', label: 'Impact' }, { value: 'Helvetica', label: 'Helvetica' }, { value: 'Georgia', label: 'Georgia' }, { value: 'Courier New', label: 'Courier New' }, ]; const COLOR_PRESETS = [ { color: '#FFFFFF', label: 'White' }, { color: '#FFFF00', label: 'Yellow' }, { color: '#00FFFF', label: 'Cyan' }, { color: '#00FF00', label: 'Green' }, { color: '#FF0000', label: 'Red' }, { color: '#FF69B4', label: 'Pink' }, ]; const HIGHLIGHT_PRESETS = [ { color: '#FFE500', label: 'Yellow' }, { color: '#FFDD00', label: 'Gold' }, { color: '#A3FF12', label: 'Lime' }, { color: '#7C3AED', label: 'Purple' }, { color: '#FF4444', label: 'Red' }, { color: '#00FF88', label: 'Green' }, { color: '#00BBFF', label: 'Blue' }, { color: '#FF69B4', label: 'Pink' }, ]; const ANIMATION_OPTIONS = [ { value: 'pop', label: 'Pop' }, { value: 'word-highlight', label: 'Glow' }, { value: 'karaoke', label: 'Box' }, { value: 'none', label: 'None' }, ]; // Preview animation -> the effect burned server-side, so what the modal // plays is what the clip gets. 'karaoke' in the preview is a box behind the // active word, which is the 'highlight' effect. const ANIMATION_TO_EFFECT = { pop: 'pop', 'word-highlight': 'glow', karaoke: 'highlight', none: 'none' }; const EFFECT_TO_ANIMATION = { pop: 'pop', glow: 'word-highlight', highlight: 'karaoke', box: 'karaoke', none: 'none' }; // Font size in the units /api/subtitle takes. M is what every clip ships // with (subtitles.AUTO_CAPTION_STYLE font_size 44), so opening the modal and // applying never shrinks the captions. const SIZE_OPTIONS = [ { value: 34, label: 'S' }, { value: 44, label: 'M' }, { value: 56, label: 'L' }, { value: 70, label: 'XL' }, ]; // Characters per line at size M, per font: Anton is condensed, Montserrat // wide. Bigger text gets proportionally fewer, so a line still fits the 9:16 // frame instead of wrapping into a wall of text. const LINE_CHARS = { Anton: 16, 'Montserrat ExtraBold': 9, Impact: 16 }; const lineBudget = (fontName, fontSize, oneWord) => oneWord ? 1 : Math.max(6, Math.round((LINE_CHARS[fontName] || 14) * 44 / fontSize)); // libass sizes text against PlayResY 288 (subtitles.generate_ass): one unit // there is ~3.85 CSS px in the 1080x1920 preview, measured on burned frames. const PREVIEW_PX_PER_UNIT = 3.85; const POSITION_OPTIONS = [ { value: 'top', label: 'top' }, { value: 'middle', label: 'middle' }, { value: 'bottom', label: 'bottom' }, ]; // Ready-made caption looks burned server-side as karaoke ASS (word highlight): // dimmed base text + strong active word, optional glow/pop/box effect. const CAPTION_PRESETS = [ // What every clip ships with (subtitles.AUTO_CAPTION_STYLE). { id: 'default', label: 'Default', style: 'karaoke', effect: 'pop', highlightColor: '#FFE500', baseOpacity: 1.0, uppercase: true, fontName: 'Anton', borderWidth: 4, fontSize: 44 }, // Trending short-form looks (2026): word-by-word build-up, a box behind // the active word, one big word at a time, and the clean shadow-only look. { id: 'hormozi', label: 'Hormozi', style: 'karaoke', effect: 'pop', highlightColor: '#FFE500', baseOpacity: 1.0, uppercase: true, fontName: 'Montserrat ExtraBold', borderWidth: 4, shadow: 2, reveal: true, fontSize: 44 }, { id: 'pill', label: 'Pill', style: 'karaoke', effect: 'highlight', highlightColor: '#7C3AED', baseOpacity: 1.0, uppercase: true, fontName: 'Montserrat ExtraBold', borderWidth: 3, fontSize: 44 }, { id: 'oneword', label: 'One word', style: 'karaoke', effect: 'pop', highlightColor: '#FFFFFF', baseOpacity: 1.0, uppercase: true, fontName: 'Anton', borderWidth: 5, oneWord: true, fontSize: 70 }, { id: 'clean', label: 'Clean', style: 'karaoke', effect: 'none', highlightColor: '#FFFFFF', baseOpacity: 0.7, uppercase: false, fontName: 'Montserrat ExtraBold', borderWidth: 0, shadow: 2, fontSize: 34 }, { id: 'lime', label: 'Lime box', style: 'karaoke', effect: 'highlight', highlightColor: '#A3FF12', baseOpacity: 1.0, uppercase: true, fontName: 'Montserrat ExtraBold', borderWidth: 3, fontSize: 44 }, { id: 'tiktok', label: 'TikTok', style: 'karaoke', effect: 'none', highlightColor: '#FE2C55', baseOpacity: 0.75, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, { id: 'reels', label: 'Reels', style: 'karaoke', effect: 'none', highlightColor: '#E1306C', baseOpacity: 0.7, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, { id: 'shorts', label: 'Shorts Pop', style: 'karaoke', effect: 'pop', highlightColor: '#FF0000', baseOpacity: 0.7, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, { id: 'gold', label: 'Gold Glow', style: 'karaoke', effect: 'glow', highlightColor: '#FFD700', baseOpacity: 0.6, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, { id: 'neon', label: 'Neon', style: 'karaoke', effect: 'glow', highlightColor: '#00FF88', baseOpacity: 0.55, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, { id: 'cyber', label: 'Cyber', style: 'karaoke', effect: 'glow', highlightColor: '#00FFFF', baseOpacity: 0.5, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, { id: 'karaoke', label: 'Karaoke', style: 'karaoke', effect: 'none', highlightColor: '#FF6B6B', baseOpacity: 0.6, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, { id: 'minimal', label: 'Minimal', style: 'karaoke', effect: 'none', highlightColor: '#FFFFFF', baseOpacity: 0.65, uppercase: false, fontName: 'Verdana', borderWidth: 1 }, { id: 'beast', label: 'Beast', style: 'karaoke', effect: 'pop', highlightColor: '#FFD700', baseOpacity: 1.0, uppercase: true, fontName: 'Impact', borderWidth: 3 }, { id: 'boxed', label: 'Boxed', style: 'karaoke', effect: 'box', highlightColor: '#7C3AED', baseOpacity: 0.85, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, { id: 'classic', label: 'Classic', style: 'classic', effect: 'none', highlightColor: '#FFD700', baseOpacity: 1.0, uppercase: false, fontName: 'Verdana', borderWidth: 2 }, ]; // Mirrors subtitles._luminance (Rec. 601). const luminance = (hex) => { const m = /^#?([0-9a-fA-F]{6})$/.exec(hex || ''); if (!m) return 1; const [r, g, b] = [0, 2, 4].map((i) => parseInt(m[1].slice(i, i + 2), 16)); return (0.299 * r + 0.587 * g + 0.114 * b) / 255; }; const swatchClass = (selected) => `w-6 h-6 rounded-full transition-all ${selected ? 'ring-2 ring-[color:var(--color-accent)] ring-offset-2 ring-offset-[color:var(--color-paper-2)]' : 'ring-1 ring-[color:var(--color-rule-2)] hover:ring-[color:var(--color-accent)]'}`; export default function SubtitleModal({ isOpen, onClose, onGenerate, onApplyAll, onRemove, isProcessing, videoUrl, jobId, clipIndex, existingHook, bulkCount = 0, bulkProgress }) { // Opens on the look the clip already has (the Default preset), so // "apply" without touching anything changes nothing. const [position, setPosition] = useState('bottom'); const [fontSize, setFontSize] = useState(44); const [fontName, setFontName] = useState('Anton'); const [fontColor, setFontColor] = useState('#FFFFFF'); const [highlightColor, setHighlightColor] = useState('#FFE500'); const [borderColor, setBorderColor] = useState('#000000'); const [borderWidth, setBorderWidth] = useState(4); const [bgColor, setBgColor] = useState('#000000'); const [bgOpacity, setBgOpacity] = useState(0.0); const [animation, setAnimationState] = useState('pop'); const [showTextEditor, setShowTextEditor] = useState(false); // Karaoke (server-side ASS burn) state const [style, setStyle] = useState('karaoke'); // classic | karaoke const [effect, setEffect] = useState('pop'); // none | glow | pop | box | highlight const [baseOpacity, setBaseOpacity] = useState(1.0); const [uppercase, setUppercase] = useState(true); const [reveal, setReveal] = useState(false); const [shadow, setShadow] = useState(0); const [oneWord, setOneWord] = useState(false); const [activePreset, setActivePreset] = useState('default'); const setAnimation = (value) => { setAnimationState(value); if (style === 'karaoke') setEffect(ANIMATION_TO_EFFECT[value] || 'none'); }; const applyPreset = (p) => { setActivePreset(p.id); setStyle(p.style); setEffect(p.effect); setHighlightColor(p.highlightColor); setBaseOpacity(p.baseOpacity); setUppercase(p.uppercase); setFontName(p.fontName); setBorderWidth(p.borderWidth); setFontColor('#FFFFFF'); setBgOpacity(0); setReveal(!!p.reveal); setShadow(p.shadow || 0); setOneWord(!!p.oneWord); if (p.fontSize) setFontSize(p.fontSize); setAnimationState(p.style === 'karaoke' ? (EFFECT_TO_ANIMATION[p.effect] || 'none') : 'none'); }; const maxChars = lineBudget(fontName, fontSize, oneWord); // Same block duration as the auto captions (AUTO_CAPTION_STYLE). const maxDuration = 1.4; // Remotion preview state const [captions, setCaptions] = useState([]); const [originalCaptions, setOriginalCaptions] = useState([]); const [editableText, setEditableText] = useState(''); const [durationSec, setDurationSec] = useState(30); const [captionsLoading, setCaptionsLoading] = useState(false); const [useRemotionPreview, setUseRemotionPreview] = useState(false); // Fetch word-level captions when modal opens useEffect(() => { if (!isOpen || !jobId || clipIndex === undefined) return; setCaptionsLoading(true); apiFetch(`/api/clip/${jobId}/${clipIndex}/transcript`) .then((res) => res.ok ? res.json() : null) .then((data) => { if (data && data.captions && data.captions.length > 0) { setCaptions(data.captions); setOriginalCaptions(data.captions); setEditableText(data.captions.map(c => c.text).join(' ')); setDurationSec(data.durationSec || 30); setUseRemotionPreview(true); } else { setUseRemotionPreview(false); } }) .catch(() => setUseRemotionPreview(false)) .finally(() => setCaptionsLoading(false)); }, [isOpen, jobId, clipIndex]); // When user edits text, redistribute words across original timestamps const handleTextEdit = (newText) => { setEditableText(newText); const newWords = newText.split(/\s+/).filter(w => w.length > 0); if (newWords.length === 0 || originalCaptions.length === 0) { setCaptions([]); return; } // Distribute new words across the time span of original captions const totalDurationMs = originalCaptions[originalCaptions.length - 1].endMs - originalCaptions[0].startMs; const startMs = originalCaptions[0].startMs; const wordDurationMs = totalDurationMs / newWords.length; const newCaptions = newWords.map((word, i) => ({ text: word, startMs: Math.round(startMs + i * wordDurationMs), endMs: Math.round(startMs + (i + 1) * wordDurationMs), })); setCaptions(newCaptions); }; if (!isOpen) return null; // Build subtitle config for Remotion const subtitleConfig = { captions, position, maxChars, maxDurationMs: maxDuration * 1000, style: { fontFamily: fontName, fontSize: Math.round(fontSize * 0.85 * PREVIEW_PX_PER_UNIT), fontColor, highlightColor, borderColor, borderWidth: borderWidth * 1.5, bgColor, bgOpacity, animation, // Karaoke look reflected live in the playable preview. baseOpacity: style === 'karaoke' ? baseOpacity : 1, uppercase: style === 'karaoke' ? uppercase : false, reveal: style === 'karaoke' && reveal, shadow: style === 'karaoke' ? shadow : 0, // Text on the active-word box: black on a light box, as burned. highlightTextColor: luminance(highlightColor) > 0.6 ? '#000000' : fontColor, }, }; // Fallback: static CSS preview (same as original) const bw = Math.max(borderWidth, 0); const bc = borderColor; const outlineShadow = bw > 0 ? [ `-${bw}px -${bw}px 0 ${bc}`, `${bw}px -${bw}px 0 ${bc}`, `-${bw}px ${bw}px 0 ${bc}`, `${bw}px ${bw}px 0 ${bc}`, `0 -${bw}px 0 ${bc}`, `0 ${bw}px 0 ${bc}`, `-${bw}px 0 0 ${bc}`, `${bw}px 0 0 ${bc}`, ].join(', ') : 'none'; const fallbackPreviewStyle = { fontFamily: fontName, color: fontColor, fontSize: '20px', fontWeight: 'bold', maxWidth: '85%', padding: '6px 12px', borderRadius: '4px', textAlign: 'center', lineHeight: '1.3', ...(bgOpacity > 0 ? { backgroundColor: `${bgColor}${Math.round(bgOpacity * 255).toString(16).padStart(2, '0')}`, textShadow: 'none', } : { textShadow: outlineShadow } ), }; return (
{/* Left: Preview */}
{captionsLoading ? (
Loading preview...
) : useRemotionPreview ? ( ) : ( <>
{/* Right: Controls */}
{/* Caption presets (server-side karaoke burn) */}

Preset

{CAPTION_PRESETS.map((p) => ( ))}
{style === 'karaoke' && (
UPPERCASE
Dim inactive words {Math.round(baseOpacity * 100)}%
setBaseOpacity(parseInt(e.target.value) / 100)} className="w-full accent-[var(--color-accent)]" />
)}
{/* Position Selector */}

Position

{/* Size */}

Size

{style === 'karaoke' && (
One word at a time
{!oneWord && (
Reveal word by word
)}
)} {/* Animation Style (new) */}

Animation

{/* Editable Transcript (collapsible) */} {useRemotionPreview && (
{showTextEditor && (