const fs=require('node:fs'),path=require('node:path'),{execFileSync}=require('node:child_process'); const bundled=path.resolve('work/frame-builder/node_modules/ffmpeg-static/ffmpeg.exe'); const ff=process.env.PVF_FFMPEG||(fs.existsSync(bundled)?bundled:'ffmpeg'); fs.writeFileSync(__dirname+'/command-output.txt',''); const logs=[];const run=(...a)=>{logs.push(a);try{return execFileSync(ff,['-hide_banner','-nostdin','-y',...a],{cwd:__dirname,maxBuffer:20e6,stdio:['ignore','pipe','pipe']});}catch(e){fs.appendFileSync(__dirname+'/command-output.txt',e.stderr);throw e;}}; for(const [file,color,hz,sr] of [['a.mov','red',440,44100],['b.mov','blue',880,48000]])run('-f','lavfi','-i',`color=${color}:s=160x90:r=24:d=2`,'-f','lavfi','-i',`sine=frequency=${hz}:sample_rate=${sr}:duration=2`,'-c:v','libx264','-pix_fmt','yuv420p','-c:a','pcm_s16le',file); fs.writeFileSync(__dirname+'/list.txt','file a.mov\nfile b.mov\n'); run('-f','concat','-safe','0','-i','list.txt','-c','copy','copied.mov'); run('-i','a.mov','-i','b.mov','-filter_complex','[0:a]aresample=48000[a0];[1:a]aresample=48000[a1];[0:v][a0][1:v][a1]concat=n=2:v=1:a=1[v][a]','-map','[v]','-map','[a]','-c:v','libx264','-pix_fmt','yuv420p','-c:a','alac','normalized.mp4'); const result={utc:new Date().toISOString(),ffmpeg:run('-version').toString().split('\n')[0],files:{},commands:logs}; for(const file of ['a.mov','b.mov','copied.mov','normalized.mp4']){ let info='';try{run('-i',file);}catch(e){info=e.stderr.toString();} const sr=Number(info.match(/Audio:.*?, (\d+) Hz/)[1]); const pcm=run('-i',file,'-map','0:a:0','-c:a','pcm_s16le','-f','s16le','-'); const freq=start=>{let crossings=0;const offset=Math.round(start*sr);for(let j=offset+1;j0)crossings++;return crossings;}; const decode=run('-i',file,'-map','0:v:0','-f','framecrc','-').toString(); result.files[file]={sampleRate:sr,decodedSamples:pcm.length/2,audioSeconds:pcm.length/2/sr,positiveCrossingsFirstSecond:freq(.25),...(file==='copied.mov'||file==='normalized.mp4'?{positiveCrossingsSecondSegment:freq(2.25)}:{}),decodedVideoFrames:decode.split('\n').filter(l=>/^0,/.test(l)).length}; } fs.writeFileSync(__dirname+'/measurements.json',JSON.stringify(result,null,2));console.log(JSON.stringify(result.files,null,2));