-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvisualization.js
81 lines (72 loc) · 2.08 KB
/
visualization.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const { input } = require('./input.js');
const { Grid, Ground } = require('./ground.js');
const fs = require('fs-extra');
const path = require('path');
const Jimp = require('jimp');
const { spawn } = require('child_process');
// Helpers
const getPaths = (bin_path) => {
const envPath = process.env.PATH || '';
const envExt = process.env.PATHEXT || '';
return envPath
.replace(/["]+/g, '')
.split(path.delimiter)
.map((chunk) => {
return envExt
.split(path.delimiter)
.map((ext) => path.join(chunk, bin_path + ext));
})
.reduce((a, b) => a.concat(b), []);
};
/**
* Helper utility to check if a binary exists in our $PATH variable.
* @see https://github.com/springernature/hasbin/blob/5af037b/lib/hasbin.js
* @license MIT
* @returns {Boolean}
*/
const hasBinary = (bin) => {
return getPaths(bin).some((filePath) => {
try {
return fs.statSync(filePath).isFile();
} catch (error) {
return false;
}
});
};
const grid = new Grid(input);
const ground = new Ground({ grid });
(async () => {
let empty_grid_image_buffer = await ground.toImage({
callback: async ({ image }) => {
return await image.getBufferAsync(Jimp.MIME_PNG);
},
});
fs.writeFileSync('grid.png', empty_grid_image_buffer);
fs.writeFileSync('grid.txt', ground.toString());
if (hasBinary('ffmpeg')) {
await ground.fill(true);
let image_buffer = await ground.toImage({
trimmed: true,
callback: async ({ image, grid_instance }) => {
const buffer = await image.getBufferAsync(Jimp.MIME_PNG);
return buffer;
},
});
fs.writeFileSync('filled-grid.png', image_buffer);
fs.writeFileSync('filled-grid.txt', ground.toString());
fs.removeSync('animation.mp4');
await new Promise((resolve, reject) => {
let video_process = spawn(
'ffmpeg',
`-framerate 60 -i frames/frame_%04d.png -c:v libx264 -pix_fmt yuv420p animation.mp4`.split(
' '
),
{ stdio: 'inherit' }
);
video_process.on('close', (code) => resolve(code));
video_process.on('error', (...args) => reject(args));
});
fs.removeSync('frames');
console.log('Wrote to "animation.mp4"');
}
})();