Files
AI-cut/scripts/render-user-cut.mjs
T

40 lines
5.4 KiB
JavaScript
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.
import fs from 'node:fs/promises';
import path from 'node:path';
import {randomUUID} from 'node:crypto';
import {newProject,commit,saveProject} from '../desktop/core/project.mjs';
import {importMedia,exportVideo,probe,run,ffmpeg} from '../desktop/core/media.mjs';
const out=path.resolve('output','无职转生_30秒燃向卡点');await fs.mkdir(out,{recursive:true});
const sources=JSON.parse(await fs.readFile('.test-output/user-cut/sources.json','utf8'));
const p=newProject('无职转生 · 30秒超燃卡点 · RTX 5080');p.settings={width:1920,height:1080,fps:60,processingMode:'gpu'};
const map=new Map();for(const ep of [5,8,11,21]){const source=sources.find(s=>s.episode===ep),m=await importMedia(source.path,path.join(out,'cache'));m.tags=['动画','动作','魔法',`第${ep}集`];p.media.push(m);map.set(ep,m);}
const music=await importMedia(path.join(out,'原创配乐_144BPM.wav'),path.join(out,'cache'));p.media.push(music);
// Source positions manually inspected using real extracted video frames, not invented model analysis.
const shots=[
[21,545.0,4,.9,0,'人物进入对峙'],[5,1037.5,2,.9,.15,'手中火焰蓄力'],[21,666.0,2,1.05,.12,'法术发动前的近景'],
[21,548.8,2,1.15,.5,'刀刃冲突'],[21,568.7,2,1.15,.5,'长枪近身交锋'],[21,572.8,1,1.25,.45,'空中兵刃动作'],[21,576.2,1,1.3,.4,'快速斩击'],[21,581.0,2,1.15,.4,'攻击冲击'],
[5,1051.6,1,1.05,.5,'火焰爆发'],[5,1059.0,1,.9,0,'人物反应特写'],[5,1075.0,2,1.3,.4,'剑士迎击'],[5,1100.8,2,1.2,.35,'跃起出剑'],
[11,648.9,2,1.1,.5,'法杖光芒'],[11,655.1,2,1.15,.4,'巨蛇突袭'],[11,663.0,2,1.2,.35,'翻身近战'],[11,669.3,2,1.2,.4,'长枪攻击'],[11,681.7,2,1.15,.35,'魔术师准备攻击'],
[21,674.7,2,.95,.4,'火球聚集'],[21,676.1,2,1.1,.45,'火球掷出'],[21,681.5,1,1.15,.35,'火光穿越画面'],[21,686.4,1,1.2,.35,'攻势衔接'],[21,694.5,2,1.15,.3,'迎击动作'],
[21,708.0,2,.9,.4,'火焰环绕'],[21,718.8,2,1.05,.45,'能量球爆发'],[21,729.0,2,.9,.2,'掌中能量蓄力'],[21,731.0,2,1.05,.3,'紫色能量成形'],
[21,734.4,2,1.05,.4,'紫色能量扩张'],[21,736.0,2,1.15,.4,'法术释放'],[8,1298.9,2,.95,.5,'巨大光柱出现'],[8,1301.9,2,.9,.5,'能量爆裂'],[8,1303.5,2,1.1,.5,'冲击波扩散'],[8,1305.3,2,1,.5,'光球席卷画面'],
[21,573.0,1,1.2,.4,'兵刃衔接'],[5,1076.0,1,1.15,.4,'剑光衔接'],[5,1101.3,2,1.15,.4,'跃击回扣'],
[8,1307.4,2,.8,.4,'冲击波延展'],[21,736.6,2,.9,.3,'能量释放收束'],[21,729.6,4,.75,.15,'掌中光球定格式收束']
];
const totalBeats=shots.reduce((s,x)=>s+x[2],0);if(totalBeats!==72)throw Error('节拍总数不等于72:'+totalBeats);
let cursor=0;const beat=60/144,edl=[];
const clips=shots.map((shot,i)=>{const [ep,input,beats,speed,volume,reason]=shot,len=beats*beat,m=map.get(ep);const c={id:randomUUID(),track:'video',mediaId:m.id,start:cursor,in:input,out:input+len*speed,speed,volume,fade:0,audioFade:.012,fit:'contain',crop:{x:0,y:.045,width:1,height:.8},saturation:1.12,contrast:1.08,brightness:.012};
if(i===0)c.motion=[{time:0,zoom:1,x:.5,y:.45},{time:len,zoom:1.08,x:.5,y:.45}];
if(i===shots.length-1){c.fade=.1;c.motion=[{time:0,zoom:1,x:.5,y:.5},{time:len,zoom:1.08,x:.5,y:.5}];}
edl.push({index:i+1,episode:ep,sourceIn:input,sourceOut:c.out,timelineStart:cursor,duration:len,beats,speed,reason});cursor+=len;return c;});
clips.push({id:randomUUID(),track:'audio',mediaId:music.id,start:0,in:0,out:30,speed:1,volume:.85,audioFade:.08});
clips.push({id:randomUUID(),track:'subtitle',start:.15,in:0,out:2.7,speed:1,volume:1,text:'无 职 转 生',color:'#ffffff',fontSize:46});
let project=commit(p,clips,'人工查看真实素材后按144BPM建立72拍时间轴');project.messages.push({id:randomUUID(),role:'user',text:'使用指定目录有声视频,制作超燃卡点30秒;使用RTX 5080。',time:new Date().toISOString()},{id:randomUUID(),role:'system',text:'真实视频帧人工核选;144BPM原创器乐,72拍,38个视频片段,保留部分原声动作音效。GPU模式:CUDA视频解码和NVENC中间/最终编码;字幕与部分滤镜使用CPU协同。',time:new Date().toISOString()});
const projectFile=path.join(out,'无职转生_30秒燃向卡点.qycut');await saveProject(projectFile,project);await fs.writeFile(path.join(out,'剪辑清单.json'),JSON.stringify({bpm:144,beats:72,shots:edl},null,2));
const dest=path.join(out,'无职转生_超燃卡点_30秒_RTX5080.mp4');let last=-1,engines=[];
await exportVideo(project,dest,{acceleration:'gpu',bitrate:18000,onEncoder:e=>{engines.push(e);console.log('VIDEO ENCODER',e);}},undefined,(v,d)=>{const n=Math.floor(v*100);if(n!==last){console.log(n+'%',d);last=n;}});
const meta=await probe(dest);const v=meta.streams.find(s=>s.codec_type==='video'),a=meta.streams.find(s=>s.codec_type==='audio');if(Math.abs(+meta.format.duration-30)>.06||!a)throw Error('最终输出时长或音轨验证失败');
await run(ffmpeg,['-v','error','-hwaccel','cuda','-i',dest,'-f','null','-']);
await run(ffmpeg,['-y','-v','error','-hwaccel','cuda','-i',dest,'-vf',"fps=1,scale=320:-2,tile=5x6",'-frames:v','1',path.join(out,'成片联系表.jpg')]);
await fs.writeFile(path.join(out,'验证报告.json'),JSON.stringify({duration:Number(meta.format.duration),width:v.width,height:v.height,fps:v.avg_frame_rate,frames:v.nb_frames,videoCodec:v.codec_name,audioCodec:a.codec_name,engines,processingMode:'gpu',bpm:144,beats:72,cuts:shots.length,fileSize:meta.format.size,fullDecode:'passed'},null,2));
console.log('DELIVERED',dest);