Spaces:
Paused
Paused
Update lib/convertFormat.js
Browse files- lib/convertFormat.js +21 -99
lib/convertFormat.js
CHANGED
|
@@ -1,111 +1,33 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
const fs = require("fs");
|
| 4 |
-
const FileType = require('file-type');
|
| 5 |
-
const { exec } = require("child_process");
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
function imageToWebp(bufferImage) {
|
| 10 |
return new Promise((resolve, reject) => {
|
| 11 |
FileType.fromBuffer(bufferImage)
|
| 12 |
.then((response) => {
|
| 13 |
try {
|
| 14 |
-
const
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
} catch(e) {
|
| 29 |
reject(e);
|
| 30 |
}
|
| 31 |
})
|
| 32 |
.catch(e => reject(e));
|
| 33 |
});
|
| 34 |
-
}
|
| 35 |
-
|
| 36 |
-
function webpToJpg(bufferImage) {
|
| 37 |
-
return new Promise((resolve, reject) => {
|
| 38 |
-
try {
|
| 39 |
-
const pathFile = "/tmp/" + Math.floor(Math.random() * 1000000 + 1) + ".webp";
|
| 40 |
-
fs.writeFileSync(pathFile, bufferImage);
|
| 41 |
-
|
| 42 |
-
exec(`dwebp ${pathFile} -o ${pathFile}.jpg`, (error, stdout, stderr) => {
|
| 43 |
-
if (!fs.existsSync(pathFile + ".jpg")) {
|
| 44 |
-
reject(new Error("failed convert file!"));
|
| 45 |
-
fs.unlinkSync(pathFile);
|
| 46 |
-
return;
|
| 47 |
-
}
|
| 48 |
-
const jpgBufferImage = fs.readFileSync(pathFile + ".jpg");
|
| 49 |
-
fs.unlinkSync(pathFile);
|
| 50 |
-
fs.unlinkSync(pathFile + ".jpg");
|
| 51 |
-
resolve(jpgBufferImage);
|
| 52 |
-
})
|
| 53 |
-
} catch(e) {
|
| 54 |
-
reject(e);
|
| 55 |
-
}
|
| 56 |
-
});
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
function gifToWebp(bufferImage) {
|
| 60 |
-
return new Promise((resolve, reject) => {
|
| 61 |
-
try {
|
| 62 |
-
const pathFile = "/tmp/" + Math.floor(Math.random() * 1000000 + 1) + ".gif";
|
| 63 |
-
fs.writeFileSync(pathFile, bufferImage);
|
| 64 |
-
|
| 65 |
-
exec(`gif2webp ${pathFile} -o ${pathFile}.webp`, (error, stdout, stderr) => {
|
| 66 |
-
if (!fs.existsSync(pathFile + ".webp")) {
|
| 67 |
-
reject(new Error("failed convert file!"));
|
| 68 |
-
fs.unlinkSync(pathFile);
|
| 69 |
-
return;
|
| 70 |
-
}
|
| 71 |
-
const webpBuffer = fs.readFileSync(pathFile + ".webp");
|
| 72 |
-
fs.unlinkSync(pathFile);
|
| 73 |
-
fs.unlinkSync(pathFile + ".webp");
|
| 74 |
-
resolve(webpBuffer);
|
| 75 |
-
})
|
| 76 |
-
} catch(e) {
|
| 77 |
-
reject(e);
|
| 78 |
-
}
|
| 79 |
-
});
|
| 80 |
-
}
|
| 81 |
-
|
| 82 |
-
function webpToVideo(bufferImage) {
|
| 83 |
-
return new Promise((resolve, reject) => {
|
| 84 |
-
try {
|
| 85 |
-
const pathFile = "/tmp/" + Math.floor(Math.random() * 1000000 + 1) + ".webp";
|
| 86 |
-
fs.writeFileSync(pathFile, bufferImage);
|
| 87 |
-
|
| 88 |
-
exec(`convert ${pathFile} ${pathFile}.gif`, (error, stdout, stderr) => {
|
| 89 |
-
|
| 90 |
-
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) => {
|
| 91 |
-
if (!fs.existsSync(pathFile + ".gif") || !fs.existsSync(pathFile + ".mp4")) {
|
| 92 |
-
reject(new Error("failed convert file!"));
|
| 93 |
-
fs.unlinkSync(pathFile);
|
| 94 |
-
return;
|
| 95 |
-
}
|
| 96 |
-
const videoBuffer = fs.readFileSync(pathFile + ".mp4");
|
| 97 |
-
fs.unlinkSync(pathFile);
|
| 98 |
-
fs.unlinkSync(pathFile + ".gif");
|
| 99 |
-
fs.unlinkSync(pathFile + ".mp4");
|
| 100 |
-
resolve(videoBuffer);
|
| 101 |
-
});
|
| 102 |
-
|
| 103 |
-
});
|
| 104 |
-
} catch(e) {
|
| 105 |
-
reject(e);
|
| 106 |
-
}
|
| 107 |
-
});
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
module.exports = { imageToWebp, webpToJpg, gifToWebp, webpToVideo }
|
|
|
|
| 1 |
+
const ffmpeg = require('fluent-ffmpeg');
|
| 2 |
+
const FileType = require("file-type");
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
function allToJpg(bufferImage) {
|
|
|
|
|
|
|
| 6 |
return new Promise((resolve, reject) => {
|
| 7 |
FileType.fromBuffer(bufferImage)
|
| 8 |
.then((response) => {
|
| 9 |
try {
|
| 10 |
+
const buffers = [];
|
| 11 |
+
ffmpeg()
|
| 12 |
+
.input(webpBuffer)
|
| 13 |
+
.inputFormat(response.ext)
|
| 14 |
+
.outputFormat('jpeg')
|
| 15 |
+
.on('data', (chunk) => {
|
| 16 |
+
buffers.push(chunk);
|
| 17 |
+
})
|
| 18 |
+
.on('end', () => {
|
| 19 |
+
const resultBuffer = Buffer.concat(buffers);
|
| 20 |
+
resolve(resultBuffer);
|
| 21 |
+
})
|
| 22 |
+
.on('error', (err) => {
|
| 23 |
+
console.log(err);
|
| 24 |
+
})
|
| 25 |
+
.run();
|
| 26 |
} catch(e) {
|
| 27 |
reject(e);
|
| 28 |
}
|
| 29 |
})
|
| 30 |
.catch(e => reject(e));
|
| 31 |
});
|
| 32 |
+
}
|
| 33 |
+
module.exports = { allToJpg }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|