@ryuu-reinzz/haruka-lib 2.2.6 → 2.3.0

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.
@@ -1,11 +1,32 @@
1
1
  import fs from 'fs-extra';
2
- import ffmpeg from 'fluent-ffmpeg';
2
+ import ffmpeg from "fluent-ffmpeg";
3
+ import ffmpegPath from "ffmpeg-static";
4
+ import ffprobePath from "ffprobe-static";
3
5
  import { stickerVid } from './video-to-webp.js';
4
6
  import { stickerImg } from './image-to-webp.js';
5
7
  import path from 'path';
6
8
  import { tmpdir } from 'os';
7
9
  import Crypto from 'crypto';
8
10
 
11
+ ffmpeg.setFfmpegPath(ffmpegPath);
12
+ ffmpeg.setFfprobePath(ffprobePath.path);
13
+ function isVideo(input) {
14
+ return new Promise((resolve) => {
15
+ ffmpeg.ffprobe(input, (err, metadata) => {
16
+ if (err) {
17
+ console.log("ffprobe error:", err);
18
+ return resolve(false);
19
+ }
20
+
21
+ const isVideo = metadata.streams.some(
22
+ (s) => s.codec_type === "video"
23
+ );
24
+
25
+ resolve(isVideo);
26
+ });
27
+ });
28
+ }
29
+
9
30
  async function bufferToTmp(buffer, ext = '.bin') {
10
31
  const tmp = path.join(
11
32
  tmpdir(),
@@ -24,39 +45,60 @@ class Sticker {
24
45
  }
25
46
 
26
47
  async toStickerImg() {
27
- const out = await stickerImg(this.media, {
28
- packname: this.pack,
29
- author: this.author,
30
- categories: this.categories
31
- })
32
- return fs.readFile(out)
48
+ let buff =
49
+ Buffer.isBuffer(this.media)
50
+ ? this.media
51
+ : /^data:.*?\/.*?;base64,/i.test(this.media)
52
+ ? Buffer.from(this.media.split(',')[1], 'base64')
53
+ : /^https?:\/\//.test(this.media)
54
+ ? Buffer.from(await (await fetch(this.media)).arrayBuffer())
55
+ : fs.existsSync(this.media)
56
+ ? await fs.readFile(this.media)
57
+ : Buffer.alloc(0)
58
+
59
+ let buffer = await stickerImg(buff, {
60
+ packname: this.pack,
61
+ author: this.author
62
+ });
63
+ return await fs.readFile(buffer)
33
64
  }
34
65
 
35
66
  async toStickerVid() {
36
- const out = await stickerVid(this.media, {
37
- packname: this.pack,
38
- author: this.author,
39
- categories: this.categories
40
- })
41
- return fs.readFile(out)
67
+ let buff =
68
+ Buffer.isBuffer(this.media)
69
+ ? this.media
70
+ : /^data:.*?\/.*?;base64,/i.test(this.media)
71
+ ? Buffer.from(this.media.split(',')[1], 'base64')
72
+ : /^https?:\/\//.test(this.media)
73
+ ? Buffer.from(await (await fetch(this.media)).arrayBuffer())
74
+ : fs.existsSync(this.media)
75
+ ? await fs.readFile(this.media)
76
+ : Buffer.alloc(0)
77
+
78
+ let buffer = await stickerVid(buff, {
79
+ packname: this.pack,
80
+ author: this.author
81
+ });
82
+
83
+ return await fs.readFile(buffer)
42
84
  }
43
85
 
44
86
  async build() {
45
- const tmpPath = await bufferToTmp(this.media)
46
-
47
- const isVideo = await new Promise(resolve => {
48
- ffmpeg.ffprobe(tmpPath, (err, meta) => {
49
- if (err) return resolve(false)
50
- resolve(meta.streams.some(
51
- s => s.codec_type === 'video' && s.duration > 0
52
- ))
53
- })
54
- })
55
-
56
- return isVideo
87
+ const buff =
88
+ Buffer.isBuffer(this.media)
89
+ ? this.media
90
+ : fs.existsSync(this.media)
91
+ ? await fs.readFile(this.media)
92
+ : Buffer.alloc(0);
93
+
94
+ const tmpPath = await bufferToTmp(buff);
95
+ const thisVideo = await isVideo(tmpPath);
96
+
97
+ return thisVideo
57
98
  ? await this.toStickerVid()
58
99
  : await this.toStickerImg()
100
+ }
59
101
  }
60
- }
61
102
 
62
- export default Sticker
103
+ export default Sticker
104
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ryuu-reinzz/haruka-lib",
3
- "version": "2.2.6",
3
+ "version": "2.3.0",
4
4
  "description": "Library extra for bot WhatsApp",
5
5
  "main": "main/index.js",
6
6
  "type": "module",
@@ -19,11 +19,12 @@
19
19
  "better-sqlite3": "^12.5.0",
20
20
  "file-type": "^16.5.3",
21
21
  "fluent-ffmpeg": "^2.1.3",
22
+ "ffmpeg-static": "^5.3.0",
23
+ "ffprobe-static": "^3.1.0",
22
24
  "fs-extra": "^11.2.0",
23
25
  "path": "^0.12.7",
24
- "node-fetch": "^3.3.2",
25
- "node-webpmux": "^3.2.1",
26
26
  "node-fetch": "^2.6.1",
27
+ "node-webpmux": "^3.2.1",
27
28
  "sharp": "^0.34.1"
28
29
  }
29
30
  }