clipwright/dashboard/src/components/SubtitleModal.jsx
juancarlos.cavero 7162667bbf fix(ui): keep subtitle preview playable for karaoke presets
Selecting a karaoke preset swapped the playable RemotionPreview for a static
CSS mock, so the preview video couldn't be played/scrubbed. Instead, teach
the Remotion Subtitles component to render dim (baseOpacity, opaque scaled
color like the backend) and uppercase, and pass those from the modal — so
the preview stays a live, playable Player that also reflects the karaoke
look. Drops the static mock.
2026-07-17 21:26:04 +02:00

489 lines
27 KiB
JavaScript

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 (
<Modal isOpen={isOpen} onClose={onClose} size="xl" eyebrow="EDITOR · SUBTITLES" title="subtitles">
<div className="flex flex-col md:flex-row gap-6">
{/* Left: Preview */}
<div className="flex-1 flex flex-col items-center justify-center bg-black rounded-card border border-rule overflow-hidden relative aspect-[9/16] max-h-[600px]">
{captionsLoading ? (
<div className="flex items-center gap-2 text-muted">
<Loader2 size={16} className="animate-spin" />
<span className="text-sm lowercase">Loading preview...</span>
</div>
) : useRemotionPreview ? (
<RemotionPreview
videoUrl={videoUrl}
durationInSeconds={durationSec}
subtitles={subtitleConfig}
hook={existingHook || null}
/>
) : (
<>
<video src={videoUrl} className="w-full h-full object-contain opacity-50" muted playsInline />
<div className={`absolute w-full px-8 text-center transition-all duration-300 pointer-events-none flex flex-col items-center justify-center
${position === 'top' ? 'top-20' : ''}
${position === 'middle' ? 'top-0 bottom-0' : ''}
${position === 'bottom' ? 'bottom-20' : ''}
`}>
<span style={fallbackPreviewStyle}>
This is how your subtitles<br/>will appear on the video
</span>
</div>
</>
)}
</div>
{/* Right: Controls */}
<div className="w-full md:w-80 flex flex-col">
<div className="space-y-5 flex-1 overflow-y-auto custom-scrollbar pr-1">
{/* Caption presets (server-side karaoke burn) */}
<div>
<p className="eyebrow mb-2">Preset</p>
<div className="grid grid-cols-3 gap-1.5">
{CAPTION_PRESETS.map((p) => (
<button
key={p.id}
onClick={() => applyPreset(p)}
className={`px-2 py-1.5 rounded-input border text-xs transition-colors flex items-center gap-1.5 justify-center
${activePreset === p.id
? 'border-[color:var(--color-accent)] text-ink'
: 'border-rule2 text-muted hover:border-[color:var(--color-accent)]'}`}
title={p.label}
>
<span className="w-2 h-2 rounded-full shrink-0" style={{ backgroundColor: p.highlightColor }} />
{p.label}
</button>
))}
</div>
{style === 'karaoke' && (
<div className="mt-3 space-y-3 animate-fade">
<div className="flex items-center justify-between">
<span className="readout">UPPERCASE</span>
<label className="relative inline-flex items-center cursor-pointer">
<input type="checkbox" checked={uppercase} onChange={(e) => setUppercase(e.target.checked)} className="sr-only peer" />
<div className="w-8 h-4 rounded-full bg-paper3 peer-checked:bg-brass transition-colors after:content-[''] after:absolute after:top-0 after:left-0 after:h-4 after:w-4 after:rounded-full after:bg-ink after:transition-all peer-checked:after:translate-x-full"></div>
</label>
</div>
<div>
<div className="flex justify-between mb-1">
<span className="readout">Dim inactive words</span>
<span className="readout">{Math.round(baseOpacity * 100)}%</span>
</div>
<input
type="range"
min="30"
max="100"
value={Math.round(baseOpacity * 100)}
onChange={(e) => setBaseOpacity(parseInt(e.target.value) / 100)}
className="w-full accent-[var(--color-accent)]"
/>
</div>
</div>
)}
</div>
{/* Position Selector */}
<div>
<p className="eyebrow mb-2">Position</p>
<SegmentedControl
options={POSITION_OPTIONS}
value={position}
onChange={setPosition}
size="sm"
/>
</div>
{/* Animation Style (new) */}
<div>
<p className="eyebrow mb-2">Animation</p>
<SegmentedControl
options={ANIMATION_OPTIONS}
value={animation}
onChange={setAnimation}
columns={2}
size="sm"
/>
</div>
{/* Editable Transcript (collapsible) */}
{useRemotionPreview && (
<div>
<button
type="button"
onClick={() => setShowTextEditor(!showTextEditor)}
className="w-full flex items-center justify-between mb-2"
>
<span className="eyebrow">Edit text ({captions.length} words)</span>
<span className={`text-muted transition-transform ${showTextEditor ? 'rotate-180' : ''}`}>▾</span>
</button>
{showTextEditor && (
<textarea
value={editableText}
onChange={(e) => handleTextEdit(e.target.value)}
rows={5}
className="input-field resize-none leading-relaxed animate-fade"
placeholder="Edit subtitle text..."
/>
)}
</div>
)}
{/* Font Family */}
<div>
<p className="eyebrow mb-2">Font</p>
<select
value={fontName}
onChange={(e) => setFontName(e.target.value)}
className="input-field"
>
{FONT_OPTIONS.map((f) => (
<option key={f.value} value={f.value} style={{ fontFamily: f.value }}>{f.label}</option>
))}
</select>
</div>
{/* Text Color */}
<div>
<p className="eyebrow mb-2">Text color</p>
<div className="flex flex-wrap items-center gap-2.5">
{COLOR_PRESETS.map((c) => (
<button
key={c.color}
onClick={() => setFontColor(c.color)}
className={swatchClass(fontColor === c.color)}
style={{ backgroundColor: c.color }}
title={c.label}
/>
))}
<label className="w-6 h-6 rounded-full border border-dashed border-rule2 cursor-pointer flex items-center justify-center hover:border-brass transition-colors overflow-hidden relative" title="Custom color">
<span className="text-xs text-muted leading-none">+</span>
<input type="color" value={fontColor} onChange={(e) => setFontColor(e.target.value)} className="absolute inset-0 opacity-0 cursor-pointer" />
</label>
</div>
</div>
{/* Highlight Color (new) */}
<div>
<p className="eyebrow mb-2">Highlight</p>
<div className="flex flex-wrap items-center gap-2.5">
{HIGHLIGHT_PRESETS.map((c) => (
<button
key={c.color}
onClick={() => setHighlightColor(c.color)}
className={swatchClass(highlightColor === c.color)}
style={{ backgroundColor: c.color }}
title={c.label}
/>
))}
</div>
</div>
{/* Border / Outline */}
<div>
<p className="eyebrow mb-2">Border</p>
<div className="flex items-center gap-3">
<label className="relative w-8 h-8 rounded-input border border-rule2 cursor-pointer overflow-hidden shrink-0" title="Border color">
<div className="w-full h-full" style={{ backgroundColor: borderColor }} />
<input type="color" value={borderColor} onChange={(e) => setBorderColor(e.target.value)} className="absolute inset-0 opacity-0 cursor-pointer" />
</label>
<div className="flex-1">
<input
type="range"
min="0"
max="5"
value={borderWidth}
onChange={(e) => setBorderWidth(parseInt(e.target.value))}
className="w-full accent-[var(--color-accent)]"
/>
<div className="flex justify-between">
<span className="readout">None</span>
<span className="readout">Thick</span>
</div>
</div>
</div>
</div>
{/* Background Box */}
<div>
<div className="flex items-center justify-between mb-2">
<p className="eyebrow">Background</p>
<label className="relative inline-flex items-center cursor-pointer">
<input type="checkbox" checked={bgOpacity > 0} onChange={(e) => setBgOpacity(e.target.checked ? 0.5 : 0)} className="sr-only peer" />
<div className="w-8 h-4 rounded-full bg-paper3 peer-checked:bg-brass transition-colors after:content-[''] after:absolute after:top-0 after:left-0 after:h-4 after:w-4 after:rounded-full after:bg-ink after:transition-all peer-checked:after:translate-x-full"></div>
</label>
</div>
{bgOpacity > 0 && (
<div className="space-y-3 animate-fade">
<div className="flex items-center gap-3">
<label className="relative w-8 h-8 rounded-input border border-rule2 cursor-pointer overflow-hidden shrink-0" title="Background color">
<div className="w-full h-full" style={{ backgroundColor: bgColor }} />
<input type="color" value={bgColor} onChange={(e) => setBgColor(e.target.value)} className="absolute inset-0 opacity-0 cursor-pointer" />
</label>
<div className="flex-1">
<input
type="range"
min="10"
max="100"
value={Math.round(bgOpacity * 100)}
onChange={(e) => setBgOpacity(parseInt(e.target.value) / 100)}
className="w-full accent-[var(--color-accent)]"
/>
<div className="flex justify-between">
<span className="readout">Transparent</span>
<span className="readout">{Math.round(bgOpacity * 100)}%</span>
</div>
</div>
</div>
</div>
)}
</div>
</div>
<div className="mt-5 shrink-0 space-y-2">
{(() => {
const styleOptions = {
position, fontSize, fontName, fontColor, borderColor, borderWidth, bgColor, bgOpacity,
// Karaoke burn (server-side ASS render)
style, effect, baseOpacity, uppercase, highlightColor,
// Remotion data
remotion: useRemotionPreview ? subtitleConfig : null,
};
const bulkRunning = bulkProgress?.running;
return (
<>
<div className="flex gap-2">
<button onClick={onClose} className="btn-ghost">
cancel
</button>
<button
onClick={() => onGenerate(styleOptions)}
disabled={isProcessing}
className="btn-primary flex-1"
>
{(isProcessing && !bulkRunning) && <Loader2 size={16} className="animate-spin text-brassink" />}
{(isProcessing && !bulkRunning) ? 'generating...' : 'apply to this clip'}
</button>
</div>
{onApplyAll && bulkCount > 1 && (
<button
onClick={() => onApplyAll(styleOptions)}
disabled={isProcessing}
className="btn-ghost w-full flex items-center justify-center gap-2"
>
{bulkRunning
? <><Loader2 size={16} className="animate-spin" />applying to all… {bulkProgress.current}/{bulkProgress.total}</>
: `apply this style to all ${bulkCount} clips`}
</button>
)}
</>
);
})()}
</div>
</div>
</div>
</Modal>
);
}