Files
flypic/frontend/src/components/VideoEditModal.jsx
T

343 lines
16 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 视频编辑弹窗
* 四项常用编辑:加水印 / 去水印 / 添加字幕+声音 / 去掉字幕+声音
* 后端: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 (
<div className="fixed inset-0 z-50 bg-black bg-opacity-60 flex items-center justify-center p-4" onClick={busy ? undefined : onClose}>
<div className="bg-white dark:bg-gray-800 rounded-xl shadow-2xl w-full max-w-3xl max-h-[92vh] overflow-y-auto" onClick={(e) => e.stopPropagation()}>
{/* 头部 */}
<div className="flex items-center justify-between px-5 py-4 border-b border-gray-200 dark:border-gray-700 sticky top-0 bg-white dark:bg-gray-800 z-10">
<div className="flex items-center gap-2">
<Scissors className="w-5 h-5 text-blue-500" />
<h2 className="text-lg font-semibold text-gray-900 dark:text-gray-100">视频编辑</h2>
<span className="text-xs text-gray-500 dark:text-gray-400 truncate max-w-xs ml-2">{file.filename}</span>
</div>
<button onClick={onClose} disabled={busy} className="p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded">
<X className="w-5 h-5 text-gray-500" />
</button>
</div>
{/* 功能切换 */}
<div className="grid grid-cols-4 gap-2 px-5 pt-4">
{ACTIONS.map((a) => {
const Icon = a.icon;
return (
<button
key={a.key}
onClick={() => { setAction(a.key); setError(''); }}
disabled={busy}
className={`flex flex-col items-center gap-1 py-3 rounded-lg border text-sm transition-colors ${
action === a.key
? 'border-blue-500 bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-300'
: 'border-gray-200 dark:border-gray-700 text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-gray-750'
}`}
>
<Icon className="w-5 h-5" />
{a.label}
</button>
);
})}
</div>
<div className="p-5 space-y-4">
{/* 预览缩略图 + 去水印区域框 */}
{thumbUrl && (
<div className="relative rounded-lg overflow-hidden bg-black max-h-64 mx-auto" style={{ aspectRatio: '16/9' }}>
<img src={thumbUrl} alt="" className="w-full h-full object-contain" />
{action === 'watermark-remove' && (
<div
className="absolute border-2 border-red-500 bg-red-500 bg-opacity-20"
style={{
left: `${clampPct(region.x)}%`,
top: `${clampPct(region.y)}%`,
width: `${Math.max(1, Math.min(100 - clampPct(region.x), region.w))}%`,
height: `${Math.max(1, Math.min(100 - clampPct(region.y), region.h))}%`
}}
/>
)}
</div>
)}
{/* ===== 加水印 ===== */}
{action === 'watermark-add' && (
<>
<div>
<label className={label}>水印文字</label>
<input className={input} value={wmText} onChange={(e) => setWmText(e.target.value)} placeholder="例如:© 资享网 或 88531.cn" maxLength={50} />
</div>
<div className="grid grid-cols-2 gap-3">
<div>
<label className={label}>位置</label>
<select className={input} value={wmPos} onChange={(e) => setWmPos(e.target.value)}>
{POSITIONS.map((p) => <option key={p.key} value={p.key}>{p.label}</option>)}
</select>
</div>
<div>
<label className={label}>颜色</label>
<input type="color" className="h-10 w-full cursor-pointer rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-700" value={wmColor} onChange={(e) => setWmColor(e.target.value)} />
</div>
<div>
<label className={label}>字号(画面高度 {wmSize}%)</label>
<input type="range" min="2" max="12" step="1" value={wmSize} onChange={(e) => setWmSize(Number(e.target.value))} className="w-full" />
</div>
<div>
<label className={label}>不透明度 {Math.round(wmOpacity * 100)}%</label>
<input type="range" min="0.1" max="1" step="0.05" value={wmOpacity} onChange={(e) => setWmOpacity(Number(e.target.value))} className="w-full" />
</div>
</div>
<label className="flex items-center gap-2 text-sm text-gray-700 dark:text-gray-300">
<input type="checkbox" checked={wmTile} onChange={(e) => setWmTile(e.target.checked)} />
平铺整个画面(九宫格水印)
</label>
</>
)}
{/* ===== 去水印 ===== */}
{action === 'watermark-remove' && (
<>
<div>
<label className={label}>水印位置预设(红框 = 将被抹除的区域)</label>
<div className="grid grid-cols-4 gap-2">
{WM_REGIONS.map((p) => (
<button
key={p.key}
onClick={() => { setRegionKey(p.key); if (p.region) setRegion(p.region); }}
className={`py-2 text-xs rounded border ${
regionKey === p.key
? 'border-blue-500 bg-blue-50 dark:bg-blue-900/30 text-blue-600 dark:text-blue-300'
: 'border-gray-200 dark:border-gray-700 text-gray-600 dark:text-gray-400'
}`}
>{p.label}</button>
))}
</div>
</div>
{regionKey === 'custom' && (
<div className="grid grid-cols-4 gap-3">
{['x', 'y', 'w', 'h'].map((k) => (
<div key={k}>
<label className={label}>{{ x: '左', y: '上', w: '宽', h: '高' }[k]} (%)</label>
<input type="number" min="0" max="100" className={input} value={region[k]} onChange={(e) => setRegion({ ...region, [k]: clampPct(e.target.value) })} />
</div>
))}
</div>
)}
<p className="text-xs text-gray-500 dark:text-gray-400">提示:去水印通过平滑插值抹除区域,适合纯色/简单背景的台标与角标。</p>
</>
)}
{/* ===== 加字幕声音 ===== */}
{action === 'subtitle-audio' && (
<>
<div>
<label className={label}>字幕内容(每行一条,多行按顺序分段显示)</label>
<textarea
className={`${input} h-28 resize-y`}
value={subText}
onChange={(e) => setSubText(e.target.value)}
placeholder={'第一段字幕\n第二段字幕\n第三段字幕'}
/>
</div>
<div className="grid grid-cols-3 gap-3">
<div>
<label className={label}>声音</label>
<select className={input} value={audioMode} onChange={(e) => setAudioMode(e.target.value)}>
<option value="tts">AI 朗读字幕(推荐)</option>
<option value="none">仅加字幕,不加声音</option>
</select>
</div>
{audioMode === 'tts' && (
<div>
<label className={label}>朗读音色</label>
<select className={input} value={voice} onChange={(e) => setVoice(e.target.value)}>
{VOICES.map((v) => <option key={v.key} value={v.key}>{v.label}</option>)}
</select>
</div>
)}
<div>
<label className={label}>字号(画面高度 {subSize}%)</label>
<input type="range" min="3" max="9" step="1" value={subSize} onChange={(e) => setSubSize(Number(e.target.value))} className="w-full mt-2" />
</div>
</div>
<p className="text-xs text-gray-500 dark:text-gray-400">开启朗读时字幕逐段与配音同步显示,生成的新视频会替换原音轨。</p>
</>
)}
{/* ===== 去字幕声音 ===== */}
{action === 'strip' && (
<>
<div className="space-y-2">
<label className="flex items-center gap-2 text-sm text-gray-700 dark:text-gray-300">
<input type="checkbox" checked={rmAudio} onChange={(e) => setRmAudio(e.target.checked)} />
去掉声音(静音,视频直拷无损)
</label>
<label className="flex items-center gap-2 text-sm text-gray-700 dark:text-gray-300">
<input type="checkbox" checked={rmSub} onChange={(e) => setRmSub(e.target.checked)} />
去掉内嵌字幕轨
</label>
</div>
<p className="text-xs text-gray-500 dark:text-gray-400">注:对已“烧录”进画面的字幕/水印无效(那是像素),烧录内容请用「去水印」。</p>
</>
)}
{/* 输出方式 */}
<div className="pt-2 border-t border-gray-200 dark:border-gray-700">
<label className={label}>输出方式</label>
<div className="flex gap-4">
<label className="flex items-center gap-2 text-sm text-gray-700 dark:text-gray-300">
<input type="radio" checked={outputMode === 'new'} onChange={() => setOutputMode('new')} />
另存为新视频(原视频不动)
</label>
<label className="flex items-center gap-2 text-sm text-gray-700 dark:text-gray-300">
<input type="radio" checked={outputMode === 'overwrite'} onChange={() => setOutputMode('overwrite')} />
覆盖原视频
</label>
</div>
</div>
{error && <div className="text-sm text-red-600 bg-red-50 dark:bg-red-900/30 border border-red-200 dark:border-red-800 rounded-lg px-3 py-2">{error}</div>}
<div className="flex justify-end gap-3 pt-2">
<button onClick={onClose} disabled={busy} className="px-4 py-2 text-sm rounded-lg border border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700 disabled:opacity-50">
取消
</button>
<button
onClick={handleSubmit}
disabled={busy}
className="px-5 py-2 text-sm rounded-lg bg-blue-600 hover:bg-blue-700 text-white font-medium disabled:opacity-50 flex items-center gap-2"
>
{busy && <Loader2 className="w-4 h-4 animate-spin" />}
{busy ? '处理中,请稍候…' : '开始处理'}
</button>
</div>
</div>
</div>
</div>
);
}
export default VideoEditModal;