/** * AI 生成视频弹窗 * 多选图片 + 标题 + 模型 KEY → 一键生成带字幕和朗读的视频 * 后端:POST /api/video/aigenerate(异步任务)+ GET /api/video/task/:id 轮询 */ import { useEffect, useRef, useState } from 'react'; import { X, Loader2, Film, CheckCircle2, AlertCircle, KeyRound } from 'lucide-react'; import { imageAPI } from '../api'; import { getToken } from '../api/client'; // 后端错误可能为字符串或 {code,message} 对象,统一取可读文本 const errText = (e, fallback) => { if (!e) return fallback; if (typeof e === 'string') return e; return e.message || JSON.stringify(e); }; const VOICES = [ { key: 'zh-CN-XiaoxiaoNeural', label: '晓晓(女声·自然)' }, { key: 'zh-CN-YunxiNeural', label: '云希(男声·温暖)' }, { key: 'zh-CN-YunjianNeural', label: '云健(男声·浑厚)' }, { key: 'zh-CN-XiaoyiNeural', label: '晓伊(女声·活泼)' }, { key: 'zh-CN-YunyangNeural', label: '云扬(男声·播音)' }, { key: 'zh-TW-HsiaoChenNeural', label: '晓臻(台湾腔)' } ]; const RESOLUTIONS = [ { key: '1920x1080', label: '横屏 1920×1080' }, { key: '1080x1920', label: '竖屏 1080×1920' }, { key: '1280x720', label: '横屏 1280×720' } ]; const STEPS = { script: '正在生成分镜脚本', tts: '正在朗读配音', compose: '正在合成画面', merge: '正在合并视频', done: '完成' }; function AIVideoModal({ libraryId, images, onClose, onDone }) { const [title, setTitle] = useState(''); const [apiKey, setApiKey] = useState(''); const [showKey, setShowKey] = useState(false); const [voice, setVoice] = useState(VOICES[0].key); const [resolution, setResolution] = useState('1920x1080'); const [extraPrompt, setExtraPrompt] = useState(''); const [taskId, setTaskId] = useState(''); const [task, setTask] = useState(null); const [error, setError] = useState(''); const [starting, setStarting] = useState(false); const timerRef = useRef(null); const list = Array.isArray(images) ? images : []; // 轮询任务进度 useEffect(() => { if (!taskId) return; timerRef.current = setInterval(async () => { try { const res = await fetch(`/api/video/task/${taskId}`, { headers: { Authorization: `Bearer ${getToken()}` } }); if (!res.ok) return; const data = await res.json(); setTask(data); if (data.status === 'done' || data.status === 'error') { clearInterval(timerRef.current); if (data.status === 'done') { setTimeout(() => { onDone && onDone(data.resultPath); onClose(); }, 1200); } } } catch (_) { /* 网络抖动继续轮询 */ } }, 1500); return () => timerRef.current && clearInterval(timerRef.current); }, [taskId, onDone, onClose]); const handleStart = async () => { setError(''); if (!title.trim()) { setError('请填写视频标题'); return; } if (list.length < 2) { setError('请至少选择 2 张图片'); return; } setStarting(true); try { const res = await fetch('/api/video/aigenerate', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}` }, body: JSON.stringify({ libraryId, images: list.map((img) => ({ path: img.path })), title: title.trim(), apiKey: apiKey.trim(), voice, resolution, extraPrompt: extraPrompt.trim() }) }); const data = await res.json().catch(() => ({})); if (!res.ok || data.error) throw new Error(errText(data.error, `启动失败 (${res.status})`)); setTaskId(data.taskId); } catch (e) { setError(e.message || '启动失败'); } finally { setStarting(false); } }; const running = !!taskId && task && task.status === 'running'; const failed = task && task.status === 'error'; const done = task && task.status === 'done'; const label = 'block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1'; const input = 'w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent'; return (
e.stopPropagation()}>

AI 生成视频

{list.length} 张图片
{/* 图片缩略条 */}
{list.map((img, i) => (
{img.filename { e.target.style.display = 'none'; }} /> {i + 1}
))}
setTitle(e.target.value)} placeholder="例如:周末探店 vlog / 产品功能速览" maxLength={60} disabled={running || done} />

AI 将按此标题撰写分镜旁白,成片文件名也用它。

setApiKey(e.target.value)} placeholder="留空则使用服务器已配置的 KEY" disabled={running || done} />
setExtraPrompt(e.target.value)} placeholder="例如:语气轻松活泼 / 强调性价比 / 结尾加行动号召" maxLength={100} disabled={running || done} />
{/* 进度 */} {(running || done || failed) && (
{failed ? : done ? : } {failed ? '生成失败' : done ? '生成完成,正在打开…' : (task.stepLabel || STEPS[task.step] || '处理中')} {running && {task.progress || 0}%}
{running && (
)} {failed &&

{task.error}

} {done && task.resultPath &&

已保存:{task.resultPath}

}
)} {error &&
{error}
}

流程:AI 写分镜脚本 → 逐段朗读配音 → 图片逐段配字幕合成 → 拼接成片入库。图片越多耗时越长(约 10–60 秒/张)。

{!running && !done && ( )}
); } export default AIVideoModal;