@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.
Files changed (2) hide show
  1. package/dist/lib/toGif.js +36 -38
  2. 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 { PassThrough } from 'stream';
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
- try {
9
- const outputStream = new PassThrough({ allowHalfOpen: false });
10
- const inputStream = new PassThrough({ allowHalfOpen: false });
11
- inputStream.write(buffer);
12
- inputStream.end();
13
- const chunks = [];
14
- outputStream.on('data', (chunk) => {
15
- chunks.push(chunk);
16
- });
17
- outputStream.on('end', () => {
18
- resolve(Buffer.concat(chunks));
19
- inputStream.destroy();
20
- outputStream.destroy();
21
- });
22
- outputStream.on('error', (err) => {
23
- reject(err);
24
- inputStream.destroy();
25
- outputStream.destroy();
26
- });
27
- const shape = type === StickerTypes.SQUARE
28
- ? 'scale=320:-1:flags=lanczos,fps=10,crop=min(iw\\,ih):min(iw\\,ih)'
29
- : 'scale=320:-1:flags=lanczos,fps=10';
30
- ffmpeg(inputStream)
31
- .inputFormat(extType)
32
- .outputOptions(['-vf', shape, '-loop', '0', '-lossless', '0', '-t', '7', '-preset', 'ultrafast'])
33
- .toFormat('gif')
34
- .pipe(outputStream)
35
- .on('error', (err) => {
36
- inputStream.destroy();
37
- outputStream.destroy();
38
- reject(err);
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shibam/sticker-maker",
3
- "version": "1.1.10",
3
+ "version": "1.1.12",
4
4
  "description": "A package for creating stickers",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",