@hiroosy/baileys-lite 0.4.3 → 0.4.4
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/README.md +8 -17
- package/package.json +1 -1
- package/src/ffmpeg/LICENSE +502 -0
- package/src/ffmpeg/index.js +64 -0
- package/src/ffmpeg/lib/bits.js +150 -0
- package/src/ffmpeg/lib/bsf.js +242 -0
- package/src/ffmpeg/lib/codec/flac.js +801 -0
- package/src/ffmpeg/lib/codec/index.js +108 -0
- package/src/ffmpeg/lib/codec/mp3.js +768 -0
- package/src/ffmpeg/lib/codec/mpa.js +65 -0
- package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
- package/src/ffmpeg/lib/codec/opus.js +140 -0
- package/src/ffmpeg/lib/codec/pcm.js +198 -0
- package/src/ffmpeg/lib/compat.js +42 -0
- package/src/ffmpeg/lib/crc.js +60 -0
- package/src/ffmpeg/lib/demux/adts.js +151 -0
- package/src/ffmpeg/lib/demux/flac.js +150 -0
- package/src/ffmpeg/lib/demux/image.js +47 -0
- package/src/ffmpeg/lib/demux/index.js +60 -0
- package/src/ffmpeg/lib/demux/matroska.js +525 -0
- package/src/ffmpeg/lib/demux/merge.js +106 -0
- package/src/ffmpeg/lib/demux/mp3.js +199 -0
- package/src/ffmpeg/lib/demux/mp4.js +622 -0
- package/src/ffmpeg/lib/demux/ogg.js +250 -0
- package/src/ffmpeg/lib/demux/ts.js +354 -0
- package/src/ffmpeg/lib/demux/wav.js +156 -0
- package/src/ffmpeg/lib/errors.js +27 -0
- package/src/ffmpeg/lib/filter/gain.js +69 -0
- package/src/ffmpeg/lib/filter/mix.js +95 -0
- package/src/ffmpeg/lib/filter/resample.js +187 -0
- package/src/ffmpeg/lib/filter/video.js +575 -0
- package/src/ffmpeg/lib/image/bmp.js +123 -0
- package/src/ffmpeg/lib/image/frame.js +216 -0
- package/src/ffmpeg/lib/image/gif.js +215 -0
- package/src/ffmpeg/lib/image/index.js +70 -0
- package/src/ffmpeg/lib/image/jpeg.js +964 -0
- package/src/ffmpeg/lib/image/png.js +256 -0
- package/src/ffmpeg/lib/image/run.js +67 -0
- package/src/ffmpeg/lib/io.js +298 -0
- package/src/ffmpeg/lib/mux/adts.js +75 -0
- package/src/ffmpeg/lib/mux/flac.js +79 -0
- package/src/ffmpeg/lib/mux/mp3.js +85 -0
- package/src/ffmpeg/lib/mux/mp4.js +450 -0
- package/src/ffmpeg/lib/mux/ogg.js +146 -0
- package/src/ffmpeg/lib/mux/wav.js +94 -0
- package/src/ffmpeg/lib/net.js +270 -0
- package/src/ffmpeg/lib/pipeline.js +579 -0
- package/src/ffmpeg/lib/probe.js +45 -0
- package/src/ffmpeg/lib/tags.js +307 -0
- package/src/ffmpeg/lib/util.js +104 -0
- package/src/foundation/image-thumb.js +4 -56
- package/src/socket/messages-recv.js +5 -6
- package/src/utils/media.js +8 -85
- package/src/utils/message-compose.js +19 -11
- package/src/voip/index.js +16 -50
- package/src/voip/media/WaAudioEngine.js +8 -216
- package/src/voip/media/WaVideoEngine.js +8 -237
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
const vorbisMap = {
|
|
2
|
+
title: 'TITLE',
|
|
3
|
+
artist: 'ARTIST',
|
|
4
|
+
album: 'ALBUM',
|
|
5
|
+
album_artist: 'ALBUMARTIST',
|
|
6
|
+
date: 'DATE',
|
|
7
|
+
genre: 'GENRE',
|
|
8
|
+
comment: 'COMMENT',
|
|
9
|
+
composer: 'COMPOSER',
|
|
10
|
+
track: 'TRACKNUMBER',
|
|
11
|
+
disc: 'DISCNUMBER',
|
|
12
|
+
copyright: 'COPYRIGHT',
|
|
13
|
+
encoder: 'ENCODER',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const riffMap = {
|
|
18
|
+
title: 'INAM',
|
|
19
|
+
artist: 'IART',
|
|
20
|
+
album: 'IPRD',
|
|
21
|
+
date: 'ICRD',
|
|
22
|
+
genre: 'IGNR',
|
|
23
|
+
comment: 'ICMT',
|
|
24
|
+
copyright: 'ICOP',
|
|
25
|
+
encoder: 'ISFT',
|
|
26
|
+
track: 'ITRK',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const id3Map = {
|
|
30
|
+
title: 'TIT2',
|
|
31
|
+
artist: 'TPE1',
|
|
32
|
+
album: 'TALB',
|
|
33
|
+
album_artist: 'TPE2',
|
|
34
|
+
date: 'TDRC',
|
|
35
|
+
genre: 'TCON',
|
|
36
|
+
composer: 'TCOM',
|
|
37
|
+
track: 'TRCK',
|
|
38
|
+
disc: 'TPOS',
|
|
39
|
+
copyright: 'TCOP',
|
|
40
|
+
encoder: 'TSSE',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const mp4Map = {
|
|
44
|
+
title: '\xa9nam',
|
|
45
|
+
artist: '\xa9ART',
|
|
46
|
+
album: '\xa9alb',
|
|
47
|
+
album_artist: 'aART',
|
|
48
|
+
date: '\xa9day',
|
|
49
|
+
genre: '\xa9gen',
|
|
50
|
+
comment: '\xa9cmt',
|
|
51
|
+
composer: '\xa9wrt',
|
|
52
|
+
encoder: '\xa9too',
|
|
53
|
+
copyright: 'cprt',
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
function invert(map) {
|
|
57
|
+
const out = {};
|
|
58
|
+
for (const k of Object.keys(map)) out[map[k]] = k;
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function clean(tags) {
|
|
63
|
+
const out = {};
|
|
64
|
+
if (!tags) return out;
|
|
65
|
+
for (const key of Object.keys(tags)) {
|
|
66
|
+
const value = tags[key];
|
|
67
|
+
if (value === undefined || value === null || value === '') continue;
|
|
68
|
+
out[key.toLowerCase()] = String(value);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseVorbisComment(buf, start = 0) {
|
|
74
|
+
const tags = {};
|
|
75
|
+
let p = start;
|
|
76
|
+
const vendorLen = buf.readUInt32LE(p);
|
|
77
|
+
p += 4 + vendorLen;
|
|
78
|
+
const count = buf.readUInt32LE(p);
|
|
79
|
+
p += 4;
|
|
80
|
+
for (let i = 0; i < count && p + 4 <= buf.length; i++) {
|
|
81
|
+
const len = buf.readUInt32LE(p);
|
|
82
|
+
p += 4;
|
|
83
|
+
if (p + len > buf.length) break;
|
|
84
|
+
const text = buf.toString('utf8', p, p + len);
|
|
85
|
+
p += len;
|
|
86
|
+
const eq = text.indexOf('=');
|
|
87
|
+
if (eq < 1) continue;
|
|
88
|
+
const name = text.slice(0, eq).toUpperCase();
|
|
89
|
+
const key = invert(vorbisMap)[name] || { DESCRIPTION: 'comment', YEAR: 'date', ALBUM_ARTIST: 'album_artist', TRACK: 'track' }[name];
|
|
90
|
+
if (key && !(key in tags)) tags[key] = text.slice(eq + 1);
|
|
91
|
+
}
|
|
92
|
+
return tags;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function buildVorbisComment(tags, vendor = 'ffnode') {
|
|
96
|
+
const items = [];
|
|
97
|
+
for (const key of Object.keys(tags)) {
|
|
98
|
+
const name = vorbisMap[key] || key.toUpperCase();
|
|
99
|
+
items.push(Buffer.from(name + '=' + tags[key], 'utf8'));
|
|
100
|
+
}
|
|
101
|
+
const vendorBuf = Buffer.from(vendor, 'utf8');
|
|
102
|
+
let size = 8 + vendorBuf.length;
|
|
103
|
+
for (const it of items) size += 4 + it.length;
|
|
104
|
+
const out = Buffer.alloc(size);
|
|
105
|
+
let p = 0;
|
|
106
|
+
out.writeUInt32LE(vendorBuf.length, p);
|
|
107
|
+
p += 4;
|
|
108
|
+
vendorBuf.copy(out, p);
|
|
109
|
+
p += vendorBuf.length;
|
|
110
|
+
out.writeUInt32LE(items.length, p);
|
|
111
|
+
p += 4;
|
|
112
|
+
for (const it of items) {
|
|
113
|
+
out.writeUInt32LE(it.length, p);
|
|
114
|
+
p += 4;
|
|
115
|
+
it.copy(out, p);
|
|
116
|
+
p += it.length;
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function parseRiffInfo(buf) {
|
|
122
|
+
const tags = {};
|
|
123
|
+
const reverse = invert(riffMap);
|
|
124
|
+
let p = 0;
|
|
125
|
+
while (p + 8 <= buf.length) {
|
|
126
|
+
const id = buf.toString('latin1', p, p + 4);
|
|
127
|
+
const size = buf.readUInt32LE(p + 4);
|
|
128
|
+
p += 8;
|
|
129
|
+
if (p + size > buf.length) break;
|
|
130
|
+
const key = reverse[id];
|
|
131
|
+
if (key) tags[key] = buf.toString('utf8', p, p + size).replace(/\0+$/, '');
|
|
132
|
+
p += size + (size & 1);
|
|
133
|
+
}
|
|
134
|
+
return tags;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function buildRiffInfo(tags) {
|
|
138
|
+
const parts = [Buffer.from('INFO', 'latin1')];
|
|
139
|
+
for (const key of Object.keys(tags)) {
|
|
140
|
+
const id = riffMap[key];
|
|
141
|
+
if (!id) continue;
|
|
142
|
+
const text = Buffer.concat([Buffer.from(tags[key], 'utf8'), Buffer.from([0])]);
|
|
143
|
+
const head = Buffer.alloc(8);
|
|
144
|
+
head.write(id, 0, 'latin1');
|
|
145
|
+
head.writeUInt32LE(text.length, 4);
|
|
146
|
+
parts.push(head, text);
|
|
147
|
+
if (text.length & 1) parts.push(Buffer.from([0]));
|
|
148
|
+
}
|
|
149
|
+
if (parts.length === 1) return null;
|
|
150
|
+
const body = Buffer.concat(parts);
|
|
151
|
+
const head = Buffer.alloc(8);
|
|
152
|
+
head.write('LIST', 0, 'latin1');
|
|
153
|
+
head.writeUInt32LE(body.length, 4);
|
|
154
|
+
return Buffer.concat([head, body]);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function syncsafe(buf, off) {
|
|
158
|
+
return ((buf[off] & 0x7f) << 21) | ((buf[off + 1] & 0x7f) << 14) | ((buf[off + 2] & 0x7f) << 7) | (buf[off + 3] & 0x7f);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function decodeId3Text(enc, buf) {
|
|
162
|
+
let text;
|
|
163
|
+
if (enc === 0) text = buf.toString('latin1');
|
|
164
|
+
else if (enc === 3) text = buf.toString('utf8');
|
|
165
|
+
else if (enc === 2) {
|
|
166
|
+
const swapped = Buffer.from(buf);
|
|
167
|
+
swapped.swap16();
|
|
168
|
+
text = swapped.toString('utf16le');
|
|
169
|
+
} else if (buf.length >= 2 && buf[0] === 0xfe && buf[1] === 0xff) {
|
|
170
|
+
const swapped = Buffer.from(buf.subarray(2));
|
|
171
|
+
if (swapped.length & 1) return '';
|
|
172
|
+
swapped.swap16();
|
|
173
|
+
text = swapped.toString('utf16le');
|
|
174
|
+
} else if (buf.length >= 2 && buf[0] === 0xff && buf[1] === 0xfe) text = buf.toString('utf16le', 2);
|
|
175
|
+
else text = buf.toString('utf16le');
|
|
176
|
+
return text.replace(/\0+$/, '').replace(/\0/g, '/');
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function id3Size(head) {
|
|
180
|
+
if (head.length < 10 || head.toString('latin1', 0, 3) !== 'ID3') return 0;
|
|
181
|
+
return 10 + syncsafe(head, 6) + (head[5] & 0x10 ? 10 : 0);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function parseId3(buf) {
|
|
185
|
+
const tags = {};
|
|
186
|
+
if (buf.length < 10 || buf.toString('latin1', 0, 3) !== 'ID3') return tags;
|
|
187
|
+
const version = buf[3];
|
|
188
|
+
const flags = buf[5];
|
|
189
|
+
const total = Math.min(buf.length, 10 + syncsafe(buf, 6));
|
|
190
|
+
let p = 10;
|
|
191
|
+
if (flags & 0x40) {
|
|
192
|
+
const ext = version === 4 ? syncsafe(buf, p) : buf.readUInt32BE(p) + 4;
|
|
193
|
+
p += ext;
|
|
194
|
+
}
|
|
195
|
+
if (version < 3) return tags;
|
|
196
|
+
const reverse = invert(id3Map);
|
|
197
|
+
reverse.TYER = 'date';
|
|
198
|
+
while (p + 10 <= total) {
|
|
199
|
+
const id = buf.toString('latin1', p, p + 4);
|
|
200
|
+
if (id.charCodeAt(0) === 0) break;
|
|
201
|
+
const size = version === 4 ? syncsafe(buf, p + 4) : buf.readUInt32BE(p + 4);
|
|
202
|
+
p += 10;
|
|
203
|
+
if (size <= 0 || p + size > total) break;
|
|
204
|
+
const body = buf.subarray(p, p + size);
|
|
205
|
+
p += size;
|
|
206
|
+
if (id === 'COMM' && body.length > 4) {
|
|
207
|
+
const enc = body[0];
|
|
208
|
+
const wide = enc === 1 || enc === 2;
|
|
209
|
+
let q = 4;
|
|
210
|
+
while (q < body.length && !(body[q] === 0 && (!wide || body[q + 1] === 0))) q += wide ? 2 : 1;
|
|
211
|
+
q += wide ? 2 : 1;
|
|
212
|
+
if (!tags.comment) tags.comment = decodeId3Text(enc, body.subarray(q));
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const key = reverse[id];
|
|
216
|
+
if (key && body.length > 1 && !(key in tags)) tags[key] = decodeId3Text(body[0], body.subarray(1));
|
|
217
|
+
}
|
|
218
|
+
return tags;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function buildId3(tags) {
|
|
222
|
+
const frames = [];
|
|
223
|
+
const add = (id, payload) => {
|
|
224
|
+
const head = Buffer.alloc(10);
|
|
225
|
+
head.write(id, 0, 'latin1');
|
|
226
|
+
const size = payload.length;
|
|
227
|
+
head[4] = (size >>> 21) & 0x7f;
|
|
228
|
+
head[5] = (size >>> 14) & 0x7f;
|
|
229
|
+
head[6] = (size >>> 7) & 0x7f;
|
|
230
|
+
head[7] = size & 0x7f;
|
|
231
|
+
frames.push(head, payload);
|
|
232
|
+
};
|
|
233
|
+
for (const key of Object.keys(tags)) {
|
|
234
|
+
if (key === 'comment') {
|
|
235
|
+
add('COMM', Buffer.concat([Buffer.from([3, 0x65, 0x6e, 0x67, 0]), Buffer.from(tags[key], 'utf8')]));
|
|
236
|
+
} else if (id3Map[key]) {
|
|
237
|
+
add(id3Map[key], Buffer.concat([Buffer.from([3]), Buffer.from(tags[key], 'utf8')]));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (!frames.length) return null;
|
|
241
|
+
const body = Buffer.concat(frames);
|
|
242
|
+
const head = Buffer.alloc(10);
|
|
243
|
+
head.write('ID3', 0, 'latin1');
|
|
244
|
+
head[3] = 4;
|
|
245
|
+
head[6] = (body.length >>> 21) & 0x7f;
|
|
246
|
+
head[7] = (body.length >>> 14) & 0x7f;
|
|
247
|
+
head[8] = (body.length >>> 7) & 0x7f;
|
|
248
|
+
head[9] = body.length & 0x7f;
|
|
249
|
+
return Buffer.concat([head, body]);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function box(type, payload) {
|
|
253
|
+
const head = Buffer.alloc(8);
|
|
254
|
+
head.writeUInt32BE(8 + payload.length, 0);
|
|
255
|
+
head.write(type, 4, 'latin1');
|
|
256
|
+
return Buffer.concat([head, payload]);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function buildMp4Meta(tags) {
|
|
260
|
+
const items = [];
|
|
261
|
+
for (const key of Object.keys(tags)) {
|
|
262
|
+
const id = mp4Map[key];
|
|
263
|
+
if (!id) continue;
|
|
264
|
+
const text = Buffer.from(tags[key], 'utf8');
|
|
265
|
+
const data = Buffer.alloc(8);
|
|
266
|
+
data.writeUInt32BE(1, 0);
|
|
267
|
+
items.push(box(id, box('data', Buffer.concat([data, text]))));
|
|
268
|
+
}
|
|
269
|
+
if (!items.length) return null;
|
|
270
|
+
const hdlr = Buffer.alloc(25);
|
|
271
|
+
hdlr.write('mdir', 8, 'latin1');
|
|
272
|
+
hdlr.write('appl', 12, 'latin1');
|
|
273
|
+
const meta = Buffer.concat([Buffer.alloc(4), box('hdlr', hdlr), box('ilst', Buffer.concat(items))]);
|
|
274
|
+
return box('udta', box('meta', meta));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function parseMp4Ilst(buf) {
|
|
278
|
+
const tags = {};
|
|
279
|
+
const reverse = {};
|
|
280
|
+
for (const key of Object.keys(mp4Map)) reverse[Buffer.from(mp4Map[key], 'latin1').toString('latin1')] = key;
|
|
281
|
+
let p = 0;
|
|
282
|
+
while (p + 8 <= buf.length) {
|
|
283
|
+
const size = buf.readUInt32BE(p);
|
|
284
|
+
if (size < 8 || p + size > buf.length) break;
|
|
285
|
+
const id = buf.toString('latin1', p + 4, p + 8);
|
|
286
|
+
const key = reverse[id];
|
|
287
|
+
if (key && size >= 24 && buf.toString('latin1', p + 12, p + 16) === 'data') {
|
|
288
|
+
tags[key] = buf.toString('utf8', p + 24, p + size);
|
|
289
|
+
}
|
|
290
|
+
p += size;
|
|
291
|
+
}
|
|
292
|
+
return tags;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export {
|
|
296
|
+
clean,
|
|
297
|
+
parseVorbisComment,
|
|
298
|
+
buildVorbisComment,
|
|
299
|
+
parseRiffInfo,
|
|
300
|
+
buildRiffInfo,
|
|
301
|
+
parseId3,
|
|
302
|
+
buildId3,
|
|
303
|
+
id3Size,
|
|
304
|
+
buildMp4Meta,
|
|
305
|
+
parseMp4Ilst,
|
|
306
|
+
mp4Map,
|
|
307
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
class Vec {
|
|
2
|
+
constructor(Type, capacity = 256) {
|
|
3
|
+
this.Type = Type;
|
|
4
|
+
this.data = new Type(capacity);
|
|
5
|
+
this.length = 0;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
push(value) {
|
|
9
|
+
if (this.length === this.data.length) {
|
|
10
|
+
const next = new this.Type(this.data.length * 2);
|
|
11
|
+
next.set(this.data);
|
|
12
|
+
this.data = next;
|
|
13
|
+
}
|
|
14
|
+
this.data[this.length++] = value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get(index) {
|
|
18
|
+
return this.data[index];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
set(index, value) {
|
|
22
|
+
this.data[index] = value;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
view() {
|
|
26
|
+
return this.data.subarray(0, this.length);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function gcd(a, b) {
|
|
31
|
+
while (b) {
|
|
32
|
+
const t = a % b;
|
|
33
|
+
a = b;
|
|
34
|
+
b = t;
|
|
35
|
+
}
|
|
36
|
+
return a;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function fourcc(buf, off) {
|
|
40
|
+
return String.fromCharCode(buf[off], buf[off + 1], buf[off + 2], buf[off + 3]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function writeFourcc(buf, off, str) {
|
|
44
|
+
for (let i = 0; i < 4; i++) buf[off + i] = str.charCodeAt(i) & 0xff;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function readU64BE(buf, off) {
|
|
48
|
+
return buf.readUInt32BE(off) * 4294967296 + buf.readUInt32BE(off + 4);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function writeU64BE(buf, off, value) {
|
|
52
|
+
buf.writeUInt32BE(Math.floor(value / 4294967296), off);
|
|
53
|
+
buf.writeUInt32BE(value >>> 0, off + 4);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function readU64LE(buf, off) {
|
|
57
|
+
return buf.readUInt32LE(off + 4) * 4294967296 + buf.readUInt32LE(off);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function writeU64LE(buf, off, value) {
|
|
61
|
+
buf.writeUInt32LE(value >>> 0, off);
|
|
62
|
+
buf.writeUInt32LE(Math.floor(value / 4294967296), off + 4);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function equalBuffers(a, b) {
|
|
66
|
+
if (a === b) return true;
|
|
67
|
+
if (!a || !b) return !a && !b;
|
|
68
|
+
return a.length === b.length && a.equals(b);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function parseTime(value) {
|
|
72
|
+
if (value === undefined || value === null || value === '') return undefined;
|
|
73
|
+
if (typeof value === 'number') return value;
|
|
74
|
+
const text = String(value).trim();
|
|
75
|
+
if (/^-?\d+(\.\d+)?$/.test(text)) return parseFloat(text);
|
|
76
|
+
const parts = text.split(':').map(Number);
|
|
77
|
+
if (parts.some((p) => Number.isNaN(p)) || parts.length > 3) return NaN;
|
|
78
|
+
let total = 0;
|
|
79
|
+
for (const p of parts) total = total * 60 + p;
|
|
80
|
+
return total;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function parseGain(value) {
|
|
84
|
+
if (value === undefined || value === null || value === '') return 1;
|
|
85
|
+
if (typeof value === 'number') return value;
|
|
86
|
+
const text = String(value).trim();
|
|
87
|
+
const db = /^(-?\d+(\.\d+)?)\s*dB$/i.exec(text);
|
|
88
|
+
if (db) return Math.pow(10, parseFloat(db[1]) / 20);
|
|
89
|
+
return parseFloat(text);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export {
|
|
93
|
+
Vec,
|
|
94
|
+
gcd,
|
|
95
|
+
fourcc,
|
|
96
|
+
writeFourcc,
|
|
97
|
+
readU64BE,
|
|
98
|
+
writeU64BE,
|
|
99
|
+
readU64LE,
|
|
100
|
+
writeU64LE,
|
|
101
|
+
equalBuffers,
|
|
102
|
+
parseTime,
|
|
103
|
+
parseGain,
|
|
104
|
+
};
|
|
@@ -1,65 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { once } from 'node:events'
|
|
1
|
+
import { imageThumb } from '../ffmpeg/index.js'
|
|
3
2
|
import { Boom } from './boom.js'
|
|
4
3
|
|
|
5
|
-
// Dependency-free image thumbnails. Used when none of sharp / @napi-rs/image / jimp is installed,
|
|
6
|
-
// so a missing optional library never results in a media message without jpegThumbnail
|
|
7
|
-
// (the recipient sees that as a grey box). Uses ffmpeg.
|
|
8
|
-
|
|
9
|
-
const FFMPEG_TIMEOUT_MS = 15_000
|
|
10
|
-
|
|
11
|
-
async function runFfmpeg(command, args, input) {
|
|
12
|
-
const child = spawn(command, args, { stdio: [Buffer.isBuffer(input) ? 'pipe' : 'ignore', 'pipe', 'pipe'] })
|
|
13
|
-
const out = []
|
|
14
|
-
const err = []
|
|
15
|
-
child.stdout.on('data', c => out.push(c))
|
|
16
|
-
child.stderr.on('data', c => err.push(c))
|
|
17
|
-
if (Buffer.isBuffer(input)) {
|
|
18
|
-
child.stdin.on('error', () => {})
|
|
19
|
-
child.stdin.end(input)
|
|
20
|
-
}
|
|
21
|
-
const timer = setTimeout(() => child.kill('SIGKILL'), FFMPEG_TIMEOUT_MS)
|
|
22
|
-
try {
|
|
23
|
-
const [code] = await once(child, 'close')
|
|
24
|
-
if (code !== 0) throw new Boom(`${command} failed (code ${code}): ${Buffer.concat(err).toString('utf8').trim()}`)
|
|
25
|
-
return Buffer.concat(out)
|
|
26
|
-
} finally {
|
|
27
|
-
clearTimeout(timer)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function probeDimensions(input) {
|
|
32
|
-
try {
|
|
33
|
-
const src = Buffer.isBuffer(input) ? 'pipe:0' : input
|
|
34
|
-
const args = ['-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'csv=p=0', src]
|
|
35
|
-
const [width, height] = (await runFfmpeg('ffprobe', args, input)).toString('utf8').trim().split(',').map(Number)
|
|
36
|
-
return width > 0 && height > 0 ? { width, height } : {}
|
|
37
|
-
} catch {
|
|
38
|
-
return {}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async function ffmpegThumb(input, width) {
|
|
43
|
-
const src = Buffer.isBuffer(input) ? 'pipe:0' : input
|
|
44
|
-
const args = [
|
|
45
|
-
'-loglevel', 'error', '-i', src, '-an', '-sn', '-dn', '-map_metadata', '-1',
|
|
46
|
-
'-vf', `scale=w='min(${width},iw)':h=-2`,
|
|
47
|
-
'-frames:v', '1', '-q:v', '12', '-c:v', 'mjpeg', '-f', 'image2pipe', 'pipe:1'
|
|
48
|
-
]
|
|
49
|
-
const buffer = await runFfmpeg('ffmpeg', args, input)
|
|
50
|
-
if (!buffer.length) throw new Boom('ffmpeg produced no output')
|
|
51
|
-
return { buffer, original: await probeDimensions(input) }
|
|
52
|
-
}
|
|
53
|
-
|
|
54
4
|
/**
|
|
55
5
|
* @param {Buffer | string} input image bytes or a file path
|
|
56
6
|
* @param {number} width thumbnail width in px
|
|
57
7
|
* @returns {Promise<{ buffer: Buffer, original: { width?: number, height?: number } }>}
|
|
58
8
|
*/
|
|
59
9
|
export async function extractImageThumbFallback(input, width = 32) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
throw new Boom(`could not generate image thumbnail (install sharp, @napi-rs/image or jimp, or ffmpeg): ${err?.message ?? String(err)}`)
|
|
64
|
-
}
|
|
10
|
+
const thumb = await imageThumb(input, width)
|
|
11
|
+
if (!thumb) throw new Boom('could not generate image thumbnail (install sharp, @napi-rs/image or jimp)')
|
|
12
|
+
return thumb
|
|
65
13
|
}
|
|
@@ -103,7 +103,8 @@ export const makeMessagesRecvSocket = (config) => {
|
|
|
103
103
|
oldestMsgFromMe: oldestMsgKey.fromMe,
|
|
104
104
|
oldestMsgId: oldestMsgKey.id,
|
|
105
105
|
oldestMsgTimestampMs: oldestMsgTimestamp,
|
|
106
|
-
onDemandMsgCount: count
|
|
106
|
+
onDemandMsgCount: count,
|
|
107
|
+
supportInlineResponse: true
|
|
107
108
|
},
|
|
108
109
|
peerDataOperationRequestType: proto.Message.PeerDataOperationRequestType.HISTORY_SYNC_ON_DEMAND
|
|
109
110
|
};
|
|
@@ -1452,11 +1453,6 @@ export const makeMessagesRecvSocket = (config) => {
|
|
|
1452
1453
|
acked = true;
|
|
1453
1454
|
await sendReceipt(msg.key.remoteJid, participant, [msg.key.id], type);
|
|
1454
1455
|
|
|
1455
|
-
const isAnyHistoryMsg = getHistoryMsg(msg.message);
|
|
1456
|
-
if (isAnyHistoryMsg) {
|
|
1457
|
-
const jid = jidNormalizedUser(msg.key.remoteJid);
|
|
1458
|
-
await sendReceipt(jid, undefined, [msg.key.id], 'hist_sync');
|
|
1459
|
-
}
|
|
1460
1456
|
}
|
|
1461
1457
|
else {
|
|
1462
1458
|
acked = true;
|
|
@@ -1466,6 +1462,9 @@ export const makeMessagesRecvSocket = (config) => {
|
|
|
1466
1462
|
}
|
|
1467
1463
|
cleanMessage(msg, authState.creds.me.id, authState.creds.me.lid);
|
|
1468
1464
|
await upsertMessage(msg, node.attrs.offline ? 'append' : 'notify');
|
|
1465
|
+
if (!isJidNewsletter(msg.key.remoteJid) && getHistoryMsg(msg.message)) {
|
|
1466
|
+
await sendReceipt(jidNormalizedUser(msg.key.remoteJid), undefined, [msg.key.id], 'hist_sync');
|
|
1467
|
+
}
|
|
1469
1468
|
});
|
|
1470
1469
|
}
|
|
1471
1470
|
catch (error) {
|
package/src/utils/media.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process'
|
|
2
1
|
import { audioDuration } from '../foundation/audio-duration.js'
|
|
3
2
|
import { decodeChannel0, waveformFromSamples } from '../foundation/audio-samples.js'
|
|
4
3
|
import { getLinkPreview } from '../foundation/link-preview.js'
|
|
@@ -11,6 +10,7 @@ import { Readable, Transform } from 'node:stream'
|
|
|
11
10
|
import { proto } from '../../WAProto/index.js'
|
|
12
11
|
import { Boom } from '../foundation/boom.js'
|
|
13
12
|
import { extractImageThumbFallback } from '../foundation/image-thumb.js'
|
|
13
|
+
import { decodeFirstChannel, probeNative } from '../ffmpeg/index.js'
|
|
14
14
|
import { getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from '../binary/wa-binary.js'
|
|
15
15
|
import { aesDecryptGCM, aesEncryptGCM, hkdf } from './wa-protocol-core.js'
|
|
16
16
|
import { generateMessageIDV2 } from './wa-protocol-core.js'
|
|
@@ -58,7 +58,7 @@ export const MEDIA_PATH_MAP = {
|
|
|
58
58
|
'thumbnail-document': '/mms/document',
|
|
59
59
|
'product-catalog-image': '/product/image',
|
|
60
60
|
'md-app-state': '',
|
|
61
|
-
'md-msg-hist': '/mms/md-
|
|
61
|
+
'md-msg-hist': '/mms/md-msg-hist',
|
|
62
62
|
'biz-cover-photo': '/pps/biz-cover-photo'
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -146,40 +146,8 @@ export async function getMediaKeys(buffer, mediaType) {
|
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
export const extractVideoThumb = async (
|
|
150
|
-
|
|
151
|
-
'ffmpeg',
|
|
152
|
-
[
|
|
153
|
-
'-loglevel',
|
|
154
|
-
'error',
|
|
155
|
-
'-ss',
|
|
156
|
-
String(time),
|
|
157
|
-
'-i',
|
|
158
|
-
path,
|
|
159
|
-
'-an',
|
|
160
|
-
'-sn',
|
|
161
|
-
'-dn',
|
|
162
|
-
'-map_metadata',
|
|
163
|
-
'-1',
|
|
164
|
-
'-vf',
|
|
165
|
-
`scale=${size.width}:-1`,
|
|
166
|
-
'-frames:v',
|
|
167
|
-
'1',
|
|
168
|
-
'-c:v',
|
|
169
|
-
'mjpeg',
|
|
170
|
-
'-f',
|
|
171
|
-
'image2pipe',
|
|
172
|
-
'pipe:1'
|
|
173
|
-
],
|
|
174
|
-
{ stdio: ['ignore', 'pipe', 'pipe'] }
|
|
175
|
-
)
|
|
176
|
-
const stdoutChunks = []
|
|
177
|
-
const stderrChunks = []
|
|
178
|
-
ffmpeg.stdout.on('data', chunk => stdoutChunks.push(chunk))
|
|
179
|
-
ffmpeg.stderr.on('data', chunk => stderrChunks.push(chunk))
|
|
180
|
-
const [code] = await once(ffmpeg, 'close')
|
|
181
|
-
if (code !== 0) throw new Boom(`FFmpeg failed (code ${code}):\n` + Buffer.concat(stderrChunks).toString('utf8'))
|
|
182
|
-
return Buffer.concat(stdoutChunks)
|
|
149
|
+
export const extractVideoThumb = async () => {
|
|
150
|
+
throw new Boom('video thumbnails are not supported by the built-in media engine')
|
|
183
151
|
}
|
|
184
152
|
|
|
185
153
|
const thumbWithLibrary = async (lib, bufferOrFilePath, width) => {
|
|
@@ -204,8 +172,6 @@ const thumbWithLibrary = async (lib, bufferOrFilePath, width) => {
|
|
|
204
172
|
}
|
|
205
173
|
}
|
|
206
174
|
|
|
207
|
-
// Never leave a media message without jpegThumbnail just because sharp / @napi-rs/image / jimp is not installed:
|
|
208
|
-
// fall back to ffmpeg (see foundation/image-thumb.js).
|
|
209
175
|
export const extractImageThumb = async (bufferOrFilePath, width = 32) => {
|
|
210
176
|
if (bufferOrFilePath instanceof Readable) bufferOrFilePath = await toBuffer(bufferOrFilePath)
|
|
211
177
|
let libraryError
|
|
@@ -259,62 +225,19 @@ export async function getAudioDuration(buffer) {
|
|
|
259
225
|
if (!Buffer.isBuffer(buffer) && typeof buffer !== 'string') buffer = await toBuffer(buffer)
|
|
260
226
|
const native = await audioDuration(buffer)
|
|
261
227
|
if (typeof native === 'number' && Number.isFinite(native)) return native
|
|
262
|
-
|
|
228
|
+
const probed = (await probeNative(buffer))?.format.duration
|
|
229
|
+
return probed > 0 ? probed : undefined
|
|
263
230
|
}
|
|
264
231
|
|
|
265
|
-
async function ffprobeDuration(input) {
|
|
266
|
-
const usesStdin = Buffer.isBuffer(input)
|
|
267
|
-
const args = ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', usesStdin ? 'pipe:0' : input]
|
|
268
|
-
try {
|
|
269
|
-
const ffprobe = spawn('ffprobe', args, { stdio: [usesStdin ? 'pipe' : 'ignore', 'pipe', 'ignore'] })
|
|
270
|
-
const outChunks = []
|
|
271
|
-
ffprobe.stdout.on('data', chunk => outChunks.push(chunk))
|
|
272
|
-
if (usesStdin) ffprobe.stdin.end(input)
|
|
273
|
-
const [code] = await once(ffprobe, 'close')
|
|
274
|
-
if (code !== 0) return undefined
|
|
275
|
-
const seconds = parseFloat(Buffer.concat(outChunks).toString('utf8').trim())
|
|
276
|
-
return Number.isFinite(seconds) ? seconds : undefined
|
|
277
|
-
} catch {
|
|
278
|
-
return undefined
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const decodeChannel0WithFfmpeg = audioData =>
|
|
283
|
-
new Promise(resolve => {
|
|
284
|
-
let child
|
|
285
|
-
try {
|
|
286
|
-
child = spawn('ffmpeg', ['-v', 'error', '-i', 'pipe:0', '-af', 'pan=mono|c0=c0', '-ar', '16000', '-f', 'f32le', 'pipe:1'], { stdio: ['pipe', 'pipe', 'ignore'] })
|
|
287
|
-
} catch {
|
|
288
|
-
return resolve(undefined)
|
|
289
|
-
}
|
|
290
|
-
const out = []
|
|
291
|
-
const timer = setTimeout(() => {
|
|
292
|
-
child.kill('SIGKILL')
|
|
293
|
-
resolve(undefined)
|
|
294
|
-
}, 60000)
|
|
295
|
-
child.on('error', () => resolve(undefined))
|
|
296
|
-
child.stdin.on('error', () => {})
|
|
297
|
-
child.stdout.on('data', c => out.push(c))
|
|
298
|
-
child.on('close', code => {
|
|
299
|
-
clearTimeout(timer)
|
|
300
|
-
const raw = Buffer.concat(out)
|
|
301
|
-
if (code !== 0 || raw.length < 4) return resolve(undefined)
|
|
302
|
-
const copy = new Float32Array(Math.floor(raw.length / 4))
|
|
303
|
-
for (let i = 0; i < copy.length; i++) copy[i] = raw.readFloatLE(i * 4)
|
|
304
|
-
resolve(copy)
|
|
305
|
-
})
|
|
306
|
-
child.stdin.end(audioData)
|
|
307
|
-
})
|
|
308
|
-
|
|
309
232
|
export async function getAudioWaveform(buffer, logger) {
|
|
310
233
|
try {
|
|
311
234
|
let audioData
|
|
312
235
|
if (Buffer.isBuffer(buffer)) audioData = buffer
|
|
313
236
|
else if (typeof buffer === 'string') audioData = await toBuffer(createReadStream(buffer))
|
|
314
237
|
else audioData = await toBuffer(buffer)
|
|
315
|
-
const rawData = decodeChannel0(audioData) ?? (await
|
|
238
|
+
const rawData = decodeChannel0(audioData) ?? (await decodeFirstChannel(audioData))
|
|
316
239
|
if (!rawData || !rawData.length) {
|
|
317
|
-
logger?.debug('Failed to generate waveform: no decoder for this audio
|
|
240
|
+
logger?.debug('Failed to generate waveform: no decoder for this audio')
|
|
318
241
|
return undefined
|
|
319
242
|
}
|
|
320
243
|
return waveformFromSamples(rawData)
|