drone-path-engine 0.0.0-bootstrap
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +34 -0
- package/THIRD-PARTY-NOTICES.md +11 -0
- package/dist/assets.d.ts +34 -0
- package/dist/assets.js +60 -0
- package/dist/config.d.ts +207 -0
- package/dist/config.js +581 -0
- package/dist/configfile.d.ts +20 -0
- package/dist/configfile.js +86 -0
- package/dist/decimate.d.ts +11 -0
- package/dist/decimate.js +84 -0
- package/dist/ffmpeg-output.d.ts +9 -0
- package/dist/ffmpeg-output.js +40 -0
- package/dist/logging.d.ts +1 -0
- package/dist/logging.js +1 -0
- package/dist/paths.d.ts +4 -0
- package/dist/paths.js +21 -0
- package/dist/pipeline.d.ts +90 -0
- package/dist/pipeline.js +430 -0
- package/dist/plan.d.ts +47 -0
- package/dist/plan.js +112 -0
- package/dist/png.d.ts +1 -0
- package/dist/png.js +32 -0
- package/dist/range.d.ts +19 -0
- package/dist/range.js +50 -0
- package/dist/recordings.d.ts +5 -0
- package/dist/recordings.js +45 -0
- package/dist/render-page.d.ts +35 -0
- package/dist/render-page.js +85 -0
- package/dist/render.d.ts +57 -0
- package/dist/render.js +397 -0
- package/dist/renderer/boot.d.ts +4 -0
- package/dist/renderer/boot.js +28 -0
- package/dist/renderer/dom.d.ts +28 -0
- package/dist/renderer/dom.js +179 -0
- package/dist/renderer/framemath.d.ts +5 -0
- package/dist/renderer/framemath.js +30 -0
- package/dist/renderer/scene.d.ts +38 -0
- package/dist/renderer/scene.js +401 -0
- package/dist/renderer/theme.d.ts +13 -0
- package/dist/renderer/theme.js +82 -0
- package/dist/renderer/tilesources.d.ts +14 -0
- package/dist/renderer/tilesources.js +38 -0
- package/dist/renderer/trail.d.ts +18 -0
- package/dist/renderer/trail.js +42 -0
- package/dist/restyle.d.ts +13 -0
- package/dist/restyle.js +16 -0
- package/dist/serve.d.ts +6 -0
- package/dist/serve.js +27 -0
- package/dist/tilecache.d.ts +12 -0
- package/dist/tilecache.js +21 -0
- package/dist/tiles.d.ts +25 -0
- package/dist/tiles.js +215 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/package.json +39 -0
package/dist/pipeline.js
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { ERR_OUTPUT_VERIFICATION, ERR_RENDER_CANCELED, errorMessage } from 'drone-path-core/errors';
|
|
4
|
+
import { probeVideo } from 'drone-path-core/ffmpeg';
|
|
5
|
+
import { outputMatchesSource, verifyOutput } from './ffmpeg-output.js';
|
|
6
|
+
import { resolveFfmpegTools } from 'drone-path-core/ffmpeg-tools';
|
|
7
|
+
import { videoBaseName } from './recordings.js';
|
|
8
|
+
import { pathKey, uniquePath } from './paths.js';
|
|
9
|
+
import { loadTelemetry } from 'drone-path-core/telemetry';
|
|
10
|
+
import { writeTrackFile } from 'drone-path-core/track-write';
|
|
11
|
+
import { prefetchTiles } from './tiles.js';
|
|
12
|
+
import { INITIAL_PREPARE_PROGRESS, PREPARE_STAGE_ORDER, planIntroOutroFrames, planView, } from './plan.js';
|
|
13
|
+
import { buildPageHtml, planPageOpts } from './render-page.js';
|
|
14
|
+
import { renderVideo } from './render.js';
|
|
15
|
+
import { effectiveZoomOut, hasAltitude, resolveHudLayout } from './config.js';
|
|
16
|
+
import { resolveFrameRange } from 'drone-path-core/frame-range';
|
|
17
|
+
import { fixCountLabel, noLockCountLabel, telemetrySummary, trackDistanceLabel, } from 'drone-path-core/telemetry-summary';
|
|
18
|
+
export { INITIAL_PREPARE_PROGRESS };
|
|
19
|
+
export function outputPathFor(videoPath, config) {
|
|
20
|
+
const base = videoBaseName(videoPath);
|
|
21
|
+
const name = `${base}${config.outSuffix}.mp4`;
|
|
22
|
+
return config.outDir
|
|
23
|
+
? path.join(config.outDir, name)
|
|
24
|
+
: path.join(path.dirname(videoPath), name);
|
|
25
|
+
}
|
|
26
|
+
export function planOutputs(videos, config) {
|
|
27
|
+
const used = new Set();
|
|
28
|
+
return videos.map((video) => {
|
|
29
|
+
const plain = outputPathFor(video, config);
|
|
30
|
+
const key = pathKey(plain);
|
|
31
|
+
if (!used.has(key)) {
|
|
32
|
+
used.add(key);
|
|
33
|
+
return { out: plain, plain };
|
|
34
|
+
}
|
|
35
|
+
const ext = path.extname(plain);
|
|
36
|
+
const stem = path.basename(plain, ext);
|
|
37
|
+
const out = uniquePath(path.dirname(plain), stem, ext, (candidate) => used.has(pathKey(candidate)));
|
|
38
|
+
used.add(pathKey(out));
|
|
39
|
+
return { out, plain };
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function resolveHud(config, tel, log) {
|
|
43
|
+
const hud = resolveHudLayout(config.hud, hasAltitude(tel.frames.map((f) => f.alt)));
|
|
44
|
+
if (config.hud.alt && !hud.alt) {
|
|
45
|
+
log('hud: altitude unavailable in this telemetry source (chip disabled)');
|
|
46
|
+
}
|
|
47
|
+
return hud;
|
|
48
|
+
}
|
|
49
|
+
export function frameChannelsOf(tel) {
|
|
50
|
+
const lat = [];
|
|
51
|
+
const lon = [];
|
|
52
|
+
tel.frames.forEach((frame, i) => {
|
|
53
|
+
if (frame.fix === null) {
|
|
54
|
+
throw new Error(`telemetry frame ${i} has no GPS position`);
|
|
55
|
+
}
|
|
56
|
+
lat.push(frame.fix.lat);
|
|
57
|
+
lon.push(frame.fix.lon);
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
lat,
|
|
61
|
+
lon,
|
|
62
|
+
alt: tel.frames.map((f) => f.alt),
|
|
63
|
+
speed: tel.frames.map((f) => f.speed),
|
|
64
|
+
headingDeg: tel.frames.map((f) => f.headingDeg),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function describeView(config, overview, tilesLabel) {
|
|
68
|
+
const zoomNote = config.zoom
|
|
69
|
+
? ' (fixed)'
|
|
70
|
+
: config.zoomOut
|
|
71
|
+
? ` (-${config.zoomOut})`
|
|
72
|
+
: '';
|
|
73
|
+
return (`view: ${config.view} at zoom ${overview.zoom}${zoomNote}, ` +
|
|
74
|
+
`${overview.tiles.length} tiles (${tilesLabel})`);
|
|
75
|
+
}
|
|
76
|
+
function mtimeOf(outPath) {
|
|
77
|
+
try {
|
|
78
|
+
return fs.statSync(outPath).mtimeMs;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function removePartialOutput(outPath, mtimeBefore, log) {
|
|
85
|
+
const mtimeNow = mtimeOf(outPath);
|
|
86
|
+
if (mtimeNow !== null && (mtimeBefore === null || mtimeNow !== mtimeBefore)) {
|
|
87
|
+
try {
|
|
88
|
+
// Retry brief Windows file-lock release delays after killing ffmpeg.
|
|
89
|
+
fs.rmSync(outPath, { force: true, maxRetries: 5, retryDelay: 50 });
|
|
90
|
+
log(`removed partial output: ${outPath}`);
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
log(`warning: could not remove partial output ${outPath}: ${errorMessage(e)}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async function renderWithOutputCleanup(outPath, log, render) {
|
|
98
|
+
const mtimeBefore = mtimeOf(outPath);
|
|
99
|
+
try {
|
|
100
|
+
await render();
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
removePartialOutput(outPath, mtimeBefore, log);
|
|
104
|
+
throw e;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const realDeps = {
|
|
108
|
+
probeVideo,
|
|
109
|
+
loadTelemetry,
|
|
110
|
+
renderVideo,
|
|
111
|
+
verifyOutput,
|
|
112
|
+
outputMatchesSource,
|
|
113
|
+
};
|
|
114
|
+
function resolveDeps(overrides, tileCache) {
|
|
115
|
+
return {
|
|
116
|
+
probeVideo: overrides?.probeVideo ?? realDeps.probeVideo,
|
|
117
|
+
loadTelemetry: overrides?.loadTelemetry ?? realDeps.loadTelemetry,
|
|
118
|
+
prefetchTiles: overrides?.prefetchTiles ??
|
|
119
|
+
((options) => prefetchTiles({ ...options, cacheDir: tileCache })),
|
|
120
|
+
renderVideo: overrides?.renderVideo ?? realDeps.renderVideo,
|
|
121
|
+
verifyOutput: overrides?.verifyOutput ?? realDeps.verifyOutput,
|
|
122
|
+
outputMatchesSource: overrides?.outputMatchesSource ?? realDeps.outputMatchesSource,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function createPrepareReporter(onProgress = () => { }) {
|
|
126
|
+
return (stage) => onProgress({
|
|
127
|
+
stage,
|
|
128
|
+
done: PREPARE_STAGE_ORDER.indexOf(stage),
|
|
129
|
+
total: PREPARE_STAGE_ORDER.length,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async function probeStage(env) {
|
|
133
|
+
const { videoPath, config, log, deps, tools, report } = env;
|
|
134
|
+
report('probe');
|
|
135
|
+
const probe = await deps.probeVideo(tools, videoPath, log);
|
|
136
|
+
log(`source: ${path.basename(videoPath)} ${probe.width}x${probe.height} @ ${probe.fps.str} fps, ${probe.frames} frames`);
|
|
137
|
+
const introOutro = planIntroOutroFrames(config, {
|
|
138
|
+
fps: probe.fps.value,
|
|
139
|
+
frames: probe.frames,
|
|
140
|
+
});
|
|
141
|
+
if (introOutro.clamped) {
|
|
142
|
+
log(`intro/outro shortened to ${introOutro.introN}/${introOutro.outroN} frames for this clip`);
|
|
143
|
+
}
|
|
144
|
+
return { probe, introOutro };
|
|
145
|
+
}
|
|
146
|
+
async function telemetryStage(env, probe) {
|
|
147
|
+
const { videoPath, config, log, deps, tools, report } = env;
|
|
148
|
+
report('telemetry');
|
|
149
|
+
const loaded = await deps.loadTelemetry(videoPath, {
|
|
150
|
+
frames: probe.frames,
|
|
151
|
+
fpsValue: probe.fps.value,
|
|
152
|
+
durationSec: probe.durationSec,
|
|
153
|
+
tools,
|
|
154
|
+
});
|
|
155
|
+
const tel = loaded.telemetry;
|
|
156
|
+
const summary = telemetrySummary(tel);
|
|
157
|
+
log(`telemetry: ${summary.source} (${summary.method}), ${fixCountLabel(summary.fixCount)}, ` +
|
|
158
|
+
`heading=${summary.headingMode}, track ${trackDistanceLabel(summary.trackDistanceM)}` +
|
|
159
|
+
(summary.noLock ? `, ${noLockCountLabel(summary.noLock)}` : ''));
|
|
160
|
+
const hud = resolveHud(config, tel, log);
|
|
161
|
+
return {
|
|
162
|
+
tel,
|
|
163
|
+
frameValues: loaded.frameValues,
|
|
164
|
+
trackValues: loaded.trackValues,
|
|
165
|
+
protoFile: loaded.protoFile,
|
|
166
|
+
gimbalDevice: loaded.gimbalDevice,
|
|
167
|
+
hud,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
async function viewPlanStage(env, tel) {
|
|
171
|
+
const { config, log, report } = env;
|
|
172
|
+
report('plan');
|
|
173
|
+
const overview = planView(tel.bbox, config.width, config.height, config);
|
|
174
|
+
log(describeView(config, overview, config.style));
|
|
175
|
+
return overview;
|
|
176
|
+
}
|
|
177
|
+
async function tileWarmStage(deps, config, overview, log, signal) {
|
|
178
|
+
if (signal?.aborted)
|
|
179
|
+
throw new Error(ERR_RENDER_CANCELED);
|
|
180
|
+
let tileStats;
|
|
181
|
+
try {
|
|
182
|
+
tileStats = await deps.prefetchTiles({
|
|
183
|
+
style: config.style,
|
|
184
|
+
tiles: overview.tiles,
|
|
185
|
+
log,
|
|
186
|
+
signal,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
if (signal?.aborted)
|
|
191
|
+
throw new Error(ERR_RENDER_CANCELED, { cause: err });
|
|
192
|
+
throw err;
|
|
193
|
+
}
|
|
194
|
+
if (signal?.aborted)
|
|
195
|
+
throw new Error(ERR_RENDER_CANCELED);
|
|
196
|
+
if (tileStats.downloaded) {
|
|
197
|
+
log(`tiles: ${tileStats.downloaded} downloaded, ${tileStats.failed} placeholders`);
|
|
198
|
+
}
|
|
199
|
+
return tileStats;
|
|
200
|
+
}
|
|
201
|
+
export async function prepareSession(videoPath, config, log, options = {}) {
|
|
202
|
+
const env = {
|
|
203
|
+
videoPath,
|
|
204
|
+
config,
|
|
205
|
+
log,
|
|
206
|
+
deps: resolveDeps(options.deps, config.tileCache),
|
|
207
|
+
tools: options.tools ?? resolveFfmpegTools(config),
|
|
208
|
+
report: createPrepareReporter(options.onProgress),
|
|
209
|
+
};
|
|
210
|
+
const { width, height } = config;
|
|
211
|
+
const { probe, introOutro } = await probeStage(env);
|
|
212
|
+
const { tel, frameValues, trackValues, protoFile, gimbalDevice, hud } = await telemetryStage(env, probe);
|
|
213
|
+
const overview = await viewPlanStage(env, tel);
|
|
214
|
+
const frames = frameChannelsOf(tel);
|
|
215
|
+
return {
|
|
216
|
+
width,
|
|
217
|
+
height,
|
|
218
|
+
probe,
|
|
219
|
+
tel,
|
|
220
|
+
frameValues,
|
|
221
|
+
trackValues,
|
|
222
|
+
protoFile,
|
|
223
|
+
gimbalDevice,
|
|
224
|
+
hud,
|
|
225
|
+
introOutro,
|
|
226
|
+
overview,
|
|
227
|
+
frames,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
export function recordingPageOpts(prep, config) {
|
|
231
|
+
return planPageOpts({
|
|
232
|
+
bbox: prep.tel.bbox,
|
|
233
|
+
width: prep.width,
|
|
234
|
+
height: prep.height,
|
|
235
|
+
config,
|
|
236
|
+
source: { fps: prep.probe.fps.value, frames: prep.probe.frames },
|
|
237
|
+
frames: prep.frames,
|
|
238
|
+
planned: {
|
|
239
|
+
overview: prep.overview,
|
|
240
|
+
introOutro: prep.introOutro,
|
|
241
|
+
hud: prep.hud,
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
function buildOkSummary(ctx, elapsedSec) {
|
|
246
|
+
const { config, probe, tel, hud, introOutro, overview, tileStats } = ctx;
|
|
247
|
+
const telemetry = telemetrySummary(tel);
|
|
248
|
+
return {
|
|
249
|
+
video: ctx.videoPath,
|
|
250
|
+
out: ctx.outPath,
|
|
251
|
+
status: 'ok',
|
|
252
|
+
width: ctx.width,
|
|
253
|
+
height: ctx.height,
|
|
254
|
+
style: config.style,
|
|
255
|
+
view: config.view,
|
|
256
|
+
introFrames: introOutro.introN,
|
|
257
|
+
outroFrames: introOutro.outroN,
|
|
258
|
+
hud,
|
|
259
|
+
// Field by field, not a spread: a TelemetrySummary rename must land here visibly, not silently drop a --json key.
|
|
260
|
+
source: telemetry.source,
|
|
261
|
+
method: telemetry.method,
|
|
262
|
+
headingMode: telemetry.headingMode,
|
|
263
|
+
fixes: telemetry.fixCount,
|
|
264
|
+
noLock: telemetry.noLock,
|
|
265
|
+
trackDistanceM: telemetry.trackDistanceM,
|
|
266
|
+
frames: ctx.outFrames,
|
|
267
|
+
fps: probe.fps.str,
|
|
268
|
+
frameRange: ctx.frameRange ?? undefined,
|
|
269
|
+
bbox: tel.bbox,
|
|
270
|
+
zoom: overview.zoom,
|
|
271
|
+
zoomFixed: config.zoom > 0,
|
|
272
|
+
zoomOut: effectiveZoomOut(config),
|
|
273
|
+
tilesDownloaded: tileStats.downloaded,
|
|
274
|
+
verified: true,
|
|
275
|
+
elapsedSec,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
async function verifyRenderedOutput(env) {
|
|
279
|
+
const { ctx, deps, tools } = env;
|
|
280
|
+
const verify = await deps.verifyOutput(tools, ctx.outPath, ctx.outFrames, ctx.probe.fps.value);
|
|
281
|
+
if (!verify.ok) {
|
|
282
|
+
throw new Error(`${ERR_OUTPUT_VERIFICATION}got ${verify.frames} frames @ ${verify.fps ?? 'unknown'}, ` +
|
|
283
|
+
`expected ${ctx.outFrames} @ ${ctx.probe.fps.str}`);
|
|
284
|
+
}
|
|
285
|
+
ctx.log(ctx.frameRange
|
|
286
|
+
? 'verified: frame count and frame rate are 1:1 within the exported range'
|
|
287
|
+
: 'verified: frame count and frame rate match the source');
|
|
288
|
+
}
|
|
289
|
+
async function renderToOutput(env) {
|
|
290
|
+
const { ctx, deps, tools, options } = env;
|
|
291
|
+
const html = buildPageHtml(recordingPageOpts(ctx, ctx.config));
|
|
292
|
+
fs.mkdirSync(path.dirname(ctx.outPath), { recursive: true });
|
|
293
|
+
await renderWithOutputCleanup(ctx.outPath, ctx.log, async () => {
|
|
294
|
+
await deps.renderVideo({
|
|
295
|
+
html,
|
|
296
|
+
capture: ctx.config.capture,
|
|
297
|
+
codec: ctx.config.codec,
|
|
298
|
+
quality: ctx.config.quality,
|
|
299
|
+
preset: ctx.config.preset,
|
|
300
|
+
style: ctx.config.style,
|
|
301
|
+
tileCache: ctx.config.tileCache,
|
|
302
|
+
tools,
|
|
303
|
+
width: ctx.width,
|
|
304
|
+
height: ctx.height,
|
|
305
|
+
frames: ctx.outFrames,
|
|
306
|
+
frameStart: ctx.frameRange?.start ?? 0,
|
|
307
|
+
fpsStr: ctx.probe.fps.str,
|
|
308
|
+
workers: ctx.config.workers,
|
|
309
|
+
outPath: ctx.outPath,
|
|
310
|
+
browserPath: ctx.config.browser,
|
|
311
|
+
signal: options.signal,
|
|
312
|
+
log: ctx.log,
|
|
313
|
+
onCapture: options.onCapture,
|
|
314
|
+
});
|
|
315
|
+
await verifyRenderedOutput(env);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
export function telemetryExportInput(prep) {
|
|
319
|
+
return {
|
|
320
|
+
source: prep.tel.source,
|
|
321
|
+
headingMode: prep.tel.headingMode,
|
|
322
|
+
frameCount: prep.probe.frames,
|
|
323
|
+
protoFile: prep.protoFile,
|
|
324
|
+
gimbalDevice: prep.gimbalDevice,
|
|
325
|
+
frames: prep.frameValues,
|
|
326
|
+
headingDeg: prep.tel.frames.map((frame) => frame.headingDeg),
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
// The track formats read the fix-faithful records (which carry positions and
|
|
330
|
+
// source times for every telemetry source); the .json document keeps its own
|
|
331
|
+
// record stream.
|
|
332
|
+
export function trackExportStream(prep) {
|
|
333
|
+
return {
|
|
334
|
+
records: prep.trackValues,
|
|
335
|
+
fpsValue: prep.probe.fps.value,
|
|
336
|
+
frameCount: prep.probe.frames,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function writeTrackExport(file, prep, log) {
|
|
340
|
+
// The export intentionally covers the whole recording: a frame range
|
|
341
|
+
// limits the render, never this export.
|
|
342
|
+
writeTrackFile(file, { document: telemetryExportInput(prep), track: trackExportStream(prep) }, null);
|
|
343
|
+
log(`track: wrote ${file}`);
|
|
344
|
+
}
|
|
345
|
+
export async function processRecording(videoPath, config, log, outPath = outputPathFor(videoPath, config), options = {}) {
|
|
346
|
+
const t0 = Date.now();
|
|
347
|
+
const deps = resolveDeps(options.deps, config.tileCache);
|
|
348
|
+
const tools = options.tools ?? resolveFfmpegTools(config);
|
|
349
|
+
if (options.signal?.aborted)
|
|
350
|
+
throw new Error(ERR_RENDER_CANCELED);
|
|
351
|
+
const prep = await prepareSession(videoPath, config, log, {
|
|
352
|
+
...options,
|
|
353
|
+
deps,
|
|
354
|
+
tools,
|
|
355
|
+
});
|
|
356
|
+
if (options.signal?.aborted)
|
|
357
|
+
throw new Error(ERR_RENDER_CANCELED);
|
|
358
|
+
if (config.trackOut) {
|
|
359
|
+
writeTrackExport(config.trackOut, prep, log);
|
|
360
|
+
}
|
|
361
|
+
const tileStats = await tileWarmStage(deps, config, prep.overview, log, options.signal);
|
|
362
|
+
const range = resolveFrameRange(options.range, prep.probe.frames);
|
|
363
|
+
const outFrames = range ? range.end - range.start + 1 : prep.probe.frames;
|
|
364
|
+
const ctx = {
|
|
365
|
+
...prep,
|
|
366
|
+
videoPath,
|
|
367
|
+
outPath,
|
|
368
|
+
config,
|
|
369
|
+
log,
|
|
370
|
+
outFrames,
|
|
371
|
+
frameRange: range,
|
|
372
|
+
tileStats,
|
|
373
|
+
};
|
|
374
|
+
await renderToOutput({ ctx, deps, tools, options });
|
|
375
|
+
return buildOkSummary(ctx, Math.round((Date.now() - t0) / 1000));
|
|
376
|
+
}
|
|
377
|
+
export async function runBatch(videos, config, log, emit, options = {}) {
|
|
378
|
+
const result = { ok: 0, skipped: 0, failed: 0 };
|
|
379
|
+
const deps = resolveDeps(options.deps, config.tileCache);
|
|
380
|
+
const tools = options.tools ?? resolveFfmpegTools(config);
|
|
381
|
+
if (config.trackOut && videos.length > 1) {
|
|
382
|
+
throw new Error('--track-out writes one file per run: give a single recording');
|
|
383
|
+
}
|
|
384
|
+
const plans = planOutputs(videos, config);
|
|
385
|
+
function collisionWarning(base, plan) {
|
|
386
|
+
if (plan.out !== plan.plain) {
|
|
387
|
+
log(`warning: output name collision for ${base} - writing ${path.basename(plan.out)} instead`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
async function maybeSkip(video, plan) {
|
|
391
|
+
if (!(config.skipExisting && (await deps.outputMatchesSource(tools, video, plan.out)))) {
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
log(`SKIP ${plan.out} (exists and matches the source)`);
|
|
395
|
+
return 'skipped';
|
|
396
|
+
}
|
|
397
|
+
async function runOne(video, plan) {
|
|
398
|
+
return processRecording(video, config, log, plan.out, { ...options, deps, tools });
|
|
399
|
+
}
|
|
400
|
+
for (const [idx, video] of videos.entries()) {
|
|
401
|
+
const base = path.basename(video);
|
|
402
|
+
log(`=== ${base} ===`);
|
|
403
|
+
const plan = plans[idx];
|
|
404
|
+
collisionWarning(base, plan);
|
|
405
|
+
if ((await maybeSkip(video, plan)) !== null) {
|
|
406
|
+
emit({ video, out: plan.out, status: 'skipped', elapsedSec: 0 });
|
|
407
|
+
result.skipped++;
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
const summary = await runOne(video, plan);
|
|
412
|
+
emit(summary);
|
|
413
|
+
log(`OK ${plan.out} (${summary.frames} frames @ ${summary.fps}, ${summary.elapsedSec} s)`);
|
|
414
|
+
result.ok++;
|
|
415
|
+
}
|
|
416
|
+
catch (e) {
|
|
417
|
+
const message = errorMessage(e);
|
|
418
|
+
log(`ERROR ${base}: ${message}`);
|
|
419
|
+
emit({
|
|
420
|
+
video,
|
|
421
|
+
out: plan.out,
|
|
422
|
+
status: 'error',
|
|
423
|
+
elapsedSec: 0,
|
|
424
|
+
error: message,
|
|
425
|
+
});
|
|
426
|
+
result.failed++;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return result;
|
|
430
|
+
}
|
package/dist/plan.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { MapStyle, RenderConfig } from './config.js';
|
|
2
|
+
import type { Bbox, MercatorPixel } from 'drone-path-core/geo';
|
|
3
|
+
export interface TileCoord {
|
|
4
|
+
z: number;
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
}
|
|
8
|
+
interface OverviewPlanOptions {
|
|
9
|
+
bbox: Bbox;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
maxZoom?: number;
|
|
13
|
+
zoomOut?: number;
|
|
14
|
+
follow?: boolean;
|
|
15
|
+
maxNativeZoom?: number | null;
|
|
16
|
+
zoomOverride?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface OverviewPlan {
|
|
19
|
+
centerPx: MercatorPixel;
|
|
20
|
+
zoom: number;
|
|
21
|
+
tiles: TileCoord[];
|
|
22
|
+
}
|
|
23
|
+
/** The zoom levels the planner renders: the floor and the map style's tile maximum. */
|
|
24
|
+
export declare function zoomLimits(style: MapStyle): {
|
|
25
|
+
min: number;
|
|
26
|
+
max: number;
|
|
27
|
+
};
|
|
28
|
+
export declare function computeOverview({ bbox, width, height, maxZoom, zoomOut, follow, maxNativeZoom, zoomOverride, }: OverviewPlanOptions): OverviewPlan;
|
|
29
|
+
export interface IntroOutroFrames {
|
|
30
|
+
introN: number;
|
|
31
|
+
outroN: number;
|
|
32
|
+
clamped: boolean;
|
|
33
|
+
}
|
|
34
|
+
export declare function planView(bbox: Bbox, width: number, height: number, config: Pick<RenderConfig, 'style' | 'view' | 'zoom' | 'zoomOut'>): OverviewPlan;
|
|
35
|
+
export declare function planIntroOutroFrames(config: Pick<RenderConfig, 'introSec' | 'outroSec'>, source: {
|
|
36
|
+
fps: number;
|
|
37
|
+
frames: number;
|
|
38
|
+
}): IntroOutroFrames;
|
|
39
|
+
export declare const PREPARE_STAGE_ORDER: readonly ["probe", "telemetry", "plan"];
|
|
40
|
+
export type PrepareStage = (typeof PREPARE_STAGE_ORDER)[number];
|
|
41
|
+
export interface PrepareProgress {
|
|
42
|
+
stage: PrepareStage;
|
|
43
|
+
done: number;
|
|
44
|
+
total: number;
|
|
45
|
+
}
|
|
46
|
+
export declare const INITIAL_PREPARE_PROGRESS: PrepareProgress;
|
|
47
|
+
export {};
|
package/dist/plan.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { project } from 'drone-path-core/geo';
|
|
2
|
+
import { TILE_SOURCES } from './renderer/tilesources.js';
|
|
3
|
+
const MIN_ZOOM = 2;
|
|
4
|
+
const HOVER_ZOOM_FLOOR = 16;
|
|
5
|
+
const MIN_TRACK_SPAN_PX = 80;
|
|
6
|
+
const TILE_SIZE_PX = 256;
|
|
7
|
+
/** The zoom levels the planner renders: the floor and the map style's tile maximum. */
|
|
8
|
+
export function zoomLimits(style) {
|
|
9
|
+
return { min: MIN_ZOOM, max: TILE_SOURCES[style].maxZoom || 19 };
|
|
10
|
+
}
|
|
11
|
+
function fitZoomToTrack(bbox, availW, availH, maxZoom) {
|
|
12
|
+
for (let zoom = maxZoom; zoom >= MIN_ZOOM; zoom--) {
|
|
13
|
+
const northWest = project(bbox.minLon, bbox.maxLat, zoom);
|
|
14
|
+
const southEast = project(bbox.maxLon, bbox.minLat, zoom);
|
|
15
|
+
const fits = southEast.x - northWest.x <= availW &&
|
|
16
|
+
southEast.y - northWest.y <= availH;
|
|
17
|
+
if (fits)
|
|
18
|
+
return zoom;
|
|
19
|
+
}
|
|
20
|
+
return MIN_ZOOM;
|
|
21
|
+
}
|
|
22
|
+
function pullBackTinyTracks(zoom, bbox) {
|
|
23
|
+
while (zoom > HOVER_ZOOM_FLOOR) {
|
|
24
|
+
const northWest = project(bbox.minLon, bbox.maxLat, zoom);
|
|
25
|
+
const southEast = project(bbox.maxLon, bbox.minLat, zoom);
|
|
26
|
+
const spansEnough = southEast.x - northWest.x >= MIN_TRACK_SPAN_PX ||
|
|
27
|
+
southEast.y - northWest.y >= MIN_TRACK_SPAN_PX;
|
|
28
|
+
if (spansEnough)
|
|
29
|
+
break;
|
|
30
|
+
zoom--;
|
|
31
|
+
}
|
|
32
|
+
return zoom;
|
|
33
|
+
}
|
|
34
|
+
function tilesCovering(centerPx, trackSpanPx, zoom, width, height, follow, maxNativeZoom) {
|
|
35
|
+
const tileZoom = maxNativeZoom
|
|
36
|
+
? Math.min(zoom, maxNativeZoom)
|
|
37
|
+
: zoom;
|
|
38
|
+
const scale = 2 ** (tileZoom - zoom);
|
|
39
|
+
const halfW = (width + TILE_SIZE_PX + (follow ? trackSpanPx.x / 2 : 0)) * scale;
|
|
40
|
+
const halfH = (height + TILE_SIZE_PX + (follow ? trackSpanPx.y / 2 : 0)) * scale;
|
|
41
|
+
const maxTileIndex = 2 ** tileZoom - 1;
|
|
42
|
+
const xMin = Math.max(0, Math.floor((centerPx.x * scale - halfW) / TILE_SIZE_PX));
|
|
43
|
+
const xMax = Math.min(maxTileIndex, Math.floor((centerPx.x * scale + halfW) / TILE_SIZE_PX));
|
|
44
|
+
const yMin = Math.max(0, Math.floor((centerPx.y * scale - halfH) / TILE_SIZE_PX));
|
|
45
|
+
const yMax = Math.min(maxTileIndex, Math.floor((centerPx.y * scale + halfH) / TILE_SIZE_PX));
|
|
46
|
+
const tiles = [];
|
|
47
|
+
for (let x = xMin; x <= xMax; x++) {
|
|
48
|
+
for (let y = yMin; y <= yMax; y++)
|
|
49
|
+
tiles.push({ z: tileZoom, x, y });
|
|
50
|
+
}
|
|
51
|
+
return tiles;
|
|
52
|
+
}
|
|
53
|
+
export function computeOverview({ bbox, width, height, maxZoom = 19, zoomOut = 0, follow = false, maxNativeZoom = null, zoomOverride = 0, }) {
|
|
54
|
+
const padX = Math.round(width * 0.08);
|
|
55
|
+
const padY = Math.round(height * 0.08);
|
|
56
|
+
const availW = Math.max(64, width - 2 * padX);
|
|
57
|
+
const availH = Math.max(64, height - 2 * padY);
|
|
58
|
+
let zoom;
|
|
59
|
+
if (zoomOverride > 0) {
|
|
60
|
+
zoom = Math.max(MIN_ZOOM, Math.min(maxZoom, Math.round(zoomOverride)));
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
zoom = pullBackTinyTracks(fitZoomToTrack(bbox, availW, availH, maxZoom), bbox);
|
|
64
|
+
zoom = Math.max(MIN_ZOOM, zoom - Math.max(0, Math.floor(zoomOut)));
|
|
65
|
+
}
|
|
66
|
+
const northWest = project(bbox.minLon, bbox.maxLat, zoom);
|
|
67
|
+
const southEast = project(bbox.maxLon, bbox.minLat, zoom);
|
|
68
|
+
const centerPx = {
|
|
69
|
+
x: (northWest.x + southEast.x) / 2,
|
|
70
|
+
y: (northWest.y + southEast.y) / 2,
|
|
71
|
+
};
|
|
72
|
+
const trackSpanPx = {
|
|
73
|
+
x: southEast.x - northWest.x,
|
|
74
|
+
y: southEast.y - northWest.y,
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
centerPx,
|
|
78
|
+
zoom,
|
|
79
|
+
tiles: tilesCovering(centerPx, trackSpanPx, zoom, width, height, follow, maxNativeZoom),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export function planView(bbox, width, height, config) {
|
|
83
|
+
const src = TILE_SOURCES[config.style];
|
|
84
|
+
return computeOverview({
|
|
85
|
+
bbox,
|
|
86
|
+
width,
|
|
87
|
+
height,
|
|
88
|
+
maxZoom: zoomLimits(config.style).max,
|
|
89
|
+
maxNativeZoom: src.maxNativeZoom ?? null,
|
|
90
|
+
zoomOut: config.zoomOut,
|
|
91
|
+
zoomOverride: config.zoom,
|
|
92
|
+
follow: config.view === 'follow',
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
export function planIntroOutroFrames(config, source) {
|
|
96
|
+
let introN = Math.max(0, Math.round(config.introSec * source.fps));
|
|
97
|
+
let outroN = Math.max(0, Math.round(config.outroSec * source.fps));
|
|
98
|
+
const budget = Math.max(2, Math.floor(source.frames / 2));
|
|
99
|
+
const clamped = introN + outroN > budget;
|
|
100
|
+
if (clamped) {
|
|
101
|
+
const scale = budget / (introN + outroN);
|
|
102
|
+
introN = Math.floor(introN * scale);
|
|
103
|
+
outroN = Math.floor(outroN * scale);
|
|
104
|
+
}
|
|
105
|
+
return { introN, outroN, clamped };
|
|
106
|
+
}
|
|
107
|
+
export const PREPARE_STAGE_ORDER = ['probe', 'telemetry', 'plan'];
|
|
108
|
+
export const INITIAL_PREPARE_PROGRESS = {
|
|
109
|
+
stage: PREPARE_STAGE_ORDER[0],
|
|
110
|
+
done: 0,
|
|
111
|
+
total: PREPARE_STAGE_ORDER.length,
|
|
112
|
+
};
|
package/dist/png.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function solidPng(hexColor: string, size?: number): Buffer;
|
package/dist/png.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import zlib from 'node:zlib';
|
|
2
|
+
function pngChunk(type, data) {
|
|
3
|
+
const len = Buffer.alloc(4);
|
|
4
|
+
len.writeUInt32BE(data.length);
|
|
5
|
+
const body = Buffer.concat([Buffer.from(type, 'ascii'), data]);
|
|
6
|
+
const crc = Buffer.alloc(4);
|
|
7
|
+
crc.writeUInt32BE(zlib.crc32(body));
|
|
8
|
+
return Buffer.concat([len, body, crc]);
|
|
9
|
+
}
|
|
10
|
+
export function solidPng(hexColor, size = 256) {
|
|
11
|
+
const r = Number.parseInt(hexColor.slice(1, 3), 16);
|
|
12
|
+
const g = Number.parseInt(hexColor.slice(3, 5), 16);
|
|
13
|
+
const b = Number.parseInt(hexColor.slice(5, 7), 16);
|
|
14
|
+
const row = Buffer.alloc(1 + size * 3);
|
|
15
|
+
for (let x = 0; x < size; x++) {
|
|
16
|
+
row[1 + x * 3] = r;
|
|
17
|
+
row[2 + x * 3] = g;
|
|
18
|
+
row[3 + x * 3] = b;
|
|
19
|
+
}
|
|
20
|
+
const raw = Buffer.concat(Array.from({ length: size }, () => row));
|
|
21
|
+
const ihdr = Buffer.alloc(13);
|
|
22
|
+
ihdr.writeUInt32BE(size, 0);
|
|
23
|
+
ihdr.writeUInt32BE(size, 4);
|
|
24
|
+
ihdr[8] = 8; // bit depth
|
|
25
|
+
ihdr[9] = 2; // color type RGB
|
|
26
|
+
return Buffer.concat([
|
|
27
|
+
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
28
|
+
pngChunk('IHDR', ihdr),
|
|
29
|
+
pngChunk('IDAT', zlib.deflateSync(raw)),
|
|
30
|
+
pngChunk('IEND', Buffer.alloc(0)),
|
|
31
|
+
]);
|
|
32
|
+
}
|
package/dist/range.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface ByteRange {
|
|
2
|
+
start: number;
|
|
3
|
+
end: number;
|
|
4
|
+
}
|
|
5
|
+
export declare function parseByteRange(header: string | string[] | undefined, size: number): ByteRange | null | 'unsatisfiable';
|
|
6
|
+
type ByteRangePlan = {
|
|
7
|
+
status: 200;
|
|
8
|
+
headers: Record<string, string>;
|
|
9
|
+
} | {
|
|
10
|
+
status: 206;
|
|
11
|
+
headers: Record<string, string>;
|
|
12
|
+
span: ByteRange;
|
|
13
|
+
} | {
|
|
14
|
+
status: 416;
|
|
15
|
+
headers: Record<string, string>;
|
|
16
|
+
body: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function planByteRange(header: string | string[] | undefined, size: number, mime: string): ByteRangePlan;
|
|
19
|
+
export {};
|
package/dist/range.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export function parseByteRange(header, size) {
|
|
2
|
+
if (header === undefined)
|
|
3
|
+
return null;
|
|
4
|
+
const text = Array.isArray(header) ? header[0] : header;
|
|
5
|
+
const m = /^bytes=(\d*)-(\d*)$/.exec(text.trim());
|
|
6
|
+
if (!m)
|
|
7
|
+
return null;
|
|
8
|
+
const from = m[1];
|
|
9
|
+
const to = m[2];
|
|
10
|
+
if (from === '' && to === '')
|
|
11
|
+
return null;
|
|
12
|
+
let start;
|
|
13
|
+
let end;
|
|
14
|
+
if (from === '') {
|
|
15
|
+
const suffix = Number(to);
|
|
16
|
+
if (!Number.isFinite(suffix) || suffix <= 0)
|
|
17
|
+
return 'unsatisfiable';
|
|
18
|
+
start = Math.max(0, size - suffix);
|
|
19
|
+
end = size - 1;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
start = Number(from);
|
|
23
|
+
end = to === '' ? size - 1 : Number(to);
|
|
24
|
+
if (!Number.isFinite(start) || !Number.isFinite(end))
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
if (start > end || start >= size)
|
|
28
|
+
return 'unsatisfiable';
|
|
29
|
+
return { start, end: Math.min(end, size - 1) };
|
|
30
|
+
}
|
|
31
|
+
export function planByteRange(header, size, mime) {
|
|
32
|
+
const range = parseByteRange(header, size);
|
|
33
|
+
if (range === 'unsatisfiable') {
|
|
34
|
+
return {
|
|
35
|
+
status: 416,
|
|
36
|
+
headers: { 'Content-Range': `bytes */${size}`, 'Content-Type': 'text/plain' },
|
|
37
|
+
body: 'range not satisfiable',
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const headers = {
|
|
41
|
+
'Content-Type': mime,
|
|
42
|
+
'Accept-Ranges': 'bytes',
|
|
43
|
+
'Cache-Control': 'no-store',
|
|
44
|
+
'Content-Length': String(range === null ? size : range.end - range.start + 1),
|
|
45
|
+
};
|
|
46
|
+
if (range === null)
|
|
47
|
+
return { status: 200, headers };
|
|
48
|
+
headers['Content-Range'] = `bytes ${range.start}-${range.end}/${size}`;
|
|
49
|
+
return { status: 206, headers, span: range };
|
|
50
|
+
}
|