@hiroosy/baileys-lite 0.4.3 → 0.4.5
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/WAProto/index.d.ts +4 -0
- package/WAProto/parts/Message.part +42 -0
- 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/socket/socket-core.js +3 -3
- package/src/utils/media.js +8 -85
- package/src/utils/message-compose.js +19 -11
- package/src/utils/wa-protocol-core.js +5 -5
- 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,579 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import pathMod from 'node:path';
|
|
3
|
+
import { badArg, noSupport, wrap } from './errors.js';
|
|
4
|
+
import { FileSink, BufferSink } from './io.js';
|
|
5
|
+
import { openDemuxer } from './demux/index.js';
|
|
6
|
+
import { WavMuxer } from './mux/wav.js';
|
|
7
|
+
import { FlacMuxer } from './mux/flac.js';
|
|
8
|
+
import { Mp3Muxer } from './mux/mp3.js';
|
|
9
|
+
import { AdtsMuxer } from './mux/adts.js';
|
|
10
|
+
import { OggMuxer } from './mux/ogg.js';
|
|
11
|
+
import { Mp4Muxer } from './mux/mp4.js';
|
|
12
|
+
import { canDecode, canEncode, createDecoderAsync, createEncoderAsync, prerollFrames } from './codec/index.js';
|
|
13
|
+
import { formats, isPcm, pcmCodecForBits } from './codec/pcm.js';
|
|
14
|
+
import { Resampler } from './filter/resample.js';
|
|
15
|
+
import { ChannelMixer } from './filter/mix.js';
|
|
16
|
+
import { Trim, Gain } from './filter/gain.js';
|
|
17
|
+
import { parseTime, parseGain, equalBuffers } from './util.js';
|
|
18
|
+
import * as tags from './tags.js';
|
|
19
|
+
import { runImage } from './image/run.js';
|
|
20
|
+
import { MergedDemuxer } from './demux/merge.js';
|
|
21
|
+
import { resolveInput, Temps } from './net.js';
|
|
22
|
+
import { videoCopyEquivalent, audioCopyEquivalent } from './compat.js';
|
|
23
|
+
import { outputFormatOf } from './image/index.js';
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
const formatNames = {
|
|
27
|
+
wav: ['wav', 'wav'],
|
|
28
|
+
wave: ['wav', 'wav'],
|
|
29
|
+
flac: ['flac', 'flac'],
|
|
30
|
+
mp3: ['mp3', 'mp3'],
|
|
31
|
+
aac: ['adts', 'adts'],
|
|
32
|
+
adts: ['adts', 'adts'],
|
|
33
|
+
ogg: ['ogg', 'ogg'],
|
|
34
|
+
oga: ['ogg', 'ogg'],
|
|
35
|
+
opus: ['ogg', 'ogg'],
|
|
36
|
+
mp4: ['mp4', 'mp4'],
|
|
37
|
+
m4v: ['mp4', 'mp4'],
|
|
38
|
+
m4a: ['mp4', 'm4a'],
|
|
39
|
+
mov: ['mp4', 'mov'],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
function resolveFormat(opts) {
|
|
44
|
+
let key = opts.format ? String(opts.format).toLowerCase() : null;
|
|
45
|
+
if (!key && typeof opts.output === 'string') key = pathMod.extname(opts.output).slice(1).toLowerCase();
|
|
46
|
+
if (!key || !formatNames[key]) throw badArg('cannot determine output format' + (key ? ': ' + key : ''));
|
|
47
|
+
const [name, brand] = formatNames[key];
|
|
48
|
+
return { name, brand, Muxer: { wav: WavMuxer, flac: FlacMuxer, mp3: Mp3Muxer, adts: AdtsMuxer, ogg: OggMuxer, mp4: Mp4Muxer }[name] };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function numberOpt(value, name, min) {
|
|
52
|
+
if (value === undefined || value === null) return undefined;
|
|
53
|
+
const n = typeof value === 'number' ? value : Number(value);
|
|
54
|
+
if (!Number.isFinite(n) || n < min) throw badArg('invalid option: ' + name);
|
|
55
|
+
return n;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function normalize(options) {
|
|
59
|
+
if (!options || typeof options !== 'object') throw badArg('options object required');
|
|
60
|
+
const o = options;
|
|
61
|
+
let inputs = o.inputs !== undefined ? o.inputs : o.input;
|
|
62
|
+
if (inputs === undefined || inputs === null) throw badArg('input required');
|
|
63
|
+
if (!Array.isArray(inputs)) inputs = [inputs];
|
|
64
|
+
if (!inputs.length) throw badArg('input required');
|
|
65
|
+
const start = parseTime(o.start !== undefined ? o.start : o.ss);
|
|
66
|
+
const endRaw = parseTime(o.end !== undefined ? o.end : o.to);
|
|
67
|
+
const dur = parseTime(o.duration !== undefined ? o.duration : o.t);
|
|
68
|
+
if ((start !== undefined && !(start >= 0)) || (endRaw !== undefined && !(endRaw >= 0)) || (dur !== undefined && !(dur >= 0))) throw badArg('invalid time option');
|
|
69
|
+
const startSec = start || 0;
|
|
70
|
+
let endSec = Infinity;
|
|
71
|
+
if (endRaw !== undefined) endSec = endRaw;
|
|
72
|
+
if (dur !== undefined) endSec = Math.min(endSec, startSec + dur);
|
|
73
|
+
if (endSec <= startSec && Number.isFinite(endSec)) throw badArg('end must be greater than start');
|
|
74
|
+
const volume = parseGain(o.volume);
|
|
75
|
+
if (!Number.isFinite(volume) || volume < 0) throw badArg('invalid volume');
|
|
76
|
+
const out = {
|
|
77
|
+
inputs,
|
|
78
|
+
output: o.output,
|
|
79
|
+
format: o.format || (o.output && typeof o.output === 'object' ? o.output.format : undefined),
|
|
80
|
+
codec: o.codec || o.audioCodec,
|
|
81
|
+
videoCodec: o.videoCodec,
|
|
82
|
+
startSec,
|
|
83
|
+
endSec,
|
|
84
|
+
sampleRate: numberOpt(o.sampleRate !== undefined ? o.sampleRate : o.ar, 'sampleRate', 1),
|
|
85
|
+
channels: numberOpt(o.channels !== undefined ? o.channels : o.ac, 'channels', 1),
|
|
86
|
+
bitDepth: numberOpt(o.bitDepth, 'bitDepth', 4),
|
|
87
|
+
bitrate: numberOpt(o.bitrate, 'bitrate', 1),
|
|
88
|
+
volume,
|
|
89
|
+
fadeIn: numberOpt(o.fadeIn, 'fadeIn', 0) || 0,
|
|
90
|
+
fadeOut: numberOpt(o.fadeOut, 'fadeOut', 0) || 0,
|
|
91
|
+
noAudio: Boolean(o.noAudio || o.an),
|
|
92
|
+
noVideo: Boolean(o.noVideo || o.vn),
|
|
93
|
+
metadata: tags.clean(o.metadata),
|
|
94
|
+
stripMetadata: Boolean(o.stripMetadata),
|
|
95
|
+
faststart: Boolean(o.faststart),
|
|
96
|
+
overwrite: o.overwrite === undefined ? true : Boolean(o.overwrite),
|
|
97
|
+
streams: Array.isArray(o.streams) ? o.streams : null,
|
|
98
|
+
videoFilter: o.videoFilter !== undefined ? o.videoFilter : o.vf,
|
|
99
|
+
frames: numberOpt(o.frames !== undefined ? o.frames : o.vframes, 'frames', 1),
|
|
100
|
+
qscale: numberOpt(o.qscale, 'qscale', 0),
|
|
101
|
+
quality: numberOpt(o.quality, 'quality', 1),
|
|
102
|
+
compressionLevel: numberOpt(o.compressionLevel, 'compressionLevel', 0),
|
|
103
|
+
pixFmt: o.pixFmt,
|
|
104
|
+
merge: Boolean(o.merge),
|
|
105
|
+
maps: Array.isArray(o.maps) ? o.maps : [],
|
|
106
|
+
shortest: Boolean(o.shortest),
|
|
107
|
+
net: o.net && typeof o.net === 'object' ? o.net : {},
|
|
108
|
+
smartCopy: Boolean(o.smartCopy),
|
|
109
|
+
compat: Boolean(o.compat),
|
|
110
|
+
maxPixels: numberOpt(o.maxPixels, 'maxPixels', 1),
|
|
111
|
+
sizeHint: numberOpt(o.sizeHint, 'sizeHint', 0),
|
|
112
|
+
};
|
|
113
|
+
if (out.quality !== undefined && out.quality > 100) throw badArg('quality must be from 1 to 100');
|
|
114
|
+
if (out.qscale !== undefined && out.qscale > 31) throw badArg('qscale must be from 0 to 31');
|
|
115
|
+
if (out.channels !== undefined && (out.channels > 8 || !Number.isInteger(out.channels))) throw badArg('channels must be an integer from 1 to 8');
|
|
116
|
+
if (out.sampleRate !== undefined && !Number.isInteger(out.sampleRate)) throw badArg('sampleRate must be an integer');
|
|
117
|
+
if (o.output !== undefined && o.output !== null && typeof o.output !== 'string' && typeof o.output !== 'object') throw badArg('invalid output');
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
class InputSet {
|
|
122
|
+
constructor(demuxers) {
|
|
123
|
+
this.demuxers = demuxers;
|
|
124
|
+
this.streams = demuxers[0].streams;
|
|
125
|
+
this.cur = 0;
|
|
126
|
+
this.maps = demuxers.map((d, k) => {
|
|
127
|
+
const map = new Map();
|
|
128
|
+
if (k === 0) {
|
|
129
|
+
for (const s of d.streams) map.set(s.index, s);
|
|
130
|
+
return map;
|
|
131
|
+
}
|
|
132
|
+
for (const type of ['audio', 'video']) {
|
|
133
|
+
const a = this.streams.find((s) => s.type === type);
|
|
134
|
+
const b = d.streams.find((s) => s.type === type);
|
|
135
|
+
if (a && b) map.set(b.index, { first: a, stream: b });
|
|
136
|
+
}
|
|
137
|
+
return map;
|
|
138
|
+
});
|
|
139
|
+
this.lastEnd = new Map();
|
|
140
|
+
this.offset = new Map();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
streamOf(seg, index) {
|
|
144
|
+
const m = this.maps[seg].get(index);
|
|
145
|
+
if (!m) return null;
|
|
146
|
+
return seg === 0 ? m : m.stream;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
matchFor(seg, firstStream) {
|
|
150
|
+
for (const m of this.maps[seg].values()) if (m.first === firstStream) return m.stream;
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async readPacket() {
|
|
155
|
+
for (;;) {
|
|
156
|
+
const d = this.demuxers[this.cur];
|
|
157
|
+
const pkt = await d.readPacket();
|
|
158
|
+
if (!pkt) {
|
|
159
|
+
if (++this.cur >= this.demuxers.length) return null;
|
|
160
|
+
for (const [idx, end] of this.lastEnd) this.offset.set(idx, end);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
let target = pkt.stream;
|
|
164
|
+
pkt.pts0 = pkt.pts;
|
|
165
|
+
if (this.cur > 0) {
|
|
166
|
+
const m = this.maps[this.cur].get(pkt.stream);
|
|
167
|
+
if (!m) continue;
|
|
168
|
+
target = m.first.index;
|
|
169
|
+
const off = this.offset.get(target) || 0;
|
|
170
|
+
pkt.pts += off;
|
|
171
|
+
pkt.dts += off;
|
|
172
|
+
pkt.stream = target;
|
|
173
|
+
}
|
|
174
|
+
const end = pkt.pts + (pkt.duration || 0);
|
|
175
|
+
if (end > (this.lastEnd.get(target) || 0)) this.lastEnd.set(target, end);
|
|
176
|
+
pkt.seg = this.cur;
|
|
177
|
+
return pkt;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function hasFilters(o) {
|
|
183
|
+
return o.sampleRate !== undefined || o.channels !== undefined || o.volume !== 1 || o.fadeIn > 0 || o.fadeOut > 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function defaultBits(stream, opts) {
|
|
187
|
+
if (opts.bitDepth) return opts.bitDepth;
|
|
188
|
+
const codec = stream.codec;
|
|
189
|
+
if (codec === 'flac') return Math.min(24, stream.bitsPerSample || 16);
|
|
190
|
+
if (isPcm(codec)) {
|
|
191
|
+
const info = formats[codec];
|
|
192
|
+
if (info.float) return 24;
|
|
193
|
+
if (codec === 'pcm_alaw' || codec === 'pcm_mulaw') return 16;
|
|
194
|
+
return Math.min(24, info.bits);
|
|
195
|
+
}
|
|
196
|
+
return 16;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function targetCodec(stream, opts, fmt) {
|
|
200
|
+
if (opts.codec && opts.codec !== 'copy') return opts.codec === 'libopus' ? 'opus' : opts.codec;
|
|
201
|
+
if (fmt.name === 'wav') return isPcm(stream.codec) ? stream.codec : pcmCodecForBits(defaultBits(stream, opts), false);
|
|
202
|
+
if (fmt.name === 'flac') return 'flac';
|
|
203
|
+
if (fmt.name === 'ogg') return 'opus';
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function streamsCompatible(a, b) {
|
|
208
|
+
if (a.type !== b.type || a.codec !== b.codec) return false;
|
|
209
|
+
if (a.type === 'audio') return a.channels === b.channels && a.sampleRate === b.sampleRate && equalBuffers(a.extradata, b.extradata);
|
|
210
|
+
return a.width === b.width && a.height === b.height && equalBuffers(a.extradata, b.extradata);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
class Chain {
|
|
214
|
+
static async create(stream, outRate, outCh) {
|
|
215
|
+
return new Chain(await createDecoderAsync(stream), stream, outRate, outCh);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
constructor(decoder, stream, outRate, outCh) {
|
|
219
|
+
this.decoder = decoder;
|
|
220
|
+
this.mixer = stream.channels !== outCh ? new ChannelMixer(stream.channels, outCh) : null;
|
|
221
|
+
this.resampler = stream.sampleRate !== outRate ? new Resampler(stream.sampleRate, outRate, outCh) : null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
push(pkt) {
|
|
225
|
+
const dec = this.decoder.decode(pkt.data, pkt);
|
|
226
|
+
const pts = dec.pts !== undefined ? dec.pts : pkt.pts;
|
|
227
|
+
if (dec.frames === 0) return { data: dec.data, frames: 0, pts };
|
|
228
|
+
let cur = dec;
|
|
229
|
+
if (this.mixer) cur = this.mixer.process(cur.data, cur.frames);
|
|
230
|
+
if (this.resampler) cur = this.resampler.process(cur.data, cur.frames);
|
|
231
|
+
return { data: cur.data, frames: cur.frames, pts };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
flush() {
|
|
235
|
+
return this.resampler ? this.resampler.flush() : null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
dispose() {
|
|
239
|
+
this.decoder.dispose?.();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
class AudioTranscode {
|
|
244
|
+
static async create(cfg) {
|
|
245
|
+
return new AudioTranscode(cfg, await createEncoderAsync(cfg.codec, { sampleRate: cfg.outRate, channels: cfg.outCh, bitsPerSample: cfg.bits, bitrate: cfg.bitrate }));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
constructor(cfg, encoder) {
|
|
249
|
+
this.cfg = cfg;
|
|
250
|
+
this.encoder = encoder;
|
|
251
|
+
this.descriptor = this.encoder.stream;
|
|
252
|
+
this.trim = null;
|
|
253
|
+
this.gain = new Gain({ volume: cfg.volume, fadeInFrames: Math.round(cfg.fadeIn * cfg.outRate), fadeOutFrames: Math.round(cfg.fadeOut * cfg.outRate), totalFrames: cfg.totalFrames }, cfg.outCh);
|
|
254
|
+
this.chain = null;
|
|
255
|
+
this.seg = -1;
|
|
256
|
+
this.started = false;
|
|
257
|
+
this.written = 0;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
get done() {
|
|
261
|
+
return Boolean(this.trim && this.trim.done);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async emit(cur) {
|
|
265
|
+
if (!cur || cur.frames === 0) return;
|
|
266
|
+
const r = this.trim.process(cur.data, cur.frames, this.cfg.outCh);
|
|
267
|
+
if (!r) return;
|
|
268
|
+
this.gain.process(r.data, r.frames);
|
|
269
|
+
await this.cfg.write(this.encoder.encode(r.data, r.frames));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async push(pkt, stream, seg) {
|
|
273
|
+
if (this.done) return;
|
|
274
|
+
const cfg = this.cfg;
|
|
275
|
+
if (!this.started && cfg.canSkip) {
|
|
276
|
+
const pre = prerollFrames(stream.codec) * (pkt.duration || 0);
|
|
277
|
+
if (pkt.pts + pkt.duration <= cfg.startSec * stream.timeScale - pre - 1e-6) return;
|
|
278
|
+
}
|
|
279
|
+
if (seg !== this.seg) {
|
|
280
|
+
await this.endChain();
|
|
281
|
+
this.chain = await Chain.create(stream, cfg.outRate, cfg.outCh);
|
|
282
|
+
this.seg = seg;
|
|
283
|
+
}
|
|
284
|
+
const cur = this.chain.push(pkt);
|
|
285
|
+
if (!this.started) {
|
|
286
|
+
if (cur.frames === 0) return;
|
|
287
|
+
const t0 = cur.pts / stream.timeScale;
|
|
288
|
+
const skip = Math.round(Math.max(0, cfg.startSec - Math.max(0, t0)) * cfg.outRate);
|
|
289
|
+
const limit = Number.isFinite(cfg.endSec) ? Math.round((cfg.endSec - cfg.startSec) * cfg.outRate) : undefined;
|
|
290
|
+
this.trim = new Trim(skip, limit);
|
|
291
|
+
this.started = true;
|
|
292
|
+
}
|
|
293
|
+
await this.emit(cur);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async endChain() {
|
|
297
|
+
if (!this.chain) return;
|
|
298
|
+
const tail = this.chain.flush();
|
|
299
|
+
this.chain.dispose();
|
|
300
|
+
this.chain = null;
|
|
301
|
+
if (this.started) await this.emit(tail);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
async finish() {
|
|
305
|
+
await this.endChain();
|
|
306
|
+
await this.cfg.write(this.encoder.flush());
|
|
307
|
+
this.encoder.dispose?.();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async function maybeYield(state) {
|
|
312
|
+
const now = Date.now();
|
|
313
|
+
if (now - state.last >= 20) {
|
|
314
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
315
|
+
state.last = Date.now();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function imageFormatFor(opts) {
|
|
320
|
+
let key = opts.format ? String(opts.format).toLowerCase() : null;
|
|
321
|
+
if (!key && typeof opts.output === 'string') key = pathMod.extname(opts.output).slice(1).toLowerCase();
|
|
322
|
+
if (key === 'image2' || key === 'image2pipe') key = typeof opts.output === 'string' ? pathMod.extname(opts.output).slice(1).toLowerCase() : null;
|
|
323
|
+
return outputFormatOf(key);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async function run(options) {
|
|
327
|
+
const opts = normalize(options);
|
|
328
|
+
const imageFormat = imageFormatFor(opts);
|
|
329
|
+
if (imageFormat) {
|
|
330
|
+
try {
|
|
331
|
+
return await runImage(opts, imageFormat);
|
|
332
|
+
} catch (e) {
|
|
333
|
+
throw wrap(e);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const fmt = resolveFormat(opts);
|
|
337
|
+
const toFile = typeof opts.output === 'string';
|
|
338
|
+
const demuxers = [];
|
|
339
|
+
const temps = new Temps();
|
|
340
|
+
let sink = null;
|
|
341
|
+
let created = false;
|
|
342
|
+
try {
|
|
343
|
+
if (toFile) {
|
|
344
|
+
const abs = pathMod.resolve(opts.output);
|
|
345
|
+
for (const src of opts.inputs) {
|
|
346
|
+
if (typeof src === 'string' && pathMod.resolve(src) === abs) throw badArg('output must differ from input');
|
|
347
|
+
}
|
|
348
|
+
if (!opts.overwrite && fs.existsSync(abs)) throw badArg('output exists: ' + opts.output);
|
|
349
|
+
}
|
|
350
|
+
const groups = [];
|
|
351
|
+
for (const src of opts.inputs) groups.push(await resolveInput(src, opts.net, temps));
|
|
352
|
+
const flat = groups.flat();
|
|
353
|
+
if (!opts.merge && opts.inputs.length > 1 && flat.length !== opts.inputs.length) throw noSupport('inputs expanded into multiple streams cannot be concatenated');
|
|
354
|
+
const opened = [];
|
|
355
|
+
for (const src of flat) opened.push(await openDemuxer(src));
|
|
356
|
+
if (opts.merge || opts.maps.length || flat.length !== opts.inputs.length) demuxers.push(new MergedDemuxer(opened, opts.maps, { shortest: opts.shortest }));
|
|
357
|
+
else demuxers.push(...opened);
|
|
358
|
+
const set = new InputSet(demuxers);
|
|
359
|
+
const first = demuxers[0];
|
|
360
|
+
const single = demuxers.length === 1;
|
|
361
|
+
let selected;
|
|
362
|
+
if (opts.streams) {
|
|
363
|
+
selected = opts.streams.map((i) => first.streams.find((s) => s.index === i)).filter(Boolean);
|
|
364
|
+
} else {
|
|
365
|
+
selected = [];
|
|
366
|
+
const video = first.streams.find((s) => s.type === 'video');
|
|
367
|
+
const audio = first.streams.find((s) => s.type === 'audio');
|
|
368
|
+
if (!opts.noVideo && video && fmt.Muxer === Mp4Muxer) selected.push(video);
|
|
369
|
+
if (!opts.noAudio && audio) selected.push(audio);
|
|
370
|
+
selected.sort((a, b) => a.index - b.index);
|
|
371
|
+
}
|
|
372
|
+
if (!selected.length) throw badArg('no streams to process');
|
|
373
|
+
|
|
374
|
+
const plans = selected.map((s) => planStream(s, opts, fmt, single));
|
|
375
|
+
plans.forEach((p, i) => {
|
|
376
|
+
p.outIndex = i;
|
|
377
|
+
});
|
|
378
|
+
for (let k = 1; k < demuxers.length; k++) {
|
|
379
|
+
for (let i = 0; i < plans.length; i++) {
|
|
380
|
+
const match = set.matchFor(k, plans[i].stream);
|
|
381
|
+
if (!match) throw badArg('input ' + k + ' lacks a matching ' + plans[i].stream.type + ' stream');
|
|
382
|
+
if (plans[i].mode === 'copy' && !streamsCompatible(plans[i].stream, match)) {
|
|
383
|
+
if (opts.codec === 'copy' || plans[i].stream.type === 'video') throw badArg('inputs have incompatible ' + plans[i].stream.type + ' streams for stream copy');
|
|
384
|
+
const outIndex = plans[i].outIndex;
|
|
385
|
+
plans[i] = planStream(plans[i].stream, opts, fmt, single, true);
|
|
386
|
+
plans[i].outIndex = outIndex;
|
|
387
|
+
}
|
|
388
|
+
if (plans[i].mode === 'transcode' && !canDecode(match.codec)) throw noSupport('no decoder available for codec ' + match.codec);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
let totalDuration;
|
|
393
|
+
if (demuxers.every((d) => d.duration > 0)) totalDuration = demuxers.reduce((n, d) => n + d.duration, 0);
|
|
394
|
+
for (const p of plans) {
|
|
395
|
+
if (p.mode !== 'transcode') continue;
|
|
396
|
+
const rate = p.cfg.outRate;
|
|
397
|
+
const end = Math.min(opts.endSec, totalDuration === undefined ? Infinity : totalDuration);
|
|
398
|
+
if (Number.isFinite(end)) p.cfg.totalFrames = Math.max(0, Math.round((end - opts.startSec) * rate));
|
|
399
|
+
if (opts.fadeOut > 0 && !p.cfg.totalFrames) throw badArg('fadeOut requires a known duration');
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const outTags = opts.stripMetadata ? {} : Object.assign({}, first.tags);
|
|
403
|
+
Object.assign(outTags, opts.metadata);
|
|
404
|
+
|
|
405
|
+
const state = { last: Date.now() };
|
|
406
|
+
const outStreams = [];
|
|
407
|
+
for (const p of plans) {
|
|
408
|
+
if (p.mode === 'copy') {
|
|
409
|
+
const d = Object.assign({}, p.stream, { index: p.outIndex });
|
|
410
|
+
if (opts.startSec > 0) {
|
|
411
|
+
d.encoderDelay = 0;
|
|
412
|
+
d.encoderPadding = 0;
|
|
413
|
+
}
|
|
414
|
+
outStreams.push(d);
|
|
415
|
+
p.rate = p.stream.timeScale;
|
|
416
|
+
} else {
|
|
417
|
+
p.audio = await AudioTranscode.create(Object.assign({}, p.cfg, { startSec: opts.startSec, endSec: opts.endSec, canSkip: single }));
|
|
418
|
+
const d = Object.assign({}, p.audio.descriptor, { index: p.outIndex });
|
|
419
|
+
if (!fmt.Muxer.accepts(d)) throw noSupport('output format ' + fmt.name + ' cannot hold codec ' + d.codec);
|
|
420
|
+
outStreams.push(d);
|
|
421
|
+
p.rate = d.timeScale;
|
|
422
|
+
}
|
|
423
|
+
p.maxEnd = 0;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (toFile) {
|
|
427
|
+
sink = await FileSink.open(pathMod.resolve(opts.output));
|
|
428
|
+
created = true;
|
|
429
|
+
} else sink = new BufferSink(opts.sizeHint);
|
|
430
|
+
const muxer = new fmt.Muxer(sink, { tags: outTags, faststart: opts.faststart, brand: fmt.brand });
|
|
431
|
+
await muxer.open(outStreams);
|
|
432
|
+
|
|
433
|
+
const byInput = new Map(plans.map((p) => [p.stream.index, p]));
|
|
434
|
+
for (const p of plans) {
|
|
435
|
+
if (p.mode === 'transcode') {
|
|
436
|
+
p.audio.cfg.write = async (packets) => {
|
|
437
|
+
for (const pk of packets) {
|
|
438
|
+
const end = (pk.pts + pk.duration) / p.rate;
|
|
439
|
+
if (end > p.maxEnd) p.maxEnd = end;
|
|
440
|
+
await muxer.writePacket(p.outIndex, pk);
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
let origin = opts.startSec === 0 ? 0 : undefined;
|
|
447
|
+
let seeked = false;
|
|
448
|
+
if (single && opts.startSec > 0 && typeof first.seek === 'function') {
|
|
449
|
+
origin = await first.seek(opts.startSec);
|
|
450
|
+
seeked = true;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
for (;;) {
|
|
454
|
+
const pkt = await set.readPacket();
|
|
455
|
+
if (!pkt) break;
|
|
456
|
+
const p = byInput.get(pkt.stream);
|
|
457
|
+
if (!p) continue;
|
|
458
|
+
if (p.mode === 'copy') {
|
|
459
|
+
if (p.done) continue;
|
|
460
|
+
const ts = p.stream.timeScale;
|
|
461
|
+
const ptsSec = pkt.pts / ts;
|
|
462
|
+
if (opts.startSec > 0) {
|
|
463
|
+
if (seeked) {
|
|
464
|
+
if (ptsSec < origin - 1e-6) continue;
|
|
465
|
+
} else if (ptsSec < opts.startSec - 1e-6) continue;
|
|
466
|
+
else if (origin === undefined) origin = ptsSec;
|
|
467
|
+
}
|
|
468
|
+
if (ptsSec >= opts.endSec - 1e-9) {
|
|
469
|
+
p.done = true;
|
|
470
|
+
if (plans.every((x) => (x.mode === 'copy' ? x.done : x.audio.done))) break;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
if (origin) {
|
|
474
|
+
const shift = Math.round(origin * ts);
|
|
475
|
+
pkt.pts -= shift;
|
|
476
|
+
pkt.dts -= shift;
|
|
477
|
+
}
|
|
478
|
+
const end = (pkt.pts + pkt.duration) / ts;
|
|
479
|
+
if (end > p.maxEnd) p.maxEnd = end;
|
|
480
|
+
await muxer.writePacket(p.outIndex, pkt);
|
|
481
|
+
} else {
|
|
482
|
+
if (p.audio.done) {
|
|
483
|
+
if (plans.every((x) => (x.mode === 'copy' ? x.done : x.audio.done))) break;
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
const stream = set.streamOf(pkt.seg, pkt.stream);
|
|
487
|
+
await p.audio.push(pkt, stream, pkt.seg);
|
|
488
|
+
}
|
|
489
|
+
await maybeYield(state);
|
|
490
|
+
}
|
|
491
|
+
for (const p of plans) if (p.mode === 'transcode') await p.audio.finish();
|
|
492
|
+
await muxer.finish();
|
|
493
|
+
await sink.flush();
|
|
494
|
+
const size = sink.length;
|
|
495
|
+
const duration = Math.max(0, ...plans.map((p) => p.maxEnd));
|
|
496
|
+
const result = { format: fmt.brand === 'mp4' ? fmt.name : fmt.brand, size, duration, streams: outStreams.map((s) => ({ index: s.index, type: s.type, codec: s.codec })) };
|
|
497
|
+
if (toFile) {
|
|
498
|
+
await sink.close();
|
|
499
|
+
sink = null;
|
|
500
|
+
result.output = opts.output;
|
|
501
|
+
} else {
|
|
502
|
+
result.output = sink.result();
|
|
503
|
+
await sink.close();
|
|
504
|
+
sink = null;
|
|
505
|
+
}
|
|
506
|
+
return result;
|
|
507
|
+
} catch (e) {
|
|
508
|
+
if (sink) {
|
|
509
|
+
try {
|
|
510
|
+
await sink.close();
|
|
511
|
+
} catch (ignored) {
|
|
512
|
+
sink = null;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
if (created) {
|
|
516
|
+
try {
|
|
517
|
+
await fs.promises.unlink(pathMod.resolve(opts.output));
|
|
518
|
+
} catch (ignored) {
|
|
519
|
+
created = false;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
throw wrap(e);
|
|
523
|
+
} finally {
|
|
524
|
+
for (const d of demuxers) {
|
|
525
|
+
try {
|
|
526
|
+
await d.close();
|
|
527
|
+
} catch (ignored) {
|
|
528
|
+
d.closed = true;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
await temps.cleanup();
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function planStream(s, opts, fmt, single, forceTranscode) {
|
|
536
|
+
const Muxer = fmt.Muxer;
|
|
537
|
+
if (s.type === 'video') {
|
|
538
|
+
if (opts.videoCodec && opts.videoCodec !== 'copy' && !videoCopyEquivalent(s, opts)) throw noSupport('video transcoding is not supported; use stream copy');
|
|
539
|
+
if (!Muxer.accepts(s)) throw noSupport('output format ' + fmt.name + ' cannot hold video codec ' + s.codec);
|
|
540
|
+
return { mode: 'copy', stream: s, done: false };
|
|
541
|
+
}
|
|
542
|
+
const filters = hasFilters(opts);
|
|
543
|
+
const explicit = opts.codec && opts.codec !== 'copy';
|
|
544
|
+
const trimmed = opts.startSec > 0 || Number.isFinite(opts.endSec);
|
|
545
|
+
if (opts.codec === 'copy') {
|
|
546
|
+
if (filters) throw badArg('filters cannot be combined with stream copy');
|
|
547
|
+
if (!Muxer.accepts(s)) throw noSupport('output format ' + fmt.name + ' cannot hold codec ' + s.codec);
|
|
548
|
+
return { mode: 'copy', stream: s, done: false };
|
|
549
|
+
}
|
|
550
|
+
if (explicit && audioCopyEquivalent(s, opts) && Muxer.accepts(s) && !trimmed) return { mode: 'copy', stream: s, done: false };
|
|
551
|
+
const sampleAccurate = isPcm(s.codec) && trimmed;
|
|
552
|
+
if (!forceTranscode && !explicit && !filters && !sampleAccurate && Muxer.accepts(s)) return { mode: 'copy', stream: s, done: false };
|
|
553
|
+
const target = targetCodec(s, opts, fmt);
|
|
554
|
+
if (!target) throw noSupport('cannot convert ' + s.codec + ' to ' + fmt.name + ': no encoder available');
|
|
555
|
+
if (!canDecode(s.codec)) throw noSupport('no decoder available for codec ' + s.codec);
|
|
556
|
+
if (!canEncode(target)) throw noSupport('no encoder available for codec ' + target);
|
|
557
|
+
const outRate = opts.sampleRate || (target === 'opus' ? 48000 : s.sampleRate);
|
|
558
|
+
const outCh = opts.channels || s.channels;
|
|
559
|
+
if (target === 'opus' && (![8000, 12000, 16000, 24000, 48000].includes(outRate) || outCh > 2)) throw badArg('opus output needs a sample rate of 8000, 12000, 16000, 24000 or 48000 Hz and 1 or 2 channels');
|
|
560
|
+
return {
|
|
561
|
+
mode: 'transcode',
|
|
562
|
+
stream: s,
|
|
563
|
+
cfg: {
|
|
564
|
+
codec: target,
|
|
565
|
+
outRate,
|
|
566
|
+
outCh,
|
|
567
|
+
bits: defaultBits(s, opts),
|
|
568
|
+
bitrate: opts.bitrate,
|
|
569
|
+
volume: opts.volume,
|
|
570
|
+
fadeIn: opts.fadeIn,
|
|
571
|
+
fadeOut: opts.fadeOut,
|
|
572
|
+
totalFrames: 0,
|
|
573
|
+
},
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
export {
|
|
578
|
+
run,
|
|
579
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { openDemuxer } from './demux/index.js';
|
|
2
|
+
import { wrap } from './errors.js';
|
|
3
|
+
|
|
4
|
+
function publicStream(s) {
|
|
5
|
+
const out = { index: s.index, type: s.type, codec: s.codec };
|
|
6
|
+
const keys = ['sampleRate', 'channels', 'bitsPerSample', 'bitRate', 'duration', 'width', 'height', 'frameRate', 'timeScale'];
|
|
7
|
+
for (const k of keys) if (s[k] !== undefined && s[k] !== null && !Number.isNaN(s[k])) out[k] = s[k];
|
|
8
|
+
return out;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function brandName(demuxer) {
|
|
12
|
+
if (demuxer.format !== 'mp4') return demuxer.format;
|
|
13
|
+
const b = (demuxer.brand || '').trim();
|
|
14
|
+
if (b === 'qt') return 'mov';
|
|
15
|
+
if (b === 'M4A') return 'm4a';
|
|
16
|
+
return 'mp4';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function probe(input) {
|
|
20
|
+
const demuxer = await openDemuxer(input);
|
|
21
|
+
try {
|
|
22
|
+
if (typeof demuxer.scanDuration === 'function') await demuxer.scanDuration();
|
|
23
|
+
const duration = demuxer.duration;
|
|
24
|
+
const size = demuxer.size;
|
|
25
|
+
return {
|
|
26
|
+
format: {
|
|
27
|
+
name: brandName(demuxer),
|
|
28
|
+
size,
|
|
29
|
+
duration,
|
|
30
|
+
bitRate: duration > 0 ? Math.round((size * 8) / duration) : undefined,
|
|
31
|
+
tags: Object.assign({}, demuxer.tags),
|
|
32
|
+
},
|
|
33
|
+
streams: demuxer.streams.map(publicStream),
|
|
34
|
+
};
|
|
35
|
+
} catch (e) {
|
|
36
|
+
throw wrap(e);
|
|
37
|
+
} finally {
|
|
38
|
+
await demuxer.close();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export {
|
|
43
|
+
probe,
|
|
44
|
+
brandName,
|
|
45
|
+
};
|