-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-braille.js
71 lines (58 loc) · 1.82 KB
/
generate-braille.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
const { braillefy } = require('img2braille');
const fs = require('fs');
const cliProgress = require('cli-progress');
const workerFarm = require('worker-farm');
// -----
// Params to modify art
const asciiWidth = 100;
const asciiOpts = {
dither: false,
invert: false,
};
// -----
const frameCount = fs.readdirSync('./video/frames').length;
const workers = workerFarm(require.resolve('./child'));
const framesPerWorker = 5;
const bar1 = new cliProgress.SingleBar({ etaBuffer: framesPerWorker * 50 });
const workerCount = Math.ceil(frameCount / framesPerWorker);
console.info(
`Generating ${frameCount} frames with ${asciiWidth} width through ${workerCount} batches`
);
bar1.start(frameCount, 0);
const allFrames = new Array(frameCount);
for (let i = 0; i < workerCount; i++) {
const startFrame = i * framesPerWorker + 1;
const endFrame = Math.min((i + 1) * framesPerWorker, frameCount);
// Create workers to process multiple frames at once
workers(
{
index: i,
startFrame,
endFrame,
asciiWidth,
asciiOpts,
},
(log, frames) => {
// console.log(log, bar1.getProgress());
// Store processed frames in their correct indices to keep order
for (let j = startFrame - 1, k = 0; j < endFrame; j++, k++) {
allFrames[j] = frames[k];
}
bar1.increment(frames.length);
// Last worker triggered callback
if (bar1.getProgress() >= 1) {
bar1.stop();
}
}
);
}
bar1.on('stop', () => {
// Kill child processes
workerFarm.end(workers);
const text = allFrames.join('\n\n');
// Replace braille character representing blank ("⠄") with actual blank braille character ("⠀")
// .replace(/\u2804/gm, '\u2800');
// Write final file
fs.writeFileSync('video/braille.txt', text);
console.info(`Written braille frames into text file`);
});