drone-path-core 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 +21 -0
- package/dist/cammeta-decode.d.ts +57 -0
- package/dist/cammeta-decode.js +237 -0
- package/dist/cammeta.d.ts +5 -0
- package/dist/cammeta.js +92 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +34 -0
- package/dist/exec-tool.d.ts +7 -0
- package/dist/exec-tool.js +14 -0
- package/dist/ffmpeg-tools.d.ts +14 -0
- package/dist/ffmpeg-tools.js +56 -0
- package/dist/ffmpeg.d.ts +21 -0
- package/dist/ffmpeg.js +58 -0
- package/dist/frame-range.d.ts +6 -0
- package/dist/frame-range.js +30 -0
- package/dist/frametime.d.ts +14 -0
- package/dist/frametime.js +32 -0
- package/dist/geo.d.ts +21 -0
- package/dist/geo.js +60 -0
- package/dist/protobuf-wire.d.ts +16 -0
- package/dist/protobuf-wire.js +102 -0
- package/dist/srt-sidecar.d.ts +1 -0
- package/dist/srt-sidecar.js +13 -0
- package/dist/srt.d.ts +19 -0
- package/dist/srt.js +98 -0
- package/dist/telemetry-json.d.ts +102 -0
- package/dist/telemetry-json.js +126 -0
- package/dist/telemetry-math.d.ts +47 -0
- package/dist/telemetry-math.js +262 -0
- package/dist/telemetry-summary.d.ts +6 -0
- package/dist/telemetry-summary.js +19 -0
- package/dist/telemetry.d.ts +18 -0
- package/dist/telemetry.js +95 -0
- package/dist/track-format.d.ts +4 -0
- package/dist/track-format.js +13 -0
- package/dist/track-geojson.d.ts +2 -0
- package/dist/track-geojson.js +44 -0
- package/dist/track-gpx.d.ts +2 -0
- package/dist/track-gpx.js +34 -0
- package/dist/track-kml.d.ts +2 -0
- package/dist/track-kml.js +47 -0
- package/dist/track-kmz.d.ts +1 -0
- package/dist/track-kmz.js +68 -0
- package/dist/track-write.d.ts +11 -0
- package/dist/track-write.js +41 -0
- package/dist/track.d.ts +35 -0
- package/dist/track.js +79 -0
- package/dist/validation.d.ts +9 -0
- package/dist/validation.js +22 -0
- package/package.json +37 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { resolveFrameRange } from './frame-range.js';
|
|
5
|
+
export const TELEMETRY_JSON_SCHEMA_VERSION = 1;
|
|
6
|
+
const GimbalDeviceSchema = z.strictObject({
|
|
7
|
+
id: z.int(),
|
|
8
|
+
type: z.int(),
|
|
9
|
+
subType: z.int(),
|
|
10
|
+
name: z.string(),
|
|
11
|
+
frequency: z.number(),
|
|
12
|
+
});
|
|
13
|
+
const TelemetryRecordSchema = z.strictObject({
|
|
14
|
+
frame: z.int(),
|
|
15
|
+
gimbalDeviceTimestampMs: z.int().optional(),
|
|
16
|
+
lat: z.number().optional(),
|
|
17
|
+
lng: z.number().optional(),
|
|
18
|
+
altAbs: z.number().optional(),
|
|
19
|
+
altRel: z.number().optional(),
|
|
20
|
+
gpsAltMm: z.int().optional(),
|
|
21
|
+
pitchDeg: z.number().optional(),
|
|
22
|
+
rollDeg: z.number().optional(),
|
|
23
|
+
yawDeg: z.number().optional(),
|
|
24
|
+
gimbalPitchDeg: z.number().optional(),
|
|
25
|
+
gimbalRollDeg: z.number().optional(),
|
|
26
|
+
gimbalYawDeg: z.number().optional(),
|
|
27
|
+
headingDeg: z.number(),
|
|
28
|
+
headingSource: z.enum(['yaw', 'cog']),
|
|
29
|
+
iso: z.int().optional(),
|
|
30
|
+
shutterTime: z.number().optional(),
|
|
31
|
+
whiteBalanceCctK: z.int().optional(),
|
|
32
|
+
fNumber: z.number().optional(),
|
|
33
|
+
});
|
|
34
|
+
export const TelemetryDocumentSchema = z.strictObject({
|
|
35
|
+
schemaVersion: z.literal(TELEMETRY_JSON_SCHEMA_VERSION),
|
|
36
|
+
protoFile: z.string().optional(),
|
|
37
|
+
source: z.enum(['srt', 'cammeta']),
|
|
38
|
+
headingMode: z.enum(['yaw', 'cog']),
|
|
39
|
+
frameCount: z.int(),
|
|
40
|
+
gimbalDevice: GimbalDeviceSchema.optional(),
|
|
41
|
+
frames: z.array(TelemetryRecordSchema),
|
|
42
|
+
});
|
|
43
|
+
// The Record annotation makes a schema field without an entry here a compile
|
|
44
|
+
// error instead of a silently dropped record key.
|
|
45
|
+
const TELEMETRY_VALUES = {
|
|
46
|
+
gimbalDeviceTimestampMs: { position: 'before-heading', integer: true },
|
|
47
|
+
lat: { position: 'before-heading' },
|
|
48
|
+
lng: { position: 'before-heading' },
|
|
49
|
+
altAbs: { position: 'before-heading' },
|
|
50
|
+
altRel: { position: 'before-heading' },
|
|
51
|
+
gpsAltMm: { position: 'before-heading', integer: true },
|
|
52
|
+
pitchDeg: { position: 'before-heading' },
|
|
53
|
+
rollDeg: { position: 'before-heading' },
|
|
54
|
+
yawDeg: { position: 'before-heading' },
|
|
55
|
+
gimbalPitchDeg: { position: 'before-heading' },
|
|
56
|
+
gimbalRollDeg: { position: 'before-heading' },
|
|
57
|
+
gimbalYawDeg: { position: 'before-heading' },
|
|
58
|
+
iso: { position: 'exposure', integer: true },
|
|
59
|
+
shutterTime: { position: 'exposure' },
|
|
60
|
+
whiteBalanceCctK: { position: 'exposure', integer: true },
|
|
61
|
+
fNumber: { position: 'exposure' },
|
|
62
|
+
};
|
|
63
|
+
function valueKeys(position) {
|
|
64
|
+
return Object.keys(TELEMETRY_VALUES).filter((key) => TELEMETRY_VALUES[key].position === position);
|
|
65
|
+
}
|
|
66
|
+
const BEFORE_HEADING_KEYS = valueKeys('before-heading');
|
|
67
|
+
const EXPOSURE_KEYS = valueKeys('exposure');
|
|
68
|
+
function copyValue(record, key, value) {
|
|
69
|
+
if (value === null || value === undefined)
|
|
70
|
+
return;
|
|
71
|
+
record[key] = TELEMETRY_VALUES[key].integer ? Math.round(value) : value;
|
|
72
|
+
}
|
|
73
|
+
function buildRecord(input, index) {
|
|
74
|
+
const row = input.frames[index] ?? {};
|
|
75
|
+
const record = {
|
|
76
|
+
frame: (input.frameStart ?? 0) + index,
|
|
77
|
+
};
|
|
78
|
+
for (const key of BEFORE_HEADING_KEYS)
|
|
79
|
+
copyValue(record, key, row[key]);
|
|
80
|
+
record.headingDeg = input.headingDeg[index];
|
|
81
|
+
record.headingSource = input.headingMode;
|
|
82
|
+
for (const key of EXPOSURE_KEYS)
|
|
83
|
+
copyValue(record, key, row[key]);
|
|
84
|
+
return record;
|
|
85
|
+
}
|
|
86
|
+
export function segmentTelemetryInput(input, range) {
|
|
87
|
+
const segment = resolveFrameRange(range, input.frameCount);
|
|
88
|
+
if (!segment)
|
|
89
|
+
return input;
|
|
90
|
+
return {
|
|
91
|
+
...input,
|
|
92
|
+
frameCount: segment.end - segment.start + 1,
|
|
93
|
+
frames: input.frames.slice(segment.start, segment.end + 1),
|
|
94
|
+
headingDeg: input.headingDeg.slice(segment.start, segment.end + 1),
|
|
95
|
+
frameStart: segment.start,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export function buildTelemetryDocument(input) {
|
|
99
|
+
if (input.headingDeg.length !== input.frameCount) {
|
|
100
|
+
throw new Error('telemetry JSON: heading count does not match the frame count');
|
|
101
|
+
}
|
|
102
|
+
const frames = [];
|
|
103
|
+
for (let index = 0; index < input.frameCount; index++) {
|
|
104
|
+
frames.push(buildRecord(input, index));
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
schemaVersion: TELEMETRY_JSON_SCHEMA_VERSION,
|
|
108
|
+
...(input.protoFile ? { protoFile: input.protoFile } : {}),
|
|
109
|
+
source: input.source,
|
|
110
|
+
headingMode: input.headingMode,
|
|
111
|
+
frameCount: input.frameCount,
|
|
112
|
+
...(input.gimbalDevice ? { gimbalDevice: input.gimbalDevice } : {}),
|
|
113
|
+
frames,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
export function serializeTelemetryDocument(doc) {
|
|
117
|
+
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
118
|
+
}
|
|
119
|
+
export function writeTelemetryDocument(file, input) {
|
|
120
|
+
const doc = buildTelemetryDocument(input);
|
|
121
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
122
|
+
fs.writeFileSync(file, serializeTelemetryDocument(doc));
|
|
123
|
+
}
|
|
124
|
+
export function generateTelemetryJsonSchema() {
|
|
125
|
+
return `${JSON.stringify(z.toJSONSchema(TelemetryDocumentSchema), null, 2)}\n`;
|
|
126
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Bbox, LatLon } from './geo.js';
|
|
2
|
+
import type { CamMetaMethod } from './cammeta-decode.js';
|
|
3
|
+
export type TelemetrySource = 'srt' | 'cammeta';
|
|
4
|
+
type TelemetryMethod = 'srt-cues' | CamMetaMethod;
|
|
5
|
+
export type HeadingMode = 'yaw' | 'cog';
|
|
6
|
+
export interface FrameChannels {
|
|
7
|
+
lat: number[];
|
|
8
|
+
lon: number[];
|
|
9
|
+
alt: (number | null)[];
|
|
10
|
+
speed: (number | null)[];
|
|
11
|
+
headingDeg: number[];
|
|
12
|
+
}
|
|
13
|
+
interface FrameData {
|
|
14
|
+
fix: LatLon | null;
|
|
15
|
+
alt: number | null;
|
|
16
|
+
speed: number | null;
|
|
17
|
+
headingDeg: number;
|
|
18
|
+
}
|
|
19
|
+
export interface Telemetry {
|
|
20
|
+
source: TelemetrySource;
|
|
21
|
+
method: TelemetryMethod;
|
|
22
|
+
headingMode: HeadingMode;
|
|
23
|
+
sampleCount: number;
|
|
24
|
+
fixCount: number;
|
|
25
|
+
noLock: number;
|
|
26
|
+
frames: FrameData[];
|
|
27
|
+
bbox: Bbox;
|
|
28
|
+
trackDistanceM: number;
|
|
29
|
+
}
|
|
30
|
+
export interface TelemetrySample {
|
|
31
|
+
tSec: number;
|
|
32
|
+
fix: LatLon | null;
|
|
33
|
+
relAlt: number | null;
|
|
34
|
+
absAlt: number | null;
|
|
35
|
+
yawDeg: number | null;
|
|
36
|
+
}
|
|
37
|
+
export interface TelemetrySamples {
|
|
38
|
+
samples: TelemetrySample[];
|
|
39
|
+
source: TelemetrySource;
|
|
40
|
+
method: TelemetryMethod;
|
|
41
|
+
noLock: number;
|
|
42
|
+
}
|
|
43
|
+
export declare function computeTelemetry(loaded: TelemetrySamples, timing: {
|
|
44
|
+
frames: number;
|
|
45
|
+
fpsValue: number;
|
|
46
|
+
}): Telemetry;
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { bearingDeg, circularMeanDeg, haversineM } from './geo.js';
|
|
2
|
+
import { timeAt } from './frametime.js';
|
|
3
|
+
const MAX_INTERPOLATION_GAP_S = 3;
|
|
4
|
+
const SPEED_SMOOTHING_SPAN = 5;
|
|
5
|
+
const HEADING_MIN_MOVE_M = 1.5;
|
|
6
|
+
const HEADING_HALF_WINDOW_S = 0.35;
|
|
7
|
+
const HEADING_MAX_BASELINE_S = 3;
|
|
8
|
+
const HEADING_MIN_SPEED_MPS = 0.5;
|
|
9
|
+
const HEADING_SMOOTHING_SPAN = 4;
|
|
10
|
+
function isFixSample(sample) {
|
|
11
|
+
return sample.fix !== null;
|
|
12
|
+
}
|
|
13
|
+
function altitudeOf(sample) {
|
|
14
|
+
return sample.relAlt ?? sample.absAlt ?? null;
|
|
15
|
+
}
|
|
16
|
+
function positionOfSample(sample) {
|
|
17
|
+
return { fix: sample.fix, alt: altitudeOf(sample) };
|
|
18
|
+
}
|
|
19
|
+
function interpolateAltitude(a, b, fraction) {
|
|
20
|
+
const altA = altitudeOf(a);
|
|
21
|
+
const altB = altitudeOf(b);
|
|
22
|
+
if (altA === null)
|
|
23
|
+
return altB;
|
|
24
|
+
if (altB === null)
|
|
25
|
+
return altA;
|
|
26
|
+
return altA + (altB - altA) * fraction;
|
|
27
|
+
}
|
|
28
|
+
function nearestFix(samples, idx, direction) {
|
|
29
|
+
let i = idx;
|
|
30
|
+
while (i >= 0 && i < samples.length) {
|
|
31
|
+
const sample = samples[i];
|
|
32
|
+
if (isFixSample(sample))
|
|
33
|
+
return sample;
|
|
34
|
+
i += direction;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
function positionAt(samples, t, maxGapSec = MAX_INTERPOLATION_GAP_S) {
|
|
39
|
+
const n = samples.length;
|
|
40
|
+
if (n === 0)
|
|
41
|
+
return { fix: null, alt: null };
|
|
42
|
+
if (t <= samples[0].tSec)
|
|
43
|
+
return positionOfSample(samples[0]);
|
|
44
|
+
if (t >= samples[n - 1].tSec)
|
|
45
|
+
return positionOfSample(samples[n - 1]);
|
|
46
|
+
const lo = sampleIndexBefore(samples, t);
|
|
47
|
+
const hi = lo + 1;
|
|
48
|
+
const a = nearestFix(samples, lo, -1);
|
|
49
|
+
const b = nearestFix(samples, hi, 1);
|
|
50
|
+
if (a !== null && b === null)
|
|
51
|
+
return positionOfSample(a);
|
|
52
|
+
if (a === null && b !== null)
|
|
53
|
+
return positionOfSample(b);
|
|
54
|
+
if (a === null || b === null)
|
|
55
|
+
return { fix: null, alt: null };
|
|
56
|
+
const dt = b.tSec - a.tSec;
|
|
57
|
+
const gapOk = dt > 0 && dt <= maxGapSec;
|
|
58
|
+
const fraction = gapOk ? (t - a.tSec) / dt : 0;
|
|
59
|
+
return {
|
|
60
|
+
fix: {
|
|
61
|
+
lat: a.fix.lat + (b.fix.lat - a.fix.lat) * fraction,
|
|
62
|
+
lon: a.fix.lon + (b.fix.lon - a.fix.lon) * fraction,
|
|
63
|
+
},
|
|
64
|
+
alt: interpolateAltitude(a, b, fraction),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function sampleIndexBefore(samples, t) {
|
|
68
|
+
let lo = 0;
|
|
69
|
+
let hi = samples.length - 1;
|
|
70
|
+
while (hi - lo > 1) {
|
|
71
|
+
const mid = (lo + hi) >> 1;
|
|
72
|
+
if (samples[mid].tSec <= t)
|
|
73
|
+
lo = mid;
|
|
74
|
+
else
|
|
75
|
+
hi = mid;
|
|
76
|
+
}
|
|
77
|
+
return lo;
|
|
78
|
+
}
|
|
79
|
+
function nearestSampleIndex(samples, t) {
|
|
80
|
+
const lo = sampleIndexBefore(samples, t);
|
|
81
|
+
const hi = Math.min(lo + 1, samples.length - 1);
|
|
82
|
+
return t - samples[lo].tSec <= samples[hi].tSec - t ? lo : hi;
|
|
83
|
+
}
|
|
84
|
+
function computeTrackStats(fixes) {
|
|
85
|
+
const bbox = {
|
|
86
|
+
minLat: Infinity,
|
|
87
|
+
maxLat: -Infinity,
|
|
88
|
+
minLon: Infinity,
|
|
89
|
+
maxLon: -Infinity,
|
|
90
|
+
};
|
|
91
|
+
let trackDistanceM = 0;
|
|
92
|
+
let prev = null;
|
|
93
|
+
for (const { fix } of fixes) {
|
|
94
|
+
bbox.minLat = Math.min(bbox.minLat, fix.lat);
|
|
95
|
+
bbox.maxLat = Math.max(bbox.maxLat, fix.lat);
|
|
96
|
+
bbox.minLon = Math.min(bbox.minLon, fix.lon);
|
|
97
|
+
bbox.maxLon = Math.max(bbox.maxLon, fix.lon);
|
|
98
|
+
if (prev !== null) {
|
|
99
|
+
trackDistanceM += haversineM(prev, fix);
|
|
100
|
+
}
|
|
101
|
+
prev = fix;
|
|
102
|
+
}
|
|
103
|
+
return { bbox, trackDistanceM };
|
|
104
|
+
}
|
|
105
|
+
function computeSpeedByFix(fixes) {
|
|
106
|
+
if (fixes.length === 2) {
|
|
107
|
+
const dt = fixes[1].tSec - fixes[0].tSec;
|
|
108
|
+
const speed = dt > 0.001 ? haversineM(fixes[0].fix, fixes[1].fix) / dt : 0;
|
|
109
|
+
return [speed, speed];
|
|
110
|
+
}
|
|
111
|
+
const rawSpeed = new Array(fixes.length).fill(null);
|
|
112
|
+
for (let k = 1; k < fixes.length - 1; k++) {
|
|
113
|
+
const dt = fixes[k + 1].tSec - fixes[k - 1].tSec;
|
|
114
|
+
if (dt > 0.001) {
|
|
115
|
+
rawSpeed[k] = haversineM(fixes[k - 1].fix, fixes[k + 1].fix) / dt;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
rawSpeed[0] = rawSpeed[1] ?? 0;
|
|
119
|
+
rawSpeed[fixes.length - 1] = rawSpeed[fixes.length - 2] ?? 0;
|
|
120
|
+
return fixes.map((_, k) => {
|
|
121
|
+
const lo = Math.max(0, k - SPEED_SMOOTHING_SPAN);
|
|
122
|
+
const hi = Math.min(fixes.length - 1, k + SPEED_SMOOTHING_SPAN);
|
|
123
|
+
let sum = 0;
|
|
124
|
+
let count = 0;
|
|
125
|
+
for (let j = lo; j <= hi; j++) {
|
|
126
|
+
const v = rawSpeed[j];
|
|
127
|
+
if (v !== null) {
|
|
128
|
+
sum += v;
|
|
129
|
+
count++;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return count ? sum / count : 0;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function computeHeadingByFix(fixes, speedByFix) {
|
|
136
|
+
const hasYaw = fixes.some((fix) => fix.yawDeg !== null);
|
|
137
|
+
const headingMode = hasYaw ? 'yaw' : 'cog';
|
|
138
|
+
const rawHeading = new Array(fixes.length).fill(null);
|
|
139
|
+
if (hasYaw) {
|
|
140
|
+
fillYawHeadings(fixes, rawHeading);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
fillCourseOverGroundHeadings(fixes, speedByFix, rawHeading);
|
|
144
|
+
}
|
|
145
|
+
fillHeadingGaps(rawHeading);
|
|
146
|
+
const defaultHeading = rawHeading[0] ?? firstKnownHeading(rawHeading) ?? 0;
|
|
147
|
+
const headingByFix = fixes.map((_, k) => {
|
|
148
|
+
const lo = Math.max(0, k - HEADING_SMOOTHING_SPAN);
|
|
149
|
+
const hi = Math.min(fixes.length - 1, k + HEADING_SMOOTHING_SPAN);
|
|
150
|
+
const window = [];
|
|
151
|
+
for (let j = lo; j <= hi; j++) {
|
|
152
|
+
window.push(rawHeading[j] ?? defaultHeading);
|
|
153
|
+
}
|
|
154
|
+
return circularMeanDeg(window);
|
|
155
|
+
});
|
|
156
|
+
return { headingByFix, headingMode };
|
|
157
|
+
}
|
|
158
|
+
function fillYawHeadings(fixes, rawHeading) {
|
|
159
|
+
for (let k = 0; k < fixes.length; k++) {
|
|
160
|
+
const yaw = fixes[k].yawDeg;
|
|
161
|
+
if (yaw !== null && Number.isFinite(yaw))
|
|
162
|
+
rawHeading[k] = ((yaw % 360) + 360) % 360;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function fillCourseOverGroundHeadings(fixes, speedByFix, rawHeading) {
|
|
166
|
+
for (let k = 0; k < fixes.length; k++) {
|
|
167
|
+
const tk = fixes[k].tSec;
|
|
168
|
+
if ((speedByFix[k] ?? 0) < HEADING_MIN_SPEED_MPS)
|
|
169
|
+
continue;
|
|
170
|
+
let ka = k;
|
|
171
|
+
let kb = k;
|
|
172
|
+
let displacement = 0;
|
|
173
|
+
for (const half of [HEADING_HALF_WINDOW_S, HEADING_MAX_BASELINE_S]) {
|
|
174
|
+
ka = k;
|
|
175
|
+
kb = k;
|
|
176
|
+
while (ka > 0 && fixes[ka].tSec > tk - half)
|
|
177
|
+
ka--;
|
|
178
|
+
while (kb < fixes.length - 1 && fixes[kb].tSec < tk + half)
|
|
179
|
+
kb++;
|
|
180
|
+
displacement = haversineM(fixes[ka].fix, fixes[kb].fix);
|
|
181
|
+
if (displacement >= HEADING_MIN_MOVE_M)
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
if (displacement >= HEADING_MIN_MOVE_M) {
|
|
185
|
+
rawHeading[k] = bearingDeg(fixes[ka].fix, fixes[kb].fix);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function fillHeadingGaps(rawHeading) {
|
|
190
|
+
let last = null;
|
|
191
|
+
for (let k = 0; k < rawHeading.length; k++) {
|
|
192
|
+
if (rawHeading[k] !== null)
|
|
193
|
+
last = rawHeading[k];
|
|
194
|
+
else
|
|
195
|
+
rawHeading[k] = last;
|
|
196
|
+
}
|
|
197
|
+
const fallback = firstKnownHeading(rawHeading) ?? 0;
|
|
198
|
+
for (let k = 0; k < rawHeading.length; k++) {
|
|
199
|
+
rawHeading[k] ??= fallback;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function firstKnownHeading(rawHeading) {
|
|
203
|
+
for (const heading of rawHeading) {
|
|
204
|
+
if (heading !== null)
|
|
205
|
+
return heading;
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
function buildFrameData(samples, fixes, frames, fpsValue, speedByFix, headingByFix) {
|
|
210
|
+
const frameData = [];
|
|
211
|
+
for (let frame = 0; frame < frames; frame++) {
|
|
212
|
+
const t = timeAt(frame, fpsValue);
|
|
213
|
+
const pos = positionAt(samples, t);
|
|
214
|
+
// Look up the nearest valid fix so mid-flight no-lock cues do not zero speed or reset heading.
|
|
215
|
+
const fi = nearestSampleIndex(fixes, t);
|
|
216
|
+
frameData.push({
|
|
217
|
+
fix: pos.fix,
|
|
218
|
+
alt: pos.alt,
|
|
219
|
+
speed: Math.round(speedByFix[fi] * 10) / 10,
|
|
220
|
+
headingDeg: Math.round(headingByFix[fi] * 10) / 10,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return frameData;
|
|
224
|
+
}
|
|
225
|
+
function bridgeMissingFramePositions(frameData) {
|
|
226
|
+
const first = frameData.find((d) => d.fix !== null);
|
|
227
|
+
const last = [...frameData].reverse().find((d) => d.fix !== null);
|
|
228
|
+
if (!first || !last)
|
|
229
|
+
return;
|
|
230
|
+
let lead = 0;
|
|
231
|
+
while (lead < frameData.length && frameData[lead].fix === null) {
|
|
232
|
+
frameData[lead].fix = first.fix;
|
|
233
|
+
lead++;
|
|
234
|
+
}
|
|
235
|
+
let trail = frameData.length - 1;
|
|
236
|
+
while (trail >= lead && frameData[trail].fix === null) {
|
|
237
|
+
frameData[trail].fix = last.fix;
|
|
238
|
+
trail--;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
export function computeTelemetry(loaded, timing) {
|
|
242
|
+
const { samples, source, method, noLock } = loaded;
|
|
243
|
+
const fixes = samples.filter(isFixSample);
|
|
244
|
+
if (fixes.length < 2)
|
|
245
|
+
throw new Error('telemetry has fewer than 2 GPS fixes');
|
|
246
|
+
const { bbox, trackDistanceM } = computeTrackStats(fixes);
|
|
247
|
+
const speedByFix = computeSpeedByFix(fixes);
|
|
248
|
+
const { headingByFix, headingMode } = computeHeadingByFix(fixes, speedByFix);
|
|
249
|
+
const frameData = buildFrameData(samples, fixes, timing.frames, timing.fpsValue, speedByFix, headingByFix);
|
|
250
|
+
bridgeMissingFramePositions(frameData);
|
|
251
|
+
return {
|
|
252
|
+
source,
|
|
253
|
+
method,
|
|
254
|
+
headingMode,
|
|
255
|
+
sampleCount: samples.length,
|
|
256
|
+
fixCount: fixes.length,
|
|
257
|
+
noLock,
|
|
258
|
+
frames: frameData,
|
|
259
|
+
bbox,
|
|
260
|
+
trackDistanceM,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Telemetry } from './telemetry-math.js';
|
|
2
|
+
export type TelemetrySummary = Pick<Telemetry, 'source' | 'method' | 'headingMode' | 'fixCount' | 'noLock' | 'trackDistanceM'>;
|
|
3
|
+
export declare function telemetrySummary(tel: Telemetry): TelemetrySummary;
|
|
4
|
+
export declare function fixCountLabel(fixCount: number): string;
|
|
5
|
+
export declare function noLockCountLabel(noLock: number): string;
|
|
6
|
+
export declare function trackDistanceLabel(trackDistanceM: number): string;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function telemetrySummary(tel) {
|
|
2
|
+
return {
|
|
3
|
+
source: tel.source,
|
|
4
|
+
method: tel.method,
|
|
5
|
+
headingMode: tel.headingMode,
|
|
6
|
+
fixCount: tel.fixCount,
|
|
7
|
+
noLock: tel.noLock,
|
|
8
|
+
trackDistanceM: Math.round(tel.trackDistanceM),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export function fixCountLabel(fixCount) {
|
|
12
|
+
return `${fixCount} fixes`;
|
|
13
|
+
}
|
|
14
|
+
export function noLockCountLabel(noLock) {
|
|
15
|
+
return `${noLock} no-lock cues`;
|
|
16
|
+
}
|
|
17
|
+
export function trackDistanceLabel(trackDistanceM) {
|
|
18
|
+
return `${trackDistanceM} m`;
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { GimbalDeviceConstants } from './cammeta-decode.js';
|
|
2
|
+
import type { FfmpegToolPaths } from './ffmpeg-tools.js';
|
|
3
|
+
import type { Telemetry } from './telemetry-math.js';
|
|
4
|
+
import type { TelemetryFrameInput } from './telemetry-json.js';
|
|
5
|
+
import type { TrackRecord } from './track.js';
|
|
6
|
+
export interface RecordingTelemetry {
|
|
7
|
+
telemetry: Telemetry;
|
|
8
|
+
frameValues: TelemetryFrameInput[];
|
|
9
|
+
trackValues: TrackRecord[];
|
|
10
|
+
protoFile: string | null;
|
|
11
|
+
gimbalDevice: GimbalDeviceConstants | null;
|
|
12
|
+
}
|
|
13
|
+
export declare function loadTelemetry(videoPath: string, { frames, fpsValue, durationSec, tools, }: {
|
|
14
|
+
frames: number;
|
|
15
|
+
fpsValue: number;
|
|
16
|
+
durationSec: number;
|
|
17
|
+
tools: FfmpegToolPaths;
|
|
18
|
+
}): Promise<RecordingTelemetry>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { ERR_NO_TELEMETRY } from './errors.js';
|
|
2
|
+
import { parseSrt } from './srt.js';
|
|
3
|
+
import { findSrtSidecar } from './srt-sidecar.js';
|
|
4
|
+
import { parseCamMeta } from './cammeta.js';
|
|
5
|
+
import { computeTelemetry } from './telemetry-math.js';
|
|
6
|
+
function srtCueToFrameValues(cue) {
|
|
7
|
+
return {
|
|
8
|
+
lat: cue.fix?.lat ?? null,
|
|
9
|
+
lng: cue.fix?.lon ?? null,
|
|
10
|
+
altAbs: cue.absAlt,
|
|
11
|
+
altRel: cue.relAlt,
|
|
12
|
+
iso: cue.iso,
|
|
13
|
+
shutterTime: cue.shutterTime,
|
|
14
|
+
whiteBalanceCctK: cue.whiteBalanceCctK,
|
|
15
|
+
fNumber: cue.fNumber,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function camSampleToFrameValues(sample) {
|
|
19
|
+
return {
|
|
20
|
+
gimbalDeviceTimestampMs: sample.gimbalDeviceTimestampMs,
|
|
21
|
+
lat: sample.fix?.lat ?? null,
|
|
22
|
+
lng: sample.fix?.lon ?? null,
|
|
23
|
+
altAbs: sample.absAlt,
|
|
24
|
+
altRel: sample.relAlt,
|
|
25
|
+
gpsAltMm: sample.gpsAltMm,
|
|
26
|
+
pitchDeg: sample.pitchDeg,
|
|
27
|
+
rollDeg: sample.rollDeg,
|
|
28
|
+
yawDeg: sample.yawDeg,
|
|
29
|
+
gimbalPitchDeg: sample.gimbalPitchDeg,
|
|
30
|
+
gimbalRollDeg: sample.gimbalRollDeg,
|
|
31
|
+
gimbalYawDeg: sample.gimbalYawDeg,
|
|
32
|
+
iso: sample.iso,
|
|
33
|
+
whiteBalanceCctK: sample.whiteBalanceCctK,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async function loadSamples(tools, videoPath, durationSec) {
|
|
37
|
+
const cam = await parseCamMeta(tools, videoPath, durationSec, { keepAll: true });
|
|
38
|
+
if (cam) {
|
|
39
|
+
const fixSamples = cam.samples.filter((sample) => sample.fix !== null);
|
|
40
|
+
if (fixSamples.length === 0) {
|
|
41
|
+
throw new Error(`${ERR_NO_TELEMETRY}the CAM meta (DVTM) stream has no GPS fix pairs; ` +
|
|
42
|
+
'the .SRT sidecar is only consulted when no CAM meta stream is present');
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
samples: fixSamples,
|
|
46
|
+
source: 'cammeta',
|
|
47
|
+
method: cam.method,
|
|
48
|
+
noLock: cam.noLock,
|
|
49
|
+
frameValues: cam.method === 'dvtm-packets' ? cam.samples.map(camSampleToFrameValues) : [],
|
|
50
|
+
trackValues: cam.samples.map((sample) => ({
|
|
51
|
+
...camSampleToFrameValues(sample),
|
|
52
|
+
tSec: sample.tSec,
|
|
53
|
+
})),
|
|
54
|
+
protoFile: cam.protoFile,
|
|
55
|
+
gimbalDevice: cam.gimbalDevice,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const srtPath = findSrtSidecar(videoPath);
|
|
59
|
+
if (srtPath) {
|
|
60
|
+
const parsed = await parseSrt(srtPath);
|
|
61
|
+
if (parsed.count === 0)
|
|
62
|
+
throw new Error(`SRT sidecar has no cues: ${srtPath}`);
|
|
63
|
+
return {
|
|
64
|
+
samples: parsed.cues.map((cue) => ({
|
|
65
|
+
tSec: cue.tSec,
|
|
66
|
+
fix: cue.fix,
|
|
67
|
+
relAlt: cue.relAlt,
|
|
68
|
+
absAlt: cue.absAlt,
|
|
69
|
+
yawDeg: null,
|
|
70
|
+
})),
|
|
71
|
+
source: 'srt',
|
|
72
|
+
method: 'srt-cues',
|
|
73
|
+
noLock: parsed.noLock,
|
|
74
|
+
frameValues: parsed.cues.map(srtCueToFrameValues),
|
|
75
|
+
trackValues: parsed.cues.map((cue) => ({
|
|
76
|
+
...srtCueToFrameValues(cue),
|
|
77
|
+
tSec: cue.tSec,
|
|
78
|
+
})),
|
|
79
|
+
protoFile: null,
|
|
80
|
+
gimbalDevice: null,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
throw new Error(`${ERR_NO_TELEMETRY}no CAM meta (DVTM) stream and no .SRT sidecar`);
|
|
84
|
+
}
|
|
85
|
+
export async function loadTelemetry(videoPath, { frames, fpsValue, durationSec, tools, }) {
|
|
86
|
+
const loaded = await loadSamples(tools, videoPath, durationSec);
|
|
87
|
+
const { frameValues, trackValues, protoFile, gimbalDevice, ...samples } = loaded;
|
|
88
|
+
return {
|
|
89
|
+
telemetry: computeTelemetry(samples, { frames, fpsValue }),
|
|
90
|
+
frameValues,
|
|
91
|
+
trackValues,
|
|
92
|
+
protoFile,
|
|
93
|
+
gimbalDevice,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// The --track-out vocabulary: the file extension alone selects the track
|
|
2
|
+
// format, shared by the CLI writer and the desktop save request. The one
|
|
3
|
+
// values list is both the vocabulary and the extension set.
|
|
4
|
+
export const TRACK_FORMAT_VALUES = ['gpx', 'geojson', 'kml', 'kmz', 'json'];
|
|
5
|
+
export function trackFormatOf(file) {
|
|
6
|
+
const match = /\.([^.\\/]+)$/.exec(file);
|
|
7
|
+
const ext = match ? match[1].toLowerCase() : '';
|
|
8
|
+
const format = TRACK_FORMAT_VALUES.find((candidate) => candidate === ext);
|
|
9
|
+
if (!format) {
|
|
10
|
+
throw new Error(`unsupported track file extension: ${file} (use .gpx, .geojson, .kml, .kmz or .json)`);
|
|
11
|
+
}
|
|
12
|
+
return format;
|
|
13
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Pure GeoJSON serializer (RFC 7946): one MultiLineString line per track
|
|
2
|
+
// segment, [lon, lat] or [lon, lat, altM] positions, and the QGIS
|
|
3
|
+
// coordinateProperties.times convention only where every vertex carries
|
|
4
|
+
// trustworthy UTC. Assembled with fixed formatting and key order, so identical
|
|
5
|
+
// input yields identical bytes.
|
|
6
|
+
import { isoUtc } from './track.js';
|
|
7
|
+
function position(vertex) {
|
|
8
|
+
return vertex.altM !== null
|
|
9
|
+
? `[${vertex.lon}, ${vertex.lat}, ${vertex.altM}]`
|
|
10
|
+
: `[${vertex.lon}, ${vertex.lat}]`;
|
|
11
|
+
}
|
|
12
|
+
function oneLineList(items) {
|
|
13
|
+
return items.length > 0 ? `[${items.join(', ')}]` : '[]';
|
|
14
|
+
}
|
|
15
|
+
function itemLines(indents, itemsBySegment) {
|
|
16
|
+
const lines = [];
|
|
17
|
+
itemsBySegment.forEach((items, index) => {
|
|
18
|
+
const last = index === itemsBySegment.length - 1;
|
|
19
|
+
lines.push(`${indents}${oneLineList(items)}${last ? '' : ','}`);
|
|
20
|
+
});
|
|
21
|
+
return lines;
|
|
22
|
+
}
|
|
23
|
+
export function serializeGeoJson(segments) {
|
|
24
|
+
const allTimed = segments.length > 0 &&
|
|
25
|
+
segments.every((segment) => segment.every((vertex) => vertex.timeMs !== null));
|
|
26
|
+
const lines = [
|
|
27
|
+
'{',
|
|
28
|
+
' "type": "Feature",',
|
|
29
|
+
' "geometry": {',
|
|
30
|
+
' "type": "MultiLineString",',
|
|
31
|
+
' "coordinates": [',
|
|
32
|
+
];
|
|
33
|
+
lines.push(...itemLines(' ', segments.map((s) => s.map(position))));
|
|
34
|
+
lines.push(' ]', ' },');
|
|
35
|
+
if (allTimed) {
|
|
36
|
+
lines.push(' "properties": {', ' "coordinateProperties": {', ' "times": [');
|
|
37
|
+
lines.push(...itemLines(' ', segments.map((segment) => segment.map((vertex) => JSON.stringify(isoUtc(vertex.timeMs))))));
|
|
38
|
+
lines.push(' ]', ' }', ' }', '}', '');
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
lines.push(' "properties": {}', '}', '');
|
|
42
|
+
}
|
|
43
|
+
return lines.join('\n');
|
|
44
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Pure GPX 1.1 serializer: one <trkseg> per track segment, altitude as <ele>,
|
|
2
|
+
// and a <time> only where the vertex carries trustworthy UTC. No metadata
|
|
3
|
+
// wall-clock or version strings, so identical input yields identical bytes.
|
|
4
|
+
import { isoUtc } from './track.js';
|
|
5
|
+
function trkptLines(vertex) {
|
|
6
|
+
const children = [];
|
|
7
|
+
if (vertex.altM !== null)
|
|
8
|
+
children.push(`<ele>${vertex.altM}</ele>`);
|
|
9
|
+
if (vertex.timeMs !== null)
|
|
10
|
+
children.push(`<time>${isoUtc(vertex.timeMs)}</time>`);
|
|
11
|
+
const open = `<trkpt lat="${vertex.lat}" lon="${vertex.lon}">`;
|
|
12
|
+
if (children.length === 0)
|
|
13
|
+
return [` <trkpt lat="${vertex.lat}" lon="${vertex.lon}"/>`];
|
|
14
|
+
return [
|
|
15
|
+
` ${open}`,
|
|
16
|
+
...children.map((child) => ` ${child}`),
|
|
17
|
+
' </trkpt>',
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
export function serializeGpx(segments) {
|
|
21
|
+
const lines = [
|
|
22
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
23
|
+
'<gpx version="1.1" creator="drone-path" xmlns="http://www.topografix.com/GPX/1/1">',
|
|
24
|
+
' <trk>',
|
|
25
|
+
];
|
|
26
|
+
for (const segment of segments) {
|
|
27
|
+
lines.push(' <trkseg>');
|
|
28
|
+
for (const vertex of segment)
|
|
29
|
+
lines.push(...trkptLines(vertex));
|
|
30
|
+
lines.push(' </trkseg>');
|
|
31
|
+
}
|
|
32
|
+
lines.push(' </trk>', '</gpx>', '');
|
|
33
|
+
return lines.join('\n');
|
|
34
|
+
}
|