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
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Config file discovery and layer resolution: the imperative side of the
|
|
2
|
+
// option precedence (CLI flags > config file > built-in defaults). The config
|
|
3
|
+
// file is discovered upward from the working directory unless --config names
|
|
4
|
+
// one. The vocabulary itself stays pure in src/config.ts.
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { userDataDir } from './apppaths.js';
|
|
8
|
+
import { CONFIG_FILE_NAME, DEFAULT_CONFIG, normalizeDimensions, normalizeOverrides, validateRenderConfig, } from './config.js';
|
|
9
|
+
// DRONEPATH_TILE_CACHE overrides the per-user cache directory.
|
|
10
|
+
export function defaultTileCache() {
|
|
11
|
+
const override = process.env['DRONEPATH_TILE_CACHE'];
|
|
12
|
+
if (override)
|
|
13
|
+
return path.resolve(override);
|
|
14
|
+
return path.join(userDataDir(), 'tiles');
|
|
15
|
+
}
|
|
16
|
+
export function discoverConfigFile(startDir) {
|
|
17
|
+
let dir = path.resolve(startDir);
|
|
18
|
+
for (;;) {
|
|
19
|
+
const candidate = path.join(dir, CONFIG_FILE_NAME);
|
|
20
|
+
if (fs.existsSync(candidate))
|
|
21
|
+
return candidate;
|
|
22
|
+
const parent = path.dirname(dir);
|
|
23
|
+
if (parent === dir)
|
|
24
|
+
return null;
|
|
25
|
+
dir = parent;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function loadConfigFile(file) {
|
|
29
|
+
let text;
|
|
30
|
+
try {
|
|
31
|
+
text = fs.readFileSync(file, 'utf8');
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
throw new Error(`cannot read config file ${file}: ${e.message}`, {
|
|
35
|
+
cause: e,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
let raw;
|
|
39
|
+
try {
|
|
40
|
+
raw = JSON.parse(text);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
throw new Error(`config file is not valid JSON: ${file}: ${e.message}`, { cause: e });
|
|
44
|
+
}
|
|
45
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
46
|
+
throw new Error(`config file must contain a JSON object: ${file}`);
|
|
47
|
+
}
|
|
48
|
+
return normalizeOverrides(raw, file);
|
|
49
|
+
}
|
|
50
|
+
function mergeConfigLayers(fileCfg, cli) {
|
|
51
|
+
return {
|
|
52
|
+
...DEFAULT_CONFIG,
|
|
53
|
+
...fileCfg,
|
|
54
|
+
...cli,
|
|
55
|
+
// hud is an object; Partial spread replaces it wholesale, which is what
|
|
56
|
+
// we want. The winner is copied so no resolved config shares mutable
|
|
57
|
+
// state with DEFAULT_CONFIG or another layer.
|
|
58
|
+
hud: { ...(cli.hud ?? fileCfg.hud ?? DEFAULT_CONFIG.hud) },
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function resolveConfigPaths(config, cwd) {
|
|
62
|
+
return {
|
|
63
|
+
...config,
|
|
64
|
+
tileCache: path.resolve(cwd, config.tileCache || defaultTileCache()),
|
|
65
|
+
outDir: config.outDir ? path.resolve(cwd, config.outDir) : null,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export function resolveConfig(opts) {
|
|
69
|
+
const cwd = opts.cwd ?? process.cwd();
|
|
70
|
+
const configPath = opts.configPath !== undefined && opts.configPath !== null
|
|
71
|
+
? path.resolve(cwd, opts.configPath)
|
|
72
|
+
: discoverConfigFile(cwd);
|
|
73
|
+
const fileCfg = configPath ? loadConfigFile(configPath) : {};
|
|
74
|
+
const merged = mergeConfigLayers(fileCfg, opts.cli ?? {});
|
|
75
|
+
validateRenderConfig(merged);
|
|
76
|
+
const normalized = {
|
|
77
|
+
...merged,
|
|
78
|
+
...normalizeDimensions(merged.width, merged.height),
|
|
79
|
+
};
|
|
80
|
+
return { config: resolveConfigPaths(normalized, cwd), configPath };
|
|
81
|
+
}
|
package/dist/decimate.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Up-front trail simplification: Douglas-Peucker in Web Mercator pixel space
|
|
2
|
+
// at the working zoom, so the drawn trail deviates from the flown track by
|
|
3
|
+
// less than about one pixel while its point count stays bounded regardless
|
|
4
|
+
// of flight length. Input arrays are index-aligned to video frames.
|
|
5
|
+
import { project } from './geo.js';
|
|
6
|
+
export const DEFAULT_EPSILON_PX = 1;
|
|
7
|
+
export const DEFAULT_MAX_POINTS = 3000;
|
|
8
|
+
export function decimateTrack(lat, lon, zoom, options = {}) {
|
|
9
|
+
const epsilonPx = options.epsilonPx ?? DEFAULT_EPSILON_PX;
|
|
10
|
+
const maxPoints = options.maxPoints ?? DEFAULT_MAX_POINTS;
|
|
11
|
+
if (!(epsilonPx >= 0))
|
|
12
|
+
throw new Error('epsilon must be >= 0');
|
|
13
|
+
if (!Number.isInteger(maxPoints) || maxPoints < 2) {
|
|
14
|
+
throw new Error('maxPoints must be an integer >= 2');
|
|
15
|
+
}
|
|
16
|
+
if (lat.length !== lon.length) {
|
|
17
|
+
throw new Error('lat and lon must have the same length');
|
|
18
|
+
}
|
|
19
|
+
const n = lat.length;
|
|
20
|
+
if (n === 0)
|
|
21
|
+
return { lat: [], lon: [], frame: [] };
|
|
22
|
+
if (n === 1)
|
|
23
|
+
return { lat: [lat[0]], lon: [lon[0]], frame: [0] };
|
|
24
|
+
const px = new Array(n);
|
|
25
|
+
for (let i = 0; i < n; i++)
|
|
26
|
+
px[i] = project(lon[i], lat[i], zoom);
|
|
27
|
+
let keep = douglasPeucker(px, epsilonPx);
|
|
28
|
+
if (keep.length > maxPoints) {
|
|
29
|
+
// The cap is a promise (<=3k visible points on a 20 min flight), so the
|
|
30
|
+
// epsilon doubles until it holds; geometric growth keeps that
|
|
31
|
+
// deterministic and cheap (one or two extra passes in practice).
|
|
32
|
+
let hi = epsilonPx > 0 ? epsilonPx * 2 : 1;
|
|
33
|
+
let hiKeep = douglasPeucker(px, hi);
|
|
34
|
+
while (hiKeep.length > maxPoints) {
|
|
35
|
+
hi *= 2;
|
|
36
|
+
hiKeep = douglasPeucker(px, hi);
|
|
37
|
+
}
|
|
38
|
+
keep = hiKeep;
|
|
39
|
+
}
|
|
40
|
+
const track = { lat: [], lon: [], frame: [] };
|
|
41
|
+
for (const i of keep) {
|
|
42
|
+
track.lat.push(lat[i]);
|
|
43
|
+
track.lon.push(lon[i]);
|
|
44
|
+
track.frame.push(i);
|
|
45
|
+
}
|
|
46
|
+
return track;
|
|
47
|
+
}
|
|
48
|
+
// Iterative (stack-based) so adversarial tracks cannot blow the call stack.
|
|
49
|
+
function douglasPeucker(px, epsilon) {
|
|
50
|
+
const n = px.length;
|
|
51
|
+
const keep = new Uint8Array(n);
|
|
52
|
+
keep[0] = 1;
|
|
53
|
+
keep[n - 1] = 1;
|
|
54
|
+
const stack = [0, n - 1];
|
|
55
|
+
while (stack.length > 0) {
|
|
56
|
+
const end = stack.pop();
|
|
57
|
+
const start = stack.pop();
|
|
58
|
+
if (end - start < 2)
|
|
59
|
+
continue;
|
|
60
|
+
const a = px[start];
|
|
61
|
+
const b = px[end];
|
|
62
|
+
const mid = (start + end) / 2;
|
|
63
|
+
let maxDist = -1;
|
|
64
|
+
let maxIdx = -1;
|
|
65
|
+
for (let i = start + 1; i < end; i++) {
|
|
66
|
+
const d = distanceToSegment(px[i], a, b);
|
|
67
|
+
// Ties go to the middle-most point so regular patterns (hover
|
|
68
|
+
// jitter, zigzags) split evenly instead of degenerating to O(n^2).
|
|
69
|
+
if (d > maxDist ||
|
|
70
|
+
(d === maxDist && Math.abs(i - mid) < Math.abs(maxIdx - mid))) {
|
|
71
|
+
maxDist = d;
|
|
72
|
+
maxIdx = i;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (maxDist > epsilon) {
|
|
76
|
+
keep[maxIdx] = 1;
|
|
77
|
+
stack.push(start, maxIdx, maxIdx, end);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const indices = [];
|
|
81
|
+
for (let i = 0; i < n; i++)
|
|
82
|
+
if (keep[i])
|
|
83
|
+
indices.push(i);
|
|
84
|
+
return indices;
|
|
85
|
+
}
|
|
86
|
+
function distanceToSegment(p, a, b) {
|
|
87
|
+
const dx = b.x - a.x;
|
|
88
|
+
const dy = b.y - a.y;
|
|
89
|
+
const lenSq = dx * dx + dy * dy;
|
|
90
|
+
if (lenSq === 0)
|
|
91
|
+
return Math.sqrt((p.x - a.x) ** 2 + (p.y - a.y) ** 2);
|
|
92
|
+
const t = Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / lenSq));
|
|
93
|
+
return Math.sqrt((p.x - (a.x + t * dx)) ** 2 + (p.y - (a.y + t * dy)) ** 2);
|
|
94
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// The desktop app's export job runner, free of Electron APIs like
|
|
2
|
+
// src/appsession.ts: one blocking job at a time that renders to a working
|
|
3
|
+
// file and replaces the destination only after the output is verified, so a
|
|
4
|
+
// failed export never destroys a pre-existing file.
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { errorMessage } from './errors.js';
|
|
8
|
+
import { samePath, uniquePath } from './paths.js';
|
|
9
|
+
import { processRecording } from './run.js';
|
|
10
|
+
// The pipeline is called exactly like the CLI calls it: one job, the
|
|
11
|
+
// snapshot's config, the frozen range. The deps let an in-app export warm
|
|
12
|
+
// through the app's shared tile queue instead of a budget of its own.
|
|
13
|
+
const runViaPipeline = (deps) => (videoPath, request, log, workingPath, signal, onCapture) => processRecording(videoPath, request.config, log, workingPath, {
|
|
14
|
+
range: request.range ?? undefined,
|
|
15
|
+
signal,
|
|
16
|
+
onCapture,
|
|
17
|
+
deps,
|
|
18
|
+
});
|
|
19
|
+
// The working file must keep the .mp4 suffix (ffmpeg infers the container
|
|
20
|
+
// from it) and share the destination's directory so the final rename stays
|
|
21
|
+
// on one volume. Existing names are never reused or deleted: a stray file
|
|
22
|
+
// there may belong to someone else.
|
|
23
|
+
function workingPathFor(outPath) {
|
|
24
|
+
const dir = path.dirname(outPath);
|
|
25
|
+
const stem = `${path.basename(outPath, path.extname(outPath))}.partial`;
|
|
26
|
+
return uniquePath(dir, stem, '.mp4', (candidate) => fs.existsSync(candidate));
|
|
27
|
+
}
|
|
28
|
+
// Best-effort removal of our own working file; a cleanup failure must never
|
|
29
|
+
// mask the job's real error.
|
|
30
|
+
function removeQuietly(working) {
|
|
31
|
+
try {
|
|
32
|
+
fs.rmSync(working, { force: true });
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// nothing sensible to do
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// The module's core invariant: the destination is replaced only after the
|
|
39
|
+
// render is complete and verified. A failed swap must keep the finished
|
|
40
|
+
// render at the working path (e.g. when a player holds the destination open).
|
|
41
|
+
function swapIntoPlace(working, outPath) {
|
|
42
|
+
try {
|
|
43
|
+
fs.renameSync(working, outPath);
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
throw new Error(`could not replace ${outPath}: ${errorMessage(e)} ` +
|
|
47
|
+
`(the finished render is at ${working})`, { cause: e });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function createExportRunner(options) {
|
|
51
|
+
const log = options.log ?? (() => { });
|
|
52
|
+
const run = options.run ?? runViaPipeline(options.deps);
|
|
53
|
+
let job = null;
|
|
54
|
+
async function execute(videoPath, request, working, controller) {
|
|
55
|
+
let summary;
|
|
56
|
+
try {
|
|
57
|
+
summary = await run(videoPath, request, log, working, controller.signal, options.onCapture);
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
removeQuietly(working);
|
|
61
|
+
return controller.signal.aborted
|
|
62
|
+
? { status: 'canceled' }
|
|
63
|
+
: { status: 'error', error: errorMessage(e) };
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
swapIntoPlace(working, request.outPath);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
return { status: 'error', error: errorMessage(e) };
|
|
70
|
+
}
|
|
71
|
+
log(`exported: ${request.outPath}`);
|
|
72
|
+
return { status: 'ok', summary: { ...summary, out: request.outPath } };
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
start(videoPath, request) {
|
|
76
|
+
if (job)
|
|
77
|
+
throw new Error('an export is already running');
|
|
78
|
+
if (!path.isAbsolute(request.outPath)) {
|
|
79
|
+
throw new Error(`output path must be absolute: ${request.outPath}`);
|
|
80
|
+
}
|
|
81
|
+
if (samePath(request.outPath, videoPath)) {
|
|
82
|
+
throw new Error('the output must not overwrite the source recording');
|
|
83
|
+
}
|
|
84
|
+
const controller = new AbortController();
|
|
85
|
+
job = { abort: () => controller.abort() };
|
|
86
|
+
const working = workingPathFor(request.outPath);
|
|
87
|
+
// The catch guard contains a consumer's throwing onDone (and any
|
|
88
|
+
// broken chain): an unhandled rejection here would kill the process.
|
|
89
|
+
void execute(videoPath, request, working, controller)
|
|
90
|
+
.then((outcome) => {
|
|
91
|
+
job = null;
|
|
92
|
+
options.onDone(outcome);
|
|
93
|
+
})
|
|
94
|
+
.catch((e) => {
|
|
95
|
+
job = null;
|
|
96
|
+
log(`export completion failed: ${errorMessage(e)}`);
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
cancel() {
|
|
100
|
+
job?.abort();
|
|
101
|
+
},
|
|
102
|
+
running() {
|
|
103
|
+
return job !== null;
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
package/dist/ff.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// ffmpeg and ffprobe invocation: the resolved tool paths come from
|
|
2
|
+
// src/fftools.ts (override, embedded binary, then PATH).
|
|
3
|
+
import { execFile } from 'node:child_process';
|
|
4
|
+
import { promisify } from 'node:util';
|
|
5
|
+
const execFileAsync = promisify(execFile);
|
|
6
|
+
const INSTALL_HINT = 'point --ffmpeg/--ffprobe at your executables, or install ffmpeg (includes ' +
|
|
7
|
+
'ffprobe) and make sure both are on PATH, e.g. "winget install Gyan.FFmpeg" ' +
|
|
8
|
+
'(Windows), "brew install ffmpeg" (macOS) or "apt install ffmpeg" (Linux)';
|
|
9
|
+
const FFPROBE_MAX_BUFFER = 64 * 1024 * 1024;
|
|
10
|
+
export async function requireTools(tools) {
|
|
11
|
+
for (const tool of ['ffmpeg', 'ffprobe']) {
|
|
12
|
+
const command = tools[tool];
|
|
13
|
+
try {
|
|
14
|
+
await execFileAsync(command, ['-version'], { maxBuffer: 1 << 20 });
|
|
15
|
+
}
|
|
16
|
+
catch (e) {
|
|
17
|
+
throw new Error(`${tool} is required but could not be run (${command}) - ${INSTALL_HINT}`, { cause: e });
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export async function ffprobeJson(tools, videoPath, args) {
|
|
22
|
+
const { stdout } = await execFileAsync(tools.ffprobe, ['-v', 'error', ...args, videoPath], { maxBuffer: FFPROBE_MAX_BUFFER });
|
|
23
|
+
return JSON.parse(stdout || '{}');
|
|
24
|
+
}
|
|
25
|
+
// One split of ffprobe's rate wire format ("30" or "30000/1001"), with each
|
|
26
|
+
// consumer's own policy on top: the probe needs a usable rate, the output
|
|
27
|
+
// verification only reads a value.
|
|
28
|
+
function parseRate(str) {
|
|
29
|
+
const [num, den] = str.split('/').map(Number);
|
|
30
|
+
return Number.isFinite(num) ? { num, den } : null;
|
|
31
|
+
}
|
|
32
|
+
function parseFpsRate(str) {
|
|
33
|
+
const rate = str ? parseRate(str) : null;
|
|
34
|
+
if (!rate || !Number.isFinite(rate.den) || rate.den === 0 || rate.num === 0) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const { num } = rate;
|
|
38
|
+
const den = rate.den;
|
|
39
|
+
return {
|
|
40
|
+
num,
|
|
41
|
+
den,
|
|
42
|
+
value: num / den,
|
|
43
|
+
str: den === 1 ? String(num) : `${num}/${den}`,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function parseCount(text) {
|
|
47
|
+
return Number.parseInt(text ?? '', 10);
|
|
48
|
+
}
|
|
49
|
+
function firstStream(j) {
|
|
50
|
+
return (j['streams'] ?? [])[0] ?? {};
|
|
51
|
+
}
|
|
52
|
+
export async function probeVideo(tools, videoPath, log = () => { }) {
|
|
53
|
+
const j = await ffprobeJson(tools, videoPath, [
|
|
54
|
+
'-select_streams', 'v:0',
|
|
55
|
+
'-show_entries',
|
|
56
|
+
'stream=r_frame_rate,avg_frame_rate,nb_frames,width,height:format=duration',
|
|
57
|
+
'-of', 'json',
|
|
58
|
+
]);
|
|
59
|
+
const stream = firstStream(j);
|
|
60
|
+
const format = j['format'] ?? {};
|
|
61
|
+
const fps = parseFpsRate(stream.r_frame_rate) ??
|
|
62
|
+
parseFpsRate(stream.avg_frame_rate);
|
|
63
|
+
if (!fps)
|
|
64
|
+
throw new Error('could not determine the frame rate of the video');
|
|
65
|
+
const durationSec = Number.parseFloat(format.duration ?? '');
|
|
66
|
+
let frames = parseCount(stream.nb_frames);
|
|
67
|
+
if (!Number.isFinite(frames) || frames <= 0) {
|
|
68
|
+
frames = Math.round(durationSec * fps.value);
|
|
69
|
+
log('warning: nb_frames missing, using duration x fps');
|
|
70
|
+
}
|
|
71
|
+
// A NaN frame count would flow into renderVideo (zero workers, empty
|
|
72
|
+
// ffmpeg input) and fail much later with a confusing message.
|
|
73
|
+
if (!Number.isFinite(frames) || frames <= 0) {
|
|
74
|
+
throw new Error(`could not determine the frame count of ${videoPath} ` +
|
|
75
|
+
'(nb_frames missing and duration unusable)');
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
fps,
|
|
79
|
+
frames,
|
|
80
|
+
durationSec,
|
|
81
|
+
width: stream.width ?? 0,
|
|
82
|
+
height: stream.height ?? 0,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export async function verifyOutput(tools, outPath, frames, fpsValue) {
|
|
86
|
+
const j = await ffprobeJson(tools, outPath, [
|
|
87
|
+
'-select_streams', 'v:0',
|
|
88
|
+
'-show_entries', 'stream=nb_frames,r_frame_rate',
|
|
89
|
+
'-of', 'json',
|
|
90
|
+
]);
|
|
91
|
+
const stream = firstStream(j);
|
|
92
|
+
const gotFrames = parseCount(stream.nb_frames);
|
|
93
|
+
const rate = parseRate(stream.r_frame_rate ?? '0/0');
|
|
94
|
+
const gotValue = rate ? (rate.den ? rate.num / rate.den : rate.num) : null;
|
|
95
|
+
return {
|
|
96
|
+
frames: gotFrames,
|
|
97
|
+
fps: stream.r_frame_rate ?? null,
|
|
98
|
+
ok: gotFrames === frames &&
|
|
99
|
+
gotValue !== null &&
|
|
100
|
+
Math.abs(gotValue - fpsValue) < 1e-6,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async function probeRateFrames(ffprobe, file) {
|
|
104
|
+
const { stdout } = await execFileAsync(ffprobe, [
|
|
105
|
+
'-v', 'error', '-select_streams', 'v:0',
|
|
106
|
+
'-show_entries', 'stream=avg_frame_rate,nb_frames',
|
|
107
|
+
'-of', 'csv=p=0', '--', file,
|
|
108
|
+
], { maxBuffer: 1 << 20 });
|
|
109
|
+
return stdout.trim();
|
|
110
|
+
}
|
|
111
|
+
// Missing or unreadable outputs count as a mismatch, not an error.
|
|
112
|
+
export async function outputMatchesSource(tools, src, out) {
|
|
113
|
+
try {
|
|
114
|
+
const [srcRate, srcFrames] = (await probeRateFrames(tools.ffprobe, src)).split(',');
|
|
115
|
+
const [outRate, outFrames] = (await probeRateFrames(tools.ffprobe, out)).split(',');
|
|
116
|
+
return Number(outFrames) > 0 && srcRate === outRate && srcFrames === outFrames;
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
package/dist/fftools.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Where the ffmpeg and ffprobe executables come from at runtime: an explicit
|
|
2
|
+
// override, then the embedded binaries (the packaged app's extraResources, or
|
|
3
|
+
// the optional ffmpeg-ffprobe-static package in dev/CLI installs), then PATH.
|
|
4
|
+
// Pure decision core with an injectable environment so the resolution order is
|
|
5
|
+
// testable without touching the filesystem or spawning anything.
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import { createRequire } from 'node:module';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
// A bare command name defers to spawn's PATH lookup; anything that looks like
|
|
10
|
+
// a path is the documented `<path>` override and must name a real file.
|
|
11
|
+
function isPathLike(value) {
|
|
12
|
+
return value.includes('/') || value.includes('\\') || path.isAbsolute(value);
|
|
13
|
+
}
|
|
14
|
+
export function pickFfTool(tool, override, candidates, exists) {
|
|
15
|
+
if (override) {
|
|
16
|
+
if (isPathLike(override) && !exists(override)) {
|
|
17
|
+
throw new Error(`invalid ${tool} override (CLI --${tool} or config key \`${tool}\`): no such file: ${override}`);
|
|
18
|
+
}
|
|
19
|
+
return override;
|
|
20
|
+
}
|
|
21
|
+
for (const candidate of candidates) {
|
|
22
|
+
if (candidate && exists(candidate))
|
|
23
|
+
return candidate;
|
|
24
|
+
}
|
|
25
|
+
return tool;
|
|
26
|
+
}
|
|
27
|
+
function exeName(tool) {
|
|
28
|
+
return process.platform === 'win32' ? `${tool}.exe` : tool;
|
|
29
|
+
}
|
|
30
|
+
function packagedCandidate(tool) {
|
|
31
|
+
const resourcesPath = process.resourcesPath;
|
|
32
|
+
return resourcesPath ? path.join(resourcesPath, 'ff', exeName(tool)) : null;
|
|
33
|
+
}
|
|
34
|
+
// The optional ffmpeg-ffprobe-static package may be absent (unsupported
|
|
35
|
+
// platform, --omit=optional, a failed install download) and must never crash
|
|
36
|
+
// module loading: resolve it lazily.
|
|
37
|
+
function devCandidate(tool) {
|
|
38
|
+
try {
|
|
39
|
+
const mod = createRequire(import.meta.url)('ffmpeg-ffprobe-static');
|
|
40
|
+
const candidate = tool === 'ffmpeg' ? mod.ffmpegPath : mod.ffprobePath;
|
|
41
|
+
return typeof candidate === 'string' ? candidate : null;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function realEnv() {
|
|
48
|
+
return {
|
|
49
|
+
exists: (file) => {
|
|
50
|
+
try {
|
|
51
|
+
return fs.statSync(file).isFile();
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
candidates: (tool) => [packagedCandidate(tool), devCandidate(tool)],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export function resolveFfTools(overrides, env = realEnv()) {
|
|
61
|
+
return {
|
|
62
|
+
ffmpeg: pickFfTool('ffmpeg', overrides.ffmpeg, env.candidates('ffmpeg'), env.exists),
|
|
63
|
+
ffprobe: pickFfTool('ffprobe', overrides.ffprobe, env.candidates('ffprobe'), env.exists),
|
|
64
|
+
};
|
|
65
|
+
}
|
package/dist/geo.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// All coordinates are WGS84 decimal degrees; distances in meters.
|
|
2
|
+
const EARTH_RADIUS_M = 6371008.8; // mean Earth radius
|
|
3
|
+
const DEG_TO_RAD = Math.PI / 180;
|
|
4
|
+
const RAD_TO_DEG = 180 / Math.PI;
|
|
5
|
+
// What counts as a real GPS fix - one rule for every telemetry source (SRT
|
|
6
|
+
// sidecar and CAM meta alike): a finite WGS84 pair in range that is not
|
|
7
|
+
// exactly the (0, 0) "no GPS lock" placeholder DJI writes before the first
|
|
8
|
+
// fix. Near-equator and near-prime-meridian coordinates are real fixes;
|
|
9
|
+
// out-of-range values never are.
|
|
10
|
+
export function isGpsFix(lat, lon) {
|
|
11
|
+
return (Number.isFinite(lat) &&
|
|
12
|
+
Number.isFinite(lon) &&
|
|
13
|
+
lat > -90 &&
|
|
14
|
+
lat < 90 &&
|
|
15
|
+
lon > -180 &&
|
|
16
|
+
lon < 180 &&
|
|
17
|
+
!(lat === 0 && lon === 0));
|
|
18
|
+
}
|
|
19
|
+
// The fix/no-lock decision as a value: a real fix or null. Parsers decide
|
|
20
|
+
// once at this point and carry `fix: LatLon | null` from there on, so no
|
|
21
|
+
// downstream code re-litigates coordinates or defends half-set pairs.
|
|
22
|
+
export function gpsFix(lat, lon) {
|
|
23
|
+
return lat !== null && lon !== null && isGpsFix(lat, lon) ? { lat, lon } : null;
|
|
24
|
+
}
|
|
25
|
+
export function haversineM(a, b) {
|
|
26
|
+
const deltaLatRad = (b.lat - a.lat) * DEG_TO_RAD;
|
|
27
|
+
const deltaLonRad = (b.lon - a.lon) * DEG_TO_RAD;
|
|
28
|
+
const latARad = a.lat * DEG_TO_RAD;
|
|
29
|
+
const latBRad = b.lat * DEG_TO_RAD;
|
|
30
|
+
const haversine = Math.sin(deltaLatRad / 2) ** 2 +
|
|
31
|
+
Math.cos(latARad) * Math.cos(latBRad) * Math.sin(deltaLonRad / 2) ** 2;
|
|
32
|
+
return 2 * EARTH_RADIUS_M * Math.asin(Math.min(1, Math.sqrt(haversine)));
|
|
33
|
+
}
|
|
34
|
+
// Initial bearing, degrees clockwise from north, range [0, 360).
|
|
35
|
+
export function bearingDeg(a, b) {
|
|
36
|
+
const latARad = a.lat * DEG_TO_RAD;
|
|
37
|
+
const latBRad = b.lat * DEG_TO_RAD;
|
|
38
|
+
const deltaLonRad = (b.lon - a.lon) * DEG_TO_RAD;
|
|
39
|
+
const y = Math.sin(deltaLonRad) * Math.cos(latBRad);
|
|
40
|
+
const x = Math.cos(latARad) * Math.sin(latBRad) -
|
|
41
|
+
Math.sin(latARad) * Math.cos(latBRad) * Math.cos(deltaLonRad);
|
|
42
|
+
return (Math.atan2(y, x) * RAD_TO_DEG + 360) % 360;
|
|
43
|
+
}
|
|
44
|
+
export function circularMeanDeg(degList) {
|
|
45
|
+
let sumCos = 0;
|
|
46
|
+
let sumSin = 0;
|
|
47
|
+
for (const deg of degList) {
|
|
48
|
+
sumCos += Math.cos(deg * DEG_TO_RAD);
|
|
49
|
+
sumSin += Math.sin(deg * DEG_TO_RAD);
|
|
50
|
+
}
|
|
51
|
+
if (sumCos === 0 && sumSin === 0)
|
|
52
|
+
return degList[0] ?? 0;
|
|
53
|
+
return (Math.atan2(sumSin, sumCos) * RAD_TO_DEG + 360) % 360;
|
|
54
|
+
}
|
|
55
|
+
// Web Mercator (EPSG:3857) global pixel coordinates, 256 px tiles,
|
|
56
|
+
// origin at the north-west corner (Leaflet convention).
|
|
57
|
+
export function project(lon, lat, zoom) {
|
|
58
|
+
const scale = 256 * 2 ** zoom;
|
|
59
|
+
const x = ((lon + 180) / 360) * scale;
|
|
60
|
+
const sinLat = Math.sin(Math.max(-85.05112878, Math.min(85.05112878, lat)) * DEG_TO_RAD);
|
|
61
|
+
const y = (0.5 - Math.log((1 + sinLat) / (1 - sinLat)) / (4 * Math.PI)) * scale;
|
|
62
|
+
return { x, y };
|
|
63
|
+
}
|
|
64
|
+
export function unproject(x, y, zoom) {
|
|
65
|
+
const scale = 256 * 2 ** zoom;
|
|
66
|
+
const lon = (x / scale) * 360 - 180;
|
|
67
|
+
const mercatorN = Math.PI - 2 * Math.PI * (y / scale);
|
|
68
|
+
const lat = RAD_TO_DEG * Math.atan(Math.sinh(mercatorN));
|
|
69
|
+
return { lat, lon };
|
|
70
|
+
}
|
package/dist/inputs.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
// The one recording-extension rule: the CLI input scan and the app's pick
|
|
4
|
+
// check share it, so accepting another container (e.g. .MOV) is one edit.
|
|
5
|
+
export function isRecordingName(name) {
|
|
6
|
+
return name.toLowerCase().endsWith('.mp4');
|
|
7
|
+
}
|
|
8
|
+
// The recording's stem: sidecar lookup and output naming both key on it.
|
|
9
|
+
export function videoBaseName(filePath) {
|
|
10
|
+
return path.basename(filePath).replace(/\.[^.]+$/, '');
|
|
11
|
+
}
|
|
12
|
+
// Sidecar lookup matches the video basename, case-insensitively (.SRT vs .srt).
|
|
13
|
+
export function findSrtSidecar(videoPath) {
|
|
14
|
+
const dir = path.dirname(videoPath);
|
|
15
|
+
const base = videoBaseName(videoPath);
|
|
16
|
+
for (const f of fs.readdirSync(dir)) {
|
|
17
|
+
if (f.toLowerCase() === `${base.toLowerCase()}.srt`) {
|
|
18
|
+
return path.join(dir, f);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
function listMp4(dir, recurse) {
|
|
24
|
+
const found = [];
|
|
25
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
26
|
+
const full = path.join(dir, entry.name);
|
|
27
|
+
if (entry.isDirectory()) {
|
|
28
|
+
if (recurse)
|
|
29
|
+
found.push(...listMp4(full, recurse));
|
|
30
|
+
}
|
|
31
|
+
else if (entry.isFile() && isRecordingName(entry.name)) {
|
|
32
|
+
found.push(full);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return found;
|
|
36
|
+
}
|
|
37
|
+
export function collectRecordings(inputs, opts) {
|
|
38
|
+
const files = [];
|
|
39
|
+
for (const item of inputs) {
|
|
40
|
+
const full = path.resolve(item);
|
|
41
|
+
if (!fs.existsSync(full))
|
|
42
|
+
throw new Error(`input not found: ${item}`);
|
|
43
|
+
const st = fs.statSync(full);
|
|
44
|
+
if (st.isDirectory()) {
|
|
45
|
+
files.push(...listMp4(full, opts.recurse));
|
|
46
|
+
}
|
|
47
|
+
else if (isRecordingName(full)) {
|
|
48
|
+
files.push(full);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
throw new Error(`not an .mp4 recording: ${full}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const unique = [...new Set(files.map((f) => path.resolve(f)))].sort((a, b) => a.localeCompare(b, 'en'));
|
|
55
|
+
if (unique.length === 0) {
|
|
56
|
+
throw new Error('no .mp4 recordings found in the given input');
|
|
57
|
+
}
|
|
58
|
+
return unique;
|
|
59
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// GitHub-compatible heading anchors: the same slug GitHub derives for a
|
|
2
|
+
// heading so in-repo links and in-app links resolve to the same anchor.
|
|
3
|
+
const SLUG_STRIP = /[^\p{L}\p{N}\s_-]/gu;
|
|
4
|
+
/** The anchor GitHub gives a heading of plain `text`. */
|
|
5
|
+
export function githubSlug(text) {
|
|
6
|
+
return text
|
|
7
|
+
.toLowerCase()
|
|
8
|
+
.replace(SLUG_STRIP, '')
|
|
9
|
+
.replace(/ /g, '-');
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Slugs one document's headings with GitHub's duplicate rule: the first
|
|
13
|
+
* "Foo" stays "foo", later ones become "foo-1", "foo-2", ...
|
|
14
|
+
*/
|
|
15
|
+
export function createSlugger() {
|
|
16
|
+
const seen = new Map();
|
|
17
|
+
return (text) => {
|
|
18
|
+
const base = githubSlug(text);
|
|
19
|
+
const count = seen.get(base) ?? 0;
|
|
20
|
+
seen.set(base, count + 1);
|
|
21
|
+
return count === 0 ? base : `${base}-${count}`;
|
|
22
|
+
};
|
|
23
|
+
}
|