Merge pull request 'feat(ai): 新增 AI 操作净化层——越界截断、重叠平移、无效操作忽略' (#4) from feat/ai-op-sanitize into main
Reviewed-on: http://git.xhmcl.com/xhgit/AI-cut/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
commit
8d83cee7c5
2 files changed
+108
-1
No files matched your search
@@ -63,6 +63,88 @@ export async function saveProject(file,p){
|
||||
}finally{db.close();}
|
||||
}
|
||||
export async function loadProject(file){const SQL=await engine(),db=new SQL.Database(await fs.readFile(file));try{const r=db.exec('SELECT data FROM state WHERE id=1');return validateProject(JSON.parse(r[0].values[0][0]));}finally{db.close();}}
|
||||
const OP_FIELDS=['start','in','out','speed','volume','fade','text','color','fontSize','brightness','contrast','saturation','fit'];
|
||||
const OP_LIMITS={start:[0,86400],in:[0,86400],out:[0.1,86400],speed:[.25,4],volume:[0,4],fade:[0,10],brightness:[-1,1],contrast:[0,2],saturation:[0,3],fontSize:[12,160]};
|
||||
function clampValue(v,key){if(v===undefined||v===null||v==='')return undefined;const n=Number(v);if(!Number.isFinite(n))return undefined;const [min,max]=OP_LIMITS[key];return Math.round(Math.min(max,Math.max(min,n))*1000)/1000;}
|
||||
function cleanText(v){return String(v??'').slice(0,10000);}
|
||||
function cleanColor(v){return /^#[0-9a-fA-F]{6}$/.test(String(v))?String(v):undefined;}
|
||||
// 净化模型返回的操作:越界数值截断、引用不存在的对象则忽略、视频轨重叠自动平移。
|
||||
// 让模型输出中的个别小错误不再导致整个方案被拒绝,并在 notes 中记录修正。
|
||||
export function sanitizeOperations(p,operations){
|
||||
if(!Array.isArray(operations)||operations.length>1000)throw Error('模型没有返回有效的操作数组');
|
||||
const ops=[],notes=[],addBySim=new Map(),updById=new Map();
|
||||
const note=n=>{if(notes.length<40)notes.push(n);};
|
||||
const handle=op=>{
|
||||
if(!op||typeof op!=='object')return note('忽略无效操作');
|
||||
if(op.type==='add'){
|
||||
const raw=op.clip&&typeof op.clip==='object'?op.clip:{};
|
||||
const track=['audio','subtitle'].includes(raw.track)?raw.track:'video';
|
||||
const m=track==='subtitle'?null:p.media.find(x=>x.id===raw.mediaId);
|
||||
if(track!=='subtitle'){
|
||||
if(!m)return note('忽略 add:引用了不存在的素材');
|
||||
if(track==='audio'&&m.type!=='audio')return note('忽略 add:音频轨道需要音频素材');
|
||||
if(track==='video'&&m.type==='audio')return note('忽略 add:音频素材不能放入视频轨道');
|
||||
}
|
||||
const clip={id:randomUUID(),track,start:clampValue(raw.start,'start')??0,in:0,speed:clampValue(raw.speed,'speed')??1,volume:clampValue(raw.volume,'volume')??1};
|
||||
if(track!=='subtitle')clip.mediaId=m.id;
|
||||
if(track==='subtitle')clip.out=clampValue(raw.out,'out')??3;
|
||||
else if(m.type==='image'){clip.in=0;clip.out=Math.min(clampValue(raw.out,'out')??Math.min(5,60),60);}
|
||||
else{clip.in=Math.min(clampValue(raw.in,'in')??0,Math.max(0,m.duration-.1));clip.out=Math.min(clampValue(raw.out,'out')??m.duration,m.duration);}
|
||||
if(!(clip.out>clip.in+.05))return note('忽略 add:出点必须大于入点');
|
||||
if(raw.text!==undefined)clip.text=cleanText(raw.text);
|
||||
for(const k of ['fade','brightness','contrast','saturation','fontSize']){const v=clampValue(raw[k],k);if(v!==undefined)clip[k]=v;}
|
||||
if(raw.color!==undefined){const col=cleanColor(raw.color);if(col)clip.color=col;}
|
||||
if(raw.fit!==undefined)clip.fit=['contain','cover'].includes(raw.fit)?raw.fit:'contain';
|
||||
const entry={type:'add',clip};ops.push(entry);addBySim.set(clip.id,{entry,clip});
|
||||
}else if(op.type==='update'){
|
||||
const c=p.clips.find(x=>x.id===op.id);
|
||||
if(!c)return note('忽略 update:引用了不存在的片段');
|
||||
const changes=op.changes&&typeof op.changes==='object'?op.changes:{};
|
||||
if(changes.id!==undefined||changes.mediaId!==undefined||changes.track!==undefined)note('update 不允许修改 id / mediaId / track,已忽略这些字段');
|
||||
const m=c.track==='subtitle'?null:p.media.find(x=>x.id===c.mediaId);
|
||||
const clean={};
|
||||
for(const k of (c.track==='subtitle'?['start','out','text','color','fontSize']:OP_FIELDS)){
|
||||
if(changes[k]===undefined)continue;
|
||||
if(k==='in'||k==='out'){
|
||||
const v=clampValue(changes[k],k);
|
||||
if(v===undefined)return note('忽略 update:'+k+' 数值无效');
|
||||
if(m&&m.type!=='image'&&k==='out'&&v>m.duration)return note('忽略 update:out 超出素材时长');
|
||||
if(m&&m.type!=='image'&&k==='in'&&v>m.duration-.1)return note('忽略 update:in 超出素材时长');
|
||||
const in2=k==='in'?v:c.in,out2=k==='out'?v:c.out;
|
||||
if(m&&m.type==='image'&&k==='in')clean.in=0;
|
||||
else if(out2<=in2+.05)return note('忽略 update:出点必须大于入点');
|
||||
else clean[k]=v;
|
||||
}else if(k==='start'){const v=clampValue(changes[k],k);if(v===undefined)return note('忽略 update:start 数值无效');clean.start=v;}
|
||||
else if(k==='text')clean.text=cleanText(changes[k]);
|
||||
else if(k==='color'){const col=cleanColor(changes[k]);if(col)clean.color=col;else note('忽略 update:color 格式无效');}
|
||||
else if(k==='fit')clean.fit=['contain','cover'].includes(changes[k])?changes[k]:'contain';
|
||||
else{const v=clampValue(changes[k],k);if(v===undefined)return note('忽略 update:'+k+' 数值无效');clean[k]=v;}
|
||||
}
|
||||
if(!Object.keys(clean).length)return;
|
||||
const entry={type:'update',id:c.id,changes:clean};ops.push(entry);updById.set(c.id,entry);
|
||||
}else if(op.type==='remove'){
|
||||
if(!p.clips.some(x=>x.id===op.id))return note('忽略 remove:片段不存在');
|
||||
ops.push({type:'remove',id:op.id});
|
||||
}else note('忽略不支持的 AI 操作:'+String(op.type));
|
||||
};
|
||||
for(const op of operations)handle(op);
|
||||
// 视频轨重叠修复:按起点排序,把后置片段自动右移而不是整体报错
|
||||
const sim=new Map(p.clips.map(c=>[c.id,structuredClone(c)]));
|
||||
for(const op of ops){if(op.type==='add')sim.set(op.clip.id,structuredClone(op.clip));else if(op.type==='remove')sim.delete(op.id);else{const c=sim.get(op.id);if(c)Object.assign(c,op.changes);}}
|
||||
const vids=[...sim.values()].filter(c=>c.track==='video').sort((a,b)=>a.start-b.start);
|
||||
for(let i=1;i<vids.length;i++){
|
||||
const end=vids[i-1].start+duration(vids[i-1]);
|
||||
if(vids[i].start<end-.02){
|
||||
const at=Math.round(end*100)/100;
|
||||
const add=addBySim.get(vids[i].id);if(add)add.clip.start=at;
|
||||
else{const upd=updById.get(vids[i].id);if(upd)upd.changes.start=at;}
|
||||
vids[i].start=at;
|
||||
note('视频片段重叠,已自动后移至 '+at.toFixed(1)+' 秒');
|
||||
}
|
||||
}
|
||||
try{validateProject({...p,clips:[...sim.values()]});}catch(e){throw Error('AI 操作校验未通过:'+e.message+';'+notes.slice(0,3).join(';'));}
|
||||
return {operations:ops,notes};
|
||||
}
|
||||
export function applyOperations(p,operations,label='AI 修改'){
|
||||
if(!Array.isArray(operations)||operations.length>500)throw Error('AI 命令数量无效');let clips=structuredClone(p.clips);
|
||||
for(const op of operations){
|
||||
|
||||
+26
-1
@@ -2,7 +2,7 @@ import {test} from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {newProject,commit,undo,saveProject,loadProject,applyOperations,validateProject,projectDuration,splitClip} from '../desktop/core/project.mjs';
|
||||
import {newProject,commit,undo,saveProject,loadProject,applyOperations,validateProject,projectDuration,splitClip,sanitizeOperations} from '../desktop/core/project.mjs';
|
||||
import {run,ffmpeg,probe,importMedia,analyzeTechnical,exportVideo,makeProxy} from '../desktop/core/media.mjs';
|
||||
import {Tasks} from '../desktop/core/tasks.mjs';
|
||||
const out=path.resolve('.test-output','中文 空格素材');await fs.mkdir(out,{recursive:true});
|
||||
@@ -26,3 +26,28 @@ test('真实 FFmpeg:导入、场景时间戳、编辑、SQLite 恢复、代理
|
||||
await assert.rejects(importMedia(path.join(out,'不存在.mp4'),cache));
|
||||
});
|
||||
test('任务队列限流、错误和取消',async()=>{let latest;const queue=new Tasks(x=>latest=x);let release;const block=new Promise(r=>release=r);queue.add('第一任务',async()=>{await block;return {ok:true};});const id=queue.add('取消任务',async()=>{throw Error('不得执行');});queue.cancel(id);queue.add('失败任务',async()=>{throw Error('真实失败');});release();for(let i=0;i<100&&queue.running;i++)await new Promise(r=>setTimeout(r,10));assert.equal(latest.find(i=>i.id===id).status,'cancelled');assert.equal(latest.find(i=>i.label==='失败任务').status,'error');});
|
||||
test('AI 操作净化:越界数值截断、无效引用忽略、视频轨重叠自动平移',()=>{
|
||||
const p=newProject('净化测试');p.media=[
|
||||
{id:'v',type:'video',duration:30,name:'v',tags:[],path:'x'},
|
||||
{id:'i',type:'image',duration:5,name:'i',tags:[],path:'x'},
|
||||
{id:'a',type:'audio',duration:20,name:'a',tags:[],path:'x'},
|
||||
];
|
||||
p.clips=[{id:'c1',mediaId:'v',track:'video',start:0,in:0,out:5,speed:1,volume:1}];
|
||||
const r=sanitizeOperations(p,[
|
||||
{type:'update',id:'c1',changes:{out:25}}, // 延长到 25s
|
||||
{type:'add',clip:{mediaId:'v',track:'video',start:0,in:0,out:999,speed:99,volume:-5,color:'red',fontSize:1,brightness:9}}, // 越界全部截断
|
||||
{type:'add',clip:{mediaId:'v',track:'video',start:1,in:5,out:10}}, // 与 c1 重叠,应后移
|
||||
{type:'add',clip:{mediaId:'nope',track:'video',start:0,in:0,out:1}}, // 素材不存在,忽略
|
||||
{type:'add',clip:{mediaId:'v',track:'audio',start:0,in:0,out:5}}, // 视频素材进音频轨,忽略
|
||||
{type:'update',id:'ghost',changes:{start:0}}, // 片段不存在,忽略
|
||||
{type:'remove',id:'ghost'}, // 片段不存在,忽略
|
||||
{type:'bogus'}, // 不支持的操作,忽略
|
||||
]);
|
||||
assert.equal(r.operations.length,3);
|
||||
const upd=r.operations.find(o=>o.type==='update');assert.equal(upd.changes.out,25);
|
||||
const add1=r.operations.find(o=>o.type==='add'&&o.clip.in===0);assert.equal(add1.clip.out,30);assert.equal(add1.clip.speed,4);assert.equal(add1.clip.volume,0);assert.equal(add1.clip.fontSize,12);assert.equal(add1.clip.brightness,1);assert.equal(add1.clip.color,undefined);
|
||||
const add2=r.operations.find(o=>o.type==='add'&&o.clip.in===5);assert.equal(add1.clip.start,25);assert.equal(add2.clip.start,32.5);
|
||||
assert.ok(r.notes.length>=5);
|
||||
const applied=applyOperations(p,r.operations); // 净化后的操作必须能直接通过校验
|
||||
assert.equal(applied.clips.filter(c=>c.track==='video').length,3);
|
||||
});
|
||||
Reference in new issue
Block a user