/** * 视频编辑弹窗 * 四项常用编辑:加水印 / 去水印 / 添加字幕+声音 / 去掉字幕+声音 * 后端:POST /api/video/edit(同步处理,另存为新图或覆盖原视频) */ import { useState } from 'react'; import { X, Droplets, Eraser, Type, Scissors, Loader2 } from 'lucide-react'; import { imageAPI } from '../api'; const ACTIONS = [ { key: 'watermark-add', label: '加水印', icon: Droplets }, { key: 'watermark-remove', label: '去水印', icon: Eraser }, { key: 'subtitle-audio', label: '加字幕声音', icon: Type }, { key: 'strip', label: '去字幕声音', icon: Scissors } ]; const POSITIONS = [ { key: 'bottom-right', label: '右下角' }, { key: 'bottom-left', label: '左下角' }, { key: 'top-right', label: '右上角' }, { key: 'top-left', label: '左上角' }, { key: 'top-center', label: '顶部居中' }, { key: 'bottom-center', label: '底部居中' }, { key: 'center', label: '画面居中' } ]; // 去水印预设区域(百分比,与批量水印一致) const WM_REGIONS = [ { key: 'bottom-right', label: '右下角', region: { x: 68, y: 86, w: 30, h: 11 } }, { key: 'bottom-left', label: '左下角', region: { x: 2, y: 86, w: 30, h: 11 } }, { key: 'top-right', label: '右上角', region: { x: 68, y: 3, w: 30, h: 11 } }, { key: 'top-left', label: '左上角', region: { x: 2, y: 3, w: 30, h: 11 } }, { key: 'center', label: '画面居中', region: { x: 35, y: 42, w: 30, h: 16 } }, { key: 'bottom-bar', label: '底部横条', region: { x: 0, y: 88, w: 100, h: 11 } }, { key: 'right-bar', label: '右侧竖条', region: { x: 85, y: 0, w: 15, h: 100 } }, { key: 'custom', label: '自定义', region: null } ]; 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-CN-liaoning-XiaobeiNeural', label: '晓北(东北话)' }, { key: 'zh-TW-HsiaoChenNeural', label: '晓臻(台湾腔)' } ]; const clampPct = (n) => Math.max(0, Math.min(100, Number(n) || 0)); function VideoEditModal({ file, libraryId, onClose, onDone }) { const [action, setAction] = useState('watermark-add'); const [busy, setBusy] = useState(false); const [error, setError] = useState(''); const [outputMode, setOutputMode] = useState('new'); // 加水印 const [wmText, setWmText] = useState(''); const [wmPos, setWmPos] = useState('bottom-right'); const [wmSize, setWmSize] = useState(4); const [wmOpacity, setWmOpacity] = useState(0.6); const [wmColor, setWmColor] = useState('#ffffff'); const [wmTile, setWmTile] = useState(false); // 去水印 const [regionKey, setRegionKey] = useState('bottom-right'); const [region, setRegion] = useState({ x: 68, y: 86, w: 30, h: 11 }); // 字幕声音 const [subText, setSubText] = useState(''); const [voice, setVoice] = useState(VOICES[0].key); const [audioMode, setAudioMode] = useState('tts'); // tts | none const [subSize, setSubSize] = useState(5); // 去字幕声音 const [rmAudio, setRmAudio] = useState(true); const [rmSub, setRmSub] = useState(true); if (!file) return null; const thumbUrl = file.thumbnailPath ? imageAPI.getThumbnailUrl(libraryId, file.thumbnailPath.replace(/\\/g, '/').split('/').pop(), file.modifiedAt) : ''; const handleSubmit = async () => { setError(''); let options = {}; if (action === 'watermark-add') { if (!wmText.trim()) { setError('请填写水印文字'); return; } options = { text: wmText.trim(), position: wmPos, fontSizePct: wmSize, opacity: wmOpacity, color: wmColor, tile: wmTile }; } else if (action === 'watermark-remove') { options = { region }; } else if (action === 'subtitle-audio') { if (!subText.trim()) { setError('请填写字幕内容'); return; } options = { subtitleText: subText.trim(), ttsText: audioMode === 'tts' ? subText.trim() : '', voice, audioMode: audioMode === 'tts' ? 'tts' : 'none', fontSizePct: subSize, opacity: 0.95 }; } else if (action === 'strip') { if (!rmAudio && !rmSub) { setError('请至少勾选一项'); return; } options = { removeAudio: rmAudio, removeSubtitle: rmSub }; } setBusy(true); try { const res = await fetch('/api/video/edit', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ libraryId, path: file.path, action, options, output: outputMode }) }); const data = await res.json().catch(() => ({})); if (!res.ok || data.error) throw new Error(data.error || `处理失败 (${res.status})`); onDone && onDone(data.path); onClose(); } catch (e) { setError(e.message || '处理失败'); } finally { setBusy(false); } }; 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()}> {/* 头部 */}

视频编辑

{file.filename}
{/* 功能切换 */}
{ACTIONS.map((a) => { const Icon = a.icon; return ( ); })}
{/* 预览缩略图 + 去水印区域框 */} {thumbUrl && (
{action === 'watermark-remove' && (
)}
)} {/* ===== 加水印 ===== */} {action === 'watermark-add' && ( <>
setWmText(e.target.value)} placeholder="例如:© 资享网 或 88531.cn" maxLength={50} />
setWmColor(e.target.value)} />
setWmSize(Number(e.target.value))} className="w-full" />
setWmOpacity(Number(e.target.value))} className="w-full" />
)} {/* ===== 去水印 ===== */} {action === 'watermark-remove' && ( <>
{WM_REGIONS.map((p) => ( ))}
{regionKey === 'custom' && (
{['x', 'y', 'w', 'h'].map((k) => (
setRegion({ ...region, [k]: clampPct(e.target.value) })} />
))}
)}

提示:去水印通过平滑插值抹除区域,适合纯色/简单背景的台标与角标。

)} {/* ===== 加字幕声音 ===== */} {action === 'subtitle-audio' && ( <>