diff --git a/backend/src/routes/video.js b/backend/src/routes/video.js index fa2d19b..c873c94 100644 --- a/backend/src/routes/video.js +++ b/backend/src/routes/video.js @@ -428,6 +428,7 @@ router.post( resolution = '1920x1080', extraPrompt = '', saveDir = '', + transition = 'fade', subtitleStyle = {} } = req.body || {}; if (!libraryId) throw new ValidationError('缺少 libraryId'); @@ -449,39 +450,53 @@ router.post( if (!url) throw new ValidationError('未配置模型接口地址(请到右上角设置 → AI 视频里填写)'); if (!key) throw new ValidationError('请先在右上角设置 → AI 视频里填写模型 KEY'); + // 转场效果白名单(防注入 ffmpeg filter) + const TRANSITIONS = new Set(['cut','fade','fadeblack','fadewhite','slideleft','slideright','slideup','slidedown','circlecrop','circleopen','circleclose','dissolve','smoothleft','smoothright','wipeleft','wiperight','pixelize','radial','hblur']); + const trans = TRANSITIONS.has(String(transition)) ? String(transition) : 'fade'; + const id = `vt${++textTaskSeq}_${Date.now()}`; const [w, h] = String(resolution).split('x').map((n) => parseInt(n, 10)); const task = { id, status: 'running', step: 'script', stepLabel: '正在生成', progress: 0, error: '', resultPath: '', createdAt: Date.now() }; textTasks.set(id, task); setTimeout(() => textTasks.delete(id), 30 * 60 * 1000); - runTextVideo({ task, library, title: String(title).trim(), content: text, images, url, key, model: mdl, voice, width: Number.isFinite(w)?w:1920, height: Number.isFinite(h)?h:1080, extraPrompt: String(extraPrompt||''), saveDir: String(saveDir||''), subtitleStyle: subtitleStyle||{}, req }).catch((err) => { + runTextVideo({ task, library, title: String(title).trim(), content: text, images, url, key, model: mdl, voice, width: Number.isFinite(w)?w:1920, height: Number.isFinite(h)?h:1080, extraPrompt: String(extraPrompt||''), saveDir: String(saveDir||''), transition: trans, subtitleStyle: subtitleStyle||{}, req }).catch((err) => { task.status = 'error'; task.error = err.message || String(err); }); res.json({ success: true, taskId: id }); }) ); async function runTextVideo(ctx) { - const { task, library, title, content, images, url, key, model, voice, width, height, extraPrompt, saveDir, subtitleStyle, req } = ctx; + const { task, library, title, content, images, url, key, model, voice, width, height, extraPrompt, saveDir, transition, subtitleStyle, req } = ctx; const work = fs.mkdtempSync(path.join(os.tmpdir(), 'flypic-tv-')); try { // 生成背景图(若无图片) - let bgPath = ''; + // 解析全部选中图片(本地库路径 / 远程 URL 下载)—— 让每张选中图片都进视频 + const imagePaths = []; if (Array.isArray(images) && images.length) { - // 用第一张图作为背景 - const rel = typeof images[0] === 'string' ? images[0] : images[0].path || images[0].url; - if (rel && fs.existsSync(path.join(library.path, rel))) bgPath = path.join(library.path, rel); - else if (rel && /^https?:/i.test(rel)) { - // 下载 - try { - const r = await fetch(rel); const buf = Buffer.from(await r.arrayBuffer()); - bgPath = path.join(work, 'bg.jpg'); fs.writeFileSync(bgPath, buf); - } catch (_) {} + for (const it of images) { + const rel = typeof it === 'string' ? it : (it && (it.path || it.url)) || ''; + if (!rel) continue; + if (/^https?:/i.test(rel)) { + try { + const r = await fetch(rel); + const buf = Buffer.from(await r.arrayBuffer()); + const ext = (path.extname(new URL(rel).pathname) || '.jpg').toLowerCase(); + const fp = path.join(work, `img${imagePaths.length}${ext}`); + fs.writeFileSync(fp, buf); + imagePaths.push(fp); + } catch (e) { logger.warn('图片下载失败: ' + e.message); } + } else { + const abs = path.join(library.path, rel); + if (fs.existsSync(abs)) imagePaths.push(abs); + } } } - if (!bgPath) { - bgPath = path.join(work, 'bg.png'); + // 兜底:无图时生成纯色背景 + if (!imagePaths.length) { + const bgPath = path.join(work, 'bg.png'); await videotool.run('ffmpeg', ['-y', '-f', 'lavfi', '-i', `color=c=0x1a1a2e:s=${width}x${height}`, '-frames:v', '1', bgPath], { timeoutMs: 60000 }); + imagePaths.push(bgPath); } // LLM 生成分镜:基于内容切分 @@ -490,18 +505,20 @@ async function runTextVideo(ctx) { if (content) { // 让模型按内容生成 N 段(N = min(8, 句子数)) const sentences = content.split(/[。!?.!?]/).map(s=>s.trim()).filter(Boolean); - const n = Math.min(8, Math.max(2, sentences.length)); + const baseN = Math.min(8, Math.max(2, sentences.length)); + const n = Math.max(baseN, imagePaths.length); // 分镜数 ≥ 选中图片数,保证每张图都出现 script = await llmScript({ url, key, model, title, count: n, extraPrompt: (extraPrompt + ' 内容:' + content.slice(0, 1500)).trim() }); } else { script = [{ subtitle: title, tts: title }]; } if (!Array.isArray(script) || !script.length) script = [{ subtitle: title, tts: title }]; - // TTS + segments + // TTS + segments:每段轮换使用选中图片,保证所有图片都出现 const segments = []; - for (let i = 0; i < script.length; i++) { - task.step = 'tts'; task.stepLabel = `正在朗读 ${i+1}/${script.length}`; task.progress = 10 + Math.round((i/script.length)*40); - const seg = script[i]; + const segCount = Math.max(script.length, imagePaths.length); + for (let i = 0; i < segCount; i++) { + task.step = 'tts'; task.stepLabel = `正在朗读 ${i+1}/${segCount}`; task.progress = 10 + Math.round((i/segCount)*40); + const seg = script[i % script.length] || {}; let ttsFile = ''; let dur = 0; if (seg.tts) { try { @@ -509,7 +526,7 @@ async function runTextVideo(ctx) { ttsFile = t.file; dur = t.seconds; } catch (e) { logger.warn('TTS fail: ' + e.message); } } - segments.push({ imagePath: bgPath, subtitle: seg.subtitle || seg.tts || title, ttsFile, duration: Math.max(2.5, Math.min(12, dur || 4)) }); + segments.push({ imagePath: imagePaths[i % imagePaths.length], subtitle: seg.subtitle || seg.tts || title, ttsFile, duration: Math.max(2.5, Math.min(12, dur || 4)) }); } // compose @@ -522,7 +539,7 @@ async function runTextVideo(ctx) { } task.step = 'merge'; task.stepLabel = '合并'; task.progress = 96; const finalTmp = path.join(work, 'final.mp4'); - await videotool.concatSegments(segFiles, finalTmp); + await videotool.concatWithTransition(segFiles, finalTmp, { transition: transition || 'fade', width, height }); // 6) 落库:保存到用户选择的目录(默认素材库根目录) const safeDir = String(saveDir || '').replace(/^\/+/, '').replace(/\.\./g, ''); diff --git a/backend/utils/videotool.js b/backend/utils/videotool.js index f7a281e..0d97f38 100644 --- a/backend/utils/videotool.js +++ b/backend/utils/videotool.js @@ -424,6 +424,57 @@ async function concatSegments(segFiles, outPath) { } } +/** + * 带转场拼接分段 mp4(xfade 交叉过渡 + 音频直拼) + * transition: cut/fade/slideleft/slideright/circleopen/dissolve/wipeleft/... (ffmpeg xfade 名) + * 失败时回退为普通硬拼接 + */ +async function concatWithTransition(segFiles, outPath, { transition = 'fade', width = 1920, height = 1080 } = {}) { + const n = segFiles.length; + if (n <= 1 || !transition || transition === 'cut') return concatSegments(segFiles, outPath); + const F = 0.6; // 转场时长(秒) + const dir = tmpDir(); + try { + // 探测每段真实时长,用于计算 xfade offset + const durs = []; + for (const f of segFiles) { + const p = await probeVideo(f); + durs.push(p.duration > 0.2 ? p.duration : 4); + } + const inputs = []; + for (const f of segFiles) inputs.push('-i', f); + const filters = []; + for (let i = 0; i < n; i++) { + filters.push(`[${i}:v]scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2,setsar=1,format=yuv420p,fps=30[v${i}]`); + } + let prev = 'v0'; + let acc = durs[0]; + for (let i = 1; i < n; i++) { + const offset = Math.max(0, acc - F); + const out = i === n - 1 ? 'vx' : `x${i}`; + filters.push(`[${prev}][v${i}]xfade=transition=${transition}:duration=${F}:offset=${offset.toFixed(3)}[${out}]`); + prev = out; + acc = acc + durs[i] - F; + } + const audioInputs = segFiles.map((_, i) => `[${i}:a]`).join(''); + filters.push(`${audioInputs}concat=n=${n}:v=0:a=1[aout]`); + const args = [ + '-y', ...inputs, '-filter_complex', filters.join(';'), + '-map', '[vx]', '-map', '[aout]', + '-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20', + '-pix_fmt', 'yuv420p', '-movflags', '+faststart', outPath + ]; + await run(FFMPEG, args, { timeoutMs: 600 * 1000 }); + return outPath; + } catch (err) { + // 转场失败回退硬拼接,保证出片 + logger.warn && logger.warn(`⚠️ 转场拼接失败,回退硬拼接: ${err.message}`); + return concatSegments(segFiles, outPath); + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +} + module.exports = { run, probeVideo, @@ -434,5 +485,6 @@ module.exports = { generateTts, buildSegment, concatSegments, + concatWithTransition, textToSrt }; diff --git a/docker-compose.yml b/docker-compose.yml index 6a50ed1..89d9658 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,7 +26,7 @@ services: - HUB_API_KEY=${HUB_API_KEY:-} - NEWAPI_KEY=${NEWAPI_KEY:-} # 当前站点版本(与 frontend/package.json、CHANGELOG 保持一致,界面版本徽标来源) - - SITE_VERSION=1.4.15 + - SITE_VERSION=1.4.17 # 让容器经 host.docker.internal 访问宿主机的 AI 侧车 extra_hosts: - "host.docker.internal:host-gateway" diff --git a/frontend/package.json b/frontend/package.json index 35cee16..dde1319 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "flypic-frontend", - "version": "1.4.0", + "version": "1.4.17", "private": true, "type": "module", "scripts": { diff --git a/frontend/src/components/AIVideoStudioModal.jsx b/frontend/src/components/AIVideoStudioModal.jsx index 840d4fa..24e0ea4 100644 --- a/frontend/src/components/AIVideoStudioModal.jsx +++ b/frontend/src/components/AIVideoStudioModal.jsx @@ -62,6 +62,7 @@ export default function AIVideoStudioModal({ libraryId, onClose, onDone }) { const [taskId, setTaskId] = useState(''); const [folders, setFolders] = useState([]); const [saveDir, setSaveDir] = useState(''); + const [transition, setTransition] = useState('fade'); const [task, setTask] = useState(null); const [error, setError] = useState(''); const [starting, setStarting] = useState(false); @@ -178,7 +179,7 @@ export default function AIVideoStudioModal({ libraryId, onClose, onDone }) { libraryId, title: title.trim(), content: content.trim(), images: imgs, - voice, resolution, extraPrompt: '', saveDir, + voice, resolution, extraPrompt: '', saveDir, transition, subtitleStyle: { position: subtitlePos, color: subtitleColor, fontSizePct: subtitleSize } }; const r = await fetch('/api/video/aigenerate-text', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}` }, body: JSON.stringify(body) }); @@ -380,6 +381,26 @@ export default function AIVideoStudioModal({ libraryId, onClose, onDone }) { +
+ + +
+
提供商 / 模型 / KEY 在右上角「设置 → AI 视频」里配置,普通文本模型即可;配音与字幕由本地合成。