@ibaraki-douji/anime4k-gif
Version:
Upscale GIFs using Anime4kCPP
69 lines (68 loc) • 2.39 kB
JavaScript
;
const Anime4k = require("@ibaraki-douji/anime4k");
const path = require("path");
const os_1 = require("os");
const fs_1 = require("fs");
const child_process_1 = require("child_process");
const generate = (length) => { const alphabet = "AZERTYUIOPQSDFGHJKLMWXCVBNazertyuiopqsdfghjklmwxcvbn123456789"; let end = ""; for (let i = 0; i < length; i++)
end += alphabet[Math.floor(Math.random() * alphabet.length)]; return end; };
module.exports = class Waifu2XGIF {
inputPath;
outputPath;
up;
endBuffer;
end = false;
constructor(inp, out, options = {}) {
if (typeof inp == "string")
this.inputPath = path.resolve(inp);
else {
this.inputPath = path.resolve(os_1.tmpdir(), generate(50) + ".gif");
fs_1.writeFileSync(this.inputPath, inp);
}
if (options.outputAsBuffer) {
this.outputPath = path.resolve(os_1.tmpdir(), generate(50) + ".gif");
}
else {
this.outputPath = path.resolve(out);
}
const vidTmp = path.resolve(os_1.tmpdir(), generate(50) + ".mp4");
const outTmp = path.resolve(os_1.tmpdir(), generate(50) + ".mp4");
child_process_1.spawnSync("ffmpeg", [
"-y",
"-i", this.inputPath,
"-pix_fmt", "yuv420p",
"-c:v", "h264",
"-movflags", "+faststart",
"-filter:v", "crop='floor(in_w/2)*2:floor(in_h/2)*2'",
vidTmp
]);
this.up = Anime4k.upscale(vidTmp, outTmp, options);
this.up.process.on("close", () => {
this.endBuffer = this.up.endBuffer;
child_process_1.spawnSync("ffmpeg", [
"-y",
"-i", outTmp,
"-c:v", "gif",
"-pix_fmt", "bgra",
this.outputPath
]);
this.end = true;
});
}
static upscale(inp, out, options) {
return new Waifu2XGIF(inp, out, options);
}
static listGPUs() {
return Anime4k.listGPUs();
}
finishedPromise() {
return new Promise(res => {
let x = setInterval(() => {
if (this.end) {
clearInterval(x);
res();
}
}, 100);
});
}
};