Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
76 lines (68 loc) · 3.12 KB
/
Copy pathapp.js
File metadata and controls
76 lines (68 loc) · 3.12 KB
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
const path = require("path");
const fs = require("fs");
const fsPromises = fs.promises;
const spawn = require('child_process').spawn;
const config = require("./config");
const assetsFolderPath = path.join('.', 'assets');
const JSONsPath = path.join(assetsFolderPath, 'json');
const updatedAssetsFolderPath = path.join('.', 'updatedAssets');
const updatedImagesPath = path.join(updatedAssetsFolderPath, 'images');
const updatedJSONsPath = path.join(updatedAssetsFolderPath, 'json');
// ffmpeg must be accesible system wide. (Just google: "install ffmepg on windows/linux")
const createGif = async (edition,trait)=> {
return new Promise(async (resolve, reject) => {
const artFromAssets = `assets/images/${edition}.png`;
const replaceWith = `replaceWith/${trait}.gif`;
var args = [
'-i',`${config.background ? replaceWith : artFromAssets}`,
'-i',`${config.background ? artFromAssets : replaceWith}`,
'-filter_complex',`[0]scale=${config.resolutionWidth}:${config.resolutionHeight}:force_original_aspect_ratio=decrease,pad=${config.resolutionWidth}:${config.resolutionHeight}:-1:-1:color=black@0[bg];[bg][1]overlay=format=auto,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse`,
'-loglevel',`${config.debug?"info":"quiet"}`,
`updatedAssets/images/${edition}.gif`
];
var proc = spawn('ffmpeg', args);
proc.stdout.setEncoding("utf8")
proc.stdout.on('data', function(data) {
console.log(data);
});
proc.stderr.setEncoding("utf8")
proc.stderr.on('data', function(data) {
console.log(data);
});
proc.on('close', function() {
console.log('Edition',edition,'done.');
return resolve();
});
});
}
const main = async () =>{
await fsPromises.rm(updatedAssetsFolderPath, { recursive: true, force: true });
await fsPromises.mkdir(updatedImagesPath, { recursive: true })
await fsPromises.mkdir(updatedJSONsPath, { recursive: true })
const allJsons = await fsPromises.readdir(JSONsPath);
//upload each video
for(let json of allJsons){
const jsonPath = path.join(JSONsPath, json);
let parsedJSON = JSON.parse(fs.readFileSync(jsonPath, { encoding: 'utf-8' }))
const trait = parsedJSON.attributes.find(element => {
if(element.trait_type == config.trait) return element.value
});
if (!trait){
console.log(`Trait "${config.trait}" that you specified in config file is not valid.`);
process.exit(1);
}
await createGif(parsedJSON.edition,trait.value);
parsedJSON.image = `${parsedJSON.edition}.gif`
parsedJSON.properties.files[0].uri = `${parsedJSON.edition}.gif`
parsedJSON.properties.files[0].type = "image/gif"
const newJSONpath = path.join(updatedJSONsPath,`${parsedJSON.edition}.json`);
await fsPromises.writeFile(newJSONpath, JSON.stringify(parsedJSON, null, 2), function (err) {
if (err) {
console.log('The Updated JSON file could not be written.', err)
process.exit(1)
}
})
console.log("All done!")
}
}
main();