@happyvertical/video 0.80.0 → 0.80.2
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/dist/index.js +1043 -1105
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1,1176 +1,1114 @@
|
|
|
1
|
-
import { execFile
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
2
|
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
-
import {
|
|
3
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
|
-
import {
|
|
5
|
+
import { basename, extname, join } from "node:path";
|
|
6
6
|
import { promisify } from "node:util";
|
|
7
7
|
import { createLogger } from "@happyvertical/logger";
|
|
8
8
|
import { ValidationError } from "@happyvertical/utils";
|
|
9
9
|
import sharp from "sharp";
|
|
10
10
|
import { getImageMetadata } from "@happyvertical/images";
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
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
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
* Escape text for FFmpeg drawtext filter
|
|
570
|
-
*/
|
|
571
|
-
escapeFFmpegText(text) {
|
|
572
|
-
return text.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/:/g, "\\:").replace(/\[/g, "\\[").replace(/\]/g, "\\]");
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
const execFile = promisify(execFile$1);
|
|
576
|
-
const VIDEO_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
577
|
-
".mp4",
|
|
578
|
-
".mov",
|
|
579
|
-
".m4v",
|
|
580
|
-
".avi",
|
|
581
|
-
".webm",
|
|
582
|
-
".insv",
|
|
583
|
-
".lrv"
|
|
11
|
+
//#region src/adapters/ffmpeg.ts
|
|
12
|
+
/**
|
|
13
|
+
* FFmpeg Video Processor Adapter
|
|
14
|
+
*
|
|
15
|
+
* Implements video processing using FFmpeg command-line tools.
|
|
16
|
+
*/
|
|
17
|
+
var execFileAsync = promisify(execFile);
|
|
18
|
+
/**
|
|
19
|
+
* FFmpeg-based video processor
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { FFmpegProcessor } from '@happyvertical/video';
|
|
24
|
+
*
|
|
25
|
+
* const processor = new FFmpegProcessor();
|
|
26
|
+
*
|
|
27
|
+
* // Get video metadata
|
|
28
|
+
* const metadata = await processor.getMetadata('input.mp4');
|
|
29
|
+
* console.log(`Duration: ${metadata.duration}s`);
|
|
30
|
+
*
|
|
31
|
+
* // Extract thumbnail
|
|
32
|
+
* const thumbnail = await processor.extractFrame('input.mp4', 2.0, {
|
|
33
|
+
* format: 'jpg',
|
|
34
|
+
* width: 1280,
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // Add lower-third overlay
|
|
38
|
+
* const output = await processor.addLowerThird('input.mp4', {
|
|
39
|
+
* title: 'John Smith',
|
|
40
|
+
* subtitle: 'News Anchor',
|
|
41
|
+
* style: 'news',
|
|
42
|
+
* duration: 5,
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
var FFmpegProcessor = class {
|
|
47
|
+
options;
|
|
48
|
+
logger;
|
|
49
|
+
constructor(options = {}) {
|
|
50
|
+
this.options = {
|
|
51
|
+
ffmpegPath: options.ffmpegPath ?? "ffmpeg",
|
|
52
|
+
ffprobePath: options.ffprobePath ?? "ffprobe",
|
|
53
|
+
tempDir: options.tempDir ?? tmpdir(),
|
|
54
|
+
timeout: options.timeout ?? 3e5
|
|
55
|
+
};
|
|
56
|
+
this.logger = createLogger({ level: "info" });
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a temporary directory for processing
|
|
60
|
+
*/
|
|
61
|
+
createTempDir() {
|
|
62
|
+
return mkdtempSync(join(this.options.tempDir, "video-"));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Save buffer to a temporary file
|
|
66
|
+
*/
|
|
67
|
+
async saveToTemp(buffer, ext, tempDir) {
|
|
68
|
+
const filepath = join(tempDir, `${crypto.randomUUID()}.${ext}`);
|
|
69
|
+
await writeFile(filepath, buffer);
|
|
70
|
+
return filepath;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get input path (save buffer to temp if needed)
|
|
74
|
+
*/
|
|
75
|
+
async getInputPath(input, ext, tempDir) {
|
|
76
|
+
if (Buffer.isBuffer(input)) return this.saveToTemp(input, ext, tempDir);
|
|
77
|
+
return input;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Run FFmpeg command
|
|
81
|
+
* Uses execFile with array arguments to prevent command injection
|
|
82
|
+
*/
|
|
83
|
+
async runFFmpeg(args, timeout) {
|
|
84
|
+
this.logger.debug("Running FFmpeg", {
|
|
85
|
+
path: this.options.ffmpegPath,
|
|
86
|
+
args
|
|
87
|
+
});
|
|
88
|
+
await execFileAsync(this.options.ffmpegPath, args, {
|
|
89
|
+
timeout: timeout ?? this.options.timeout,
|
|
90
|
+
maxBuffer: 100 * 1024 * 1024
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Run FFprobe command and parse JSON output
|
|
95
|
+
* Uses execFile with array arguments to prevent command injection
|
|
96
|
+
*/
|
|
97
|
+
async runFFprobe(input) {
|
|
98
|
+
const args = [
|
|
99
|
+
"-v",
|
|
100
|
+
"quiet",
|
|
101
|
+
"-print_format",
|
|
102
|
+
"json",
|
|
103
|
+
"-show_format",
|
|
104
|
+
"-show_streams",
|
|
105
|
+
input
|
|
106
|
+
];
|
|
107
|
+
const { stdout } = await execFileAsync(this.options.ffprobePath, args, { timeout: 3e4 });
|
|
108
|
+
return JSON.parse(stdout);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Get video metadata
|
|
112
|
+
*/
|
|
113
|
+
async getMetadata(video) {
|
|
114
|
+
let tempDir = null;
|
|
115
|
+
let inputPath;
|
|
116
|
+
try {
|
|
117
|
+
if (Buffer.isBuffer(video)) {
|
|
118
|
+
tempDir = this.createTempDir();
|
|
119
|
+
inputPath = await this.saveToTemp(video, "mp4", tempDir);
|
|
120
|
+
} else inputPath = video;
|
|
121
|
+
const probe = await this.runFFprobe(inputPath);
|
|
122
|
+
const videoStream = probe.streams?.find((s) => s.codec_type === "video");
|
|
123
|
+
const audioStream = probe.streams?.find((s) => s.codec_type === "audio");
|
|
124
|
+
const format = probe.format;
|
|
125
|
+
if (!videoStream) throw new ValidationError("No video stream found in file");
|
|
126
|
+
const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
|
|
127
|
+
const aspectGcd = gcd(videoStream.width, videoStream.height);
|
|
128
|
+
const aspectRatio = `${videoStream.width / aspectGcd}:${videoStream.height / aspectGcd}`;
|
|
129
|
+
let fps = 30;
|
|
130
|
+
if (videoStream.r_frame_rate) {
|
|
131
|
+
const [num, den] = videoStream.r_frame_rate.split("/").map(Number);
|
|
132
|
+
fps = den ? num / den : num;
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
duration: parseFloat(format.duration) || 0,
|
|
136
|
+
width: videoStream.width,
|
|
137
|
+
height: videoStream.height,
|
|
138
|
+
fps,
|
|
139
|
+
codec: videoStream.codec_name,
|
|
140
|
+
bitrate: parseInt(format.bit_rate, 10),
|
|
141
|
+
aspectRatio,
|
|
142
|
+
hasAudio: !!audioStream,
|
|
143
|
+
audioCodec: audioStream?.codec_name,
|
|
144
|
+
audioSampleRate: audioStream?.sample_rate ? parseInt(audioStream.sample_rate, 10) : void 0,
|
|
145
|
+
fileSize: parseInt(format.size, 10),
|
|
146
|
+
format: format.format_name.split(",")[0]
|
|
147
|
+
};
|
|
148
|
+
} finally {
|
|
149
|
+
if (tempDir) rmSync(tempDir, {
|
|
150
|
+
recursive: true,
|
|
151
|
+
force: true
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Extract a single frame from video
|
|
157
|
+
*/
|
|
158
|
+
async extractFrame(video, timestamp, options = {}) {
|
|
159
|
+
const tempDir = this.createTempDir();
|
|
160
|
+
try {
|
|
161
|
+
const inputPath = await this.getInputPath(video, "mp4", tempDir);
|
|
162
|
+
const format = options.format ?? "png";
|
|
163
|
+
const outputPath = join(tempDir, `frame.${format}`);
|
|
164
|
+
const args = [
|
|
165
|
+
"-y",
|
|
166
|
+
"-ss",
|
|
167
|
+
timestamp.toFixed(3),
|
|
168
|
+
"-i",
|
|
169
|
+
inputPath,
|
|
170
|
+
"-vframes",
|
|
171
|
+
"1"
|
|
172
|
+
];
|
|
173
|
+
if (options.width || options.height) {
|
|
174
|
+
const w = options.width ?? -1;
|
|
175
|
+
const h = options.height ?? -1;
|
|
176
|
+
args.push("-vf", `scale=${w}:${h}`);
|
|
177
|
+
}
|
|
178
|
+
if (format === "jpg" || format === "webp") {
|
|
179
|
+
const quality = options.quality ?? 90;
|
|
180
|
+
args.push("-q:v", Math.round((100 - quality) / 100 * 31).toString());
|
|
181
|
+
}
|
|
182
|
+
args.push(outputPath);
|
|
183
|
+
await this.runFFmpeg(args);
|
|
184
|
+
return await readFile(outputPath);
|
|
185
|
+
} finally {
|
|
186
|
+
rmSync(tempDir, {
|
|
187
|
+
recursive: true,
|
|
188
|
+
force: true
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Transcode video to different format/codec
|
|
194
|
+
*/
|
|
195
|
+
async transcode(video, options) {
|
|
196
|
+
const tempDir = this.createTempDir();
|
|
197
|
+
try {
|
|
198
|
+
const inputPath = await this.getInputPath(video, "mp4", tempDir);
|
|
199
|
+
const outputPath = join(tempDir, `output.${options.format}`);
|
|
200
|
+
const args = [
|
|
201
|
+
"-y",
|
|
202
|
+
"-i",
|
|
203
|
+
inputPath
|
|
204
|
+
];
|
|
205
|
+
if (options.startTime !== void 0) args.push("-ss", options.startTime.toFixed(3));
|
|
206
|
+
if (options.endTime !== void 0) args.push("-to", options.endTime.toFixed(3));
|
|
207
|
+
if (options.video) args.push(...this.getVideoCodecArgs(options.video));
|
|
208
|
+
if (options.audio) {
|
|
209
|
+
args.push("-c:a", options.audio.codec);
|
|
210
|
+
if (options.audio.bitrate) args.push("-b:a", options.audio.bitrate);
|
|
211
|
+
if (options.audio.sampleRate) args.push("-ar", options.audio.sampleRate.toString());
|
|
212
|
+
if (options.audio.channels) args.push("-ac", options.audio.channels.toString());
|
|
213
|
+
}
|
|
214
|
+
if (options.width || options.height) {
|
|
215
|
+
const w = options.width ?? -1;
|
|
216
|
+
const h = options.height ?? -1;
|
|
217
|
+
args.push("-vf", `scale=${w}:${h}`);
|
|
218
|
+
}
|
|
219
|
+
if (options.fps) args.push("-r", options.fps.toString());
|
|
220
|
+
args.push(outputPath);
|
|
221
|
+
await this.runFFmpeg(args);
|
|
222
|
+
return await readFile(outputPath);
|
|
223
|
+
} finally {
|
|
224
|
+
rmSync(tempDir, {
|
|
225
|
+
recursive: true,
|
|
226
|
+
force: true
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Get FFmpeg args for video codec options
|
|
232
|
+
*/
|
|
233
|
+
getVideoCodecArgs(options) {
|
|
234
|
+
const args = [];
|
|
235
|
+
args.push("-c:v", {
|
|
236
|
+
h264: "libx264",
|
|
237
|
+
h265: "libx265",
|
|
238
|
+
vp9: "libvpx-vp9",
|
|
239
|
+
av1: "libaom-av1"
|
|
240
|
+
}[options.codec] ?? options.codec);
|
|
241
|
+
if (options.preset) args.push("-preset", options.preset);
|
|
242
|
+
if (options.crf !== void 0) args.push("-crf", options.crf.toString());
|
|
243
|
+
if (options.bitrate) args.push("-b:v", options.bitrate);
|
|
244
|
+
if (options.pixelFormat) args.push("-pix_fmt", options.pixelFormat);
|
|
245
|
+
else args.push("-pix_fmt", "yuv420p");
|
|
246
|
+
return args;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Add an overlay to video
|
|
250
|
+
*/
|
|
251
|
+
async addOverlay(video, overlay) {
|
|
252
|
+
const tempDir = this.createTempDir();
|
|
253
|
+
try {
|
|
254
|
+
const inputPath = await this.getInputPath(video, "mp4", tempDir);
|
|
255
|
+
const outputPath = join(tempDir, "output.mp4");
|
|
256
|
+
let filterComplex = "";
|
|
257
|
+
const inputs = ["-i", inputPath];
|
|
258
|
+
if (overlay.type === "image") {
|
|
259
|
+
const imagePath = await this.getInputPath(overlay.content, "png", tempDir);
|
|
260
|
+
inputs.push("-i", imagePath);
|
|
261
|
+
const pos = this.getOverlayPosition(overlay.position);
|
|
262
|
+
filterComplex = `[1:v]${overlay.width ? `scale=${overlay.width}:-1,` : ""}format=rgba,colorchannelmixer=aa=${overlay.opacity ?? 1}[ovr];[0:v][ovr]overlay=${pos.x}:${pos.y}`;
|
|
263
|
+
if (overlay.startTime !== void 0 || overlay.endTime !== void 0) {
|
|
264
|
+
const enable = this.getEnableExpression(overlay.startTime, overlay.endTime);
|
|
265
|
+
filterComplex += `:enable='${enable}'`;
|
|
266
|
+
}
|
|
267
|
+
} else if (overlay.type === "text") {
|
|
268
|
+
const textOpts = overlay.textOptions ?? {};
|
|
269
|
+
const pos = this.getOverlayPosition(overlay.position);
|
|
270
|
+
filterComplex = `drawtext=text='${this.escapeFFmpegText(overlay.content)}'`;
|
|
271
|
+
filterComplex += `:x=${pos.x}:y=${pos.y}`;
|
|
272
|
+
filterComplex += `:fontsize=${textOpts.fontSize ?? 24}`;
|
|
273
|
+
filterComplex += `:fontcolor=${textOpts.fontColor ?? "white"}`;
|
|
274
|
+
if (textOpts.fontFamily) filterComplex += `:fontfile=${textOpts.fontFamily}`;
|
|
275
|
+
if (textOpts.backgroundColor) filterComplex += `:box=1:boxcolor=${textOpts.backgroundColor}:boxborderw=${textOpts.padding ?? 10}`;
|
|
276
|
+
if (overlay.startTime !== void 0 || overlay.endTime !== void 0) {
|
|
277
|
+
const enable = this.getEnableExpression(overlay.startTime, overlay.endTime);
|
|
278
|
+
filterComplex += `:enable='${enable}'`;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const args = [
|
|
282
|
+
"-y",
|
|
283
|
+
...inputs,
|
|
284
|
+
"-filter_complex",
|
|
285
|
+
filterComplex,
|
|
286
|
+
"-c:a",
|
|
287
|
+
"copy",
|
|
288
|
+
outputPath
|
|
289
|
+
];
|
|
290
|
+
await this.runFFmpeg(args);
|
|
291
|
+
return await readFile(outputPath);
|
|
292
|
+
} finally {
|
|
293
|
+
rmSync(tempDir, {
|
|
294
|
+
recursive: true,
|
|
295
|
+
force: true
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Add a lower-third overlay
|
|
301
|
+
*/
|
|
302
|
+
async addLowerThird(video, config) {
|
|
303
|
+
const tempDir = this.createTempDir();
|
|
304
|
+
try {
|
|
305
|
+
const inputPath = await this.getInputPath(video, "mp4", tempDir);
|
|
306
|
+
const outputPath = join(tempDir, "output.mp4");
|
|
307
|
+
const metadata = await this.getMetadata(inputPath);
|
|
308
|
+
const startTime = config.startTime ?? 0;
|
|
309
|
+
const endTime = startTime + (config.duration ?? 5);
|
|
310
|
+
const primaryColor = config.primaryColor ?? "0x1a1a1a";
|
|
311
|
+
const titleY = metadata.height - 100;
|
|
312
|
+
const subtitleY = metadata.height - 60;
|
|
313
|
+
let filter = `drawtext=text='${this.escapeFFmpegText(config.title)}':x=50:y=${titleY}:fontsize=32:fontcolor=white:box=1:boxcolor=${primaryColor}@0.8:boxborderw=10:enable='between(t,${startTime},${endTime})'`;
|
|
314
|
+
if (config.subtitle) filter += `,drawtext=text='${this.escapeFFmpegText(config.subtitle)}':x=50:y=${subtitleY}:fontsize=20:fontcolor=white@0.9:enable='between(t,${startTime},${endTime})'`;
|
|
315
|
+
const args = [
|
|
316
|
+
"-y",
|
|
317
|
+
"-i",
|
|
318
|
+
inputPath,
|
|
319
|
+
"-vf",
|
|
320
|
+
filter,
|
|
321
|
+
"-c:a",
|
|
322
|
+
"copy",
|
|
323
|
+
outputPath
|
|
324
|
+
];
|
|
325
|
+
await this.runFFmpeg(args);
|
|
326
|
+
return await readFile(outputPath);
|
|
327
|
+
} finally {
|
|
328
|
+
rmSync(tempDir, {
|
|
329
|
+
recursive: true,
|
|
330
|
+
force: true
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Compose video with overlays and audio
|
|
336
|
+
*/
|
|
337
|
+
async compose(options) {
|
|
338
|
+
const tempDir = this.createTempDir();
|
|
339
|
+
try {
|
|
340
|
+
let currentVideo = await this.getInputPath(options.baseVideo, "mp4", tempDir);
|
|
341
|
+
if (options.overlays) for (const overlay of options.overlays) {
|
|
342
|
+
const result = await this.addOverlay(currentVideo, overlay);
|
|
343
|
+
const tempOutput = join(tempDir, `overlay_${crypto.randomUUID()}.mp4`);
|
|
344
|
+
await writeFile(tempOutput, result);
|
|
345
|
+
currentVideo = tempOutput;
|
|
346
|
+
}
|
|
347
|
+
if (options.audio) {
|
|
348
|
+
const result = await this.mixAudio(currentVideo, options.audio);
|
|
349
|
+
const tempOutput = join(tempDir, `audio_${crypto.randomUUID()}.mp4`);
|
|
350
|
+
await writeFile(tempOutput, result);
|
|
351
|
+
currentVideo = tempOutput;
|
|
352
|
+
}
|
|
353
|
+
const outputPath = join(tempDir, `output.${options.outputFormat ?? "mp4"}`);
|
|
354
|
+
const args = [
|
|
355
|
+
"-y",
|
|
356
|
+
"-i",
|
|
357
|
+
currentVideo
|
|
358
|
+
];
|
|
359
|
+
if (options.width || options.height) {
|
|
360
|
+
const w = options.width ?? -1;
|
|
361
|
+
const h = options.height ?? -1;
|
|
362
|
+
args.push("-vf", `scale=${w}:${h}`);
|
|
363
|
+
}
|
|
364
|
+
if (options.fps) args.push("-r", options.fps.toString());
|
|
365
|
+
const quality = options.quality ?? 23;
|
|
366
|
+
args.push("-crf", quality.toString());
|
|
367
|
+
args.push("-preset", "medium");
|
|
368
|
+
args.push("-c:a", "aac", "-b:a", "128k");
|
|
369
|
+
args.push(outputPath);
|
|
370
|
+
await this.runFFmpeg(args);
|
|
371
|
+
return await readFile(outputPath);
|
|
372
|
+
} finally {
|
|
373
|
+
rmSync(tempDir, {
|
|
374
|
+
recursive: true,
|
|
375
|
+
force: true
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Concatenate multiple videos
|
|
381
|
+
*/
|
|
382
|
+
async concatenate(videos) {
|
|
383
|
+
const tempDir = this.createTempDir();
|
|
384
|
+
try {
|
|
385
|
+
const videoPaths = [];
|
|
386
|
+
for (let i = 0; i < videos.length; i++) {
|
|
387
|
+
const path = await this.getInputPath(videos[i], "mp4", tempDir);
|
|
388
|
+
videoPaths.push(path);
|
|
389
|
+
}
|
|
390
|
+
const listPath = join(tempDir, "concat.txt");
|
|
391
|
+
writeFileSync(listPath, videoPaths.map((p) => `file '${p}'`).join("\n"));
|
|
392
|
+
const outputPath = join(tempDir, "output.mp4");
|
|
393
|
+
const args = [
|
|
394
|
+
"-y",
|
|
395
|
+
"-f",
|
|
396
|
+
"concat",
|
|
397
|
+
"-safe",
|
|
398
|
+
"0",
|
|
399
|
+
"-i",
|
|
400
|
+
listPath,
|
|
401
|
+
"-c",
|
|
402
|
+
"copy",
|
|
403
|
+
outputPath
|
|
404
|
+
];
|
|
405
|
+
await this.runFFmpeg(args);
|
|
406
|
+
return await readFile(outputPath);
|
|
407
|
+
} finally {
|
|
408
|
+
rmSync(tempDir, {
|
|
409
|
+
recursive: true,
|
|
410
|
+
force: true
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Mix audio into video
|
|
416
|
+
*/
|
|
417
|
+
async mixAudio(video, audio, options = {}) {
|
|
418
|
+
const tempDir = this.createTempDir();
|
|
419
|
+
try {
|
|
420
|
+
const videoPath = await this.getInputPath(video, "mp4", tempDir);
|
|
421
|
+
const audioPath = await this.getInputPath(audio, "mp3", tempDir);
|
|
422
|
+
const outputPath = join(tempDir, "output.mp4");
|
|
423
|
+
const mode = options.mode ?? "replace";
|
|
424
|
+
const args = [
|
|
425
|
+
"-y",
|
|
426
|
+
"-i",
|
|
427
|
+
videoPath,
|
|
428
|
+
"-i",
|
|
429
|
+
audioPath
|
|
430
|
+
];
|
|
431
|
+
if (mode === "replace") {
|
|
432
|
+
args.push("-map", "0:v", "-map", "1:a");
|
|
433
|
+
if (options.newVolume !== void 0 && options.newVolume !== 1) args.push("-af", `volume=${options.newVolume}`);
|
|
434
|
+
if (options.fadeIn || options.fadeOut) {
|
|
435
|
+
const fadeFilters = [];
|
|
436
|
+
if (options.fadeIn) fadeFilters.push(`afade=t=in:d=${options.fadeIn}`);
|
|
437
|
+
if (options.fadeOut) fadeFilters.push(`afade=t=out:d=${options.fadeOut}`);
|
|
438
|
+
args.push("-af", fadeFilters.join(","));
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
const origVol = options.originalVolume ?? .5;
|
|
442
|
+
const newVol = options.newVolume ?? 1;
|
|
443
|
+
args.push("-filter_complex", `[0:a]volume=${origVol}[a0];[1:a]volume=${newVol}[a1];[a0][a1]amix=inputs=2:duration=first[a]`, "-map", "0:v", "-map", "[a]");
|
|
444
|
+
}
|
|
445
|
+
args.push("-c:v", "copy", "-shortest", outputPath);
|
|
446
|
+
await this.runFFmpeg(args);
|
|
447
|
+
return await readFile(outputPath);
|
|
448
|
+
} finally {
|
|
449
|
+
rmSync(tempDir, {
|
|
450
|
+
recursive: true,
|
|
451
|
+
force: true
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Convert animated WEBP to MP4
|
|
457
|
+
*
|
|
458
|
+
* Uses sharp to extract frames from animated WEBP (which ffmpeg can't decode
|
|
459
|
+
* natively), then assembles them into an MP4 using ffmpeg.
|
|
460
|
+
*/
|
|
461
|
+
async convertWebpToMp4(webp, options = {}) {
|
|
462
|
+
const fps = options.fps ?? 16;
|
|
463
|
+
const tempDir = this.createTempDir();
|
|
464
|
+
try {
|
|
465
|
+
const metadata = await sharp(webp, {
|
|
466
|
+
animated: true,
|
|
467
|
+
pages: -1
|
|
468
|
+
}).metadata();
|
|
469
|
+
const pageHeight = metadata.pageHeight ?? metadata.height ?? 480;
|
|
470
|
+
const frameCount = metadata.pages ?? 1;
|
|
471
|
+
this.logger.debug("Extracting WEBP frames", {
|
|
472
|
+
frameCount,
|
|
473
|
+
width: metadata.width,
|
|
474
|
+
pageHeight
|
|
475
|
+
});
|
|
476
|
+
for (let i = 0; i < frameCount; i++) {
|
|
477
|
+
const frame = await sharp(webp, {
|
|
478
|
+
page: i,
|
|
479
|
+
pages: 1
|
|
480
|
+
}).png().toBuffer();
|
|
481
|
+
await writeFile(join(tempDir, `frame_${String(i + 1).padStart(4, "0")}.png`), frame);
|
|
482
|
+
}
|
|
483
|
+
const outputPath = join(tempDir, "output.mp4");
|
|
484
|
+
const args = [
|
|
485
|
+
"-y",
|
|
486
|
+
"-framerate",
|
|
487
|
+
fps.toString(),
|
|
488
|
+
"-start_number",
|
|
489
|
+
"1",
|
|
490
|
+
"-i",
|
|
491
|
+
join(tempDir, "frame_%04d.png")
|
|
492
|
+
];
|
|
493
|
+
if (options.video) args.push(...this.getVideoCodecArgs(options.video));
|
|
494
|
+
else args.push("-c:v", "libx264", "-pix_fmt", "yuv420p");
|
|
495
|
+
args.push(outputPath);
|
|
496
|
+
await this.runFFmpeg(args);
|
|
497
|
+
return await readFile(outputPath);
|
|
498
|
+
} finally {
|
|
499
|
+
rmSync(tempDir, {
|
|
500
|
+
recursive: true,
|
|
501
|
+
force: true
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Get overlay position as FFmpeg expression
|
|
507
|
+
*/
|
|
508
|
+
getOverlayPosition(position) {
|
|
509
|
+
const padding = position.padding ?? 20;
|
|
510
|
+
let x;
|
|
511
|
+
let y;
|
|
512
|
+
if (typeof position.x === "number") x = position.x.toString();
|
|
513
|
+
else switch (position.x) {
|
|
514
|
+
case "left":
|
|
515
|
+
x = padding.toString();
|
|
516
|
+
break;
|
|
517
|
+
case "center":
|
|
518
|
+
x = "(W-w)/2";
|
|
519
|
+
break;
|
|
520
|
+
case "right":
|
|
521
|
+
x = `W-w-${padding}`;
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
if (typeof position.y === "number") y = position.y.toString();
|
|
525
|
+
else switch (position.y) {
|
|
526
|
+
case "top":
|
|
527
|
+
y = padding.toString();
|
|
528
|
+
break;
|
|
529
|
+
case "center":
|
|
530
|
+
y = "(H-h)/2";
|
|
531
|
+
break;
|
|
532
|
+
case "bottom":
|
|
533
|
+
y = `H-h-${padding}`;
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
return {
|
|
537
|
+
x,
|
|
538
|
+
y
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Get FFmpeg enable expression for time-based visibility
|
|
543
|
+
*/
|
|
544
|
+
getEnableExpression(startTime, endTime) {
|
|
545
|
+
if (startTime !== void 0 && endTime !== void 0) return `between(t,${startTime},${endTime})`;
|
|
546
|
+
if (startTime !== void 0) return `gte(t,${startTime})`;
|
|
547
|
+
if (endTime !== void 0) return `lte(t,${endTime})`;
|
|
548
|
+
return "1";
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Escape text for FFmpeg drawtext filter
|
|
552
|
+
*/
|
|
553
|
+
escapeFFmpegText(text) {
|
|
554
|
+
return text.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/:/g, "\\:").replace(/\[/g, "\\[").replace(/\]/g, "\\]");
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
//#endregion
|
|
558
|
+
//#region src/media-bundles/utils.ts
|
|
559
|
+
var execFile$1 = promisify(execFile);
|
|
560
|
+
var VIDEO_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
561
|
+
".mp4",
|
|
562
|
+
".mov",
|
|
563
|
+
".m4v",
|
|
564
|
+
".avi",
|
|
565
|
+
".webm",
|
|
566
|
+
".insv",
|
|
567
|
+
".lrv"
|
|
584
568
|
]);
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
569
|
+
var IMAGE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
570
|
+
".jpg",
|
|
571
|
+
".jpeg",
|
|
572
|
+
".png",
|
|
573
|
+
".webp",
|
|
574
|
+
".gif",
|
|
575
|
+
".tif",
|
|
576
|
+
".tiff",
|
|
577
|
+
".heic",
|
|
578
|
+
".insp"
|
|
595
579
|
]);
|
|
596
580
|
function displayName(file) {
|
|
597
|
-
|
|
581
|
+
return file.name || basename(file.relativePath || file.path);
|
|
598
582
|
}
|
|
599
583
|
function fileExtension(file) {
|
|
600
|
-
|
|
584
|
+
return extname(displayName(file)).toLowerCase();
|
|
601
585
|
}
|
|
602
586
|
function isVideoFile(file) {
|
|
603
|
-
|
|
604
|
-
return mime.startsWith("video/") || VIDEO_EXTENSIONS.has(fileExtension(file));
|
|
587
|
+
return (file.mimeType || "").toLowerCase().startsWith("video/") || VIDEO_EXTENSIONS.has(fileExtension(file));
|
|
605
588
|
}
|
|
606
589
|
function isImageFile(file) {
|
|
607
|
-
|
|
608
|
-
return mime.startsWith("image/") || IMAGE_EXTENSIONS.has(fileExtension(file));
|
|
590
|
+
return (file.mimeType || "").toLowerCase().startsWith("image/") || IMAGE_EXTENSIONS.has(fileExtension(file));
|
|
609
591
|
}
|
|
610
592
|
function formatError(error) {
|
|
611
|
-
|
|
593
|
+
return error instanceof Error ? error.message : String(error);
|
|
612
594
|
}
|
|
613
595
|
async function runJsonCommand(bin, args) {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
});
|
|
617
|
-
return JSON.parse(stdout || "{}");
|
|
596
|
+
const { stdout } = await execFile$1(bin, args, { maxBuffer: 100 * 1024 * 1024 });
|
|
597
|
+
return JSON.parse(stdout || "{}");
|
|
618
598
|
}
|
|
619
599
|
async function runJsonArrayCommand(bin, args) {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
});
|
|
623
|
-
return JSON.parse(stdout || "[]");
|
|
600
|
+
const { stdout } = await execFile$1(bin, args, { maxBuffer: 100 * 1024 * 1024 });
|
|
601
|
+
return JSON.parse(stdout || "[]");
|
|
624
602
|
}
|
|
625
603
|
async function probeVideoMetadata(file, ffprobePath = "ffprobe") {
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
streams: normalizedStreams,
|
|
650
|
-
raw: { ffprobe: probe }
|
|
651
|
-
};
|
|
604
|
+
const probe = await runJsonCommand(ffprobePath, [
|
|
605
|
+
"-v",
|
|
606
|
+
"error",
|
|
607
|
+
"-print_format",
|
|
608
|
+
"json",
|
|
609
|
+
"-show_streams",
|
|
610
|
+
"-show_format",
|
|
611
|
+
file.path
|
|
612
|
+
]);
|
|
613
|
+
const format = isRecord(probe.format) ? probe.format : {};
|
|
614
|
+
const normalizedStreams = (Array.isArray(probe.streams) ? probe.streams.filter(isRecord) : []).map(normalizeFfprobeStream);
|
|
615
|
+
const videoStream = normalizedStreams.find((stream) => stream.kind === "video");
|
|
616
|
+
const tags = isRecord(format.tags) ? format.tags : {};
|
|
617
|
+
const durationMs = toDurationMs(format.duration);
|
|
618
|
+
return {
|
|
619
|
+
captureTime: parseDateString(String(tags.creation_time || tags.CreateDate || "")) ?? null,
|
|
620
|
+
width: videoStream?.width,
|
|
621
|
+
height: videoStream?.height,
|
|
622
|
+
durationMs,
|
|
623
|
+
mimeType: file.mimeType,
|
|
624
|
+
streams: normalizedStreams,
|
|
625
|
+
raw: { ffprobe: probe }
|
|
626
|
+
};
|
|
652
627
|
}
|
|
653
628
|
async function probeEmbeddedMetadata(file, exiftoolPath = "exiftool") {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
629
|
+
const rows = await runJsonArrayCommand(exiftoolPath, [
|
|
630
|
+
"-ee",
|
|
631
|
+
"-G3:1",
|
|
632
|
+
"-json",
|
|
633
|
+
"-n",
|
|
634
|
+
file.path
|
|
635
|
+
]);
|
|
636
|
+
const row = rows[0] && isRecord(rows[0]) ? rows[0] : {};
|
|
637
|
+
const flat = flattenExiftoolRecord(row);
|
|
638
|
+
const device = readDevice(flat);
|
|
639
|
+
const gpsTrack = parseExiftoolGpsRecord(row, file.path);
|
|
640
|
+
const captureTime = flat.DateTimeOriginal || flat.CreateDate || flat.MediaCreateDate || flat.GPSDateTime || "";
|
|
641
|
+
return {
|
|
642
|
+
captureTime: parseDateString(String(captureTime)) ?? null,
|
|
643
|
+
device,
|
|
644
|
+
width: toInteger(flat.ImageWidth ?? flat.ExifImageWidth),
|
|
645
|
+
height: toInteger(flat.ImageHeight ?? flat.ExifImageHeight),
|
|
646
|
+
gpsTrack,
|
|
647
|
+
raw: { exiftool: row }
|
|
648
|
+
};
|
|
674
649
|
}
|
|
675
650
|
function mergeMetadata(primary, fallback) {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
651
|
+
return {
|
|
652
|
+
captureTime: primary.captureTime ?? fallback.captureTime,
|
|
653
|
+
device: {
|
|
654
|
+
...fallback.device,
|
|
655
|
+
...primary.device
|
|
656
|
+
},
|
|
657
|
+
width: primary.width ?? fallback.width,
|
|
658
|
+
height: primary.height ?? fallback.height,
|
|
659
|
+
durationMs: primary.durationMs ?? fallback.durationMs,
|
|
660
|
+
mimeType: primary.mimeType ?? fallback.mimeType,
|
|
661
|
+
streams: primary.streams && primary.streams.length > 0 ? primary.streams : fallback.streams,
|
|
662
|
+
gpsTrack: primary.gpsTrack && primary.gpsTrack.length > 0 ? primary.gpsTrack : fallback.gpsTrack,
|
|
663
|
+
raw: {
|
|
664
|
+
...fallback.raw,
|
|
665
|
+
...primary.raw
|
|
666
|
+
},
|
|
667
|
+
private: {
|
|
668
|
+
...fallback.private,
|
|
669
|
+
...primary.private
|
|
670
|
+
}
|
|
671
|
+
};
|
|
697
672
|
}
|
|
698
673
|
function normalizeFfprobeStream(stream) {
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
674
|
+
const kind = String(stream.codec_type || "unknown");
|
|
675
|
+
return {
|
|
676
|
+
kind: [
|
|
677
|
+
"video",
|
|
678
|
+
"audio",
|
|
679
|
+
"data"
|
|
680
|
+
].includes(kind) ? kind : "unknown",
|
|
681
|
+
codec: readString(stream.codec_name),
|
|
682
|
+
width: toInteger(stream.width),
|
|
683
|
+
height: toInteger(stream.height),
|
|
684
|
+
fps: parseFrameRate(readString(stream.r_frame_rate)),
|
|
685
|
+
durationMs: toDurationMs(stream.duration),
|
|
686
|
+
bitrate: toInteger(stream.bit_rate),
|
|
687
|
+
metadata: stream
|
|
688
|
+
};
|
|
712
689
|
}
|
|
713
690
|
function flattenExiftoolRecord(row) {
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
691
|
+
const out = {};
|
|
692
|
+
for (const [key, value] of Object.entries(row)) {
|
|
693
|
+
out[key] = value;
|
|
694
|
+
const suffix = key.split(":").at(-1);
|
|
695
|
+
if (suffix && !(suffix in out)) out[suffix] = value;
|
|
696
|
+
}
|
|
697
|
+
return out;
|
|
721
698
|
}
|
|
722
699
|
function readDevice(row) {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
700
|
+
const model = readString(row.Model);
|
|
701
|
+
const make = readString(row.Make) || inferMake(model);
|
|
702
|
+
const serialNumber = readString(row.SerialNumber);
|
|
703
|
+
const firmwareVersion = readString(row.Firmware);
|
|
704
|
+
if (!make && !model && !serialNumber && !firmwareVersion) return void 0;
|
|
705
|
+
return {
|
|
706
|
+
make,
|
|
707
|
+
model,
|
|
708
|
+
serialNumber,
|
|
709
|
+
firmwareVersion
|
|
710
|
+
};
|
|
729
711
|
}
|
|
730
712
|
function inferMake(model) {
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
713
|
+
if (!model) return void 0;
|
|
714
|
+
const vendors = [
|
|
715
|
+
"Insta360",
|
|
716
|
+
"GoPro",
|
|
717
|
+
"DJI",
|
|
718
|
+
"Sony",
|
|
719
|
+
"Canon",
|
|
720
|
+
"Nikon",
|
|
721
|
+
"Ricoh",
|
|
722
|
+
"Panasonic",
|
|
723
|
+
"Fujifilm"
|
|
724
|
+
];
|
|
725
|
+
const lowered = model.toLowerCase();
|
|
726
|
+
return vendors.find((vendor) => lowered.startsWith(vendor.toLowerCase()));
|
|
745
727
|
}
|
|
746
728
|
function parseExiftoolGpsRecord(row, sourceFilePath) {
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
}
|
|
804
|
-
];
|
|
729
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
730
|
+
const mainFields = {};
|
|
731
|
+
for (const [key, value] of Object.entries(row)) {
|
|
732
|
+
const docMatch = /^(Doc\d+):[^:]+:(\w+)$/.exec(key);
|
|
733
|
+
if (docMatch) {
|
|
734
|
+
const bucket = grouped.get(docMatch[1]) ?? {};
|
|
735
|
+
bucket[docMatch[2]] = value;
|
|
736
|
+
grouped.set(docMatch[1], bucket);
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
const mainMatch = /^Main:[^:]+:(\w+)$/.exec(key);
|
|
740
|
+
if (mainMatch) mainFields[mainMatch[1]] = value;
|
|
741
|
+
}
|
|
742
|
+
const out = [];
|
|
743
|
+
let firstEpochMs = null;
|
|
744
|
+
let lastLat = null;
|
|
745
|
+
let lastLon = null;
|
|
746
|
+
for (const docId of [...grouped.keys()].sort(compareDocIds)) {
|
|
747
|
+
const record = grouped.get(docId);
|
|
748
|
+
if (!record) continue;
|
|
749
|
+
const lat = toNumber(record.GPSLatitude);
|
|
750
|
+
const lon = toNumber(record.GPSLongitude);
|
|
751
|
+
const epochMs = record.GPSDateTime == null ? null : parseDateMs(String(record.GPSDateTime));
|
|
752
|
+
if (lat == null || lon == null || epochMs == null) continue;
|
|
753
|
+
if (firstEpochMs == null) firstEpochMs = epochMs;
|
|
754
|
+
if (lat === lastLat && lon === lastLon) continue;
|
|
755
|
+
lastLat = lat;
|
|
756
|
+
lastLon = lon;
|
|
757
|
+
out.push({
|
|
758
|
+
tSeconds: Math.max(0, (epochMs - firstEpochMs) / 1e3),
|
|
759
|
+
recordedAt: new Date(epochMs).toISOString(),
|
|
760
|
+
latitude: lat,
|
|
761
|
+
longitude: lon,
|
|
762
|
+
altitude: toNumber(record.GPSAltitude),
|
|
763
|
+
heading: toNumber(record.GPSTrack),
|
|
764
|
+
speedMps: toNumber(record.GPSSpeed),
|
|
765
|
+
sourceFilePath
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
if (out.length > 0) return out;
|
|
769
|
+
const flat = flattenExiftoolRecord(row);
|
|
770
|
+
const fallback = Object.keys(mainFields).length > 0 ? mainFields : flat;
|
|
771
|
+
const lat = toNumber(fallback.GPSLatitude);
|
|
772
|
+
const lon = toNumber(fallback.GPSLongitude);
|
|
773
|
+
const epochMs = fallback.GPSDateTime == null ? null : parseDateMs(String(fallback.GPSDateTime));
|
|
774
|
+
if (lat == null || lon == null || epochMs == null) return [];
|
|
775
|
+
return [{
|
|
776
|
+
tSeconds: 0,
|
|
777
|
+
recordedAt: new Date(epochMs).toISOString(),
|
|
778
|
+
latitude: lat,
|
|
779
|
+
longitude: lon,
|
|
780
|
+
altitude: toNumber(fallback.GPSAltitude),
|
|
781
|
+
heading: toNumber(fallback.GPSTrack ?? fallback.GPSImgDirection),
|
|
782
|
+
speedMps: toNumber(fallback.GPSSpeed),
|
|
783
|
+
sourceFilePath
|
|
784
|
+
}];
|
|
805
785
|
}
|
|
806
786
|
function compareDocIds(left, right) {
|
|
807
|
-
|
|
787
|
+
return Number(left.replace(/\D+/g, "")) - Number(right.replace(/\D+/g, ""));
|
|
808
788
|
}
|
|
809
789
|
function parseFrameRate(value) {
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
790
|
+
if (!value) return void 0;
|
|
791
|
+
const [num, den] = value.split("/").map(Number);
|
|
792
|
+
const fps = den ? num / den : num;
|
|
793
|
+
return Number.isFinite(fps) && fps > 0 ? fps : void 0;
|
|
814
794
|
}
|
|
815
795
|
function toDurationMs(value) {
|
|
816
|
-
|
|
817
|
-
|
|
796
|
+
const seconds = Number(value);
|
|
797
|
+
return Number.isFinite(seconds) && seconds > 0 ? Math.round(seconds * 1e3) : void 0;
|
|
818
798
|
}
|
|
819
799
|
function toInteger(value) {
|
|
820
|
-
|
|
821
|
-
|
|
800
|
+
const number = Number(value);
|
|
801
|
+
return Number.isInteger(number) && number > 0 ? number : void 0;
|
|
822
802
|
}
|
|
823
803
|
function toNumber(value) {
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
804
|
+
if (value == null || value === "") return null;
|
|
805
|
+
const number = Number(value);
|
|
806
|
+
return Number.isFinite(number) ? number : null;
|
|
827
807
|
}
|
|
828
808
|
function readString(value) {
|
|
829
|
-
|
|
809
|
+
return typeof value === "string" && value.trim() ? value.trim() : void 0;
|
|
830
810
|
}
|
|
831
811
|
function parseDateString(value) {
|
|
832
|
-
|
|
833
|
-
|
|
812
|
+
const ms = parseDateMs(value);
|
|
813
|
+
return ms == null ? null : new Date(ms).toISOString();
|
|
834
814
|
}
|
|
835
815
|
function parseDateMs(value) {
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
816
|
+
const normalized = value.trim().replace(/^(\d{4}):(\d{2}):(\d{2})/, "$1-$2-$3");
|
|
817
|
+
if (!normalized) return null;
|
|
818
|
+
const ms = Date.parse(normalized);
|
|
819
|
+
return Number.isFinite(ms) ? ms : null;
|
|
840
820
|
}
|
|
841
821
|
function isRecord(value) {
|
|
842
|
-
|
|
822
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
843
823
|
}
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
}
|
|
824
|
+
//#endregion
|
|
825
|
+
//#region src/media-bundles/handlers/generic-image.ts
|
|
826
|
+
var genericImageBundleHandler = {
|
|
827
|
+
id: "generic-image",
|
|
828
|
+
version: "1.0.0",
|
|
829
|
+
priority: 10,
|
|
830
|
+
capabilities: ["image-metadata"],
|
|
831
|
+
supports(files) {
|
|
832
|
+
return files.some(isImageFile);
|
|
833
|
+
},
|
|
834
|
+
async inspect(files, context) {
|
|
835
|
+
const primary = files.find(isImageFile) ?? files[0];
|
|
836
|
+
const warnings = [];
|
|
837
|
+
let metadata = { mimeType: primary.mimeType };
|
|
838
|
+
if (context.probe === false) warnings.push("probe disabled; returning file-level generic image inspection");
|
|
839
|
+
else try {
|
|
840
|
+
const image = await getImageMetadata(primary.path);
|
|
841
|
+
metadata = {
|
|
842
|
+
width: image.width,
|
|
843
|
+
height: image.height,
|
|
844
|
+
mimeType: primary.mimeType,
|
|
845
|
+
raw: { image }
|
|
846
|
+
};
|
|
847
|
+
} catch (error) {
|
|
848
|
+
warnings.push(`image metadata probe failed for ${displayName(primary)}: ${formatError(error)}`);
|
|
849
|
+
}
|
|
850
|
+
return {
|
|
851
|
+
handlerId: genericImageBundleHandler.id,
|
|
852
|
+
handlerVersion: genericImageBundleHandler.version,
|
|
853
|
+
formatFamily: "generic-image",
|
|
854
|
+
primary: {
|
|
855
|
+
...primary,
|
|
856
|
+
role: "primary"
|
|
857
|
+
},
|
|
858
|
+
supportFiles: files.filter((file) => file !== primary).map((file) => ({
|
|
859
|
+
file: {
|
|
860
|
+
...file,
|
|
861
|
+
role: file.role ?? "support"
|
|
862
|
+
},
|
|
863
|
+
role: "support",
|
|
864
|
+
relationship: "co-located-file",
|
|
865
|
+
visibility: context.defaultSupportFileVisibility ?? "hidden-retained"
|
|
866
|
+
})),
|
|
867
|
+
metadata,
|
|
868
|
+
capabilities: [...genericImageBundleHandler.capabilities ?? []],
|
|
869
|
+
warnings,
|
|
870
|
+
errors: []
|
|
871
|
+
};
|
|
872
|
+
}
|
|
894
873
|
};
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
primary: { ...primary, role: "primary" },
|
|
949
|
-
supportFiles,
|
|
950
|
-
metadata,
|
|
951
|
-
capabilities: [...capabilities],
|
|
952
|
-
warnings,
|
|
953
|
-
errors: []
|
|
954
|
-
};
|
|
955
|
-
}
|
|
874
|
+
//#endregion
|
|
875
|
+
//#region src/media-bundles/handlers/generic-video.ts
|
|
876
|
+
var genericVideoBundleHandler = {
|
|
877
|
+
id: "generic-video",
|
|
878
|
+
version: "1.0.0",
|
|
879
|
+
priority: 10,
|
|
880
|
+
capabilities: ["stream-info"],
|
|
881
|
+
supports(files) {
|
|
882
|
+
return files.some(isVideoFile);
|
|
883
|
+
},
|
|
884
|
+
async inspect(files, context) {
|
|
885
|
+
const primary = files.find(isVideoFile) ?? files[0];
|
|
886
|
+
const warnings = [];
|
|
887
|
+
const supportFiles = files.filter((file) => file !== primary).map((file) => ({
|
|
888
|
+
file: {
|
|
889
|
+
...file,
|
|
890
|
+
role: file.role ?? "support"
|
|
891
|
+
},
|
|
892
|
+
role: "support",
|
|
893
|
+
relationship: "co-located-file",
|
|
894
|
+
visibility: context.defaultSupportFileVisibility ?? "hidden-retained"
|
|
895
|
+
}));
|
|
896
|
+
let metadata = { mimeType: primary.mimeType };
|
|
897
|
+
if (context.probe === false) warnings.push("probe disabled; returning file-level generic video inspection");
|
|
898
|
+
else {
|
|
899
|
+
try {
|
|
900
|
+
metadata = mergeMetadata(await probeVideoMetadata(primary, context.tools?.ffprobePath), metadata);
|
|
901
|
+
} catch (error) {
|
|
902
|
+
warnings.push(`ffprobe failed for ${displayName(primary)}: ${formatError(error)}`);
|
|
903
|
+
}
|
|
904
|
+
try {
|
|
905
|
+
metadata = mergeMetadata(await probeEmbeddedMetadata(primary, context.tools?.exiftoolPath), metadata);
|
|
906
|
+
} catch (error) {
|
|
907
|
+
warnings.push(`exiftool failed for ${displayName(primary)}: ${formatError(error)}`);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
const capabilities = new Set(genericVideoBundleHandler.capabilities);
|
|
911
|
+
if (metadata.gpsTrack && metadata.gpsTrack.length > 0) capabilities.add("gps-track");
|
|
912
|
+
return {
|
|
913
|
+
handlerId: genericVideoBundleHandler.id,
|
|
914
|
+
handlerVersion: genericVideoBundleHandler.version,
|
|
915
|
+
formatFamily: "generic-video",
|
|
916
|
+
primary: {
|
|
917
|
+
...primary,
|
|
918
|
+
role: "primary"
|
|
919
|
+
},
|
|
920
|
+
supportFiles,
|
|
921
|
+
metadata,
|
|
922
|
+
capabilities: [...capabilities],
|
|
923
|
+
warnings,
|
|
924
|
+
errors: []
|
|
925
|
+
};
|
|
926
|
+
}
|
|
956
927
|
};
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
if (metadata.gpsTrack && metadata.gpsTrack.length > 0)
|
|
1056
|
-
capabilities.add("gps-track");
|
|
1057
|
-
return {
|
|
1058
|
-
handlerId: insta360BundleHandler.id,
|
|
1059
|
-
handlerVersion: insta360BundleHandler.version,
|
|
1060
|
-
formatFamily: "insta360",
|
|
1061
|
-
primary: { ...primary, role: "primary" },
|
|
1062
|
-
supportFiles,
|
|
1063
|
-
metadata,
|
|
1064
|
-
capabilities: [...capabilities],
|
|
1065
|
-
warnings,
|
|
1066
|
-
errors: [],
|
|
1067
|
-
raw: {
|
|
1068
|
-
insta360: {
|
|
1069
|
-
bundleKey,
|
|
1070
|
-
files: Object.fromEntries(
|
|
1071
|
-
bundleFiles.map((file) => [file.path, parsedByPath.get(file.path)])
|
|
1072
|
-
),
|
|
1073
|
-
ignoredFiles: ignoredFiles.map((file) => ({
|
|
1074
|
-
file,
|
|
1075
|
-
identity: parsedByPath.get(file.path) ?? null
|
|
1076
|
-
}))
|
|
1077
|
-
}
|
|
1078
|
-
}
|
|
1079
|
-
};
|
|
1080
|
-
}
|
|
928
|
+
//#endregion
|
|
929
|
+
//#region src/media-bundles/handlers/insta360.ts
|
|
930
|
+
var INSTA360_RE = /^(VID|LRV)_(\d{8})_(\d{6})_(\d{2})_(\d+)\.(insv|lrv)$/i;
|
|
931
|
+
var insta360BundleHandler = {
|
|
932
|
+
id: "insta360-insv-lrv",
|
|
933
|
+
version: "1.0.0",
|
|
934
|
+
priority: 100,
|
|
935
|
+
capabilities: [
|
|
936
|
+
"sidecar-binding",
|
|
937
|
+
"stitch-export",
|
|
938
|
+
"stream-info"
|
|
939
|
+
],
|
|
940
|
+
supports(files) {
|
|
941
|
+
return files.some((file) => parseInsta360FileName(file));
|
|
942
|
+
},
|
|
943
|
+
async inspect(files, context) {
|
|
944
|
+
const parsedByPath = new Map(files.map((file) => [file.path, parseInsta360FileName(file)]));
|
|
945
|
+
const bundleKey = chooseBundleKey(files, parsedByPath);
|
|
946
|
+
const bundleFiles = files.filter((file) => {
|
|
947
|
+
const parsed = parsedByPath.get(file.path);
|
|
948
|
+
return Boolean(parsed && (!bundleKey || parsed.bundleKey === bundleKey));
|
|
949
|
+
});
|
|
950
|
+
const ignoredFiles = files.filter((file) => !bundleFiles.includes(file));
|
|
951
|
+
const primary = choosePrimary(bundleFiles, parsedByPath);
|
|
952
|
+
const warnings = [];
|
|
953
|
+
if (ignoredFiles.length > 0) warnings.push(`ignored ${ignoredFiles.length} file(s) outside Insta360 bundle ${bundleKey ?? "unknown"}: ${ignoredFiles.map(displayName).join(", ")}`);
|
|
954
|
+
let metadata = {
|
|
955
|
+
mimeType: primary.mimeType,
|
|
956
|
+
private: { "happyvertical.video.insta360": {
|
|
957
|
+
primary: parsedByPath.get(primary.path) ?? null,
|
|
958
|
+
supportFiles: bundleFiles.filter((file) => file !== primary).map((file) => parsedByPath.get(file.path) ?? null)
|
|
959
|
+
} }
|
|
960
|
+
};
|
|
961
|
+
const nonPrimaryBundleFiles = bundleFiles.filter((file) => file !== primary);
|
|
962
|
+
const supportFiles = await Promise.all(nonPrimaryBundleFiles.map(async (file) => {
|
|
963
|
+
const parsed = parsedByPath.get(file.path);
|
|
964
|
+
let supportMetadata;
|
|
965
|
+
if (context.probe !== false) try {
|
|
966
|
+
supportMetadata = await probeEmbeddedMetadata(file, context.tools?.exiftoolPath);
|
|
967
|
+
} catch (error) {
|
|
968
|
+
warnings.push(`exiftool failed for support file ${displayName(file)}: ${formatError(error)}`);
|
|
969
|
+
}
|
|
970
|
+
return {
|
|
971
|
+
file: {
|
|
972
|
+
...file,
|
|
973
|
+
role: "support"
|
|
974
|
+
},
|
|
975
|
+
role: "support",
|
|
976
|
+
relationship: relationshipForInsta360SupportFile(file, parsed),
|
|
977
|
+
visibility: context.defaultSupportFileVisibility ?? "hidden-retained",
|
|
978
|
+
metadata: supportMetadata
|
|
979
|
+
};
|
|
980
|
+
}));
|
|
981
|
+
if (context.probe === false) warnings.push("probe disabled; returning file-level Insta360 bundle inspection");
|
|
982
|
+
else {
|
|
983
|
+
try {
|
|
984
|
+
metadata = mergeMetadata(await probeVideoMetadata(primary, context.tools?.ffprobePath), metadata);
|
|
985
|
+
} catch (error) {
|
|
986
|
+
warnings.push(`ffprobe failed for primary ${displayName(primary)}: ${formatError(error)}`);
|
|
987
|
+
}
|
|
988
|
+
try {
|
|
989
|
+
metadata = mergeMetadata(await probeEmbeddedMetadata(primary, context.tools?.exiftoolPath), metadata);
|
|
990
|
+
} catch (error) {
|
|
991
|
+
warnings.push(`exiftool failed for primary ${displayName(primary)}: ${formatError(error)}`);
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
if (!metadata.gpsTrack || metadata.gpsTrack.length === 0) {
|
|
995
|
+
const supportGps = supportFiles.map((support) => support.metadata).find((candidate) => candidate?.gpsTrack && candidate.gpsTrack.length > 0);
|
|
996
|
+
if (supportGps) metadata = {
|
|
997
|
+
...metadata,
|
|
998
|
+
gpsTrack: supportGps.gpsTrack
|
|
999
|
+
};
|
|
1000
|
+
}
|
|
1001
|
+
const capabilities = new Set(insta360BundleHandler.capabilities);
|
|
1002
|
+
if (metadata.gpsTrack && metadata.gpsTrack.length > 0) capabilities.add("gps-track");
|
|
1003
|
+
return {
|
|
1004
|
+
handlerId: insta360BundleHandler.id,
|
|
1005
|
+
handlerVersion: insta360BundleHandler.version,
|
|
1006
|
+
formatFamily: "insta360",
|
|
1007
|
+
primary: {
|
|
1008
|
+
...primary,
|
|
1009
|
+
role: "primary"
|
|
1010
|
+
},
|
|
1011
|
+
supportFiles,
|
|
1012
|
+
metadata,
|
|
1013
|
+
capabilities: [...capabilities],
|
|
1014
|
+
warnings,
|
|
1015
|
+
errors: [],
|
|
1016
|
+
raw: { insta360: {
|
|
1017
|
+
bundleKey,
|
|
1018
|
+
files: Object.fromEntries(bundleFiles.map((file) => [file.path, parsedByPath.get(file.path)])),
|
|
1019
|
+
ignoredFiles: ignoredFiles.map((file) => ({
|
|
1020
|
+
file,
|
|
1021
|
+
identity: parsedByPath.get(file.path) ?? null
|
|
1022
|
+
}))
|
|
1023
|
+
} }
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1081
1026
|
};
|
|
1082
1027
|
function parseInsta360FileName(file) {
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
};
|
|
1028
|
+
const match = INSTA360_RE.exec(displayName(file));
|
|
1029
|
+
if (!match) return null;
|
|
1030
|
+
const prefix = match[1].toUpperCase();
|
|
1031
|
+
const date = match[2];
|
|
1032
|
+
const time = match[3];
|
|
1033
|
+
const streamIndex = match[4];
|
|
1034
|
+
const sequence = match[5];
|
|
1035
|
+
return {
|
|
1036
|
+
prefix,
|
|
1037
|
+
date,
|
|
1038
|
+
time,
|
|
1039
|
+
streamIndex,
|
|
1040
|
+
sequence,
|
|
1041
|
+
extension: match[6].toLowerCase(),
|
|
1042
|
+
bundleKey: `${date}_${time}_${sequence}`
|
|
1043
|
+
};
|
|
1100
1044
|
}
|
|
1101
1045
|
function chooseBundleKey(files, parsedByPath) {
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1046
|
+
const candidate = files.map((file) => parsedByPath.get(file.path)).find((parsed) => parsed?.prefix === "VID" && parsed.streamIndex === "00");
|
|
1047
|
+
if (candidate) return candidate.bundleKey;
|
|
1048
|
+
return files.map((file) => parsedByPath.get(file.path)).find(Boolean)?.bundleKey ?? null;
|
|
1105
1049
|
}
|
|
1106
1050
|
function choosePrimary(files, parsedByPath) {
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1051
|
+
return files.find((file) => {
|
|
1052
|
+
const parsed = parsedByPath.get(file.path);
|
|
1053
|
+
return parsed?.prefix === "VID" && parsed.streamIndex === "00";
|
|
1054
|
+
}) ?? files.find((file) => {
|
|
1055
|
+
const parsed = parsedByPath.get(file.path);
|
|
1056
|
+
return parsed?.prefix === "VID" && parsed.extension === "insv";
|
|
1057
|
+
}) ?? files.find((file) => fileExtension(file) === ".insv") ?? files[0];
|
|
1114
1058
|
}
|
|
1115
1059
|
function relationshipForInsta360SupportFile(file, parsed) {
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
return "paired-video-stream";
|
|
1120
|
-
return "support-file";
|
|
1060
|
+
if (parsed?.prefix === "LRV" || fileExtension(file) === ".lrv") return "proxy-video";
|
|
1061
|
+
if (parsed?.prefix === "VID" && parsed.streamIndex !== "00") return "paired-video-stream";
|
|
1062
|
+
return "support-file";
|
|
1121
1063
|
}
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1064
|
+
//#endregion
|
|
1065
|
+
//#region src/media-bundles/index.ts
|
|
1066
|
+
var defaultMediaBundleHandlers = [
|
|
1067
|
+
insta360BundleHandler,
|
|
1068
|
+
genericVideoBundleHandler,
|
|
1069
|
+
genericImageBundleHandler
|
|
1126
1070
|
];
|
|
1127
1071
|
async function inspectMediaBundle(files, options = {}) {
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1072
|
+
if (files.length === 0) throw new Error("inspectMediaBundle requires at least one file");
|
|
1073
|
+
const context = {
|
|
1074
|
+
...options,
|
|
1075
|
+
defaultSupportFileVisibility: options.defaultSupportFileVisibility ?? "hidden-retained"
|
|
1076
|
+
};
|
|
1077
|
+
const orderedHandlers = (options.handlers ?? defaultMediaBundleHandlers).map((handler, index) => ({
|
|
1078
|
+
handler,
|
|
1079
|
+
index
|
|
1080
|
+
})).sort((left, right) => right.handler.priority - left.handler.priority || left.index - right.index);
|
|
1081
|
+
for (const { handler } of orderedHandlers) {
|
|
1082
|
+
if (!await handler.supports(files, context)) continue;
|
|
1083
|
+
const inspection = await handler.inspect(files, context);
|
|
1084
|
+
const validationWarnings = handler.validate ? await handler.validate(inspection, context) : [];
|
|
1085
|
+
if (validationWarnings.length > 0) inspection.warnings.push(...validationWarnings);
|
|
1086
|
+
return inspection;
|
|
1087
|
+
}
|
|
1088
|
+
return {
|
|
1089
|
+
handlerId: "unknown",
|
|
1090
|
+
handlerVersion: "1.0.0",
|
|
1091
|
+
formatFamily: "unknown",
|
|
1092
|
+
primary: {
|
|
1093
|
+
...files[0],
|
|
1094
|
+
role: "primary"
|
|
1095
|
+
},
|
|
1096
|
+
supportFiles: files.slice(1).map((file) => ({
|
|
1097
|
+
file: {
|
|
1098
|
+
...file,
|
|
1099
|
+
role: file.role ?? "support"
|
|
1100
|
+
},
|
|
1101
|
+
role: "support",
|
|
1102
|
+
relationship: "co-located-file",
|
|
1103
|
+
visibility: context.defaultSupportFileVisibility
|
|
1104
|
+
})),
|
|
1105
|
+
metadata: { mimeType: files[0].mimeType },
|
|
1106
|
+
capabilities: [],
|
|
1107
|
+
warnings: ["no media bundle handler matched; returned unknown inspection"],
|
|
1108
|
+
errors: []
|
|
1109
|
+
};
|
|
1166
1110
|
}
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
genericVideoBundleHandler,
|
|
1172
|
-
inspectMediaBundle,
|
|
1173
|
-
insta360BundleHandler,
|
|
1174
|
-
parseInsta360FileName
|
|
1175
|
-
};
|
|
1176
|
-
//# sourceMappingURL=index.js.map
|
|
1111
|
+
//#endregion
|
|
1112
|
+
export { FFmpegProcessor, defaultMediaBundleHandlers, genericImageBundleHandler, genericVideoBundleHandler, inspectMediaBundle, insta360BundleHandler, parseInsta360FileName };
|
|
1113
|
+
|
|
1114
|
+
//# sourceMappingURL=index.js.map
|