@shibam/sticker-maker 1.1.10 → 1.1.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/dist/lib/toGif.js +36 -38
- package/package.json +1 -1
package/dist/lib/toGif.js
CHANGED
|
@@ -1,46 +1,44 @@
|
|
|
1
1
|
import ffmpeg from 'fluent-ffmpeg';
|
|
2
2
|
import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
|
|
3
|
-
import {
|
|
3
|
+
import { tmpdir } from 'os';
|
|
4
|
+
import { writeFile, readFile, unlink } from 'fs/promises';
|
|
5
|
+
import { join } from 'path';
|
|
4
6
|
import { StickerTypes } from '../types/StickerTypes.js';
|
|
5
7
|
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
|
|
6
|
-
const videoToGif = (buffer, extType, type) => {
|
|
8
|
+
const videoToGif = (buffer, extType, type, retries = 3) => {
|
|
7
9
|
return new Promise(async (resolve, reject) => {
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
reject(error);
|
|
43
|
-
}
|
|
10
|
+
const execute = async (attempt) => {
|
|
11
|
+
const filename = join(tmpdir(), `${Math.random().toString(36)}.${extType}`);
|
|
12
|
+
const outputFilename = join(tmpdir(), `${Math.random().toString(36)}.gif`);
|
|
13
|
+
try {
|
|
14
|
+
await writeFile(filename, buffer);
|
|
15
|
+
const shape = type === StickerTypes.SQUARE
|
|
16
|
+
? 'scale=320:-1:flags=lanczos,fps=10,crop=min(iw\\,ih):min(iw\\,ih)'
|
|
17
|
+
: 'scale=320:-1:flags=lanczos,fps=20';
|
|
18
|
+
await new Promise((resolveFfmpeg, rejectFfmpeg) => {
|
|
19
|
+
ffmpeg(filename)
|
|
20
|
+
.inputFormat(extType)
|
|
21
|
+
.outputOptions(['-vf', shape, '-loop', '0', '-lossless', '0', '-t', '7', '-preset', 'ultrafast'])
|
|
22
|
+
.toFormat('gif')
|
|
23
|
+
.save(outputFilename)
|
|
24
|
+
.on('end', resolveFfmpeg)
|
|
25
|
+
.on('error', rejectFfmpeg);
|
|
26
|
+
});
|
|
27
|
+
const gifBuffer = await readFile(outputFilename);
|
|
28
|
+
await Promise.all([unlink(filename), unlink(outputFilename)]);
|
|
29
|
+
resolve(gifBuffer);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (attempt < retries) {
|
|
33
|
+
execute(++attempt);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
await Promise.all([unlink(filename).catch(() => { }), unlink(outputFilename).catch(() => { })]);
|
|
37
|
+
reject(error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
execute(1);
|
|
44
42
|
});
|
|
45
43
|
};
|
|
46
44
|
export default videoToGif;
|