Spaces:
Running
Running
File size: 3,222 Bytes
7ec6802 6d2b8e8 7ec6802 6d2b8e8 7ec6802 6d2b8e8 7ec6802 6d2b8e8 7ec6802 6d2b8e8 7ec6802 |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
// https://github.com/salismazaya/whatsapp-bot
const fs = require("fs");
const FileType = require('file-type');
const { exec } = require("child_process");
if (!fs.existsSync("/tmp")) fs.mkdirSync("/tmp");
function imageToWebp(bufferImage) {
return new Promise((resolve, reject) => {
FileType.fromBuffer(bufferImage)
.then((response) => {
try {
const pathFile = "/tmp/" + Math.floor(Math.random() * 1000000 + 1) + "." + response.ext;
fs.writeFileSync(pathFile, bufferImage);
exec(`cwebp -q 50 ${pathFile} -o ${pathFile}.webp`, (error, stdout, stderr) => {
if (!fs.existsSync(pathFile + ".webp")) {
reject(new Error("failed convert file!"));
fs.unlinkSync(pathFile);
return;
}
const webpBufferImage = fs.readFileSync(pathFile + ".webp");
fs.unlinkSync(pathFile);
fs.unlinkSync(pathFile + ".webp");
resolve(webpBufferImage);
});
} catch(e) {
reject(e);
}
})
.catch(e => reject(e));
});
}
function webpToJpg(bufferImage) {
return new Promise((resolve, reject) => {
try {
const pathFile = "/tmp/" + Math.floor(Math.random() * 1000000 + 1) + ".webp";
fs.writeFileSync(pathFile, bufferImage);
exec(`dwebp ${pathFile} -o ${pathFile}.jpg`, (error, stdout, stderr) => {
if (!fs.existsSync(pathFile + ".jpg")) {
reject(new Error("failed convert file!"));
fs.unlinkSync(pathFile);
return;
}
const jpgBufferImage = fs.readFileSync(pathFile + ".jpg");
fs.unlinkSync(pathFile);
fs.unlinkSync(pathFile + ".jpg");
resolve(jpgBufferImage);
})
} catch(e) {
reject(e);
}
});
}
function gifToWebp(bufferImage) {
return new Promise((resolve, reject) => {
try {
const pathFile = "/tmp/" + Math.floor(Math.random() * 1000000 + 1) + ".gif";
fs.writeFileSync(pathFile, bufferImage);
exec(`gif2webp ${pathFile} -o ${pathFile}.webp`, (error, stdout, stderr) => {
if (!fs.existsSync(pathFile + ".webp")) {
reject(new Error("failed convert file!"));
fs.unlinkSync(pathFile);
return;
}
const webpBuffer = fs.readFileSync(pathFile + ".webp");
fs.unlinkSync(pathFile);
fs.unlinkSync(pathFile + ".webp");
resolve(webpBuffer);
})
} catch(e) {
reject(e);
}
});
}
function webpToVideo(bufferImage) {
return new Promise((resolve, reject) => {
try {
const pathFile = "/tmp/" + Math.floor(Math.random() * 1000000 + 1) + ".webp";
fs.writeFileSync(pathFile, bufferImage);
exec(`convert ${pathFile} ${pathFile}.gif`, (error, stdout, stderr) => {
exec(`ffmpeg -i ${pathFile}.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" ${pathFile}.mp4`, (error, stdout, stderr) => {
if (!fs.existsSync(pathFile + ".gif") || !fs.existsSync(pathFile + ".mp4")) {
reject(new Error("failed convert file!"));
fs.unlinkSync(pathFile);
return;
}
const videoBuffer = fs.readFileSync(pathFile + ".mp4");
fs.unlinkSync(pathFile);
fs.unlinkSync(pathFile + ".gif");
fs.unlinkSync(pathFile + ".mp4");
resolve(videoBuffer);
});
});
} catch(e) {
reject(e);
}
});
}
module.exports = { imageToWebp, webpToJpg, gifToWebp, webpToVideo } |