SplatAnything / scripts /capture_scene_previews.cjs
ritianyu's picture
Publish preview-first SplatAnything demo for ZeroGPU
dc09dcc verified
Raw History Blame Contribute Delete
2.3 kB
// Build-time helper. Runtime serves the generated WebP files without a browser.
const fs = require('node:fs');
const path = require('node:path');
const {pathToFileURL} = require('node:url');
const {chromium} = require(process.env.SPLATANYTHING_PLAYWRIGHT_MODULE || 'playwright');
async function capture() {
const [catalogPath, source, output] = process.argv.slice(2);
if (!catalogPath || !source || !output) throw new Error('Expected catalog.json, HTML source directory, and output directory.');
const catalog = JSON.parse(fs.readFileSync(catalogPath, 'utf8'));
fs.mkdirSync(output, {recursive:true});
const browser = await chromium.launch({
...(process.env.SPLATANYTHING_CHROMIUM ? {executablePath:process.env.SPLATANYTHING_CHROMIUM} : {}),
args:['--no-sandbox','--disable-dev-shm-usage','--use-angle=swiftshader','--enable-unsafe-swiftshader']
});
try {
for (const [id, record] of Object.entries(catalog.scenes)) {
const target = path.join(output, record.poster);
if (fs.existsSync(target)) continue;
const page = await browser.newPage({viewport:{width:1440,height:900}});
try {
await page.goto(pathToFileURL(path.join(source, id+'.html')).href+'?noanim=1',
{waitUntil:'domcontentloaded',timeout:180000});
await page.waitForFunction(()=>typeof window.captureFrame==='function', null, {timeout:180000});
const data = await page.evaluate(async()=>{
const image = await window.captureFrame({time:0,width:1440,height:900,supersample:1});
const raw = atob(image.data);
const pixels = new Uint8ClampedArray(raw.length);
for (let i=0;i<raw.length;i++) pixels[i]=raw.charCodeAt(i);
const canvas = document.createElement('canvas');
canvas.width=image.width;canvas.height=image.height;
canvas.getContext('2d').putImageData(new ImageData(pixels,image.width,image.height),0,0);
return canvas.toDataURL('image/webp',.88);
});
fs.writeFileSync(target,Buffer.from(data.split(',')[1],'base64'));
console.log('Captured',id,fs.statSync(target).size,'bytes');
} finally { await page.close(); }
}
} finally { await browser.close(); }
}
capture().catch(error=>{console.error(error);process.exitCode=1;});