Khrisna commited on
Commit
7ec6802
·
1 Parent(s): 30e276c

Create convertFormat.js

Browse files
Files changed (1) hide show
  1. lib/convertFormat.js +111 -0
lib/convertFormat.js ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // https://github.com/salismazaya/whatsapp-bot
2
+
3
+ const fs = require("fs");
4
+ const FileType = require('file-type');
5
+ const { exec } = require("child_process");
6
+
7
+ if (!fs.existsSync(".temp")) fs.mkdirSync(".temp");
8
+
9
+ function imageToWebp(bufferImage) {
10
+ return new Promise((resolve, reject) => {
11
+ FileType.fromBuffer(bufferImage)
12
+ .then((response) => {
13
+ try {
14
+ const pathFile = ".temp/" + Math.floor(Math.random() * 1000000 + 1) + "." + response.ext;
15
+ fs.writeFileSync(pathFile, bufferImage);
16
+ exec(`cwebp -q 50 ${pathFile} -o ${pathFile}.webp`, (error, stdout, stderr) => {
17
+ if (!fs.existsSync(pathFile + ".webp")) {
18
+ reject(new Error("failed convert file!"));
19
+ fs.unlinkSync(pathFile);
20
+ return;
21
+ }
22
+ const webpBufferImage = fs.readFileSync(pathFile + ".webp");
23
+ fs.unlinkSync(pathFile);
24
+ fs.unlinkSync(pathFile + ".webp");
25
+ resolve(webpBufferImage);
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 = ".temp/" + 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 = ".temp/" + 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 = ".temp/" + 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 }