@shibam/sticker-maker 1.2.11 → 1.2.12
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 +2 -2
- package/package.json +3 -2
- package/utils/convert.js +23 -25
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Exif from "./utils/changeMetaInfo.js";
|
|
2
2
|
import MediaConverter from "./utils/convert.js";
|
|
3
3
|
import extractMetadata from "./utils/extractMetaInfo.js";
|
|
4
|
-
import
|
|
4
|
+
import fs from "fs-extra";
|
|
5
5
|
import { Readable } from "stream";
|
|
6
6
|
|
|
7
7
|
export default class Sticker {
|
|
@@ -36,7 +36,7 @@ export default class Sticker {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
try {
|
|
39
|
-
return await fs.
|
|
39
|
+
return await fs.readFile(image);
|
|
40
40
|
} catch (error) {
|
|
41
41
|
throw new Error("Failed to read image file: " + error.message);
|
|
42
42
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shibam/sticker-maker",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.12",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"description": "",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"file-type": "^20.0.0",
|
|
15
|
-
"
|
|
15
|
+
"fs-extra": "^11.3.0",
|
|
16
|
+
"node-webpmux": "3.1.0",
|
|
16
17
|
"sharp": "0.30.0"
|
|
17
18
|
}
|
|
18
19
|
}
|
package/utils/convert.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { exec as execCallback } from "child_process";
|
|
2
|
-
import {
|
|
3
|
-
import { promises as fs } from "fs";
|
|
2
|
+
import { writeFile, readFile, unlink } from "fs-extra";
|
|
4
3
|
import { tmpdir } from "os";
|
|
5
|
-
import {
|
|
4
|
+
import { promisify } from "util";
|
|
6
5
|
import { fileTypeFromBuffer } from "file-type";
|
|
7
6
|
import sharp from "sharp";
|
|
8
7
|
|
|
@@ -34,33 +33,32 @@ export default class MediaConverter {
|
|
|
34
33
|
throw new Error(`Unsupported file type: ${this.mime}`);
|
|
35
34
|
}
|
|
36
35
|
|
|
37
|
-
async
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
|
|
36
|
+
videoToGif = async (data) => {
|
|
37
|
+
const filename = `${tmpdir()}/${Math.random().toString(36).substring(2)}`;
|
|
38
|
+
const videoPath = `${filename}.mp4`;
|
|
39
|
+
const gifPath = `${filename}.gif`;
|
|
40
|
+
|
|
41
41
|
try {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
// Write the input video buffer to a temporary file
|
|
43
|
+
await writeFile(videoPath, data);
|
|
44
|
+
|
|
45
|
+
// Execute FFmpeg command to convert video to GIF with lossless option
|
|
46
|
+
const ffmpegCmd = `ffmpeg -i "${videoPath}" -vf "fps=15,scale=320:-1:flags=lanczos" -loop 0 -lossless 0 "${gifPath}"`;
|
|
45
47
|
await exec(ffmpegCmd);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
|
|
49
|
+
// Read the resulting GIF as a buffer
|
|
50
|
+
const gifBuffer = await readFile(gifPath);
|
|
51
|
+
|
|
52
|
+
// Clean up temporary files
|
|
53
|
+
await Promise.all([unlink(videoPath).catch(() => {}), unlink(gifPath).catch(() => {})]);
|
|
54
|
+
|
|
54
55
|
return gifBuffer;
|
|
55
56
|
} catch (error) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
]);
|
|
60
|
-
throw new Error(`Failed to convert video to gif: ${error.message}`);
|
|
57
|
+
// Clean up files in case of an error
|
|
58
|
+
await Promise.all([unlink(videoPath).catch(() => {}), unlink(gifPath).catch(() => {})]);
|
|
59
|
+
throw new Error(`Failed to convert video to GIF: ${error.message}`);
|
|
61
60
|
}
|
|
62
|
-
}
|
|
63
|
-
|
|
61
|
+
};
|
|
64
62
|
async ToWebp(buffer) {
|
|
65
63
|
const fileType = await fileTypeFromBuffer(buffer);
|
|
66
64
|
if (!fileType) throw new Error("Invalid file type");
|