-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (42 loc) · 1.58 KB
/
index.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
const fs = require("fs");
const extractFrames = require("gif-extract-frames");
const jp2a = require("jp2a");
const makeItDance = require("./makeItDance");
/**
*
* @param {string} gifFile the gif file which will convert to ascii
* @param {Object} asciiConfig config for how the ascii should looks like
* @param {Object} danceConfig config for how should ascii dance on a terminal
*/
async function asciidance(gifFile, asciiConfig = {}) {
const frameDir = "./frames";
const asciiDir = "./ascii";
if (!fs.existsSync(frameDir) || !fs.existsSync(asciiDir)) {
fs.mkdirSync(frameDir, { recursive: true });
fs.mkdirSync(asciiDir, { recursive: true });
}
const gifToFrames = await extractFrames({
input: `./gifs/${gifFile}`,
output: "./frames/frame-%d.jpg",
});
const NumberOfFrames = fs.readdirSync(frameDir).length;
for (let i = 0; i < NumberOfFrames; ++i) {
jp2a(
[
`./frames/frame-${i}.jpg`,
`${asciiConfig.chars ? `--chars=${asciiConfig.chars}` : ``}`,
`${asciiConfig.bg ? `--background=${asciiConfig.bg}` : ``}`,
`${asciiConfig.border ? `--${asciiConfig.border}` : ``}`,
`${asciiConfig.flipx ? `--${asciiConfig.flipx}` : ``}`,
`${asciiConfig.flipy ? `--${asciiConfig.flipy}` : ``}`,
`${asciiConfig.size ? `--size=${asciiConfig.size}` : ``}`,
`${asciiConfig.width ? `--width=${asciiConfig.width}` : ``}`,
],
function (ouput) {
fs.writeFileSync(`./${asciiDir}/ascii-${i}.txt`, ouput);
}
);
}
setTimeout(() => makeItDance(asciiConfig), 2000);
}
module.exports = asciidance;