const {execFileSync,spawnSync}=require('node:child_process'); const fs=require('node:fs'); const exe=process.env.FFMPEG || 'ffmpeg'; const run=(...args)=>execFileSync(exe,['-v','error','-y',...args],{cwd:__dirname}); for(const [name,color] of [['a','red'],['b','blue']]){ run('-f','lavfi','-i',`color=${color}:s=160x90:r=24:d=2`,'-c:v','libx264','-g','72','-sc_threshold','0',name+'.mp4'); } fs.writeFileSync(__dirname+'/list.txt',"file 'a.mp4'\nfile 'b.mp4'\n"); const input=['-f','concat','-safe','0','-i','list.txt']; run(...input,'-c','copy','copy.mp4'); run(...input,'-c:v','libx264','-g','72','-keyint_min','72','-sc_threshold','0','fixed.mp4'); run(...input,'-c:v','libx264','-g','72','-keyint_min','72','-sc_threshold','0','-force_key_frames','2','forced.mp4'); const result={}; for(const name of ['a','b','copy','fixed','forced']){ const output=spawnSync(exe,['-hide_banner','-i',name+'.mp4','-vf','showinfo','-f','null','-'],{cwd:__dirname,encoding:'utf8'}); if(output.status!==0) throw Error(output.stderr); const frames=[...output.stderr.matchAll(/n:\s*(\d+).*?pts_time:\s*(\S+).*?iskey:(\d)/g)]; if(!frames.length) throw Error('No decoded frame records'); result[name]={frames:frames.length,keyframes:frames.filter(frame=>frame[3]==='1').map(frame=>({frame:Number(frame[1]),seconds:Number(frame[2])}))}; } fs.writeFileSync(__dirname+'/measurements.json',JSON.stringify(result,null,2)); console.log(result);