drone-path 0.0.0 → 0.8.0
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/CHANGELOG.md +179 -0
- package/LICENSE +21 -0
- package/README.md +83 -6
- package/THIRD-PARTY-NOTICES.md +77 -0
- package/app/main.mjs +496 -0
- package/app/preload.cjs +31 -0
- package/app/update-policy.d.mts +6 -0
- package/app/update-policy.mjs +6 -0
- package/app/update.d.mts +10 -0
- package/app/update.mjs +54 -0
- package/bin/drone-path.mjs +5 -0
- package/dist/appassets.js +44 -0
- package/dist/apppaths.js +28 -0
- package/dist/appsession.js +128 -0
- package/dist/appstate.js +109 -0
- package/dist/assets.js +67 -0
- package/dist/cammeta.js +292 -0
- package/dist/changeset-gate.js +46 -0
- package/dist/cli.js +156 -0
- package/dist/config.js +469 -0
- package/dist/configfile.js +81 -0
- package/dist/decimate.js +94 -0
- package/dist/errors.js +4 -0
- package/dist/exportjob.js +106 -0
- package/dist/ff.js +121 -0
- package/dist/fftools.js +65 -0
- package/dist/geo.js +70 -0
- package/dist/inputs.js +59 -0
- package/dist/manual/anchors.js +23 -0
- package/dist/manual/build.js +78 -0
- package/dist/manual/cliref.js +36 -0
- package/dist/manual/links.js +76 -0
- package/dist/manual/markdown.js +101 -0
- package/dist/manual/model.js +4 -0
- package/dist/manual/module.js +8 -0
- package/dist/manual/nav.js +13 -0
- package/dist/manual/reference.js +26 -0
- package/dist/page.js +108 -0
- package/dist/paths.js +28 -0
- package/dist/plan.js +119 -0
- package/dist/png.js +52 -0
- package/dist/proto.js +102 -0
- package/dist/range.js +58 -0
- package/dist/render.js +408 -0
- package/dist/renderer/boot.js +7 -0
- package/dist/renderer/dom.js +132 -0
- package/dist/renderer/framemath.js +34 -0
- package/dist/renderer/leaflet.js +5 -0
- package/dist/renderer/scene.js +358 -0
- package/dist/renderer/theme.js +34 -0
- package/dist/renderer/tilesources.js +34 -0
- package/dist/renderer/trail.js +53 -0
- package/dist/restyle.js +24 -0
- package/dist/run.js +446 -0
- package/dist/serve.js +70 -0
- package/dist/srt.js +76 -0
- package/dist/summary.js +22 -0
- package/dist/telemetry-math.js +302 -0
- package/dist/telemetry.js +38 -0
- package/dist/tilecache.js +26 -0
- package/dist/tiles.js +241 -0
- package/dist/tracing.js +208 -0
- package/dist/version.js +3 -0
- package/package.json +93 -7
package/dist/run.js
ADDED
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { errorMessage } from './errors.js';
|
|
4
|
+
import { outputMatchesSource, probeVideo, verifyOutput } from './ff.js';
|
|
5
|
+
import { resolveFfTools } from './fftools.js';
|
|
6
|
+
import { videoBaseName } from './inputs.js';
|
|
7
|
+
import { pathKey, uniquePath } from './paths.js';
|
|
8
|
+
import { loadTelemetry } from './telemetry.js';
|
|
9
|
+
import { prefetchTiles } from './tiles.js';
|
|
10
|
+
import { planIntroOutroFrames, planView } from './plan.js';
|
|
11
|
+
import { buildPageHtml, planPageOpts } from './page.js';
|
|
12
|
+
import { renderVideo } from './render.js';
|
|
13
|
+
import { assertValidFrameRange, effectiveZoomOut, hasAltitude, resolveHudSpec, } from './config.js';
|
|
14
|
+
import { fixesLabel, noLockCuesLabel, telemetrySummary, trackDistanceLabel, } from './summary.js';
|
|
15
|
+
import { NOOP_TRACER, stage } from './tracing.js';
|
|
16
|
+
// The preparation stages in execution order. The loading-bar step numbers
|
|
17
|
+
// derive from this sequence, so adding or reordering a stage cannot silently
|
|
18
|
+
// skew the bar (see PrepareProgress).
|
|
19
|
+
const PREPARE_STAGE_ORDER = ['probe', 'telemetry', 'plan'];
|
|
20
|
+
export function outputPathFor(videoPath, config) {
|
|
21
|
+
const base = videoBaseName(videoPath);
|
|
22
|
+
const name = `${base}${config.outSuffix}.mp4`;
|
|
23
|
+
return config.outDir
|
|
24
|
+
? path.join(config.outDir, name)
|
|
25
|
+
: path.join(path.dirname(videoPath), name);
|
|
26
|
+
}
|
|
27
|
+
export function planOutputs(videos, config) {
|
|
28
|
+
const used = new Set();
|
|
29
|
+
return videos.map((video) => {
|
|
30
|
+
const plain = outputPathFor(video, config);
|
|
31
|
+
const key = pathKey(plain);
|
|
32
|
+
if (!used.has(key)) {
|
|
33
|
+
used.add(key);
|
|
34
|
+
return { out: plain, plain };
|
|
35
|
+
}
|
|
36
|
+
const ext = path.extname(plain);
|
|
37
|
+
const stem = path.basename(plain, ext);
|
|
38
|
+
const out = uniquePath(path.dirname(plain), stem, ext, (candidate) => used.has(pathKey(candidate)));
|
|
39
|
+
used.add(pathKey(out));
|
|
40
|
+
return { out, plain };
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// HUD sanity: drop chips whose data does not exist for this source.
|
|
44
|
+
function resolveHud(config, tel, log) {
|
|
45
|
+
const hud = resolveHudSpec(config.hud, hasAltitude(tel.frames.map((f) => f.alt)));
|
|
46
|
+
if (config.hud.alt && !hud.alt) {
|
|
47
|
+
log('hud: altitude unavailable in this telemetry source (chip disabled)');
|
|
48
|
+
}
|
|
49
|
+
return hud;
|
|
50
|
+
}
|
|
51
|
+
export function frameArrays(tel) {
|
|
52
|
+
const lat = [];
|
|
53
|
+
const lon = [];
|
|
54
|
+
tel.frames.forEach((frame, i) => {
|
|
55
|
+
if (frame.fix === null) {
|
|
56
|
+
throw new Error(`telemetry frame ${i} has no GPS position`);
|
|
57
|
+
}
|
|
58
|
+
lat.push(frame.fix.lat);
|
|
59
|
+
lon.push(frame.fix.lon);
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
lat,
|
|
63
|
+
lon,
|
|
64
|
+
alt: tel.frames.map((f) => f.alt),
|
|
65
|
+
speed: tel.frames.map((f) => f.speed),
|
|
66
|
+
headingDeg: tel.frames.map((f) => f.headingDeg),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function describeView(config, overview, tilesLabel) {
|
|
70
|
+
const zoomNote = config.zoom
|
|
71
|
+
? ' (fixed)'
|
|
72
|
+
: config.zoomOut
|
|
73
|
+
? ` (-${config.zoomOut})`
|
|
74
|
+
: '';
|
|
75
|
+
return (`view: ${config.view} at zoom ${overview.zoom}${zoomNote}, ` +
|
|
76
|
+
`${overview.tiles.length} tiles (${tilesLabel})`);
|
|
77
|
+
}
|
|
78
|
+
function mtimeOf(outPath) {
|
|
79
|
+
return fs.existsSync(outPath) ? fs.statSync(outPath).mtimeMs : null;
|
|
80
|
+
}
|
|
81
|
+
// Never leave a broken or partial render behind: on any failure the output
|
|
82
|
+
// this run wrote is deleted (a pre-existing file we did not touch stays).
|
|
83
|
+
function removePartialOutput(outPath, mtimeBefore, log) {
|
|
84
|
+
const mtimeNow = mtimeOf(outPath);
|
|
85
|
+
if (mtimeNow !== null && (mtimeBefore === null || mtimeNow !== mtimeBefore)) {
|
|
86
|
+
fs.rmSync(outPath, { force: true });
|
|
87
|
+
log(`removed partial output: ${outPath}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async function renderWithOutputCleanup(outPath, log, render) {
|
|
91
|
+
const mtimeBefore = mtimeOf(outPath);
|
|
92
|
+
try {
|
|
93
|
+
await render();
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
removePartialOutput(outPath, mtimeBefore, log);
|
|
97
|
+
throw e;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const realDeps = {
|
|
101
|
+
probeVideo,
|
|
102
|
+
loadTelemetry,
|
|
103
|
+
renderVideo,
|
|
104
|
+
verifyOutput,
|
|
105
|
+
outputMatchesSource,
|
|
106
|
+
};
|
|
107
|
+
// A caller overrides only the dep its composition owns (the app shares its
|
|
108
|
+
// tile queue with every lane); nullish overrides - possible from untyped
|
|
109
|
+
// shell code - count as omitted, so no real dep is ever dropped. The bulk
|
|
110
|
+
// warm carries no cacheDir (providers own their cache), so the default
|
|
111
|
+
// binds the run's own cache here, where the run's config is in hand.
|
|
112
|
+
function resolveDeps(overrides, tileCache) {
|
|
113
|
+
return {
|
|
114
|
+
probeVideo: overrides?.probeVideo ?? realDeps.probeVideo,
|
|
115
|
+
loadTelemetry: overrides?.loadTelemetry ?? realDeps.loadTelemetry,
|
|
116
|
+
prefetchTiles: overrides?.prefetchTiles ??
|
|
117
|
+
((options) => prefetchTiles({ ...options, cacheDir: tileCache })),
|
|
118
|
+
renderVideo: overrides?.renderVideo ?? realDeps.renderVideo,
|
|
119
|
+
verifyOutput: overrides?.verifyOutput ?? realDeps.verifyOutput,
|
|
120
|
+
outputMatchesSource: overrides?.outputMatchesSource ?? realDeps.outputMatchesSource,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
// A segment export renders source frames start..end inclusive; the guard
|
|
124
|
+
// keeps the render and the verification honest about what the source has.
|
|
125
|
+
function resolveRange(range, frames) {
|
|
126
|
+
if (!range)
|
|
127
|
+
return null;
|
|
128
|
+
assertValidFrameRange(range);
|
|
129
|
+
if (range.end >= frames) {
|
|
130
|
+
throw new Error(`frame range ${range.start}-${range.end} is beyond the recording (${frames} frames)`);
|
|
131
|
+
}
|
|
132
|
+
return range;
|
|
133
|
+
}
|
|
134
|
+
function createPrepareReporter(onProgress = () => { }) {
|
|
135
|
+
return (stage) => onProgress({
|
|
136
|
+
stage,
|
|
137
|
+
done: PREPARE_STAGE_ORDER.indexOf(stage),
|
|
138
|
+
total: PREPARE_STAGE_ORDER.length,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
async function probeStage(env) {
|
|
142
|
+
const { videoPath, config, log, tracer, parent, deps, tools, report } = env;
|
|
143
|
+
report('probe');
|
|
144
|
+
const probe = await stage(tracer, 'probe', parent, {}, () => deps.probeVideo(tools, videoPath, log));
|
|
145
|
+
log(`source: ${path.basename(videoPath)} ${probe.width}x${probe.height} @ ${probe.fps.str} fps, ${probe.frames} frames`);
|
|
146
|
+
const introOutro = planIntroOutroFrames(config, {
|
|
147
|
+
fps: probe.fps.value,
|
|
148
|
+
frames: probe.frames,
|
|
149
|
+
});
|
|
150
|
+
if (introOutro.clamped) {
|
|
151
|
+
log(`intro/outro shortened to ${introOutro.introN}/${introOutro.outroN} frames for this clip`);
|
|
152
|
+
}
|
|
153
|
+
return { probe, introOutro };
|
|
154
|
+
}
|
|
155
|
+
async function telemetryStage(env, probe) {
|
|
156
|
+
const { videoPath, config, log, tracer, parent, deps, tools, report } = env;
|
|
157
|
+
report('telemetry');
|
|
158
|
+
const tel = await stage(tracer, 'telemetry.load', parent, {}, async (span) => {
|
|
159
|
+
const loaded = await deps.loadTelemetry(videoPath, {
|
|
160
|
+
frames: probe.frames,
|
|
161
|
+
fpsValue: probe.fps.value,
|
|
162
|
+
durationSec: probe.durationSec,
|
|
163
|
+
tools,
|
|
164
|
+
});
|
|
165
|
+
span.setAttributes({
|
|
166
|
+
'telemetry.source': loaded.source,
|
|
167
|
+
'telemetry.method': loaded.method,
|
|
168
|
+
'telemetry.fix.count': loaded.fixCount,
|
|
169
|
+
'telemetry.noLock.count': loaded.noLock,
|
|
170
|
+
});
|
|
171
|
+
return loaded;
|
|
172
|
+
});
|
|
173
|
+
const summary = telemetrySummary(tel);
|
|
174
|
+
log(`telemetry: ${summary.source} (${summary.method}), ${fixesLabel(summary.fixes)}, ` +
|
|
175
|
+
`heading=${summary.headingMode}, track ${trackDistanceLabel(summary.trackDistanceM)}` +
|
|
176
|
+
(summary.noLock ? `, ${noLockCuesLabel(summary.noLock)}` : ''));
|
|
177
|
+
const hud = resolveHud(config, tel, log);
|
|
178
|
+
return { tel, hud };
|
|
179
|
+
}
|
|
180
|
+
async function viewPlanStage(env, tel) {
|
|
181
|
+
const { config, log, tracer, parent, report } = env;
|
|
182
|
+
report('plan');
|
|
183
|
+
const overview = await stage(tracer, 'plan', parent, { 'plan.view': config.view }, async (span) => {
|
|
184
|
+
const computed = planView(tel.bbox, config.width, config.height, config);
|
|
185
|
+
span.setAttributes({
|
|
186
|
+
'plan.zoom': computed.zoom,
|
|
187
|
+
'plan.zoomOut': effectiveZoomOut(config),
|
|
188
|
+
'plan.tile.count': computed.tiles.length,
|
|
189
|
+
});
|
|
190
|
+
return computed;
|
|
191
|
+
});
|
|
192
|
+
log(describeView(config, overview, config.style));
|
|
193
|
+
return overview;
|
|
194
|
+
}
|
|
195
|
+
// The render path's tile pre-warm: rendered frames must never fall back to
|
|
196
|
+
// placeholder tiles, so the whole plan settles before capture starts. The
|
|
197
|
+
// app's open flow skips this and lets the player fetch on demand.
|
|
198
|
+
async function tileWarmStage(deps, config, overview, log, tracer, parent) {
|
|
199
|
+
const tileStats = await stage(tracer, 'tiles.prefetch', parent, {}, async (span) => {
|
|
200
|
+
const stats = await deps.prefetchTiles({
|
|
201
|
+
style: config.style,
|
|
202
|
+
tiles: overview.tiles,
|
|
203
|
+
log,
|
|
204
|
+
});
|
|
205
|
+
span.setAttributes({
|
|
206
|
+
'tiles.total': stats.total,
|
|
207
|
+
'tiles.downloaded': stats.downloaded,
|
|
208
|
+
'tiles.cached': stats.total - stats.downloaded - stats.failed,
|
|
209
|
+
'tiles.failed': stats.failed,
|
|
210
|
+
});
|
|
211
|
+
return stats;
|
|
212
|
+
});
|
|
213
|
+
if (tileStats.downloaded) {
|
|
214
|
+
log(`tiles: ${tileStats.downloaded} downloaded, ${tileStats.failed} placeholders`);
|
|
215
|
+
}
|
|
216
|
+
return tileStats;
|
|
217
|
+
}
|
|
218
|
+
// The preparation stages shared by the headless pipeline and the app: probe,
|
|
219
|
+
// telemetry, view plan. One trace context: the stages nest under the caller's
|
|
220
|
+
// parent span (or stand alone when none is given).
|
|
221
|
+
export async function prepareSession(videoPath, config, log, options = {}) {
|
|
222
|
+
const env = {
|
|
223
|
+
videoPath,
|
|
224
|
+
config,
|
|
225
|
+
log,
|
|
226
|
+
tracer: options.trace?.tracer ?? NOOP_TRACER,
|
|
227
|
+
parent: options.trace?.parent,
|
|
228
|
+
deps: resolveDeps(options.deps, config.tileCache),
|
|
229
|
+
tools: options.tools ?? resolveFfTools(config),
|
|
230
|
+
report: createPrepareReporter(options.onProgress),
|
|
231
|
+
};
|
|
232
|
+
// Config producers normalize the canvas (config.normalizeDimensions); the
|
|
233
|
+
// pipeline renders exactly what the vocabulary resolved.
|
|
234
|
+
const { width, height } = config;
|
|
235
|
+
const { probe, introOutro } = await probeStage(env);
|
|
236
|
+
const { tel, hud } = await telemetryStage(env, probe);
|
|
237
|
+
const overview = await viewPlanStage(env, tel);
|
|
238
|
+
const frames = frameArrays(tel);
|
|
239
|
+
return {
|
|
240
|
+
width,
|
|
241
|
+
height,
|
|
242
|
+
probe,
|
|
243
|
+
tel,
|
|
244
|
+
hud,
|
|
245
|
+
introOutro,
|
|
246
|
+
overview,
|
|
247
|
+
frames,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
// Maps a prepared recording onto the shared renderer page's inputs through
|
|
251
|
+
// the same planning the app's live restyle uses; callers add the video option
|
|
252
|
+
// when they drive the interactive variant. The planning triple is computed
|
|
253
|
+
// once in prepareSession and passed down, so the summary and the rendered
|
|
254
|
+
// page cannot drift apart.
|
|
255
|
+
export function recordingPageOpts(prep, config) {
|
|
256
|
+
return planPageOpts({
|
|
257
|
+
bbox: prep.tel.bbox,
|
|
258
|
+
width: prep.width,
|
|
259
|
+
height: prep.height,
|
|
260
|
+
config,
|
|
261
|
+
source: { fps: prep.probe.fps.value, frames: prep.probe.frames },
|
|
262
|
+
frames: prep.frames,
|
|
263
|
+
planned: {
|
|
264
|
+
overview: prep.overview,
|
|
265
|
+
introOutro: prep.introOutro,
|
|
266
|
+
hud: prep.hud,
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
function buildOkSummary(ctx, elapsedSec) {
|
|
271
|
+
const { config, probe, tel, hud, introOutro, overview, tileStats } = ctx;
|
|
272
|
+
const telemetry = telemetrySummary(tel);
|
|
273
|
+
return {
|
|
274
|
+
video: ctx.videoPath,
|
|
275
|
+
out: ctx.outPath,
|
|
276
|
+
status: 'ok',
|
|
277
|
+
width: ctx.width,
|
|
278
|
+
height: ctx.height,
|
|
279
|
+
style: config.style,
|
|
280
|
+
view: config.view,
|
|
281
|
+
introFrames: introOutro.introN,
|
|
282
|
+
outroFrames: introOutro.outroN,
|
|
283
|
+
hud,
|
|
284
|
+
// Mapped field by field instead of spreading: a rename in
|
|
285
|
+
// TelemetrySummary must land here visibly, not silently drop a key
|
|
286
|
+
// from the --json line.
|
|
287
|
+
source: telemetry.source,
|
|
288
|
+
method: telemetry.method,
|
|
289
|
+
headingMode: telemetry.headingMode,
|
|
290
|
+
fixes: telemetry.fixes,
|
|
291
|
+
noLock: telemetry.noLock,
|
|
292
|
+
trackDistanceM: telemetry.trackDistanceM,
|
|
293
|
+
frames: ctx.outFrames,
|
|
294
|
+
fps: probe.fps.str,
|
|
295
|
+
frameRange: ctx.frameRange ?? undefined,
|
|
296
|
+
bbox: tel.bbox,
|
|
297
|
+
zoom: overview.zoom,
|
|
298
|
+
zoomFixed: config.zoom > 0,
|
|
299
|
+
zoomOut: effectiveZoomOut(config),
|
|
300
|
+
tilesDownloaded: tileStats.downloaded,
|
|
301
|
+
verified: true,
|
|
302
|
+
elapsedSec,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
async function verifyRenderedOutput(env) {
|
|
306
|
+
const { ctx, deps, tools, tracer, root } = env;
|
|
307
|
+
await stage(tracer, 'output.verify', root, {}, async (span) => {
|
|
308
|
+
const verify = await deps.verifyOutput(tools, ctx.outPath, ctx.outFrames, ctx.probe.fps.value);
|
|
309
|
+
span.setAttributes({
|
|
310
|
+
'verify.frames.expected': ctx.outFrames,
|
|
311
|
+
'verify.frames.actual': verify.frames,
|
|
312
|
+
'verify.fps.expected': ctx.probe.fps.str,
|
|
313
|
+
'verify.fps.actual': verify.fps ?? 'unknown',
|
|
314
|
+
'verify.ok': verify.ok,
|
|
315
|
+
});
|
|
316
|
+
if (!verify.ok) {
|
|
317
|
+
throw new Error(`output verification failed: got ${verify.frames} frames @ ${verify.fps ?? 'unknown'}, ` +
|
|
318
|
+
`expected ${ctx.outFrames} @ ${ctx.probe.fps.str}`);
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
ctx.log(ctx.frameRange
|
|
322
|
+
? 'verified: frame count and frame rate are 1:1 within the exported range'
|
|
323
|
+
: 'verified: frame count and frame rate match the source');
|
|
324
|
+
}
|
|
325
|
+
async function renderToOutput(env) {
|
|
326
|
+
const { ctx, deps, tools, options, tracer, root } = env;
|
|
327
|
+
const html = buildPageHtml(recordingPageOpts(ctx, ctx.config));
|
|
328
|
+
fs.mkdirSync(path.dirname(ctx.outPath), { recursive: true });
|
|
329
|
+
await renderWithOutputCleanup(ctx.outPath, ctx.log, async () => {
|
|
330
|
+
await deps.renderVideo({
|
|
331
|
+
html,
|
|
332
|
+
capture: ctx.config.capture,
|
|
333
|
+
codec: ctx.config.codec,
|
|
334
|
+
quality: ctx.config.quality,
|
|
335
|
+
preset: ctx.config.preset,
|
|
336
|
+
style: ctx.config.style,
|
|
337
|
+
tileCache: ctx.config.tileCache,
|
|
338
|
+
tools,
|
|
339
|
+
width: ctx.width,
|
|
340
|
+
height: ctx.height,
|
|
341
|
+
frames: ctx.outFrames,
|
|
342
|
+
frameStart: ctx.frameRange?.start ?? 0,
|
|
343
|
+
fpsStr: ctx.probe.fps.str,
|
|
344
|
+
workers: ctx.config.workers,
|
|
345
|
+
outPath: ctx.outPath,
|
|
346
|
+
browserPath: ctx.config.browser,
|
|
347
|
+
signal: options.signal,
|
|
348
|
+
log: ctx.log,
|
|
349
|
+
trace: { tracer, parent: root },
|
|
350
|
+
onCapture: options.onCapture,
|
|
351
|
+
});
|
|
352
|
+
await verifyRenderedOutput(env);
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
// Throws on failure (the caller decides policy). outPath may be pre-planned by
|
|
356
|
+
// runBatch (collision-free); it defaults to the plain output path for direct
|
|
357
|
+
// callers. One trace per recording: the root span wraps the pipeline stages
|
|
358
|
+
// below.
|
|
359
|
+
export async function processRecording(videoPath, config, log, outPath = outputPathFor(videoPath, config), options = {}) {
|
|
360
|
+
const t0 = Date.now();
|
|
361
|
+
const tracer = options.trace?.tracer ?? NOOP_TRACER;
|
|
362
|
+
const deps = resolveDeps(options.deps, config.tileCache);
|
|
363
|
+
const tools = options.tools ?? resolveFfTools(config);
|
|
364
|
+
if (options.signal?.aborted)
|
|
365
|
+
throw new Error('render canceled');
|
|
366
|
+
return stage(tracer, 'recording.process', options.trace?.parent, {
|
|
367
|
+
'source.file.name': path.basename(videoPath),
|
|
368
|
+
}, async (root) => {
|
|
369
|
+
const prep = await prepareSession(videoPath, config, log, {
|
|
370
|
+
...options,
|
|
371
|
+
deps,
|
|
372
|
+
tools,
|
|
373
|
+
trace: { tracer, parent: root },
|
|
374
|
+
});
|
|
375
|
+
root.setAttributes({
|
|
376
|
+
'canvas.width': prep.width,
|
|
377
|
+
'canvas.height': prep.height,
|
|
378
|
+
'map.style': config.style,
|
|
379
|
+
'map.view': config.view,
|
|
380
|
+
'source.frames': prep.probe.frames,
|
|
381
|
+
'source.fps': prep.probe.fps.value,
|
|
382
|
+
'telemetry.source': prep.tel.source,
|
|
383
|
+
});
|
|
384
|
+
const tileStats = await tileWarmStage(deps, config, prep.overview, log, tracer, root);
|
|
385
|
+
const range = resolveRange(options.range, prep.probe.frames);
|
|
386
|
+
const outFrames = range ? range.end - range.start + 1 : prep.probe.frames;
|
|
387
|
+
const ctx = {
|
|
388
|
+
...prep,
|
|
389
|
+
videoPath,
|
|
390
|
+
outPath,
|
|
391
|
+
config,
|
|
392
|
+
log,
|
|
393
|
+
outFrames,
|
|
394
|
+
frameRange: range,
|
|
395
|
+
tileStats,
|
|
396
|
+
};
|
|
397
|
+
await renderToOutput({ ctx, deps, tools, options, tracer, root });
|
|
398
|
+
return buildOkSummary(ctx, Math.round((Date.now() - t0) / 1000));
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
// Failures never abort the batch.
|
|
402
|
+
export async function runBatch(videos, config, log, emit, options = {}) {
|
|
403
|
+
const result = { ok: 0, skipped: 0, failed: 0 };
|
|
404
|
+
const deps = resolveDeps(options.deps, config.tileCache);
|
|
405
|
+
const tools = options.tools ?? resolveFfTools(config);
|
|
406
|
+
// Collision-free outputs up front: two same-basename recordings must not
|
|
407
|
+
// silently overwrite each other in a shared --out-dir.
|
|
408
|
+
const plans = planOutputs(videos, config);
|
|
409
|
+
for (const [idx, video] of videos.entries()) {
|
|
410
|
+
const base = path.basename(video);
|
|
411
|
+
log(`=== ${base} ===`);
|
|
412
|
+
const plan = plans[idx];
|
|
413
|
+
if (plan.out !== plan.plain) {
|
|
414
|
+
log(`warning: output name collision for ${base} - writing ${path.basename(plan.out)} instead`);
|
|
415
|
+
}
|
|
416
|
+
if (config.skipExisting && (await deps.outputMatchesSource(tools, video, plan.out))) {
|
|
417
|
+
log(`SKIP ${plan.out} (exists and matches the source)`);
|
|
418
|
+
emit({ video, out: plan.out, status: 'skipped', elapsedSec: 0 });
|
|
419
|
+
result.skipped++;
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
try {
|
|
423
|
+
const summary = await processRecording(video, config, log, plan.out, {
|
|
424
|
+
...options,
|
|
425
|
+
deps,
|
|
426
|
+
tools,
|
|
427
|
+
});
|
|
428
|
+
emit(summary);
|
|
429
|
+
log(`OK ${plan.out} (${summary.frames} frames @ ${summary.fps}, ${summary.elapsedSec} s)`);
|
|
430
|
+
result.ok++;
|
|
431
|
+
}
|
|
432
|
+
catch (e) {
|
|
433
|
+
const message = errorMessage(e);
|
|
434
|
+
log(`ERROR ${base}: ${message}`);
|
|
435
|
+
emit({
|
|
436
|
+
video,
|
|
437
|
+
out: plan.out,
|
|
438
|
+
status: 'error',
|
|
439
|
+
elapsedSec: 0,
|
|
440
|
+
error: message,
|
|
441
|
+
});
|
|
442
|
+
result.failed++;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return result;
|
|
446
|
+
}
|
package/dist/serve.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Static assets and video streaming shared by the headless render server and
|
|
2
|
+
// the app's session server. URL routing and its guards live in src/assets.ts;
|
|
3
|
+
// this shell performs the I/O and writes the resolved result.
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
7
|
+
import { pipeline } from 'node:stream';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { mimeFor, resolveAsset } from './assets.js';
|
|
10
|
+
import { rangePlan } from './range.js';
|
|
11
|
+
const require_ = createRequire(import.meta.url);
|
|
12
|
+
const LEAFLET_DIR = path.dirname(require_.resolve('leaflet/dist/leaflet.js'));
|
|
13
|
+
/** Compiled shared renderer modules, served to generated pages as ESM. */
|
|
14
|
+
export const RENDERER_DIR = fileURLToPath(new URL('./renderer/', import.meta.url));
|
|
15
|
+
// Writes the shared router's result: the routed file (or its placeholder
|
|
16
|
+
// fallback) with its mime, or a 404 for a claimed URL without a file.
|
|
17
|
+
// Returns true when the URL belongs to one of the routes; callers route
|
|
18
|
+
// everything else themselves.
|
|
19
|
+
export function serveAsset(url, res, routes) {
|
|
20
|
+
const asset = resolveAsset(url, { ...routes, leafletDir: LEAFLET_DIR });
|
|
21
|
+
if (asset.kind === 'unrouted')
|
|
22
|
+
return false;
|
|
23
|
+
if (asset.kind === 'file') {
|
|
24
|
+
let file = asset.filePath;
|
|
25
|
+
if (!fs.existsSync(file) && asset.fallbackPath && fs.existsSync(asset.fallbackPath)) {
|
|
26
|
+
file = asset.fallbackPath;
|
|
27
|
+
}
|
|
28
|
+
if (fs.existsSync(file) && fs.statSync(file).isFile()) {
|
|
29
|
+
res.writeHead(200, { 'Content-Type': asset.mime });
|
|
30
|
+
res.end(fs.readFileSync(file));
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
35
|
+
res.end('not found');
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
// Streams a file with HTTP Range support so the <video> element can seek.
|
|
39
|
+
// The returned promise resolves once the file handle is released, so callers
|
|
40
|
+
// can order file cleanup after the stream.
|
|
41
|
+
export function serveFileWithRange(req, res, filePath) {
|
|
42
|
+
let size;
|
|
43
|
+
try {
|
|
44
|
+
size = fs.statSync(filePath).size;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
48
|
+
res.end('not found');
|
|
49
|
+
return Promise.resolve();
|
|
50
|
+
}
|
|
51
|
+
const mime = mimeFor(filePath);
|
|
52
|
+
const plan = rangePlan(req.headers['range'], size, mime);
|
|
53
|
+
res.writeHead(plan.status, plan.headers);
|
|
54
|
+
if (plan.status === 416) {
|
|
55
|
+
res.end(plan.body);
|
|
56
|
+
return Promise.resolve();
|
|
57
|
+
}
|
|
58
|
+
if (req.method === 'HEAD') {
|
|
59
|
+
res.end();
|
|
60
|
+
return Promise.resolve();
|
|
61
|
+
}
|
|
62
|
+
const stream = plan.status === 206
|
|
63
|
+
? fs.createReadStream(filePath, { start: plan.span.start, end: plan.span.end })
|
|
64
|
+
: fs.createReadStream(filePath);
|
|
65
|
+
// pipeline tears the read stream down when the client aborts (every video
|
|
66
|
+
// seek does), so the file handle is released and the promise settles.
|
|
67
|
+
return new Promise((resolve) => {
|
|
68
|
+
pipeline(stream, res, () => resolve());
|
|
69
|
+
});
|
|
70
|
+
}
|
package/dist/srt.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// One cue per video frame; the cue index equals the video frame index. Cues
|
|
2
|
+
// whose latitude/longitude line is not a real GPS fix (src/geo.ts's
|
|
3
|
+
// gpsFix - the (0, 0) placeholder or worse) are kept with fix = null so
|
|
4
|
+
// alignment is never lost.
|
|
5
|
+
import { readFile } from 'node:fs/promises';
|
|
6
|
+
import { gpsFix } from './geo.js';
|
|
7
|
+
const RX_TIMING = /^(\d{2}):(\d{2}):(\d{2})[,.](\d{3})\s*-->/;
|
|
8
|
+
const RX_LATLON = /\[latitude:\s*(-?\d+\.?\d*)\]\s*\[longitude:\s*(-?\d+\.?\d*)\]/;
|
|
9
|
+
const RX_ALT = /\[rel_alt:\s*(-?\d+\.?\d*)\s+abs_alt:\s*(-?\d+\.?\d*)\]/;
|
|
10
|
+
function parseNumber(text) {
|
|
11
|
+
const value = Number.parseFloat(text);
|
|
12
|
+
return Number.isFinite(value) ? value : null;
|
|
13
|
+
}
|
|
14
|
+
function stripFontTags(line) {
|
|
15
|
+
return line.replace(/<[^>]*>/g, '').trim();
|
|
16
|
+
}
|
|
17
|
+
function timingLineToMs(match) {
|
|
18
|
+
return (Number(match[1]) * 3600000 +
|
|
19
|
+
Number(match[2]) * 60000 +
|
|
20
|
+
Number(match[3]) * 1000 +
|
|
21
|
+
Number(match[4]));
|
|
22
|
+
}
|
|
23
|
+
function newCue(tSec) {
|
|
24
|
+
return {
|
|
25
|
+
tSec,
|
|
26
|
+
fix: null,
|
|
27
|
+
relAlt: null,
|
|
28
|
+
absAlt: null,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export function parseSrtText(text) {
|
|
32
|
+
const cues = [];
|
|
33
|
+
let noLock = 0;
|
|
34
|
+
let baseMs = null;
|
|
35
|
+
// A second (repeated) lat/lon line in one cue must not count as another
|
|
36
|
+
// no-lock event - only the current cue can receive lines, so one flag is
|
|
37
|
+
// enough to remember whether it already saw one.
|
|
38
|
+
let cueSawLatLon = false;
|
|
39
|
+
for (const rawLine of text.split(/\r?\n/)) {
|
|
40
|
+
const line = stripFontTags(rawLine);
|
|
41
|
+
if (!line)
|
|
42
|
+
continue;
|
|
43
|
+
const timing = RX_TIMING.exec(line);
|
|
44
|
+
if (timing) {
|
|
45
|
+
const ms = timingLineToMs(timing);
|
|
46
|
+
baseMs ??= ms;
|
|
47
|
+
// A timing line always starts a new cue.
|
|
48
|
+
cues.push(newCue((ms - baseMs) / 1000));
|
|
49
|
+
cueSawLatLon = false;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (cues.length === 0)
|
|
53
|
+
continue;
|
|
54
|
+
const cue = cues[cues.length - 1];
|
|
55
|
+
const latLon = RX_LATLON.exec(line);
|
|
56
|
+
if (latLon && !cueSawLatLon) {
|
|
57
|
+
cueSawLatLon = true;
|
|
58
|
+
const fix = gpsFix(parseNumber(latLon[1]), parseNumber(latLon[2]));
|
|
59
|
+
if (fix !== null) {
|
|
60
|
+
cue.fix = fix;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
noLock++;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const alt = RX_ALT.exec(line);
|
|
67
|
+
if (alt && cue.relAlt === null) {
|
|
68
|
+
cue.relAlt = parseNumber(alt[1]);
|
|
69
|
+
cue.absAlt = parseNumber(alt[2]);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return { cues, noLock, count: cues.length };
|
|
73
|
+
}
|
|
74
|
+
export async function parseSrt(path) {
|
|
75
|
+
return parseSrtText(await readFile(path, 'utf8'));
|
|
76
|
+
}
|
package/dist/summary.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// The shared value vocabulary of a recording's telemetry summary: one mapping
|
|
2
|
+
// from Telemetry, consumed by the batch summary and the CLI log line, so their
|
|
3
|
+
// numbers cannot drift apart.
|
|
4
|
+
export function telemetrySummary(tel) {
|
|
5
|
+
return {
|
|
6
|
+
source: tel.source,
|
|
7
|
+
method: tel.method,
|
|
8
|
+
headingMode: tel.headingMode,
|
|
9
|
+
fixes: tel.fixCount,
|
|
10
|
+
noLock: tel.noLock,
|
|
11
|
+
trackDistanceM: Math.round(tel.trackDistanceM),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function fixesLabel(fixes) {
|
|
15
|
+
return `${fixes} fixes`;
|
|
16
|
+
}
|
|
17
|
+
export function noLockCuesLabel(noLock) {
|
|
18
|
+
return `${noLock} no-lock cues`;
|
|
19
|
+
}
|
|
20
|
+
export function trackDistanceLabel(trackDistanceM) {
|
|
21
|
+
return `${trackDistanceM} m`;
|
|
22
|
+
}
|