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: '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: '#FFDD00', label: 'Gold' }, { 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: 'Karaoke' }, { value: 'none', label: 'None' }, ]; 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 = [ { 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 }, ]; 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, isProcessing, videoUrl, jobId, clipIndex, existingHook, bulkCount = 0, bulkProgress }) { const [position, setPosition] = useState('bottom'); const [fontSize] = useState(24); const [fontName, setFontName] = useState('Verdana'); const [fontColor, setFontColor] = useState('#FFFFFF'); const [highlightColor, setHighlightColor] = useState('#FFDD00'); const [borderColor, setBorderColor] = useState('#000000'); const [borderWidth, setBorderWidth] = useState(2); const [bgColor, setBgColor] = useState('#000000'); const [bgOpacity, setBgOpacity] = useState(0.0); const [animation, setAnimation] = useState('pop'); const [showTextEditor, setShowTextEditor] = useState(false); // Karaoke (server-side ASS burn) state const [style, setStyle] = useState('classic'); // classic | karaoke const [effect, setEffect] = useState('none'); // none | glow | pop | box const [baseOpacity, setBaseOpacity] = useState(1.0); const [uppercase, setUppercase] = useState(false); const [activePreset, setActivePreset] = useState(null); 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); // Keep the Remotion preview roughly in sync with the burned look setAnimation(p.style === 'karaoke' ? (p.effect === 'pop' ? 'pop' : p.effect === 'glow' ? 'word-highlight' : 'karaoke') : 'none'); }; // 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, style: { fontFamily: fontName, fontSize: fontSize * 2.2, // Scale up for 1080p (modal fontSize is for small preview) 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, }, }; // 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

{/* Animation Style (new) */}

Animation

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