@shibam/sticker-maker 1.1.9 → 1.1.11
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/index.js +1 -1
- package/dist/lib/toGif.js +53 -35
- package/package.json +1 -1
package/dist/index.js
CHANGED
package/dist/lib/toGif.js
CHANGED
|
@@ -3,44 +3,62 @@ import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
|
|
|
3
3
|
import { PassThrough } from 'stream';
|
|
4
4
|
import { StickerTypes } from '../types/StickerTypes.js';
|
|
5
5
|
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
|
|
6
|
-
const videoToGif = (buffer, extType, type) => {
|
|
6
|
+
const videoToGif = (buffer, extType, type, retries = 3) => {
|
|
7
7
|
return new Promise(async (resolve, reject) => {
|
|
8
|
-
|
|
8
|
+
const execute = async (attempt) => {
|
|
9
9
|
const outputStream = new PassThrough({ allowHalfOpen: false });
|
|
10
10
|
const inputStream = new PassThrough({ allowHalfOpen: false });
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
11
|
+
try {
|
|
12
|
+
const chunks = [];
|
|
13
|
+
inputStream.write(buffer);
|
|
14
|
+
inputStream.end();
|
|
15
|
+
outputStream.on('data', (chunk) => {
|
|
16
|
+
chunks.push(chunk);
|
|
17
|
+
});
|
|
18
|
+
outputStream.on('end', () => {
|
|
19
|
+
resolve(Buffer.concat(chunks));
|
|
20
|
+
inputStream.destroy();
|
|
21
|
+
outputStream.destroy();
|
|
22
|
+
});
|
|
23
|
+
const handleError = (err) => {
|
|
24
|
+
inputStream.destroy();
|
|
25
|
+
outputStream.destroy();
|
|
26
|
+
if (attempt < retries) {
|
|
27
|
+
execute(++attempt);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
reject(err);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
inputStream.on('error', handleError);
|
|
34
|
+
outputStream.on('error', handleError);
|
|
35
|
+
const shape = type === StickerTypes.SQUARE
|
|
36
|
+
? 'scale=320:-1:flags=lanczos,fps=10,crop=min(iw\\,ih):min(iw\\,ih)'
|
|
37
|
+
: 'scale=320:-1:flags=lanczos,fps=20';
|
|
38
|
+
ffmpeg(inputStream)
|
|
39
|
+
.inputFormat(extType)
|
|
40
|
+
.outputOptions(['-vf', shape, '-loop', '0', '-lossless', '0', '-t', '7', '-preset', 'ultrafast'])
|
|
41
|
+
.toFormat('gif')
|
|
42
|
+
.pipe(outputStream)
|
|
43
|
+
.on('error', async (err) => {
|
|
44
|
+
if (attempt < retries) {
|
|
45
|
+
execute(++attempt);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
reject(err);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
if (attempt < retries) {
|
|
54
|
+
execute(++attempt);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
reject(error);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
execute(1);
|
|
44
62
|
});
|
|
45
63
|
};
|
|
46
64
|
export default videoToGif;
|