@shibam/sticker-maker 1.2.5 → 1.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +4 -0
- package/package.json +3 -2
- package/test/gif-image.gif +0 -0
- package/test/index.js +11 -0
- package/test/lol.mp4 +0 -0
- package/utils/convert.js +20 -34
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shibam/sticker-maker",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"cwebp": "^3.1.0",
|
|
15
15
|
"file-type": "^20.0.0",
|
|
16
16
|
"jimp": "^1.6.0",
|
|
17
|
-
"node-webpmux": "^3.2.0"
|
|
17
|
+
"node-webpmux": "^3.2.0",
|
|
18
|
+
"webp-converter": "^2.3.3"
|
|
18
19
|
}
|
|
19
20
|
}
|
|
Binary file
|
package/test/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Sticker from '../index.js';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
const sticker = new Sticker('./test/lol.mp4',{
|
|
4
|
+
pack: 'My Pack',
|
|
5
|
+
author: 'Me',
|
|
6
|
+
category: ['😹'],
|
|
7
|
+
quality: 80,
|
|
8
|
+
text: 'Hello World'
|
|
9
|
+
});
|
|
10
|
+
sticker.metadata().then(console.log);
|
|
11
|
+
fs.writeFileSync('sticker.webp', await sticker.build());
|
package/test/lol.mp4
ADDED
|
Binary file
|
package/utils/convert.js
CHANGED
|
@@ -4,7 +4,7 @@ import { promises as fs } from "fs";
|
|
|
4
4
|
import { tmpdir } from "os";
|
|
5
5
|
import { join } from "path";
|
|
6
6
|
import { fileTypeFromBuffer } from "file-type";
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
|
|
9
9
|
const exec = promisify(execCallback);
|
|
10
10
|
|
|
@@ -31,7 +31,7 @@ export default class MediaConverter {
|
|
|
31
31
|
return await this.imageToWebp(buffer);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
throw new Error(`Unsupported file type: ${mime}`);
|
|
34
|
+
throw new Error(`Unsupported file type: ${this.mime}`);
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
async videoToGif(buffer) {
|
|
@@ -46,17 +46,11 @@ export default class MediaConverter {
|
|
|
46
46
|
|
|
47
47
|
const gifBuffer = await fs.readFile(tempOutput);
|
|
48
48
|
|
|
49
|
-
await Promise.all([
|
|
50
|
-
fs.unlink(tempInput).catch(() => {}),
|
|
51
|
-
fs.unlink(tempOutput).catch(() => {}),
|
|
52
|
-
]);
|
|
49
|
+
await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
|
|
53
50
|
|
|
54
51
|
return gifBuffer;
|
|
55
52
|
} catch (error) {
|
|
56
|
-
await Promise.all([
|
|
57
|
-
fs.unlink(tempInput).catch(() => {}),
|
|
58
|
-
fs.unlink(tempOutput).catch(() => {}),
|
|
59
|
-
]);
|
|
53
|
+
await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
|
|
60
54
|
throw new Error(`Failed to convert video to gif: ${error.message}`);
|
|
61
55
|
}
|
|
62
56
|
}
|
|
@@ -68,22 +62,17 @@ export default class MediaConverter {
|
|
|
68
62
|
try {
|
|
69
63
|
await fs.writeFile(tempInput, buffer);
|
|
70
64
|
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
// Use gif2webp to convert GIF to WebP
|
|
66
|
+
const gif2webpCmd = `gif2webp "${tempInput}" -q ${this.quality} -o "${tempOutput}"`;
|
|
67
|
+
await exec(gif2webpCmd);
|
|
73
68
|
|
|
74
69
|
const webpBuffer = await fs.readFile(tempOutput);
|
|
75
70
|
|
|
76
|
-
await Promise.all([
|
|
77
|
-
fs.unlink(tempInput).catch(() => {}),
|
|
78
|
-
fs.unlink(tempOutput).catch(() => {}),
|
|
79
|
-
]);
|
|
71
|
+
await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
|
|
80
72
|
|
|
81
73
|
return webpBuffer;
|
|
82
74
|
} catch (error) {
|
|
83
|
-
await Promise.all([
|
|
84
|
-
fs.unlink(tempInput).catch(() => {}),
|
|
85
|
-
fs.unlink(tempOutput).catch(() => {}),
|
|
86
|
-
]);
|
|
75
|
+
await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
|
|
87
76
|
throw new Error(`Failed to convert gif to webp: ${error.message}`);
|
|
88
77
|
}
|
|
89
78
|
}
|
|
@@ -93,26 +82,23 @@ export default class MediaConverter {
|
|
|
93
82
|
const tempOutput = join(tmpdir(), `output_${Date.now()}.webp`);
|
|
94
83
|
|
|
95
84
|
try {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
await image.writeAsync(tempInput);
|
|
85
|
+
// Write the input buffer to a temporary file
|
|
86
|
+
await fs.writeFile(tempInput, buffer);
|
|
99
87
|
|
|
100
|
-
|
|
101
|
-
|
|
88
|
+
// Use cwebp to convert the image to WebP
|
|
89
|
+
const cwebpCmd = `cwebp "${tempInput}" -q ${this.quality} -o "${tempOutput}"`;
|
|
90
|
+
await exec(cwebpCmd);
|
|
102
91
|
|
|
92
|
+
// Read the resulting WebP file as a buffer
|
|
103
93
|
const webpBuffer = await fs.readFile(tempOutput);
|
|
104
94
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
fs.unlink(tempOutput).catch(() => {}),
|
|
108
|
-
]);
|
|
95
|
+
// Clean up temporary files
|
|
96
|
+
await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
|
|
109
97
|
|
|
110
|
-
return webpBuffer;
|
|
98
|
+
return webpBuffer; // Return the WebP buffer as output
|
|
111
99
|
} catch (error) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
fs.unlink(tempOutput).catch(() => {}),
|
|
115
|
-
]);
|
|
100
|
+
// Clean up temporary files in case of error
|
|
101
|
+
await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
|
|
116
102
|
throw new Error(`Failed to convert image to webp: ${error.message}`);
|
|
117
103
|
}
|
|
118
104
|
}
|