10000 don't generate unchanged frames, fix for --fromsecond which was broke… · devforth/scriptimate@0c6ef6e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c6ef6e

Browse files
committed
don't generate unchanged frames, fix for --fromsecond which was broken after pptr optimizations
1 parent f456b36 commit 0c6ef6e

File tree

2 files changed

+74
-43
lines changed

2 files changed

+74
-43
lines changed

bin/puWorker.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,29 @@ const input = {
1313
index: +process.argv[4],
1414
totalFramesCount: +process.argv[5],
1515
framesDir: process.argv[6],
16-
skipFrames: 0,
1716
format: process.argv[7],
1817
quality: process.argv[8],
18+
skipFrames: +process.argv[9],
1919
}
2020

21-
const fileHtml = fs.readFileSync(`${input.framesDir}/_index${(''+input.index).padStart(MAX_FILENAME_DIGS, '0')}.html`, 'utf8')
22-
const jpegFileName = `${input.framesDir}/${(''+(input.index - input.skipFrames)).padStart(MAX_FILENAME_DIGS, '0')}.${input.format}`;
23-
24-
const genScreenshots = async () => {
25-
const browser = await puppeteer.launch({args: [
26-
`--window-size=${input.pageW},${input.pageH}`,
27-
'--no-sandbox',
28-
'--disk-cache-dir=/tmp/pup',
29-
], headless: true,});
30-
31-
const page = await browser.newPage();
32-
await page.setViewport({width: input.pageW, height: input.pageH, deviceScaleFactor: 1});
33-
await page._client.send('Emulation.clearDeviceMetricsOverride');
34-
await page.setContent(fileHtml);
35-
await page.screenshot({path: jpegFileName, type: input.format, omitBackground: true});
36-
37-
process.exit();
38-
}
39-
40-
genScreenshots()
21+
if (input.index >= input.skipFrames) {
22+
const fileHtml = fs.readFileSync(`${input.framesDir}/_index${(''+input.index).padStart(MAX_FILENAME_DIGS, '0')}.html`, 'utf8')
23+
const jpegFileName = `${input.framesDir}/${(''+(input.index - input.skipFrames)).padStart(MAX_FILENAME_DIGS, '0')}.${input.format}`;
24+
25+
const genScreenshots = async () => {
26+
const browser = await puppeteer.launch({args: [
27+
`--window-size=${input.pageW},${input.pageH}`,
28+
'--no-sandbox',
29+
'--disk-cache-dir=/tmp/pup',
30+
], headless: true,});
31+
32+
const page = await browser.newPage();
33+
await page.setViewport({width: input.pageW, height: input.pageH, deviceScaleFactor: 1});
34+
await page._client.send('Emulation.clearDeviceMetricsOverride');
35+
await page.setContent(fileHtml);
36+
await page.screenshot({path: jpegFileName, type: input.format, omitBackground: true});
37+
38+
process.exit();
39+
}
40+
genScreenshots()
41+
}

bin/scriptimate.js

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { version } = require('../package.json');
99
const { exception } = require('console');
1010
const svgDim = require('svg-dimensions');
1111
const YAML = require('yaml');
12+
const crypto = require('crypto');
1213

1314
const path = require('path');
1415

@@ -420,6 +421,8 @@ if (! script) {
420421
throw "Please specify .smte file e.g. -i demo.smte"
421422
}
422423

424+
const htmlHashed = {};
425+
const reuseFrames = {};
423426

424427
const runGeneration = async (lang) => {
425428
initVariables();
@@ -435,17 +438,22 @@ const runGeneration = async (lang) => {
435438

436439
const doFrame = async () => {
437440
const html = genHtml(parts);
441+
const hash = crypto.createHash('sha1').update(html).digest('base64');
438442
if (cntr < skipFrames || (globalLastFrame && cntr > globalLastFrame)) {
439443
cntr += 1;
440444
return;
441445
}
442446
totalFramesCount += 1;
443-
444-
await fs.writeFile(`${FRAMES_DIR}/_index${(''+cntr).padStart(MAX_FILENAME_DIGS, '0')}.html`, html, function(err) {
445-
if(err) {
446-
return console.log(err);
447-
}
448-
});
447+
if (!htmlHashed[hash]) {
448+
htmlHashed[hash] = cntr;
449+
await fs.writeFile(`${FRAMES_DIR}/_index${(''+cntr).padStart(MAX_FILENAME_DIGS, '0')}.html`, html, function(err) {
450+
if(err) {
451+
return console.log(err);
452+
}
453+
});
454+
} else {
455+
reuseFrames[cntr] = htmlHashed[hash];
456+
}
449457

450458
cntr += 1;
451459
log(`HTML pages gen: ${(cntr * 100.0 / (totalFrames + 1)).toFixed(2)}%`, '\033[F');
@@ -666,27 +674,44 @@ const runGeneration = async (lang) => {
666674

667675
async function genScreenshots(index) {
668676
await new Promise((resolve) => {
669-
const proc = spawn('node', [path.resolve(__dirname, 'puWorker.js'), pageW, pageH, index, totalFramesCount, FRAMES_DIR, FORMAT, QUALITY], { shell: true });
670-
proc.stdout.on('data', (data) => {
671-
// console.log(`NodeOUT: ${data}`);
672-
});
673-
674-
proc.stderr.on('data', (data) => {
675-
console.error(`NodeERR: ${data}`);
676-
});
677-
678-
proc.on('close', (code) => {
677+
if (!reuseFrames[index]) {
678+
679+
const proc = spawn('node', [path.resolve(__dirname, 'puWorker.js'), pageW, pageH, index, totalFramesCount, FRAMES_DIR, FORMAT, QUALITY, skipFrames || 0], { shell: true });
680+
proc.stdout.on('data', (data) => {
681+
// console.log(`NodeOUT: ${data}`);
682+
});
683+
proc.stderr.on('data', (data) => {
684+
console.error(`NodeERR: ${data}`);
685+
});
686+
proc.on('close', (code) => {
687+
totalGenCntr += 1;
688+
log(`Frames gen: ${(totalGenCntr * 100.0 / totalFramesCount).toFixed(2)}%`, '\033[F');
689+
if (code !== 0) {
690+
log('🔴 node failed')
691+
}
692+
resolve();
693+
});
694+
} else {
679695
totalGenCntr += 1;
680-
log(`Frames gen: ${(totalGenCntr * 100.0 / totalFramesCount).toFixed(2)}%`, '\033[F');
696+
resolve();
697+
}
698+
})
699+
}
681700

682-
if (code !== 0) {
683-
log('🔴 node failed')
701+
async function copyReusedScreenshots(index) {
702+
if (reuseFrames[index]) {
703+
const reuseIndex = reuseFrames[index];
704+
const srcFile = `${FRAMES_DIR}/${(''+(reuseIndex - skipFrames)).padStart(MAX_FILENAME_DIGS, '0')}.${FORMAT}`;
705+
const dstFile = `${FRAMES_DIR}/${(''+(index - skipFrames)).padStart(MAX_FILENAME_DIGS, '0')}.${FORMAT}`;
706+
fsExtra.copyFile(srcFile, dstFile, (err) => {
707+
if (err) {
708+
log(`🔴 failed to copy frame ${srcFile} to ${dstFile}`,)
709+
throw err;
684710
}
685-
resolve();
686711
});
687-
})
712+
}
688713
}
689-
714+
690715
async function genScreenshotsForChunk(indexesChunk) {
691716
for (let i=0; i < indexesChunk.length; i+=1) {
692717
await genScreenshots(indexesChunk[i]);
@@ -697,6 +722,11 @@ const runGeneration = async (lang) => {
697722

698723
await Promise.all(arrayChunks(indexes, Math.round( (indexes.length) / THREADS) ).map(async (indexesChunk) => await genScreenshotsForChunk(indexesChunk)))
699724

725+
// another run to copy all duplicate files
726+
indexes.forEach(copyReusedScreenshots);
727+
728+
729+
700730

701731
log('✅ [3/4] Frames generation done')
702732

0 commit comments

Comments
 (0)
0