8000 fixes for skip frames, bump index of part on second place, clearer va… · devforth/scriptimate@94cf467 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94cf467

Browse files
committed
fixes for skip frames, bump index of part on second place, clearer variable names
1 parent 0c6ef6e commit 94cf467

File tree

3 files changed

+64
-44
lines changed

3 files changed

+64
-44
lines changed

bin/puWorker.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const fs = require("fs");
55

66
const MAX_FILENAME_DIGS = 7;
77

8-
fs.writeFileSync("/tmp/test.txt", JSON.stringify(process.argv), () => {})
98

109
const input = {
1110
pageW: +process.argv[2],
@@ -18,24 +17,24 @@ const input = {
1817
skipFrames: +process.argv[9],
1918
}
2019

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-
}
20+
fs.writeFileSync(`/tmp/scriptimate_debug_test${input.index}.txt`, JSON.stringify(input, null, 2), () => {});
21+
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()

bin/scriptimate.js

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ function firstDefined(...vals) {
245245

246246
const addPart = async (lang, filename, left, top, opacity, scale, toBoxHole, dashoffset) => {
247247
let f;
248+
if (parts[filename]) {
249+
// treat second place as attmpt to bump an index
250+
parts[filename].index = Object.values(parts).reduce((a, p) => Math.max(a, p.index), 0) + 1;
251+
return;
252+
}
248253

249254
const readFname = (fn) => {
250255
const filePath = `${proc_args.basedir}/src/${fn}.svg`;
@@ -301,7 +306,7 @@ const addPart = async (lang, filename, left, top, opacity, scale, toBoxHole, das
301306
top: +firstDefined(eval(top), 0),
302307
left: +firstDefined(eval(left), 0),
303308
opacity: +firstDefined(eval(opacity), 1),
304-
index: Object.values(parts).length,
309+
index: Object.values(parts).reduce((a, p) => Math.max(a, p.index), 0) + 1,
305310
scale: +firstDefined(eval(scale), 1.0),
306311
rotate: +firstDefined(0, 0),
307312
dashoffset: +firstDefined(eval(dashoffset), 0),
@@ -376,8 +381,8 @@ const schedule_eval = (name, ms, ...rest) => {
376381
return parts[part].content;
377382
}
378383
const set = (part, value) => {
379-
parts[part].content = eval(value);
380-
global[part+'_value'] = eval(value)
384+
parts[part].content = value;
385+
global[part+'_value'] = value;
381386
}
382387
eval(code)
383388
},
@@ -421,8 +426,8 @@ if (! script) {
421426
throw "Please specify .smte file e.g. -i demo.smte"
422427
}
423428

424-
const htmlHashed = {};
425-
const reuseFrames = {};
429+
const frameAbsIndexByHTMLHash = {};
430+
const reuseAbsFrameIndexForAbsFrameIndex = {};
426431

427432
const runGeneration = async (lang) => {
428433
initVariables();
@@ -437,22 +442,25 @@ const runGeneration = async (lang) => {
437442
}
438443

439444
const doFrame = async () => {
440-
const html = genHtml(parts);
441-
const hash = crypto.createHash('sha1').update(html).digest('base64');
445+
442446
if (cntr < skipFrames || (globalLastFrame && cntr > globalLastFrame)) {
443447
cntr += 1;
444448
return;
445449
}
446450
totalFramesCount += 1;
447-
if (!htmlHashed[hash]) {
448-
htmlHashed[hash] = cntr;
451+
452+
const html = genHtml(parts);
453+
const hash = crypto.createHash('sha1').update(html).digest('base64');
454+
455+
if (!frameAbsIndexByHTMLHash[hash]) {
456+
frameAbsIndexByHTMLHash[hash] = cntr;
449457
await fs.writeFile(`${FRAMES_DIR}/_index${(''+cntr).padStart(MAX_FILENAME_DIGS, '0')}.html`, html, function(err) {
450-
if(err) {
451-
return console.log(err);
458+
if (err) {
459+
return console.log(err);
452460
}
453461
});
454462
} else {
455-
reuseFrames[cntr] = htmlHashed[hash];
463+
reuseAbsFrameIndexForAbsFrameIndex[cntr] = frameAbsIndexByHTMLHash[hash];
456464
}
457465

458466
cntr += 1;
@@ -667,16 +675,25 @@ const runGeneration = async (lang) => {
667675

668676

669677
log('✅ [2/4] HTML generation done')
670-
log(`🕗 Total duration: ${(globalFramesCounter / FPS).toFixed(1)}s FPS: ${FPS}`)
678+
log(`🕗 Total duration: ${(globalFramesCounter / FPS).toFixed(1)}s 🎞️ FPS: ${FPS}`)
671679

672680
const THREADS = + proc_args.threads;
673681
let totalGenCntr = 0;
674682

675683
async function genScreenshots(index) {
684+
const absoluteIndex = index + skipFrames;
676685
await new Promise((resolve) => {
677-
if (!reuseFrames[index]) {
686+
if (!reuseAbsFrameIndexForAbsFrameIndex[absoluteIndex]) {
678687

679-
const proc = spawn('node', [path.resolve(__dirname, 'puWorker.js'), pageW, pageH, index, totalFramesCount, FRAMES_DIR, FORMAT, QUALITY, skipFrames || 0], { shell: true });
688+
const proc = spawn('node', [
689+
path.resolve(__dirname, 'puWorker.js'),
690+
pageW, pageH,
691+
absoluteIndex,
692+
totalFramesCount,
693+
FRAMES_DIR,
694+
FORMAT,
695+
QUALITY, skipFrames || 0
696+
], { shell: true });
680697
proc.stdout.on('data', (data) => {
681698
// console.log(`NodeOUT: ${data}`);
682699
});
@@ -688,21 +705,23 @@ const runGeneration = async (lang) => {
688705
log(`Frames gen: ${(totalGenCntr * 100.0 / totalFramesCount).toFixed(2)}%`, '\033[F');
689706
if (code !== 0) {
690707
log('🔴 node failed')
708+
process.exit(-1)
691709
}
692710
resolve();
693711
});
694712
} else {
695713
totalGenCntr += 1;
696714
resolve();
697715
}
698-
})
716+
});
699717
}
700718

701719
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}`;
720+
const absoluteIndex = index + skipFrames;
721+
if (reuseAbsFrameIndexForAbsFrameIndex[absoluteIndex]) {
722+
const reuseAbsIndex = reuseAbsFrameIndexForAbsFrameIndex[absoluteIndex];
723+
const srcFile = `${FRAMES_DIR}/${(''+(reuseAbsIndex - skipFrames)).padStart(MAX_FILENAME_DIGS, '0')}.${FORMAT}`;
724+
const dstFile = `${FRAMES_DIR}/${(''+(index)).padStart(MAX_FILENAME_DIGS, '0')}.${FORMAT}`;
706725
fsExtra.copyFile(srcFile, dstFile, (err) => {
707726
if (err) {
708727
log(`🔴 failed to copy frame ${srcFile} to ${dstFile}`,)
@@ -720,7 +739,9 @@ const runGeneration = async (lang) => {
720739

721740
const indexes = Array.from( Array(totalFramesCount).keys() );
722741

723-
await Promise.all(arrayChunks(indexes, Math.round( (indexes.length) / THREADS) ).map(async (indexesChunk) => await genScreenshotsForChunk(indexesChunk)))
742+
await Promise.all(
743+
arrayChunks(indexes, Math.round( (indexes.length) / THREADS) ).map(async (indexesChunk) => await genScreenshotsForChunk(indexesChunk))
744+
)
724745

725746
// another run to copy all duplicate files
726747
indexes.forEach(copyReusedScreenshots);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptimate",
3-
"version": "1.2.14",
3+
"version": "1.2.16",
44
"description": "Open-source SVG animation tool",
55
"main": "bin/index.js",
66
"keywords": [

0 commit comments

Comments
 (0)
0