v1.4.18: AI视频支持设置目标时长(画面与配音精确对齐)

This commit is contained in:
jinghui committed 2026-11-22 14:25:31 +08:00
1 parent edfbb67cdb
commit 9c7b695081
5 files changed
+83 -10

No files matched your search

+42 -5
View File
@@ -137,6 +137,7 @@ router.post(
resolution = '1920x1080',
extraPrompt = '',
saveDir = '',
duration = 0,
subtitleStyle = {}
} = req.body || {};
@@ -188,7 +189,7 @@ router.post(
runAiVideo({
task, library, images, title: String(title).trim(), url, key, model: mdl,
voice, width: Number.isFinite(w) ? w : 1920, height: Number.isFinite(h) ? h : 1080,
extraPrompt: String(extraPrompt || ''), subtitleStyle: subtitleStyle || {},
extraPrompt: String(extraPrompt || ''), duration: Math.min(600, Math.max(0, Number(duration) || 0)), subtitleStyle: subtitleStyle || {},
req
}).catch((err) => {
task.status = 'error';
@@ -304,10 +305,24 @@ async function runAiVideo(ctx) {
imagePath: files[i],
subtitle: seg.subtitle || seg.tts || title,
ttsFile,
ttsSeconds: dur,
duration: Math.max(2.5, Math.min(12, dur || 4))
});
}
// 目标时长:按各段自然时长比例分配(该路径无转场重叠)
const targetDur = Number(ctx.duration) > 0 ? Number(ctx.duration) : 0;
if (targetDur > 0 && segments.length) {
const avail = Math.max(segments.length * 1.0, targetDur);
const natural = segments.reduce((s, x) => s + x.duration, 0) || 1;
for (const s of segments) {
const d = Math.max(1.0, (s.duration / natural) * avail);
s.duration = d;
s.audioDuration = d;
}
logger.info(`⏱ AI 视频目标时长 ${targetDur}s → 分配 ${segments.length} 段`);
}
// 4) 逐段合成
const segFiles = [];
for (let i = 0; i < segments.length; i++) {
@@ -429,6 +444,7 @@ router.post(
extraPrompt = '',
saveDir = '',
transition = 'fade',
duration = 0,
subtitleStyle = {}
} = req.body || {};
if (!libraryId) throw new ValidationError('缺少 libraryId');
@@ -460,7 +476,8 @@ router.post(
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||''), transition: trans, subtitleStyle: subtitleStyle||{}, req }).catch((err) => {
const durSec = Math.min(600, Math.max(0, Number(duration) || 0)); // 目标时长(秒),0=自动
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, duration: durSec, subtitleStyle: subtitleStyle||{}, req }).catch((err) => {
task.status = 'error'; task.error = err.message || String(err);
});
res.json({ success: true, taskId: id });
@@ -502,11 +519,13 @@ async function runTextVideo(ctx) {
// LLM 生成分镜:基于内容切分
task.step = 'script'; task.stepLabel = '正在生成脚本'; task.progress = 5;
let script = [];
const targetDur0 = Number(ctx.duration) > 0 ? Number(ctx.duration) : 0;
if (content) {
// 让模型按内容生成 N 段(N = min(8, 句子数))
const sentences = content.split(/[。!?.!?]/).map(s=>s.trim()).filter(Boolean);
const baseN = Math.min(8, Math.max(2, sentences.length));
const n = Math.max(baseN, imagePaths.length); // 分镜数 ≥ 选中图片数,保证每张图都出现
let n = Math.max(baseN, imagePaths.length); // 分镜数 ≥ 选中图片数,保证每张图都出现
if (targetDur0 > 0) n = Math.max(n, Math.min(30, Math.ceil(targetDur0 / 5))); // 时长越长分镜越多(约5s/段)
script = await llmScript({ url, key, model, title, count: n, extraPrompt: (extraPrompt + ' 内容:' + content.slice(0, 1500)).trim() });
} else {
script = [{ subtitle: title, tts: title }];
@@ -515,7 +534,8 @@ async function runTextVideo(ctx) {
// TTS + segments:每段轮换使用选中图片,保证所有图片都出现
const segments = [];
const segCount = Math.max(script.length, imagePaths.length);
let segCount = Math.max(script.length, imagePaths.length);
if (targetDur0 > 0) segCount = Math.max(segCount, Math.min(30, Math.ceil(targetDur0 / 5))); // 按目标时长补齐段数
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] || {};
@@ -526,7 +546,24 @@ async function runTextVideo(ctx) {
ttsFile = t.file; dur = t.seconds;
} catch (e) { logger.warn('TTS fail: ' + e.message); }
}
segments.push({ imagePath: imagePaths[i % imagePaths.length], 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, ttsSeconds: dur, duration: Math.max(2.5, Math.min(12, dur || 4)) });
}
// 目标时长:画面段总长 = 目标 + 转场重叠(xfade 每次吃掉 0.6s),
// 配音段总长 = 目标(音频走 concat 无重叠),使音画总长都精确等于目标时长
const targetDur = Number(ctx.duration) > 0 ? Number(ctx.duration) : 0;
if (targetDur > 0 && segments.length) {
const n = segments.length;
const overlap = (transition && transition !== 'cut' && n > 1) ? 0.6 * (n - 1) : 0;
const natural = segments.reduce((s, x) => s + x.duration, 0) || 1;
const videoTotal = targetDur + overlap; // 画面段之和
const audioShare = overlap / n; // 每段配音比画面短的秒数(落在转场区)
for (const s of segments) {
const d = Math.max(1.0, (s.duration / natural) * videoTotal);
s.duration = d;
s.audioDuration = Math.max(0.5, d - audioShare); // 精确模式:配音对齐
}
logger.info(`⏱ AI 视频目标时长 ${targetDur}s → 画面 ${videoTotal.toFixed(1)}s + 转场重叠 ${overlap.toFixed(1)}s,分 ${n} 段`);
}
// compose
+18 -2
View File
@@ -382,10 +382,26 @@ async function buildSegment(seg, outPath, { width = 1920, height = 1080, fontSiz
`drawtext=fontfile=/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc:textfile=${textFile}:fontsize=${fontSize}:fontcolor=${color}@0.95:borderw=2:bordercolor=black@0.6:x=${xExpr}:y=${yExpr}:line_spacing=8`
].join(',');
const exact = seg.audioDuration != null; // 目标时长模式:画面用 duration,配音对齐 audioDuration
const vDur = Math.max(0.5, Number(seg.duration) || 4);
const args = [
'-y', '-loop', '1', '-t', seg.duration.toFixed(3), '-i', seg.imagePath
'-y', '-loop', '1', '-t', vDur.toFixed(3), '-i', seg.imagePath
];
if (seg.ttsFile) {
if (exact) {
const aDur = Math.max(0.5, Number(seg.audioDuration) || vDur);
const ttsDur = Number(seg.ttsSeconds) || 0;
let chain = '';
if (seg.ttsFile && ttsDur > aDur + 0.05) {
const f = Math.min(4.0, ttsDur / aDur); // 配音超长则加速(最多4倍),避免截断内容
chain += `atempo=${f.toFixed(4)},`;
}
chain += `atrim=0:${aDur.toFixed(3)},apad=whole_dur=${aDur.toFixed(3)}`;
const src = seg.ttsFile
? ['-i', seg.ttsFile]
: ['-f', 'lavfi', '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100'];
args.push(...src, '-filter_complex', `[1:a]${chain}[a]`,
'-map', '0:v', '-map', '[a]', '-c:a', 'aac', '-ar', '44100', '-ac', '2');
} else if (seg.ttsFile) {
args.push('-i', seg.ttsFile, '-map', '0:v', '-map', '1:a', '-c:a', 'aac', '-ar', '44100', '-ac', '2', '-shortest');
} else {
args.push('-f', 'lavfi', '-i', 'anullsrc=channel_layout=stereo:sample_rate=44100', '-map', '0:v', '-map', '1:a', '-shortest');
+1 -1
View File
@@ -26,7 +26,7 @@ services:
- HUB_API_KEY=${HUB_API_KEY:-}
- NEWAPI_KEY=${NEWAPI_KEY:-}
# 当前站点版本(与 frontend/package.json、CHANGELOG 保持一致,界面版本徽标来源)
- SITE_VERSION=1.4.17
- SITE_VERSION=1.4.18
# 让容器经 host.docker.internal 访问宿主机的 AI 侧车
extra_hosts:
- "host.docker.internal:host-gateway"
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "flypic-frontend",
"version": "1.4.17",
"version": "1.4.18",
"private": true,
"type": "module",
"scripts": {
+21 -1
View File
@@ -63,6 +63,7 @@ export default function AIVideoStudioModal({ libraryId, onClose, onDone }) {
const [folders, setFolders] = useState([]);
const [saveDir, setSaveDir] = useState('');
const [transition, setTransition] = useState('fade');
const [duration, setDuration] = useState(0); // 目标时长(秒),0=自动按配音
const [task, setTask] = useState(null);
const [error, setError] = useState('');
const [starting, setStarting] = useState(false);
@@ -179,7 +180,7 @@ export default function AIVideoStudioModal({ libraryId, onClose, onDone }) {
libraryId, title: title.trim(),
content: content.trim(),
images: imgs,
voice, resolution, extraPrompt: '', saveDir, transition,
voice, resolution, extraPrompt: '', saveDir, transition, duration: Number(duration) || 0,
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) });
@@ -381,6 +382,25 @@ export default function AIVideoStudioModal({ libraryId, onClose, onDone }) {
</select>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">视频时长</label>
<div className="flex flex-wrap gap-1.5 mb-1.5">
{[0, 15, 30, 45, 60, 90, 120, 180, 300].map((s) => (
<button key={s} type="button" disabled={running||done}
onClick={() => setDuration(s)}
className={`px-2 py-1 rounded text-xs border transition ${Number(duration)===s ? 'bg-blue-500 border-blue-500 text-white' : 'bg-white dark:bg-gray-700 border-gray-300 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:border-blue-400'}`}>
{s === 0 ? '自动' : s < 60 ? `${s}秒` : `${s/60}分钟`}
</button>
))}
</div>
<div className="flex items-center gap-2">
<input type="number" min="0" max="600" step="5" disabled={running||done}
className={input} value={duration || ''} placeholder="自动(按配音)"
onChange={(e) => setDuration(Math.min(600, Math.max(0, Number(e.target.value) || 0)))} />
<span className="text-xs text-gray-500 dark:text-gray-400 whitespace-nowrap">秒 · 0=自动</span>
</div>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">转场效果</label>
<select className={input} value={transition} onChange={(e) => setTransition(e.target.value)} disabled={running||done}>