@shibam/sticker-maker 1.1.11 → 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 +18 -38
  2. package/package.json +1 -1
package/dist/lib/toGif.js CHANGED
@@ -1,59 +1,39 @@
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
8
  const videoToGif = (buffer, extType, type, retries = 3) => {
7
9
  return new Promise(async (resolve, reject) => {
8
10
  const execute = async (attempt) => {
9
- const outputStream = new PassThrough({ allowHalfOpen: false });
10
- const inputStream = new PassThrough({ allowHalfOpen: false });
11
+ const filename = join(tmpdir(), `${Math.random().toString(36)}.${extType}`);
12
+ const outputFilename = join(tmpdir(), `${Math.random().toString(36)}.gif`);
11
13
  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);
14
+ await writeFile(filename, buffer);
35
15
  const shape = type === StickerTypes.SQUARE
36
16
  ? 'scale=320:-1:flags=lanczos,fps=10,crop=min(iw\\,ih):min(iw\\,ih)'
37
17
  : '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
- }
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);
50
26
  });
27
+ const gifBuffer = await readFile(outputFilename);
28
+ await Promise.all([unlink(filename), unlink(outputFilename)]);
29
+ resolve(gifBuffer);
51
30
  }
52
31
  catch (error) {
53
32
  if (attempt < retries) {
54
33
  execute(++attempt);
55
34
  }
56
35
  else {
36
+ await Promise.all([unlink(filename).catch(() => { }), unlink(outputFilename).catch(() => { })]);
57
37
  reject(error);
58
38
  }
59
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shibam/sticker-maker",
3
- "version": "1.1.11",
3
+ "version": "1.1.12",
4
4
  "description": "A package for creating stickers",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",