const fs=require('fs'),path=require('path'),{execFileSync}=require('child_process');const d=__dirname,ff=path.resolve('work/frame-builder/node_modules/ffmpeg-static/ffmpeg.exe');function run(...a){return execFileSync(ff,['-hide_banner','-nostdin',...a],{cwd:d,encoding:'utf8',stdio:['ignore','pipe','pipe']})}for(const [f,c]of [['a.mp4','red'],['b.mp4','blue']])run('-v','error','-y','-f','lavfi','-i',`color=${c}:s=160x90:r=24:d=2`,'-c:v','libx264','-bf','2','-g','48',f);fs.writeFileSync(d+'/list.txt','file a.mp4\nfile b.mp4\n');run('-v','error','-y','-f','concat','-safe','0','-i','list.txt','-c','copy','join.mp4');let out={utc:new Date().toISOString(),version:run('-version').split('\n')[0],inputs:'two generated 160x90 24fps 2s H264 clips, red then blue, bf2 g48',files:{}};for(const f of ['a.mp4','b.mp4','join.mp4']){const packets=run('-v','error','-copyts','-i',f,'-map','0:v:0','-c','copy','-f','framecrc','-');const frames=run('-v','error','-i',f,'-map','0:v:0','-f','framecrc','-');fs.writeFileSync(d+'/'+f+'-packets.txt',packets);fs.writeFileSync(d+'/'+f+'-frames.txt',frames);out.files[f]={timebase:packets.match(/#tb 0: (.*)/)[1],packets:packets.split(/\r?\n/).filter(x=>/^0,/.test(x)).map(x=>x.split(',').map(v=>v.trim())),frames:frames.split(/\r?\n/).filter(x=>/^0,/.test(x)).map(x=>x.split(',').at(-1).trim())}}out.decodedSequenceMatches=JSON.stringify(out.files['join.mp4'].frames)===JSON.stringify([...out.files['a.mp4'].frames,...out.files['b.mp4'].frames]);fs.writeFileSync(d+'/measurements.json',JSON.stringify(out,null,2));console.log(JSON.stringify({version:out.version,match:out.decodedSequenceMatches,files:Object.fromEntries(Object.entries(out.files).map(([k,v])=>[k,{timebase:v.timebase,packets:v.packets.length,frames:v.frames.length,firstPackets:v.packets.slice(0,6)}]))},null,2));