@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 CHANGED
@@ -116,6 +116,10 @@ export default class Sticker {
116
116
  return this.buffer;
117
117
  }
118
118
 
119
+ async toBuffer() {
120
+ return await this.build();
121
+ }
122
+
119
123
  async metadata() {
120
124
  if (!this.buffer) {
121
125
  await this.build();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shibam/sticker-maker",
3
- "version": "1.2.5",
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
- import { Jimp } from "jimp";
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
- const ffmpegCmd = `ffmpeg -i "${tempInput}" -vf "scale=${this.width}:-1" -c:v libwebp -quality ${this.quality} -loop 0 -lossless ${this.mime.startsWith("image/")?'1':'0'} -preset picture -an -vsync 0 "${tempOutput}"`;
72
- await exec(ffmpegCmd);
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
- const image = await Jimp.read(buffer);
97
- image.resize(this.width, Jimp.AUTO);
98
- await image.writeAsync(tempInput);
85
+ // Write the input buffer to a temporary file
86
+ await fs.writeFile(tempInput, buffer);
99
87
 
100
- const ffmpegCmd = `ffmpeg -i "${tempInput}" -quality ${this.quality} "${tempOutput}"`;
101
- await exec(ffmpegCmd);
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
- await Promise.all([
106
- fs.unlink(tempInput).catch(() => {}),
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
- await Promise.all([
113
- fs.unlink(tempInput).catch(() => {}),
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
  }