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