@shibam/sticker-maker 1.2.12 → 1.2.13
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/package.json +1 -1
- package/utils/convert.js +16 -11
package/package.json
CHANGED
package/utils/convert.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { exec as execCallback } from "child_process";
|
|
2
|
-
import
|
|
2
|
+
import fs from "fs-extra";
|
|
3
3
|
import { tmpdir } from "os";
|
|
4
4
|
import { promisify } from "util";
|
|
5
5
|
import { fileTypeFromBuffer } from "file-type";
|
|
6
6
|
import sharp from "sharp";
|
|
7
|
-
|
|
8
7
|
const exec = promisify(execCallback);
|
|
9
8
|
|
|
10
9
|
export default class MediaConverter {
|
|
@@ -37,25 +36,31 @@ export default class MediaConverter {
|
|
|
37
36
|
const filename = `${tmpdir()}/${Math.random().toString(36).substring(2)}`;
|
|
38
37
|
const videoPath = `${filename}.mp4`;
|
|
39
38
|
const gifPath = `${filename}.gif`;
|
|
40
|
-
|
|
39
|
+
|
|
41
40
|
try {
|
|
42
41
|
// Write the input video buffer to a temporary file
|
|
43
|
-
await writeFile(videoPath, data);
|
|
44
|
-
|
|
42
|
+
await fs.writeFile(videoPath, data);
|
|
43
|
+
|
|
45
44
|
// Execute FFmpeg command to convert video to GIF with lossless option
|
|
46
45
|
const ffmpegCmd = `ffmpeg -i "${videoPath}" -vf "fps=15,scale=320:-1:flags=lanczos" -loop 0 -lossless 0 "${gifPath}"`;
|
|
47
46
|
await exec(ffmpegCmd);
|
|
48
|
-
|
|
47
|
+
|
|
49
48
|
// Read the resulting GIF as a buffer
|
|
50
|
-
const gifBuffer = await readFile(gifPath);
|
|
51
|
-
|
|
49
|
+
const gifBuffer = await fs.readFile(gifPath);
|
|
50
|
+
|
|
52
51
|
// Clean up temporary files
|
|
53
|
-
await Promise.all([
|
|
54
|
-
|
|
52
|
+
await Promise.all([
|
|
53
|
+
fs.unlink(videoPath).catch(() => {}),
|
|
54
|
+
fs.unlink(gifPath).catch(() => {}),
|
|
55
|
+
]);
|
|
56
|
+
|
|
55
57
|
return gifBuffer;
|
|
56
58
|
} catch (error) {
|
|
57
59
|
// Clean up files in case of an error
|
|
58
|
-
await Promise.all([
|
|
60
|
+
await Promise.all([
|
|
61
|
+
fs.unlink(videoPath).catch(() => {}),
|
|
62
|
+
fs.unlink(gifPath).catch(() => {}),
|
|
63
|
+
]);
|
|
59
64
|
throw new Error(`Failed to convert video to GIF: ${error.message}`);
|
|
60
65
|
}
|
|
61
66
|
};
|