UNPKG

@ibaraki-douji/anime4k

Version:

Upscale Videos using Anime4KCPP

69 lines (68 loc) 2.61 kB
"use strict"; const child_process_1 = require("child_process"); const path = require("path"); const os_1 = require("os"); const fs_1 = require("fs"); 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 Anime4KCPP { inputPath; outputPath; process; endBuffer; constructor(inp, out, options = {}) { if (typeof inp == "string") this.inputPath = path.resolve(inp); else { this.inputPath = path.resolve(os_1.tmpdir(), generate(50) + ".png"); fs_1.writeFileSync(this.inputPath, inp); } if (options.outputAsBuffer) { this.outputPath = path.resolve(os_1.tmpdir(), generate(50) + ".png"); } else { this.outputPath = path.resolve(out); } this.process = child_process_1.spawn(__dirname + "/../anime4k/Anime4KCPP_CLI/Anime4KCPP_CLI", [ "-i", this.inputPath, "-o", this.outputPath, "-v", ...(options.gpu ? ["-q"] : []), ...(options.scale ? ['-z', '' + options.scale] : []) ]); this.process.on('close', () => { try { if (options.outputAsBuffer) this.endBuffer = fs_1.readFileSync(this.outputPath); } catch (e) { } }); } static upscale(inp, out, options) { return new Anime4KCPP(inp, out, options); } static async listGPUs() { const process = child_process_1.spawn(__dirname + "/../anime4k/Anime4KCPP_CLI/Anime4KCPP_CLI", [ "-l" ]); let out = []; process.on('error', console.log); process.stdout.on('data', d => out.push(...d.toString().split("\r\n"))); process.stderr.on('data', d => out.push(...d.toString().split("\r\n"))); await new Promise(res => (process.on('close', res))); const GPUs = []; out = out.filter(e => e != ""); for (let std of out) { const str = std.split(" ").filter(e => e != ""); if (str.includes("Device") && +(str[1].replace(":", "")) >= 0) { GPUs[+(str[1].replace(":", ""))] = std.split(": ")[1]; } } return GPUs; } finishedPromise() { return new Promise(res => { process.on('exit', res); }); } };