nodejs-audio-visualizer 4.4.12 → 4.5.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/LICENSE +21 -21
- package/README.md +80 -75
- package/dist/audio.d.ts +12 -12
- package/dist/audio.js +120 -120
- package/dist/beats.d.ts +19 -19
- package/dist/beats.js +43 -43
- package/dist/bpmEncoder.d.ts +14 -14
- package/dist/bpmEncoder.js +97 -97
- package/dist/config.d.ts +77 -67
- package/dist/config.js +369 -356
- package/dist/config.js.map +1 -1
- package/dist/dsp.d.ts +16 -16
- package/dist/dsp.js +76 -76
- package/dist/highlight.d.ts +32 -32
- package/dist/highlight.js +325 -325
- package/dist/image.d.ts +79 -79
- package/dist/image.js +458 -458
- package/dist/index.d.ts +79 -74
- package/dist/index.js +531 -528
- package/dist/index.js.map +1 -1
- package/dist/subtitleConvert.d.ts +3 -3
- package/dist/subtitleConvert.js +61 -61
- package/dist/video.d.ts +80 -80
- package/dist/video.js +434 -430
- package/dist/video.js.map +1 -1
- package/dist/waitForEventLoop.d.ts +1 -1
- package/dist/waitForEventLoop.js +5 -5
- package/package.json +61 -61
- package/src/audio.ts +120 -120
- package/src/beats.ts +68 -68
- package/src/bpmEncoder.ts +97 -97
- package/src/config.ts +476 -462
- package/src/dsp.ts +95 -95
- package/src/highlight.ts +284 -284
- package/src/image.ts +602 -602
- package/src/index.ts +713 -685
- package/src/subtitleConvert.ts +63 -63
- package/src/tests/audio.test.ts +96 -96
- package/src/tests/config.test.ts +496 -475
- package/src/tests/dsp.test.ts +24 -24
- package/src/tests/highlight.test.ts +178 -178
- package/src/tests/image.test.ts +222 -222
- package/src/tests/index.test.ts +112 -112
- package/src/tests/subtitleConvert.test.ts +23 -23
- package/src/tests/video.test.ts +285 -216
- package/src/typings.d.ts +6 -6
- package/src/video.ts +550 -547
- package/src/waitForEventLoop.ts +2 -2
package/src/video.ts
CHANGED
|
@@ -1,547 +1,550 @@
|
|
|
1
|
-
import { Readable, Writable } from 'stream';
|
|
2
|
-
import { spawn } from 'child_process';
|
|
3
|
-
import { writeFileSync, unlinkSync } from 'fs';
|
|
4
|
-
import { resolve as resolvePath, join as joinPath } from 'path';
|
|
5
|
-
import { tmpdir } from 'os';
|
|
6
|
-
import ffmpegPath from 'ffmpeg-static';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
.replace(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
'-
|
|
61
|
-
'-
|
|
62
|
-
'-
|
|
63
|
-
'-
|
|
64
|
-
'-
|
|
65
|
-
'-
|
|
66
|
-
'-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
sourceWidth
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
cropW = makeEven(
|
|
181
|
-
cropH = makeEven(
|
|
182
|
-
cropX = Math.floor((sourceWidth - cropW) / 2);
|
|
183
|
-
cropY = Math.floor((sourceHeight - cropH) / 2);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
)
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
if (timer) {
|
|
236
|
-
clearTimeout(timer);
|
|
237
|
-
timer = undefined;
|
|
238
|
-
}
|
|
239
|
-
stream.removeListener('readable', onReadable);
|
|
240
|
-
stream.removeListener('end', onEnd);
|
|
241
|
-
stream.removeListener('error', onErrorOrClose);
|
|
242
|
-
stream.removeListener('close', onErrorOrClose);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
};
|
|
251
|
-
const onErrorOrClose = () => {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
stream.removeListener('
|
|
311
|
-
stream.removeListener('
|
|
312
|
-
|
|
313
|
-
processStream?.removeListener('
|
|
314
|
-
processStream?.removeListener('
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
stream.once('
|
|
334
|
-
stream.once('
|
|
335
|
-
|
|
336
|
-
processStream?.once('
|
|
337
|
-
processStream?.once('
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
processStream.removeListener('
|
|
368
|
-
processStream.removeListener('
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
exitCode,
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
exitCode,
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
processStream.once('
|
|
403
|
-
processStream.once('
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
let
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
const
|
|
489
|
-
|
|
490
|
-
content += `
|
|
491
|
-
content += `
|
|
492
|
-
content += `
|
|
493
|
-
remaining -= take;
|
|
494
|
-
pos = 0;
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
const concatPath = joinPath(tmpdir(), `av-concat-${Date.now()}.txt`);
|
|
499
|
-
writeFileSync(concatPath, content, 'utf-8');
|
|
500
|
-
return concatPath;
|
|
501
|
-
};
|
|
502
|
-
|
|
503
|
-
export const writeSubtitlesFile = (subtitles: string): string => {
|
|
504
|
-
const subtitlePath = joinPath(tmpdir(), `av-subtitles-${Date.now()}.srt`);
|
|
505
|
-
writeFileSync(subtitlePath, subtitles, 'utf-8');
|
|
506
|
-
return subtitlePath;
|
|
507
|
-
};
|
|
508
|
-
|
|
509
|
-
export const spawnConcatVideoFrameReader = (config: {
|
|
510
|
-
concatFilePath: string;
|
|
511
|
-
fps: number;
|
|
512
|
-
totalFrames: number;
|
|
513
|
-
width?: number;
|
|
514
|
-
height?: number;
|
|
515
|
-
sourceWidth?: number;
|
|
516
|
-
sourceHeight?: number;
|
|
517
|
-
}) => {
|
|
518
|
-
if (!ffmpegPath) {
|
|
519
|
-
throw new Error('ffmpeg path not found');
|
|
520
|
-
}
|
|
521
|
-
const args = [
|
|
522
|
-
'-f', 'concat',
|
|
523
|
-
'-safe', '0',
|
|
524
|
-
'-
|
|
525
|
-
'-
|
|
526
|
-
'-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
'-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
'
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
1
|
+
import { Readable, Writable } from 'stream';
|
|
2
|
+
import { spawn } from 'child_process';
|
|
3
|
+
import { writeFileSync, unlinkSync } from 'fs';
|
|
4
|
+
import { resolve as resolvePath, join as joinPath } from 'path';
|
|
5
|
+
import { tmpdir } from 'os';
|
|
6
|
+
import ffmpegPath from 'ffmpeg-static';
|
|
7
|
+
import { defaults } from './config';
|
|
8
|
+
|
|
9
|
+
export interface AudioMuxSegment {
|
|
10
|
+
seekSeconds: number;
|
|
11
|
+
durationSeconds: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface FfmpegVideoWriterConfig {
|
|
15
|
+
audioFilename: string;
|
|
16
|
+
subtitleFilename?: string;
|
|
17
|
+
/** ASS `Alignment` in FFmpeg `subtitles` `force_style` (e.g. 2 bottom, 5 middle, 8 top). Default 2. */
|
|
18
|
+
subtitleAlignmentAss?: number;
|
|
19
|
+
videoFileName: string;
|
|
20
|
+
fps: number;
|
|
21
|
+
crf?: string;
|
|
22
|
+
preset?: string;
|
|
23
|
+
onStderr?: (data: any) => any;
|
|
24
|
+
/** When set with `audioDurationSeconds`, passed to ffmpeg before `-i` for muxed highlight. */
|
|
25
|
+
audioSeekSeconds?: number;
|
|
26
|
+
audioDurationSeconds?: number;
|
|
27
|
+
/** One contiguous trim of the same audio file (muxed highlight). */
|
|
28
|
+
audioSegment?: AudioMuxSegment;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const escapeSubtitleFilterPath = (subtitlePath: string) =>
|
|
32
|
+
subtitlePath
|
|
33
|
+
.replace(/\\/g, '\\\\')
|
|
34
|
+
.replace(/:/g, '\\:')
|
|
35
|
+
.replace(/'/g, "\\'")
|
|
36
|
+
.replace(/,/g, '\\,')
|
|
37
|
+
.replace(/\[/g, '\\[')
|
|
38
|
+
.replace(/\]/g, '\\]');
|
|
39
|
+
|
|
40
|
+
export const spawnFfmpegVideoWriter = (config: FfmpegVideoWriterConfig) => {
|
|
41
|
+
if (!ffmpegPath) {
|
|
42
|
+
throw new Error('ffmpeg path not found');
|
|
43
|
+
}
|
|
44
|
+
const crf = config.crf || '23';
|
|
45
|
+
const preset = config.preset || 'medium';
|
|
46
|
+
const args: string[] = ['-y'];
|
|
47
|
+
const seek =
|
|
48
|
+
config.audioSegment?.seekSeconds ?? config.audioSeekSeconds;
|
|
49
|
+
const duration =
|
|
50
|
+
config.audioSegment?.durationSeconds ?? config.audioDurationSeconds;
|
|
51
|
+
if (seek !== undefined && duration !== undefined) {
|
|
52
|
+
args.push('-ss', String(seek), '-t', String(duration));
|
|
53
|
+
}
|
|
54
|
+
args.push(
|
|
55
|
+
'-i', config.audioFilename,
|
|
56
|
+
'-f', 'image2pipe',
|
|
57
|
+
'-vcodec', 'bmp',
|
|
58
|
+
'-framerate', `${config.fps}`,
|
|
59
|
+
'-i', '-',
|
|
60
|
+
'-crf', crf,
|
|
61
|
+
'-c:a', 'aac', '-b:a', '384k', '-profile:a', 'aac_low',
|
|
62
|
+
'-c:v', 'libx264',
|
|
63
|
+
'-r', `${config.fps}`,
|
|
64
|
+
'-pix_fmt', 'yuv420p',
|
|
65
|
+
'-preset', preset,
|
|
66
|
+
'-shortest',
|
|
67
|
+
);
|
|
68
|
+
if (config.subtitleFilename) {
|
|
69
|
+
const subPath = escapeSubtitleFilterPath(config.subtitleFilename);
|
|
70
|
+
const alignment = config.subtitleAlignmentAss ?? 2;
|
|
71
|
+
args.push(
|
|
72
|
+
'-vf',
|
|
73
|
+
`subtitles='${subPath}':force_style='Alignment=${alignment}'`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
args.push(config.videoFileName);
|
|
77
|
+
const ffmpeg = spawn(ffmpegPath, args);
|
|
78
|
+
if (config.onStderr) {
|
|
79
|
+
ffmpeg.stderr.on('data', config.onStderr);
|
|
80
|
+
}
|
|
81
|
+
return ffmpeg;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export interface VideoInfo {
|
|
85
|
+
width: number;
|
|
86
|
+
height: number;
|
|
87
|
+
duration: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface SceneChange {
|
|
91
|
+
frameNumber: number;
|
|
92
|
+
pts: number;
|
|
93
|
+
ptsTime: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const detectSceneChanges = (videoPath: string, threshold = 0.4): Promise<SceneChange[]> =>
|
|
97
|
+
new Promise((resolve, reject) => {
|
|
98
|
+
if (!ffmpegPath) {
|
|
99
|
+
reject(new Error('ffmpeg path not found'));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const ffmpeg = spawn(ffmpegPath, [
|
|
103
|
+
'-i', videoPath,
|
|
104
|
+
'-filter:v', `select='gt(scene,${threshold})',showinfo`,
|
|
105
|
+
'-f', 'null',
|
|
106
|
+
'-'
|
|
107
|
+
]);
|
|
108
|
+
let stderr = '';
|
|
109
|
+
ffmpeg.stderr.on('data', (data: Buffer) => { stderr += data.toString(); });
|
|
110
|
+
ffmpeg.on('close', () => {
|
|
111
|
+
const sceneChanges: SceneChange[] = [];
|
|
112
|
+
for (const line of stderr.split('\n')) {
|
|
113
|
+
if (!line.includes('Parsed_showinfo')) continue;
|
|
114
|
+
const nMatch = line.match(/n:\s*(\d+)/);
|
|
115
|
+
const ptsMatch = line.match(/pts:\s*(\d+)/);
|
|
116
|
+
const ptsTimeMatch = line.match(/pts_time:\s*([\d.]+)/);
|
|
117
|
+
if (nMatch && ptsTimeMatch) {
|
|
118
|
+
sceneChanges.push({
|
|
119
|
+
frameNumber: parseInt(nMatch[1]),
|
|
120
|
+
pts: ptsMatch ? parseInt(ptsMatch[1]) : 0,
|
|
121
|
+
ptsTime: parseFloat(ptsTimeMatch[1]),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
resolve(sceneChanges);
|
|
126
|
+
});
|
|
127
|
+
ffmpeg.on('error', reject);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
export const getVideoInfo = (videoPath: string): Promise<VideoInfo> =>
|
|
131
|
+
new Promise((resolvePromise, reject) => {
|
|
132
|
+
if (!ffmpegPath) {
|
|
133
|
+
reject(new Error('ffmpeg path not found'));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const ffmpeg = spawn(ffmpegPath, ['-i', videoPath, '-hide_banner']);
|
|
137
|
+
let stderr = '';
|
|
138
|
+
ffmpeg.stderr.on('data', (data: Buffer) => { stderr += data.toString(); });
|
|
139
|
+
ffmpeg.on('close', () => {
|
|
140
|
+
const dimMatch = stderr.match(/Stream.*Video:.*?(\d{2,5})x(\d{2,5})/);
|
|
141
|
+
if (!dimMatch) {
|
|
142
|
+
reject(new Error(`Could not determine video dimensions for: ${videoPath}`));
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const durMatch = stderr.match(/Duration:\s*(\d+):(\d+):([\d.]+)/);
|
|
146
|
+
const duration = durMatch
|
|
147
|
+
? parseInt(durMatch[1]) * 3600 + parseInt(durMatch[2]) * 60 + parseFloat(durMatch[3])
|
|
148
|
+
: 0;
|
|
149
|
+
resolvePromise({
|
|
150
|
+
width: parseInt(dimMatch[1]),
|
|
151
|
+
height: parseInt(dimMatch[2]),
|
|
152
|
+
duration,
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const isOrientationMismatch = (sw: number, sh: number, tw: number, th: number): boolean =>
|
|
158
|
+
(sw > sh && tw < th) || (sw < sh && tw > th);
|
|
159
|
+
|
|
160
|
+
export const buildVideoFilter = (
|
|
161
|
+
sourceWidth: number,
|
|
162
|
+
sourceHeight: number,
|
|
163
|
+
targetWidth: number,
|
|
164
|
+
targetHeight: number,
|
|
165
|
+
): string => {
|
|
166
|
+
if (!isOrientationMismatch(sourceWidth, sourceHeight, targetWidth, targetHeight)) {
|
|
167
|
+
return `scale=${targetWidth}:${targetHeight}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const targetAspect = targetWidth / targetHeight;
|
|
171
|
+
const makeEven = (n: number) => Math.floor(n / 2) * 2;
|
|
172
|
+
let cropW: number, cropH: number, cropX: number, cropY: number;
|
|
173
|
+
|
|
174
|
+
if (sourceWidth / sourceHeight > targetAspect) {
|
|
175
|
+
cropW = makeEven(Math.round(sourceHeight * targetAspect));
|
|
176
|
+
cropH = makeEven(sourceHeight);
|
|
177
|
+
cropX = Math.floor((sourceWidth - cropW) / 2);
|
|
178
|
+
cropY = Math.floor((sourceHeight - cropH) / 2);
|
|
179
|
+
} else {
|
|
180
|
+
cropW = makeEven(sourceWidth);
|
|
181
|
+
cropH = makeEven(Math.round(sourceWidth / targetAspect));
|
|
182
|
+
cropX = Math.floor((sourceWidth - cropW) / 2);
|
|
183
|
+
cropY = Math.floor((sourceHeight - cropH) / 2);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return `crop=${cropW}:${cropH}:${cropX}:${cropY},scale=${targetWidth}:${targetHeight}`;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export const spawnVideoFrameReader = (config: {
|
|
190
|
+
videoPath: string;
|
|
191
|
+
fps: number;
|
|
192
|
+
totalFrames: number;
|
|
193
|
+
width?: number;
|
|
194
|
+
height?: number;
|
|
195
|
+
sourceWidth?: number;
|
|
196
|
+
sourceHeight?: number;
|
|
197
|
+
}) => {
|
|
198
|
+
if (!ffmpegPath) {
|
|
199
|
+
throw new Error('ffmpeg path not found');
|
|
200
|
+
}
|
|
201
|
+
const args = [
|
|
202
|
+
'-stream_loop', '-1',
|
|
203
|
+
'-i', config.videoPath,
|
|
204
|
+
'-r', `${config.fps}`,
|
|
205
|
+
'-frames:v', `${config.totalFrames}`,
|
|
206
|
+
];
|
|
207
|
+
if (config.width && config.height) {
|
|
208
|
+
const filter = config.sourceWidth && config.sourceHeight
|
|
209
|
+
? buildVideoFilter(config.sourceWidth, config.sourceHeight, config.width, config.height)
|
|
210
|
+
: `scale=${config.width}:${config.height}`;
|
|
211
|
+
args.push('-vf', filter);
|
|
212
|
+
}
|
|
213
|
+
args.push(
|
|
214
|
+
'-f', 'rawvideo',
|
|
215
|
+
'-pix_fmt', 'bgr24',
|
|
216
|
+
'-v', 'quiet',
|
|
217
|
+
'pipe:1'
|
|
218
|
+
);
|
|
219
|
+
return spawn(ffmpegPath, args);
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
export const readVideoFrame = (
|
|
223
|
+
stream: Readable,
|
|
224
|
+
frameSize: number,
|
|
225
|
+
timeoutMs = defaults.timeouts.readVideoFrame,
|
|
226
|
+
): Promise<Buffer | null> =>
|
|
227
|
+
new Promise((resolve) => {
|
|
228
|
+
let timer: NodeJS.Timeout | undefined;
|
|
229
|
+
let settled = false;
|
|
230
|
+
const finish = (result: Buffer | null) => {
|
|
231
|
+
if (settled) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
settled = true;
|
|
235
|
+
if (timer) {
|
|
236
|
+
clearTimeout(timer);
|
|
237
|
+
timer = undefined;
|
|
238
|
+
}
|
|
239
|
+
stream.removeListener('readable', onReadable);
|
|
240
|
+
stream.removeListener('end', onEnd);
|
|
241
|
+
stream.removeListener('error', onErrorOrClose);
|
|
242
|
+
stream.removeListener('close', onErrorOrClose);
|
|
243
|
+
resolve(result);
|
|
244
|
+
};
|
|
245
|
+
const onReadable = () => {
|
|
246
|
+
tryRead();
|
|
247
|
+
};
|
|
248
|
+
const onEnd = () => {
|
|
249
|
+
finish(null);
|
|
250
|
+
};
|
|
251
|
+
const onErrorOrClose = () => {
|
|
252
|
+
finish(null);
|
|
253
|
+
};
|
|
254
|
+
const tryRead = () => {
|
|
255
|
+
const data = stream.read(frameSize) as Buffer | null;
|
|
256
|
+
if (data !== null) {
|
|
257
|
+
finish(data.length === frameSize ? data : null);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
tryRead();
|
|
261
|
+
if (settled) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (timeoutMs > 0) {
|
|
265
|
+
timer = setTimeout(() => {
|
|
266
|
+
console.error('readVideoFrame timeout:', timeoutMs);
|
|
267
|
+
finish(null);
|
|
268
|
+
}, timeoutMs);
|
|
269
|
+
}
|
|
270
|
+
stream.on('readable', onReadable);
|
|
271
|
+
stream.once('end', onEnd);
|
|
272
|
+
stream.once('error', onErrorOrClose);
|
|
273
|
+
stream.once('close', onErrorOrClose);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
export const getProgress = (onProgress: (currentFrame: number) => any) =>
|
|
277
|
+
(stderrOutput: Buffer) => {
|
|
278
|
+
const matchResult = stderrOutput.toString().match(/frame=[ ]+(\d+)/);
|
|
279
|
+
if (!matchResult) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const currentFrame = +matchResult[1];
|
|
283
|
+
if (isNaN(currentFrame)) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
onProgress(currentFrame);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
export const calculateProgress = (framesCount: number, callback: (progress: number) => any) =>
|
|
290
|
+
(currentFrame: number) =>
|
|
291
|
+
callback(
|
|
292
|
+
+(currentFrame / framesCount * 100).toFixed(2)
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
export const waitDrain = (
|
|
296
|
+
stream: Writable,
|
|
297
|
+
processStream?: {
|
|
298
|
+
once: (eventName: string, listener: (...args: any[]) => void) => any;
|
|
299
|
+
removeListener: (eventName: string, listener: (...args: any[]) => void) => any;
|
|
300
|
+
},
|
|
301
|
+
timeoutMs = defaults.timeouts.waitDrain,
|
|
302
|
+
) =>
|
|
303
|
+
new Promise<boolean>(resolve => {
|
|
304
|
+
const cleanup = () => {
|
|
305
|
+
if (timer) {
|
|
306
|
+
clearTimeout(timer);
|
|
307
|
+
timer = undefined;
|
|
308
|
+
}
|
|
309
|
+
stream.removeListener('drain', onDrain);
|
|
310
|
+
stream.removeListener('error', onErrorOrClose);
|
|
311
|
+
stream.removeListener('close', onErrorOrClose);
|
|
312
|
+
processStream?.removeListener('error', onErrorOrClose);
|
|
313
|
+
processStream?.removeListener('exit', onErrorOrClose);
|
|
314
|
+
processStream?.removeListener('close', onErrorOrClose);
|
|
315
|
+
};
|
|
316
|
+
const onDrain = () => {
|
|
317
|
+
cleanup();
|
|
318
|
+
resolve(true);
|
|
319
|
+
};
|
|
320
|
+
const onErrorOrClose = () => {
|
|
321
|
+
cleanup();
|
|
322
|
+
resolve(false);
|
|
323
|
+
};
|
|
324
|
+
let timer: NodeJS.Timeout | undefined;
|
|
325
|
+
if (timeoutMs > 0) {
|
|
326
|
+
timer = setTimeout(() => {
|
|
327
|
+
console.error('waitDrain timeout:', timeoutMs);
|
|
328
|
+
cleanup();
|
|
329
|
+
resolve(false);
|
|
330
|
+
}, timeoutMs);
|
|
331
|
+
}
|
|
332
|
+
stream.once('drain', onDrain);
|
|
333
|
+
stream.once('error', onErrorOrClose);
|
|
334
|
+
stream.once('close', onErrorOrClose);
|
|
335
|
+
processStream?.once('error', onErrorOrClose);
|
|
336
|
+
processStream?.once('exit', onErrorOrClose);
|
|
337
|
+
processStream?.once('close', onErrorOrClose);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
export interface ProcessExitResult {
|
|
341
|
+
exitCode: number;
|
|
342
|
+
reason?: string;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const resolveProcessExit = (
|
|
346
|
+
resolve: (result: ProcessExitResult) => void,
|
|
347
|
+
exitCode: number,
|
|
348
|
+
reason?: string,
|
|
349
|
+
) => {
|
|
350
|
+
resolve({ exitCode, ...(reason && { reason }) });
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
export const waitForProcessExit = (
|
|
354
|
+
processStream: {
|
|
355
|
+
once: (eventName: string, listener: (...args: any[]) => void) => any;
|
|
356
|
+
removeListener: (eventName: string, listener: (...args: any[]) => void) => any;
|
|
357
|
+
},
|
|
358
|
+
timeoutMs = defaults.timeouts.waitForProcessExit,
|
|
359
|
+
) =>
|
|
360
|
+
new Promise<ProcessExitResult>(resolve => {
|
|
361
|
+
const cleanup = () => {
|
|
362
|
+
if (timer) {
|
|
363
|
+
clearTimeout(timer);
|
|
364
|
+
timer = undefined;
|
|
365
|
+
}
|
|
366
|
+
processStream.removeListener('exit', onExit);
|
|
367
|
+
processStream.removeListener('close', onClose);
|
|
368
|
+
processStream.removeListener('error', onError);
|
|
369
|
+
};
|
|
370
|
+
const onExit = (code: number | null) => {
|
|
371
|
+
cleanup();
|
|
372
|
+
const exitCode = code ?? 0;
|
|
373
|
+
resolveProcessExit(
|
|
374
|
+
resolve,
|
|
375
|
+
exitCode,
|
|
376
|
+
exitCode !== 0 ? `ffmpeg exited with code ${exitCode}` : undefined,
|
|
377
|
+
);
|
|
378
|
+
};
|
|
379
|
+
const onClose = (code: number | null) => {
|
|
380
|
+
cleanup();
|
|
381
|
+
const exitCode = code ?? 0;
|
|
382
|
+
resolveProcessExit(
|
|
383
|
+
resolve,
|
|
384
|
+
exitCode,
|
|
385
|
+
exitCode !== 0 ? `ffmpeg exited with code ${exitCode}` : undefined,
|
|
386
|
+
);
|
|
387
|
+
};
|
|
388
|
+
const onError = () => {
|
|
389
|
+
cleanup();
|
|
390
|
+
resolveProcessExit(resolve, 1, 'ffmpeg process error');
|
|
391
|
+
};
|
|
392
|
+
let timer: NodeJS.Timeout | undefined;
|
|
393
|
+
if (timeoutMs > 0) {
|
|
394
|
+
timer = setTimeout(() => {
|
|
395
|
+
console.error('waitForProcessExit timeout:', timeoutMs);
|
|
396
|
+
cleanup();
|
|
397
|
+
resolveProcessExit(resolve, 1, `waitForProcessExit timeout (${timeoutMs}ms)`);
|
|
398
|
+
}, timeoutMs);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
processStream.once('exit', onExit);
|
|
402
|
+
processStream.once('close', onClose);
|
|
403
|
+
processStream.once('error', onError);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
export interface VideoSegment {
|
|
407
|
+
outputStartFrame: number;
|
|
408
|
+
videoSeekSeconds: number;
|
|
409
|
+
frameCount: number;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const mergeSmallScenes = (sceneChanges: SceneChange[], videoDuration: number, minDuration = 2): SceneChange[] => {
|
|
413
|
+
if (sceneChanges.length === 0) return [];
|
|
414
|
+
const merged: SceneChange[] = [sceneChanges[0]];
|
|
415
|
+
for (let i = 1; i < sceneChanges.length; i++) {
|
|
416
|
+
const lastKept = merged[merged.length - 1];
|
|
417
|
+
const gap = sceneChanges[i].ptsTime - lastKept.ptsTime;
|
|
418
|
+
if (gap >= minDuration) {
|
|
419
|
+
merged.push(sceneChanges[i]);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const lastScene = merged[merged.length - 1];
|
|
423
|
+
if (videoDuration - lastScene.ptsTime < minDuration && merged.length > 1) {
|
|
424
|
+
merged.pop();
|
|
425
|
+
}
|
|
426
|
+
return merged;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
export const buildBeatSyncedSegments = (
|
|
430
|
+
beatFrameIndices: number[],
|
|
431
|
+
totalFrames: number,
|
|
432
|
+
sceneChanges: SceneChange[],
|
|
433
|
+
videoDuration: number,
|
|
434
|
+
): VideoSegment[] => {
|
|
435
|
+
if (sceneChanges.length === 0) {
|
|
436
|
+
return [
|
|
437
|
+
{
|
|
438
|
+
outputStartFrame: 0,
|
|
439
|
+
videoSeekSeconds: 0,
|
|
440
|
+
frameCount: totalFrames,
|
|
441
|
+
},
|
|
442
|
+
];
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const consolidated = mergeSmallScenes(sceneChanges, videoDuration);
|
|
446
|
+
let seekPositions: number[];
|
|
447
|
+
if (consolidated.length >= 2) {
|
|
448
|
+
seekPositions = consolidated.map(sc => sc.ptsTime);
|
|
449
|
+
} else {
|
|
450
|
+
const count = Math.max(10, Math.ceil(videoDuration / 3));
|
|
451
|
+
seekPositions = Array.from({ length: count }, (_, i) => (i * videoDuration) / count);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const boundaries = [0, ...beatFrameIndices];
|
|
455
|
+
if (boundaries[boundaries.length - 1] !== totalFrames) {
|
|
456
|
+
boundaries.push(totalFrames);
|
|
457
|
+
}
|
|
458
|
+
const unique = Array.from(new Set(boundaries)).sort((a, b) => a - b);
|
|
459
|
+
|
|
460
|
+
const segments: VideoSegment[] = [];
|
|
461
|
+
for (let i = 0; i < unique.length - 1; i++) {
|
|
462
|
+
segments.push({
|
|
463
|
+
outputStartFrame: unique[i],
|
|
464
|
+
videoSeekSeconds: seekPositions[i % seekPositions.length],
|
|
465
|
+
frameCount: unique[i + 1] - unique[i],
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
return segments;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
export const writeConcatFile = (
|
|
472
|
+
segments: VideoSegment[],
|
|
473
|
+
videoPath: string,
|
|
474
|
+
videoDuration: number,
|
|
475
|
+
fps: number,
|
|
476
|
+
): string => {
|
|
477
|
+
const absVideoPath = resolvePath(videoPath).replace(/\\/g, '/');
|
|
478
|
+
const escaped = absVideoPath.replace(/'/g, "'\\''");
|
|
479
|
+
let content = '';
|
|
480
|
+
|
|
481
|
+
for (const seg of segments) {
|
|
482
|
+
const segDuration = seg.frameCount / fps;
|
|
483
|
+
let remaining = segDuration;
|
|
484
|
+
let pos = seg.videoSeekSeconds % videoDuration;
|
|
485
|
+
|
|
486
|
+
while (remaining > 0.001) {
|
|
487
|
+
const available = videoDuration - pos;
|
|
488
|
+
const take = Math.min(remaining, available);
|
|
489
|
+
content += `file '${escaped}'\n`;
|
|
490
|
+
content += `inpoint ${pos.toFixed(6)}\n`;
|
|
491
|
+
content += `outpoint ${(pos + take).toFixed(6)}\n`;
|
|
492
|
+
content += `duration ${take.toFixed(6)}\n\n`;
|
|
493
|
+
remaining -= take;
|
|
494
|
+
pos = 0;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const concatPath = joinPath(tmpdir(), `av-concat-${Date.now()}.txt`);
|
|
499
|
+
writeFileSync(concatPath, content, 'utf-8');
|
|
500
|
+
return concatPath;
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
export const writeSubtitlesFile = (subtitles: string): string => {
|
|
504
|
+
const subtitlePath = joinPath(tmpdir(), `av-subtitles-${Date.now()}.srt`);
|
|
505
|
+
writeFileSync(subtitlePath, subtitles, 'utf-8');
|
|
506
|
+
return subtitlePath;
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
export const spawnConcatVideoFrameReader = (config: {
|
|
510
|
+
concatFilePath: string;
|
|
511
|
+
fps: number;
|
|
512
|
+
totalFrames: number;
|
|
513
|
+
width?: number;
|
|
514
|
+
height?: number;
|
|
515
|
+
sourceWidth?: number;
|
|
516
|
+
sourceHeight?: number;
|
|
517
|
+
}) => {
|
|
518
|
+
if (!ffmpegPath) {
|
|
519
|
+
throw new Error('ffmpeg path not found');
|
|
520
|
+
}
|
|
521
|
+
const args = [
|
|
522
|
+
'-f', 'concat',
|
|
523
|
+
'-safe', '0',
|
|
524
|
+
'-fflags', '+genpts',
|
|
525
|
+
'-i', config.concatFilePath,
|
|
526
|
+
'-an',
|
|
527
|
+
'-avoid_negative_ts', 'make_zero',
|
|
528
|
+
'-r', `${config.fps}`,
|
|
529
|
+
'-frames:v', `${config.totalFrames}`,
|
|
530
|
+
];
|
|
531
|
+
if (config.width && config.height) {
|
|
532
|
+
const filter = config.sourceWidth && config.sourceHeight
|
|
533
|
+
? buildVideoFilter(config.sourceWidth, config.sourceHeight, config.width, config.height)
|
|
534
|
+
: `scale=${config.width}:${config.height}`;
|
|
535
|
+
args.push('-vf', filter);
|
|
536
|
+
}
|
|
537
|
+
args.push(
|
|
538
|
+
'-f', 'rawvideo',
|
|
539
|
+
'-pix_fmt', 'bgr24',
|
|
540
|
+
'-v', 'quiet',
|
|
541
|
+
'pipe:1'
|
|
542
|
+
);
|
|
543
|
+
return spawn(ffmpegPath, args);
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
export const cleanupTempFile = (filePath: string) => {
|
|
547
|
+
try { unlinkSync(filePath); } catch {}
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
export const cleanupConcatFile = cleanupTempFile;
|