const fs=require('fs'),path=require('path'),{spawn}=require('child_process'),{once}=require('events'); const {createCanvas,loadImage,GlobalFonts}=require('@napi-rs/canvas'); require('./camera.js'); const R=__dirname, out=path.dirname(R); const W=1280,H=720,FPS=24,duration=96; GlobalFonts.registerFromPath('/System/Library/Fonts/Hiragino Sans GB.ttc','FilmSans'); GlobalFonts.registerFromPath('/System/Library/Fonts/Supplemental/Songti.ttc','FilmSong'); GlobalFonts.registerFromPath('/System/Library/Fonts/Supplemental/Georgia.ttf','FilmLatin'); const ink='#142729',ivory='#fff6e3',gold='#eac28c'; const pics=[ ['01-cave','洞穴岩画','法国 · 旧石器时代晚期','LASCAUX'], ['02-egypt','墓室壁画','古埃及 · 约公元前 1350 年','THEBES'], ['03-greece','红绘陶器','古希腊 · 公元前 5 世纪','ATTICA'], ['04-dunhuang','敦煌壁画','中国 · 盛唐,8 世纪','DUNHUANG'], ['05-maya','玛雅宫廷壁画','博南帕克 · 8 世纪末','BONAMPAK'], ['06-renaissance','文艺复兴绘画','意大利 · 15 世纪','FLORENCE'], ['07-ukiyoe','浮世绘木版画','日本 · 19 世纪前期','EDO'], ['08-pointillism','新印象主义点彩','法国 · 19 世纪末','POINTILLISM'], ['09-pop','波普艺术','美国 · 20 世纪 60 年代','POP ART'], ['10-particles','生成艺术','当代 · 21 世纪','GENERATIVE ART'] ]; const scenes=[],particleLayers=[]; const clamp=(n,lo=0,hi=1)=>Math.min(hi,Math.max(lo,n)),lerp=(a,b,t)=>a+(b-a)*t; function ease(x){x=clamp(x);return x*x*(3-2*x);} function canvas(){return createCanvas(W,H);} function rect(c,x,y,w,h,col){c.fillStyle=col;c.fillRect(x,y,w,h);} function text(c,str,x,y,size,color=ivory,family='FilmSans',align='left',weight='normal'){ c.save();c.font=weight+' '+size+'px '+family;c.textAlign=align;c.textBaseline='alphabetic';c.fillStyle=color;c.fillText(str,x,y);c.restore(); } function gradient(c,x1,y1,x2,y2,colors){const g=c.createLinearGradient(x1,y1,x2,y2);for(const [p,v] of colors)g.addColorStop(p,v);return g;} function shade(c,heavy=false){rect(c,0,H-123,W,123,gradient(c,0,H-123,0,H,[[0,'#0b181900'],[.55,heavy?'#0b1819b3':'#0b181990'],[1,'#071213e9']]));} function label(c,letter,title,sub){ c.save();const width=Number.isInteger(letter)?307:Math.max(220,title.length*32+58); rect(c,39,34,width,99,'#142527e8');rect(c,39,34,4,99,gold); text(c,Number.isInteger(letter)?('展厅 '+String(letter).padStart(2,'0')+' / 10'):'镜头实验 / '+letter,58,59,14,gold); text(c,title,57,94,30,ivory,'FilmSong');text(c,sub,58,118,15,'#e2dbcb'); c.restore(); } function section(c,id,small){text(c,id,44,56,17,gold);text(c,small,44,85,22,ivory);} let person,cues; function crop(c,img,x,y,w,h){c.drawImage(img,0,0,img.width,img.height,x,y,w,h);} function buildParticles(){ // Seeded colour samples of the actual character. Moving subsets reform the original silhouette. const tiny=createCanvas(258,387),tc=tiny.getContext('2d');tc.drawImage(person,0,0,258,387); const pixels=tc.getImageData(0,0,258,387).data; let seed=388655762;const rand=()=>((seed=(Math.imul(seed,1664525)+1013904223)>>>0)/4294967296); for(let g=0;g<20;g++){const cv=canvas();particleLayers.push({cv,c:cv.getContext('2d'),angle:rand()*6.283,amp:20+rand()*105});} for(let y=0;y<300;y+=1.35)for(let x=0;x<258;x+=1.35){ const xx=Math.min(257,Math.floor(x+rand())),yy=Math.min(386,Math.floor(y+rand())),i=(yy*258+xx)*4; if(pixels[i+3]<90)continue; const group=particleLayers[Math.floor(rand()*20)],c=group.c; const [r,g,b]=pixels.slice(i,i+3),light=(r*.30+g*.59+b*.11)/255; const warm=(r>g*1.12);let col; if(warm)col='rgb('+[Math.round(128+122*light),Math.round(62+156*light),Math.round(34+132*light)].join(',')+')'; else col='rgb('+[Math.round(39+160*light),Math.round(89+154*light),Math.round(107+141*light)].join(',')+')'; const tx=872+(xx-126)*3.28,ty=191+(yy-51)*3.28; c.fillStyle=col;c.beginPath();c.arc(tx,ty,.85+rand()*1.12,0,Math.PI*2);c.fill(); } } function particleScene(c,u){ rect(c,0,0,W,H,'#10262e'); const glow=c.createRadialGradient(774,310,6,774,310,600);glow.addColorStop(0,'#255058');glow.addColorStop(1,'#10262e');rect(c,0,0,W,H,glow); for(let k=0;k<28;k++){c.beginPath();c.strokeStyle='rgba(103,194,180,'+(.12+.05*(1+Math.sin(k)))+')';c.lineWidth=1; for(let x=-16;x<=W+16;x+=16){const y=142+k*12+56*Math.sin(x/168+k*.056+u*.85)+46*Math.sin(x/260-k*.023-u); if(x===-16)c.moveTo(x,y);else c.lineTo(x,y);}c.stroke();} const pulse=Math.sin(Math.PI*ease(clamp((u-.12)/.75)))**2; for(const g of particleLayers){const a=g.angle+.27*u;const k=g.amp*pulse;c.drawImage(g.cv,Math.cos(a)*k,Math.sin(a)*k);} } function paintArt(c,i,u,backgroundOnly=false){ if(i===9){particleScene(c,clamp(u));return;} const a=scenes[i],progress=clamp(u); // The tracked human holds position; the distant wall moves past it at a slower rate than the frame. const zoom=1.074+.014*progress; const dw=W*zoom,dh=H*zoom; const dx=(W-dw)/2+lerp(31,-31,progress),dy=(H-dh)/2+6; crop(c,a.bg,dx,dy,dw,dh); if(!backgroundOnly)crop(c,a.fg,(W-dw)/2+lerp(1,-1,progress),dy,dw,dh); } function rail(c,x){ const g=gradient(c,x-12,0,x+13,0,[[0,'#312c23'],[.17,'#c4ac7b'],[.31,'#f5d7a0'],[.41,'#68523e'],[.81,'#352e25'],[1,'#be9c68']]); rect(c,x-12,0,25,H,g); } function shot(c,kind,u,factor=1){HaiCamera.draw(c,person,HaiCamera.state(kind,u,factor));} function splitCameras(c){ rect(c,0,0,W,H,ink); for(const [i,k] of ['zoom','push'].entries()){ const x=32+i*622, y=180;c.save();c.translate(x,y);c.scale(600/W,600/W);c.beginPath();c.rect(0,0,W,H);c.clip();shot(c,k,1);c.restore(); text(c,i?'B':'A',x+5,150,52,gold,'FilmLatin'); rect(c,x,y+337.5,600,2,'#9d976f'); } text(c,'终帧对照',40,63,35,ivory,'FilmSong');text(c,'主体等大 / 观察门框及近处栏杆',40,98,19,'#d5cdbb'); } function gardenBackdrop(c){ const img=scenes[7].original; // Crop the unobstructed garden from the same painted plate for an identical A/B background. c.drawImage(img,0,0,img.width*.44,img.height*.44,0,0,W,H); } function cropCheek(c,img,i,x,y,w,h,prog){ // Same displayed region of the face for both printed and painted samples. const eye=i===7?[.682,.248]:[.673,.255],sw=img.width*lerp(.215,.136,prog),sh=sw*h/w; const sx=img.width*eye[0]-sw*.48,sy=img.height*(eye[1]+.065)-sh*.48; c.drawImage(img,sx,sy,sw,sh,x,y,w,h); } function artPass(c,t){ const n=Math.min(9,Math.floor((t-26)/5)),dt=(t-26)-n*5,u=dt/5; if(dt<1.0){ if(n===0)shot(c,'vertigo',1);else paintArt(c,n-1,1); const edge=W*(1-ease(dt));c.save();c.beginPath();c.rect(edge,0,W-edge,H);c.clip(); paintArt(c,n,u);c.restore();rail(c,edge); } else paintArt(c,n,u); const info=pics[n]; c.save();c.globalAlpha=ease((dt-.38)/.36);label(c,n+1,info[1],info[2]);c.restore(); } function render(c,t){ c.save();c.clearRect(0,0,W,H); if(t<6){ shot(c,'push',.35+.28*ease(t/6)); rect(c,0,0,720,H,gradient(c,0,0,720,0,[[0,'#081b20ec'],[.57,'#0b2224bb'],[1,'#0c212300']])); text(c,'海叔 · 在场',49,137,24,gold);rect(c,50,163,53,2,gold); text(c,'如果我走进',45,292,77,ivory,'FilmSong');text(c,'一幅画',44,389,89,ivory,'FilmSong'); text(c,'一场关于镜头与画风的实验',50,462,22,'#e5ddc8'); }else if(t<14){ const A=t<10,u=ease(clamp((t-(A?6:10)-.12)/3.37));shot(c,A?'zoom':'push',u); label(c,A?'A':'B','观察镜头 '+(A?'A':'B'),'记住远处的门框和近处的栏杆'); }else if(t<18)splitCameras(c); else if(t<26){shot(c,'vertigo',clamp((t-18)/6.7));label(c,'032','滑动变焦','机位后退 × 焦距同步增加');} else if(t<76)artPass(c,t); else if(t<80.45){ gardenBackdrop(c); const ht=1160;crop(c,person,854-ht*(500/1536),172-ht*(205/1536),ht*2/3,ht); label(c,'对照 01','只改背景','留意脸颊和衣服的表面'); }else if(t<84.25){ gardenBackdrop(c);crop(c,scenes[7].fg,0,0,W,H);label(c,'对照 02','人物也入画','同一张脸 / 冷暖色点重构明暗'); }else if(t<88){ rect(c,0,0,W,H,ink); text(c,'两种点,两种用途',43,76,42,ivory,'FilmSong'); const u=ease((t-84.25)/3.75); for(const [j,i] of [7,8].entries()){ const x=38+617*j;cropCheek(c,scenes[i].original,i,x,130,587,370,u); text(c,j?'波普 · 规则印刷网点':'点彩 · 冷暖色点混合',x,548,26,gold); } }else{ shot(c,'vertigo',1-ease((t-88)/7.8)); rect(c,0,0,680,H,gradient(c,0,0,680,0,[[0,'#101e24f2'],[.56,'#10262ac9'],[1,'#10262a00']])); text(c,'轮到你掌镜',49,242,65,ivory,'FilmSong'); text(c,'机位后退,关闭焦距补偿。',51,315,27,gold); text(c,'海叔的脸会怎么变?',50,375,31,ivory); } const line=cues.find(([a,b])=>t>=a&&t<=b+.035); if(line){shade(c,true);text(c,line[2],640,671,26,'#fffaf1','FilmSans','center');} if(t<.32)rect(c,0,0,W,H,'rgba(12,26,26,'+(1-ease(t/.32))+')'); if(t>95.5)rect(c,0,0,W,H,'rgba(12,26,26,'+ease((t-95.5)/.5)+')'); c.restore(); } async function main(){ person=await loadImage(path.join(out,'海叔-卡通角色母版.png')); cues=JSON.parse(fs.readFileSync(path.join(R,'字幕时间.json'))); for(const [key] of pics.slice(0,9)){ const [bg,fg,original]=await Promise.all([['分层',key+'-bg.jpg'],['分层',key+'-fg.png'],['画稿',key+'.png']].map(parts=>loadImage(path.join(R,...parts)))); scenes.push({bg,fg,original}); } buildParticles(); const preview=process.argv.includes('--preview'),scale=preview?1:1.5; const surface=createCanvas(W*scale,H*scale),c=surface.getContext('2d');c.scale(scale,scale); if(preview){ const ts=[.9,9.5,13.6,15.7,24,29,34,39,44,49,54,59,64,69,73.7,78,82,86,91]; const sheet=createCanvas(960,Math.ceil(ts.length/3)*180),s=sheet.getContext('2d'); for(const [i,t] of ts.entries()){render(c,t);s.drawImage(surface,i%3*320,Math.floor(i/3)*180,320,180);} fs.writeFileSync('/tmp/hsfilm-cut.jpg',sheet.toBuffer('image/jpeg',86));return; } const bin=process.env.FFMPEG||'ffmpeg'; const output=path.join(out,'海叔在场-如果我走进一幅画.mp4'),temp=output.replace('.mp4','.rendering.mp4'); const enc=spawn(bin,['-y','-nostdin','-loglevel','error','-f','rawvideo','-pix_fmt','rgba','-s','1920x1080','-r','24','-i','pipe:0','-i','/tmp/hsfilm-master.wav','-c:v','libx264','-preset','veryfast','-crf','17','-pix_fmt','yuv420p','-color_primaries','bt709','-color_trc','bt709','-colorspace','bt709','-vf','scale=out_color_matrix=bt709','-c:a','aac','-b:a','192k','-movflags','+faststart','-t','96',temp],{stdio:['pipe','ignore','inherit']}); const close=once(enc,'close'); for(let f=0;f{console.error(e);process.exitCode=1;});