@itznomo/baileys 0.0.0-stage → 0.0.1
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/BUTTONS.md +153 -0
- package/Example/call-bot.mjs +38 -0
- package/Example/example.mjs +36 -0
- package/Example/full-bot-commands.mjs +319 -0
- package/Example/full-bot.mjs +77 -0
- package/LICENSE +21 -0
- package/README.md +543 -2
- package/WAProto/@dnuzi +1 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +113300 -0
- package/assets/banner.svg +17 -0
- package/assets/devider.svg +1 -0
- package/assets/features.svg +21 -0
- package/docs/CALLS.md +106 -0
- package/docs/FEATURES.md +1152 -0
- package/engine-requirements.js +19 -0
- package/index.d.ts +815 -0
- package/package.json +51 -4
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +627 -0
- package/src/constants.js +147 -0
- package/src/defaults.js +106 -0
- package/src/foundation/ai-rich.js +654 -0
- package/src/foundation/audio-duration.js +470 -0
- package/src/foundation/audio-samples.js +250 -0
- package/src/foundation/boom.js +39 -0
- package/src/foundation/builders.js +851 -0
- package/src/foundation/cache.js +134 -0
- package/src/foundation/concurrency.js +69 -0
- package/src/foundation/curve25519.js +189 -0
- package/src/foundation/image-thumb.js +65 -0
- package/src/foundation/link-preview.js +278 -0
- package/src/foundation/logger.js +78 -0
- package/src/foundation/long.js +130 -0
- package/src/foundation/performance.js +385 -0
- package/src/foundation/protobuf-minimal.js +587 -0
- package/src/foundation/qrcode-terminal.js +273 -0
- package/src/foundation/sql-database.js +716 -0
- package/src/foundation/wa-crypto.js +47 -0
- package/src/foundation/zip.js +108 -0
- package/src/index.js +67 -0
- package/src/signal/libsignal.js +996 -0
- package/src/signal/signal-group.js +455 -0
- package/src/signal/signal-repository.js +635 -0
- package/src/socket/business-communities.js +811 -0
- package/src/socket/chats.js +702 -0
- package/src/socket/groups-newsletter.js +524 -0
- package/src/socket/messages-recv.js +1815 -0
- package/src/socket/messages-send.js +1526 -0
- package/src/socket/socket-builder.js +131 -0
- package/src/socket/socket-core.js +913 -0
- package/src/socket/usync.js +287 -0
- package/src/socket-client/native-websocket.js +344 -0
- package/src/socket-client/websocket-client.js +170 -0
- package/src/utils/auth-state-core.js +731 -0
- package/src/utils/auth-state-storage.js +594 -0
- package/src/utils/chat-sync.js +1046 -0
- package/src/utils/check-status.js +159 -0
- package/src/utils/cstoken.js +17 -0
- package/src/utils/media.js +807 -0
- package/src/utils/message-compose.js +1747 -0
- package/src/utils/message-processing.js +912 -0
- package/src/utils/modern-messages.js +435 -0
- package/src/utils/nomo-buttons.js +138 -0
- package/src/utils/nomo-commands.js +168 -0
- package/src/utils/spam-report-utils.js +136 -0
- package/src/utils/use-keyv-auth-state.js +307 -0
- package/src/utils/username.js +19 -0
- package/src/utils/wa-protocol-core.js +1227 -0
- package/src/voip/WaVoipCoordinator.js +158 -0
- package/src/voip/app-data/WaAppDataStream.js +193 -0
- package/src/voip/app-data/protocol.js +172 -0
- package/src/voip/auto.js +139 -0
- package/src/voip/bytes.js +63 -0
- package/src/voip/call/WaCallManager.js +442 -0
- package/src/voip/call/WaCallMediaSession.js +1727 -0
- package/src/voip/call/call-state.js +203 -0
- package/src/voip/crypto/encryption.js +13 -0
- package/src/voip/crypto/primitives.js +21 -0
- package/src/voip/crypto/rtcp.js +66 -0
- package/src/voip/crypto/srtp.js +180 -0
- package/src/voip/crypto/ssrc.js +15 -0
- package/src/voip/index.js +730 -0
- package/src/voip/media/WaAudioEngine.js +486 -0
- package/src/voip/media/WaVideoEngine.js +275 -0
- package/src/voip/media/audio-codec.js +0 -0
- package/src/voip/media/audio-reorder.js +107 -0
- package/src/voip/media/h264.js +246 -0
- package/src/voip/media/mlow-codec.js +465 -0
- package/src/voip/media/rtcp.js +68 -0
- package/src/voip/media/rtp.js +242 -0
- package/src/voip/protobuf.js +30 -0
- package/src/voip/relay/WaManualRelay.js +609 -0
- package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
- package/src/voip/relay/dtls/aead.js +46 -0
- package/src/voip/relay/dtls/cert-builder.js +102 -0
- package/src/voip/relay/dtls/der.js +98 -0
- package/src/voip/relay/dtls/ecdhe.js +37 -0
- package/src/voip/relay/dtls/handshake-framing.js +100 -0
- package/src/voip/relay/dtls/handshake-messages.js +158 -0
- package/src/voip/relay/dtls/handshake.js +582 -0
- package/src/voip/relay/dtls/prf.js +39 -0
- package/src/voip/relay/dtls/record.js +56 -0
- package/src/voip/relay/relay-ack.js +141 -0
- package/src/voip/relay/sctp/association.js +434 -0
- package/src/voip/relay/sctp/crc32c.js +21 -0
- package/src/voip/relay/sctp/wire.js +226 -0
- package/src/voip/relay/stun.js +463 -0
- package/src/voip/shim/baileys-resolve.js +7 -0
- package/src/voip/shim/core.js +66 -0
- package/src/voip/shim/crypto.js +6 -0
- package/src/voip/shim/proto.js +10 -0
- package/src/voip/shim/protocol.js +130 -0
- package/src/voip/shim/transport.js +82 -0
- package/src/voip/shim/util.js +207 -0
- package/src/voip/signaling/bridge.js +102 -0
- package/src/voip/signaling/screen-share.js +60 -0
- package/src/voip/signaling/signaling.js +491 -0
- package/src/voip/types.js +69 -0
- package/src/voip/voip-deps.js +243 -0
- package/src/voip/voipClient.js +314 -0
- package/src/wam/wam-constants.js +22852 -0
- package/src/wam/wam.js +146 -0
- package/voip.d.ts +144 -0
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs'
|
|
2
|
+
import { open, stat } from 'node:fs/promises'
|
|
3
|
+
|
|
4
|
+
const u32be = (b, o) => b.readUInt32BE(o)
|
|
5
|
+
const u64be = (b, o) => Number(b.readBigUInt64BE(o))
|
|
6
|
+
const ascii = (b, o, n) => b.toString('latin1', o, o + n)
|
|
7
|
+
|
|
8
|
+
const oggDuration = buf => {
|
|
9
|
+
|
|
10
|
+
const streams = new Map()
|
|
11
|
+
let pos = 0
|
|
12
|
+
let endOfStream = false
|
|
13
|
+
while (true) {
|
|
14
|
+
if (pos + 27 > buf.length) {
|
|
15
|
+
|
|
16
|
+
if (pos < buf.length) endOfStream = true
|
|
17
|
+
else endOfStream = true
|
|
18
|
+
break
|
|
19
|
+
}
|
|
20
|
+
if (ascii(buf, pos, 4) !== 'OggS') break
|
|
21
|
+
const headerType = buf[pos + 5]
|
|
22
|
+
const granule = buf.readBigInt64LE(pos + 6)
|
|
23
|
+
const serial = buf.readUInt32LE(pos + 14)
|
|
24
|
+
const nseg = buf[pos + 26]
|
|
25
|
+
let s = streams.get(serial)
|
|
26
|
+
if (!s) {
|
|
27
|
+
s = { codec: undefined, sampleRate: undefined, preSkip: 0, last: undefined, closed: false, pageNumber: 0, fullPages: 0, pageCount: 0 }
|
|
28
|
+
streams.set(serial, s)
|
|
29
|
+
}
|
|
30
|
+
s.pageNumber = buf.readUInt32LE(pos + 18)
|
|
31
|
+
if (pos + 27 + nseg > buf.length) {
|
|
32
|
+
endOfStream = true
|
|
33
|
+
break
|
|
34
|
+
}
|
|
35
|
+
let dataLen = 0
|
|
36
|
+
for (let i = 0; i < nseg; i++) dataLen += buf[pos + 27 + i]
|
|
37
|
+
const dataStart = pos + 27 + nseg
|
|
38
|
+
if (dataStart + dataLen > buf.length) {
|
|
39
|
+
endOfStream = true
|
|
40
|
+
break
|
|
41
|
+
}
|
|
42
|
+
if (headerType & 0x02) {
|
|
43
|
+
const idText = [...buf.subarray(dataStart, dataStart + 7)].filter(b => b >= 32 && b <= 126).map(b => String.fromCharCode(b)).join('')
|
|
44
|
+
if (idText === 'OpusHea') {
|
|
45
|
+
s.codec = 'opus'
|
|
46
|
+
s.preSkip = buf.readUInt16LE(dataStart + 10)
|
|
47
|
+
s.sampleRate = buf.readUInt32LE(dataStart + 12)
|
|
48
|
+
} else if (idText === 'vorbis') {
|
|
49
|
+
s.codec = 'vorbis'
|
|
50
|
+
s.sampleRate = buf.readUInt32LE(dataStart + 12)
|
|
51
|
+
} else {
|
|
52
|
+
s.codec = 'other'
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (headerType & 0x04) s.closed = true
|
|
56
|
+
else if (!(headerType & 0x02)) s.fullPages++
|
|
57
|
+
s.pageCount++
|
|
58
|
+
s.last = { granule, lastPage: !!(headerType & 0x04) }
|
|
59
|
+
pos = dataStart + dataLen
|
|
60
|
+
|
|
61
|
+
if ([...streams.values()].every(x => x.closed)) break
|
|
62
|
+
}
|
|
63
|
+
for (const s of streams.values()) {
|
|
64
|
+
if (!s.last || !s.sampleRate) continue
|
|
65
|
+
if (!(endOfStream || s.last.lastPage)) continue
|
|
66
|
+
if (s.last.granule < 0n) continue
|
|
67
|
+
|
|
68
|
+
if (s.codec === 'vorbis' && endOfStream && !s.last.lastPage && s.fullPages === 0 && s.pageCount > 0) return undefined
|
|
69
|
+
const g = Number(s.last.granule)
|
|
70
|
+
if (s.codec === 'opus') return (g - s.preSkip) / 48000
|
|
71
|
+
if (s.codec === 'vorbis') return g / s.sampleRate
|
|
72
|
+
}
|
|
73
|
+
return undefined
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const wavDuration = buf => {
|
|
77
|
+
if (ascii(buf, 0, 4) !== 'RIFF' || ascii(buf, 8, 4) !== 'WAVE') return undefined
|
|
78
|
+
let pos = 12
|
|
79
|
+
let sampleRate
|
|
80
|
+
let blockAlign = 0
|
|
81
|
+
let fact
|
|
82
|
+
while (pos + 8 <= buf.length) {
|
|
83
|
+
const id = ascii(buf, pos, 4)
|
|
84
|
+
const size = buf.readUInt32LE(pos + 4)
|
|
85
|
+
const body = pos + 8
|
|
86
|
+
if (id === 'fmt ') {
|
|
87
|
+
sampleRate = buf.readUInt32LE(body + 4)
|
|
88
|
+
blockAlign = buf.readUInt16LE(body + 12)
|
|
89
|
+
} else if (id === 'fact') {
|
|
90
|
+
fact = buf.readUInt32LE(body)
|
|
91
|
+
} else if (id === 'data') {
|
|
92
|
+
let chunkSize = size
|
|
93
|
+
const remaining = buf.length - body
|
|
94
|
+
if (remaining < chunkSize) chunkSize = remaining
|
|
95
|
+
const samples = fact !== undefined ? fact : chunkSize === 0xffffffff ? undefined : chunkSize / blockAlign
|
|
96
|
+
if (samples && sampleRate) return samples / sampleRate
|
|
97
|
+
return undefined
|
|
98
|
+
}
|
|
99
|
+
pos = body + size + (size % 2)
|
|
100
|
+
}
|
|
101
|
+
return undefined
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const flacDuration = buf => {
|
|
105
|
+
if (ascii(buf, 0, 4) !== 'fLaC') return undefined
|
|
106
|
+
let pos = 4
|
|
107
|
+
while (pos + 4 <= buf.length) {
|
|
108
|
+
const h = buf[pos]
|
|
109
|
+
const type = h & 0x7f
|
|
110
|
+
const len = (buf[pos + 1] << 16) | (buf[pos + 2] << 8) | buf[pos + 3]
|
|
111
|
+
if (type === 0) {
|
|
112
|
+
|
|
113
|
+
if (len !== 34) return undefined
|
|
114
|
+
|
|
115
|
+
if (pos + 4 + 34 > buf.length) return undefined
|
|
116
|
+
const o = pos + 4
|
|
117
|
+
const sampleRate = ((buf[o + 10] << 16) | (buf[o + 11] << 8) | buf[o + 12]) >> 4
|
|
118
|
+
const totalSamples = (buf[o + 13] & 0x0f) * 2 ** 32 + buf.readUInt32BE(o + 14)
|
|
119
|
+
return totalSamples > 0 && sampleRate ? totalSamples / sampleRate : undefined
|
|
120
|
+
}
|
|
121
|
+
if (h & 0x80) break
|
|
122
|
+
pos += 4 + len
|
|
123
|
+
}
|
|
124
|
+
return undefined
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const CONTAINERS = new Set(['moov', 'trak', 'mdia', 'minf', 'stbl', 'moof', 'traf', 'mvex'])
|
|
128
|
+
|
|
129
|
+
const mp4Duration = buf => {
|
|
130
|
+
const tracks = []
|
|
131
|
+
const walk = (start, end, cur) => {
|
|
132
|
+
let pos = start
|
|
133
|
+
while (pos + 8 <= end) {
|
|
134
|
+
let size = u32be(buf, pos)
|
|
135
|
+
const type = ascii(buf, pos + 4, 4)
|
|
136
|
+
let hdr = 8
|
|
137
|
+
if (size === 1) {
|
|
138
|
+
size = u64be(buf, pos + 8)
|
|
139
|
+
hdr = 16
|
|
140
|
+
} else if (size === 0) size = end - pos
|
|
141
|
+
|
|
142
|
+
if (size >= hdr && pos + size > end) {
|
|
143
|
+
if (type === 'moov') truncated = true
|
|
144
|
+
size = end - pos
|
|
145
|
+
} else if (size < hdr) size = Math.min(Math.max(size, hdr), end - pos)
|
|
146
|
+
const body = pos + hdr
|
|
147
|
+
const bodyEnd = pos + size
|
|
148
|
+
if (type === 'trak') {
|
|
149
|
+
const t = { handler: undefined, timeScale: 0, duration: 0, hasMdhd: false }
|
|
150
|
+
tracks.push(t)
|
|
151
|
+
walk(body, bodyEnd, t)
|
|
152
|
+
} else if (CONTAINERS.has(type)) {
|
|
153
|
+
walk(body, bodyEnd, cur)
|
|
154
|
+
} else if (cur && type === 'hdlr') {
|
|
155
|
+
cur.handler = ascii(buf, body + 8, 4)
|
|
156
|
+
} else if (cur && type === 'mdhd') {
|
|
157
|
+
const v = buf[body]
|
|
158
|
+
cur.hasMdhd = true
|
|
159
|
+
if (v === 1) {
|
|
160
|
+
cur.timeScale = u32be(buf, body + 20)
|
|
161
|
+
cur.duration = u64be(buf, body + 24)
|
|
162
|
+
} else {
|
|
163
|
+
cur.timeScale = u32be(buf, body + 12)
|
|
164
|
+
cur.duration = u32be(buf, body + 16)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
pos += size
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
let truncated = false
|
|
171
|
+
walk(0, buf.length, undefined)
|
|
172
|
+
if (truncated) return undefined
|
|
173
|
+
const audio = tracks.find(t => t.handler === 'soun' || t.handler === 'audi')
|
|
174
|
+
if (audio && audio.timeScale > 0 && audio.duration > 0) return audio.duration / audio.timeScale
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const MPEG_BITRATES = {
|
|
179
|
+
|
|
180
|
+
'1-1': [32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448],
|
|
181
|
+
'1-2': [32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384],
|
|
182
|
+
'1-3': [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320],
|
|
183
|
+
'2-1': [32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256],
|
|
184
|
+
'2-2': [8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160],
|
|
185
|
+
'2-3': [8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160]
|
|
186
|
+
}
|
|
187
|
+
const MPEG_SAMPLE_RATES = { 3: [44100, 48000, 32000], 2: [22050, 24000, 16000], 0: [11025, 12000, 8000] }
|
|
188
|
+
|
|
189
|
+
const mpegHeader = (buf, o) => {
|
|
190
|
+
if (o + 4 > buf.length || buf[o] !== 0xff || (buf[o + 1] & 0xe0) !== 0xe0) return null
|
|
191
|
+
const versionBits = (buf[o + 1] >> 3) & 3
|
|
192
|
+
const layerBits = (buf[o + 1] >> 1) & 3
|
|
193
|
+
const bitrateIdx = buf[o + 2] >> 4
|
|
194
|
+
const srIdx = (buf[o + 2] >> 2) & 3
|
|
195
|
+
const padding = (buf[o + 2] >> 1) & 1
|
|
196
|
+
if (versionBits === 1 || layerBits === 0 || srIdx === 3) return null
|
|
197
|
+
const layer = 4 - layerBits
|
|
198
|
+
const verKey = versionBits === 3 ? 1 : 2
|
|
199
|
+
if (bitrateIdx === 0 || bitrateIdx === 15) return null
|
|
200
|
+
const bitrate = MPEG_BITRATES[`${verKey}-${layer}`][bitrateIdx - 1] * 1000
|
|
201
|
+
const sampleRate = MPEG_SAMPLE_RATES[versionBits][srIdx]
|
|
202
|
+
const samplesPerFrame = layer === 1 ? 384 : layer === 2 ? 1152 : versionBits === 3 ? 1152 : 576
|
|
203
|
+
const slot = layer === 1 ? 4 : 1
|
|
204
|
+
const frameSize = layer === 1 ? Math.floor(((12 * bitrate) / sampleRate + padding) * 4) : Math.floor((samplesPerFrame / 8) * bitrate / sampleRate + (padding ? slot : 0))
|
|
205
|
+
const mono = (buf[o + 3] >> 6) === 3
|
|
206
|
+
return { versionBits, layer, bitrate, sampleRate, samplesPerFrame, frameSize, padding, crc: (buf[o + 1] & 1) === 0, mono }
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const id3v2Size = buf => {
|
|
210
|
+
if (buf.length >= 10 && ascii(buf, 0, 3) === 'ID3') {
|
|
211
|
+
const size = ((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f)
|
|
212
|
+
return 10 + size + (buf[5] & 0x10 ? 10 : 0)
|
|
213
|
+
}
|
|
214
|
+
return 0
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const mp3Duration = buf => {
|
|
218
|
+
let pos = id3v2Size(buf)
|
|
219
|
+
|
|
220
|
+
let first
|
|
221
|
+
while (pos + 4 <= buf.length) {
|
|
222
|
+
const h = mpegHeader(buf, pos)
|
|
223
|
+
if (h && h.frameSize > 0 && (pos + h.frameSize === buf.length || mpegHeader(buf, pos + h.frameSize) || pos + h.frameSize > buf.length)) {
|
|
224
|
+
first = { ...h, pos }
|
|
225
|
+
break
|
|
226
|
+
}
|
|
227
|
+
pos++
|
|
228
|
+
}
|
|
229
|
+
if (!first) return undefined
|
|
230
|
+
const hasId3v1 = buf.length >= 128 && ascii(buf, buf.length - 128, 3) === 'TAG'
|
|
231
|
+
|
|
232
|
+
const sideInfo = first.versionBits === 3 ? (first.mono ? 17 : 32) : first.mono ? 9 : 17
|
|
233
|
+
const tagOff = first.pos + 4 + (first.crc ? 2 : 0) + sideInfo
|
|
234
|
+
const tag = tagOff + 4 <= buf.length ? ascii(buf, tagOff, 4) : ''
|
|
235
|
+
if (tag === 'Xing' || tag === 'Info') {
|
|
236
|
+
const flags = u32be(buf, tagOff + 4)
|
|
237
|
+
let o = tagOff + 8
|
|
238
|
+
let numFrames = null
|
|
239
|
+
let streamSize = null
|
|
240
|
+
if (flags & 1) {
|
|
241
|
+
numFrames = u32be(buf, o)
|
|
242
|
+
o += 4
|
|
243
|
+
}
|
|
244
|
+
if (flags & 2) {
|
|
245
|
+
streamSize = u32be(buf, o)
|
|
246
|
+
o += 4
|
|
247
|
+
}
|
|
248
|
+
if (flags & 4) o += 100
|
|
249
|
+
if (flags & 8) o += 4
|
|
250
|
+
let lameMusicLength
|
|
251
|
+
if (o + 4 <= buf.length && ascii(buf, o, 4) === 'LAME') {
|
|
252
|
+
const ver = ascii(buf, o + 4, 5)
|
|
253
|
+
const m = ver.match(/\d+.\d+/g)
|
|
254
|
+
if (m) {
|
|
255
|
+
const [maj, min] = m[0].split('.').map(n => parseInt(n, 10))
|
|
256
|
+
if (maj >= 3 && min >= 90) lameMusicLength = buf.readUInt32BE(o + 9 + 0x14)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (streamSize && numFrames !== null) return (numFrames * first.samplesPerFrame) / first.sampleRate
|
|
261
|
+
if (lameMusicLength !== undefined && lameMusicLength > 0) return lameMusicLength / 1000
|
|
262
|
+
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const bitrates = []
|
|
266
|
+
let p = first.pos
|
|
267
|
+
let last
|
|
268
|
+
for (let i = 0; i < 4 && p + 4 <= buf.length; i++) {
|
|
269
|
+
const h = mpegHeader(buf, p)
|
|
270
|
+
if (!h) break
|
|
271
|
+
bitrates.push(h.bitrate)
|
|
272
|
+
last = h
|
|
273
|
+
p += h.frameSize
|
|
274
|
+
}
|
|
275
|
+
if (bitrates.length === 4 && bitrates.every(b => b === bitrates[0])) {
|
|
276
|
+
|
|
277
|
+
const mpegSize = buf.length - first.pos - (hasId3v1 ? 128 : 0)
|
|
278
|
+
return (Math.round(mpegSize / last.frameSize) * first.samplesPerFrame) / first.sampleRate
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
let frames = 0
|
|
282
|
+
p = first.pos
|
|
283
|
+
while (p + 4 <= buf.length) {
|
|
284
|
+
const h = mpegHeader(buf, p)
|
|
285
|
+
if (!h) {
|
|
286
|
+
|
|
287
|
+
p++
|
|
288
|
+
continue
|
|
289
|
+
}
|
|
290
|
+
frames++
|
|
291
|
+
p += h.frameSize
|
|
292
|
+
}
|
|
293
|
+
return frames ? (frames * first.samplesPerFrame) / first.sampleRate : undefined
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const ADTS_SAMPLE_RATES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350]
|
|
297
|
+
|
|
298
|
+
const adtsDuration = buf => {
|
|
299
|
+
let pos = id3v2Size(buf)
|
|
300
|
+
let frames = 0
|
|
301
|
+
let sampleRate
|
|
302
|
+
while (pos + 7 <= buf.length) {
|
|
303
|
+
|
|
304
|
+
if (buf[pos] !== 0xff || (buf[pos + 1] & 0xf6) !== 0xf0) {
|
|
305
|
+
if (frames === 0) {
|
|
306
|
+
pos++
|
|
307
|
+
continue
|
|
308
|
+
}
|
|
309
|
+
break
|
|
310
|
+
}
|
|
311
|
+
const sfi = (buf[pos + 2] >> 2) & 0x0f
|
|
312
|
+
const frameLength = ((buf[pos + 3] & 0x03) << 11) | (buf[pos + 4] << 3) | (buf[pos + 5] >> 5)
|
|
313
|
+
if (sfi > 12 || frameLength < 7) {
|
|
314
|
+
if (frames === 0) {
|
|
315
|
+
pos++
|
|
316
|
+
continue
|
|
317
|
+
}
|
|
318
|
+
break
|
|
319
|
+
}
|
|
320
|
+
if (sampleRate === undefined) sampleRate = ADTS_SAMPLE_RATES[sfi]
|
|
321
|
+
frames++
|
|
322
|
+
pos += frameLength
|
|
323
|
+
}
|
|
324
|
+
return frames && sampleRate ? (frames * 1024) / sampleRate : undefined
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const aiffDuration = buf => {
|
|
328
|
+
if (ascii(buf, 0, 4) !== 'FORM') return undefined
|
|
329
|
+
const kind = ascii(buf, 8, 4)
|
|
330
|
+
if (kind !== 'AIFF' && kind !== 'AIFC') return undefined
|
|
331
|
+
let pos = 12
|
|
332
|
+
while (pos + 8 <= buf.length) {
|
|
333
|
+
const id = ascii(buf, pos, 4)
|
|
334
|
+
const size = u32be(buf, pos + 4)
|
|
335
|
+
const body = pos + 8
|
|
336
|
+
if (id === 'COMM') {
|
|
337
|
+
if (size < 18 || body + size > buf.length) return undefined
|
|
338
|
+
const frames = u32be(buf, body + 2)
|
|
339
|
+
|
|
340
|
+
const exp = buf.readUInt16BE(body + 8) & 0x7fff
|
|
341
|
+
const mantissa = buf.readBigUInt64BE(body + 10)
|
|
342
|
+
const sampleRate = Number(mantissa) * 2 ** (exp - 16383 - 63)
|
|
343
|
+
return sampleRate > 0 ? frames / sampleRate : undefined
|
|
344
|
+
}
|
|
345
|
+
pos = body + size + (size & 1)
|
|
346
|
+
}
|
|
347
|
+
return undefined
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const ebmlVint = (buf, pos, keepMarker) => {
|
|
351
|
+
if (pos >= buf.length) return undefined
|
|
352
|
+
const first = buf[pos]
|
|
353
|
+
if (first === 0) return undefined
|
|
354
|
+
const len = Math.clz32(first) - 23
|
|
355
|
+
if (pos + len > buf.length) return undefined
|
|
356
|
+
let value = keepMarker ? first : first & (0xff >> len)
|
|
357
|
+
let allOnes = (first & (0xff >> len)) === 0xff >> len
|
|
358
|
+
for (let i = 1; i < len; i++) {
|
|
359
|
+
value = value * 256 + buf[pos + i]
|
|
360
|
+
if (buf[pos + i] !== 0xff) allOnes = false
|
|
361
|
+
}
|
|
362
|
+
return { value, length: len, unknown: !keepMarker && allOnes }
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const matroskaDuration = buf => {
|
|
366
|
+
const info = { timecodeScale: undefined, duration: undefined }
|
|
367
|
+
const readUInt = (o, n) => {
|
|
368
|
+
let v = 0
|
|
369
|
+
for (let i = 0; i < n; i++) v = v * 256 + buf[o + i]
|
|
370
|
+
return v
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const walk = (start, end, depth) => {
|
|
374
|
+
let pos = start
|
|
375
|
+
while (pos < end) {
|
|
376
|
+
const id = ebmlVint(buf, pos, true)
|
|
377
|
+
if (!id) return false
|
|
378
|
+
const size = ebmlVint(buf, pos + id.length, false)
|
|
379
|
+
if (!size) return false
|
|
380
|
+
const body = pos + id.length + size.length
|
|
381
|
+
const bodyEnd = size.unknown ? end : Math.min(end, body + size.value)
|
|
382
|
+
if (id.value === 0x18538067 && depth === 0) {
|
|
383
|
+
if (walk(body, bodyEnd, 1)) return true
|
|
384
|
+
} else if (id.value === 0x1549a966 && depth === 1) {
|
|
385
|
+
|
|
386
|
+
let q = body
|
|
387
|
+
while (q < bodyEnd) {
|
|
388
|
+
const cid = ebmlVint(buf, q, true)
|
|
389
|
+
const csz = cid && ebmlVint(buf, q + cid.length, false)
|
|
390
|
+
if (!csz) break
|
|
391
|
+
const cb = q + cid.length + csz.length
|
|
392
|
+
if (cb + csz.value > buf.length) break
|
|
393
|
+
if (cid.value === 0x2ad7b1) info.timecodeScale = readUInt(cb, csz.value)
|
|
394
|
+
else if (cid.value === 0x4489) info.duration = csz.value === 4 ? buf.readFloatBE(cb) : csz.value === 8 ? buf.readDoubleBE(cb) : undefined
|
|
395
|
+
q = cb + csz.value
|
|
396
|
+
}
|
|
397
|
+
return true
|
|
398
|
+
}
|
|
399
|
+
if (size.unknown) return false
|
|
400
|
+
pos = body + size.value
|
|
401
|
+
}
|
|
402
|
+
return false
|
|
403
|
+
}
|
|
404
|
+
walk(0, buf.length, 0)
|
|
405
|
+
if (typeof info.duration !== 'number' || !Number.isFinite(info.duration)) return undefined
|
|
406
|
+
return (info.duration * (info.timecodeScale || 1000000)) / 1e9
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const AMR_NB_FRAME_BYTES = [13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1]
|
|
410
|
+
const AMR_WB_FRAME_BYTES = [18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 0, 0, 0, 0, 1, 1]
|
|
411
|
+
|
|
412
|
+
const amrDuration = buf => {
|
|
413
|
+
const wb = ascii(buf, 0, 9) === '#!AMR-WB\n'
|
|
414
|
+
if (!wb && ascii(buf, 0, 6) !== '#!AMR\n') return undefined
|
|
415
|
+
const table = wb ? AMR_WB_FRAME_BYTES : AMR_NB_FRAME_BYTES
|
|
416
|
+
let pos = wb ? 9 : 6
|
|
417
|
+
let frames = 0
|
|
418
|
+
while (pos < buf.length) {
|
|
419
|
+
const size = table[(buf[pos] >> 3) & 0x0f]
|
|
420
|
+
if (!size) break
|
|
421
|
+
if (pos + size > buf.length) break
|
|
422
|
+
frames++
|
|
423
|
+
pos += size
|
|
424
|
+
}
|
|
425
|
+
return frames ? frames * 0.02 : undefined
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
export const durationFromBuffer = buf => {
|
|
429
|
+
if (!Buffer.isBuffer(buf)) buf = Buffer.from(buf)
|
|
430
|
+
if (buf.length < 12) return undefined
|
|
431
|
+
try {
|
|
432
|
+
const magic = ascii(buf, 0, 4)
|
|
433
|
+
if (magic === 'OggS') return oggDuration(buf)
|
|
434
|
+
if (magic === 'RIFF') return wavDuration(buf)
|
|
435
|
+
if (magic === 'fLaC') return flacDuration(buf)
|
|
436
|
+
if (ascii(buf, 4, 4) === 'ftyp') return mp4Duration(buf)
|
|
437
|
+
if (magic === 'FORM') return aiffDuration(buf)
|
|
438
|
+
if (buf[0] === 0x1a && buf[1] === 0x45 && buf[2] === 0xdf && buf[3] === 0xa3) return matroskaDuration(buf)
|
|
439
|
+
if (magic === '#!AM') return amrDuration(buf)
|
|
440
|
+
if (buf[0] === 0xff && (buf[1] & 0xf6) === 0xf0) return adtsDuration(buf)
|
|
441
|
+
if (magic.startsWith('ID3') || (buf[0] === 0xff && (buf[1] & 0xe0) === 0xe0)) return mp3Duration(buf)
|
|
442
|
+
} catch {
|
|
443
|
+
return undefined
|
|
444
|
+
}
|
|
445
|
+
return undefined
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export const audioDuration = async input => {
|
|
449
|
+
if (Buffer.isBuffer(input)) return durationFromBuffer(input)
|
|
450
|
+
if (typeof input === 'string') {
|
|
451
|
+
const { size } = await stat(input)
|
|
452
|
+
if (size > 64 * 1024 * 1024) {
|
|
453
|
+
|
|
454
|
+
const fh = await open(input, 'r')
|
|
455
|
+
try {
|
|
456
|
+
const head = Buffer.alloc(Math.min(size, 4 * 1024 * 1024))
|
|
457
|
+
await fh.read(head, 0, head.length, 0)
|
|
458
|
+
return durationFromBuffer(head)
|
|
459
|
+
} finally {
|
|
460
|
+
await fh.close()
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const chunks = []
|
|
464
|
+
for await (const c of createReadStream(input)) chunks.push(c)
|
|
465
|
+
return durationFromBuffer(Buffer.concat(chunks))
|
|
466
|
+
}
|
|
467
|
+
const chunks = []
|
|
468
|
+
for await (const c of input) chunks.push(c)
|
|
469
|
+
return durationFromBuffer(Buffer.concat(chunks))
|
|
470
|
+
}
|