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
package/dist/ffmpeg.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ERR_NO_FRAME_RATE } from './errors.js';
|
|
2
|
+
import { runTool } from './exec-tool.js';
|
|
3
|
+
import { framesForSeconds, parseFpsRate } from './frametime.js';
|
|
4
|
+
import { FFMPEG_TOOL_NAMES } from './ffmpeg-tools.js';
|
|
5
|
+
const INSTALL_HINT = 'point --ffmpeg/--ffprobe at your executables, or install ffmpeg (includes ' +
|
|
6
|
+
'ffprobe) and make sure both are on PATH, e.g. "winget install Gyan.FFmpeg" ' +
|
|
7
|
+
'(Windows), "brew install ffmpeg" (macOS) or "apt install ffmpeg" (Linux)';
|
|
8
|
+
const FFPROBE_MAX_BUFFER = 64 * 1024 * 1024;
|
|
9
|
+
const TOOL_VERSION_MAX_BUFFER = 1 << 20;
|
|
10
|
+
export async function requireFfmpegTools(tools) {
|
|
11
|
+
for (const tool of FFMPEG_TOOL_NAMES) {
|
|
12
|
+
const command = tools[tool];
|
|
13
|
+
try {
|
|
14
|
+
await runTool(command, ['-version'], { maxBuffer: TOOL_VERSION_MAX_BUFFER });
|
|
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 runTool(tools.ffprobe, ['-v', 'error', ...args, videoPath], { maxBuffer: FFPROBE_MAX_BUFFER });
|
|
23
|
+
return JSON.parse(stdout || '{}');
|
|
24
|
+
}
|
|
25
|
+
export function firstVideoStream(j) {
|
|
26
|
+
return (j['streams'] ?? [])[0] ?? {};
|
|
27
|
+
}
|
|
28
|
+
export async function probeVideo(tools, videoPath, log = () => { }) {
|
|
29
|
+
const j = await ffprobeJson(tools, videoPath, [
|
|
30
|
+
'-select_streams', 'v:0',
|
|
31
|
+
'-show_entries',
|
|
32
|
+
'stream=r_frame_rate,avg_frame_rate,nb_frames,width,height:format=duration',
|
|
33
|
+
'-of', 'json',
|
|
34
|
+
]);
|
|
35
|
+
const stream = firstVideoStream(j);
|
|
36
|
+
const format = j['format'] ?? {};
|
|
37
|
+
const fps = parseFpsRate(stream.r_frame_rate) ??
|
|
38
|
+
parseFpsRate(stream.avg_frame_rate);
|
|
39
|
+
if (!fps)
|
|
40
|
+
throw new Error(ERR_NO_FRAME_RATE);
|
|
41
|
+
const durationSec = Number.parseFloat(format.duration ?? '');
|
|
42
|
+
let frames = Number.parseInt(stream.nb_frames ?? '', 10);
|
|
43
|
+
if (!Number.isFinite(frames) || frames <= 0) {
|
|
44
|
+
frames = framesForSeconds(fps.value, durationSec);
|
|
45
|
+
log('warning: nb_frames missing, using duration x fps');
|
|
46
|
+
}
|
|
47
|
+
if (!Number.isFinite(frames) || frames <= 0) {
|
|
48
|
+
throw new Error(`could not determine the frame count of ${videoPath} ` +
|
|
49
|
+
'(nb_frames missing and duration unusable)');
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
fps,
|
|
53
|
+
frames,
|
|
54
|
+
durationSec,
|
|
55
|
+
width: stream.width ?? 0,
|
|
56
|
+
height: stream.height ?? 0,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { numberValue, parseValue, strictShape } from './validation.js';
|
|
2
|
+
function assertValidFrameRange(range, name = 'frame range') {
|
|
3
|
+
if (!Number.isInteger(range.start) || !Number.isInteger(range.end)) {
|
|
4
|
+
throw new Error(`invalid ${name}: start and end must be integer frame indices`);
|
|
5
|
+
}
|
|
6
|
+
// A single-frame span (start === end) is a valid export range.
|
|
7
|
+
if (range.start < 0 || range.start > range.end) {
|
|
8
|
+
throw new Error(`invalid ${name}: expected 0 <= start <= end, got ${range.start}-${range.end}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function parseFrameRange(raw, name = 'frame range') {
|
|
12
|
+
const range = parseValue(strictShape({
|
|
13
|
+
start: numberValue(`${name}: start`),
|
|
14
|
+
end: numberValue(`${name}: end`),
|
|
15
|
+
}, {
|
|
16
|
+
invalid: `invalid ${name}`,
|
|
17
|
+
unknownKey: (key) => `unknown ${name} key ${key}`,
|
|
18
|
+
}), raw);
|
|
19
|
+
assertValidFrameRange(range, name);
|
|
20
|
+
return range;
|
|
21
|
+
}
|
|
22
|
+
export function resolveFrameRange(range, frames) {
|
|
23
|
+
if (!range)
|
|
24
|
+
return null;
|
|
25
|
+
assertValidFrameRange(range);
|
|
26
|
+
if (range.end >= frames) {
|
|
27
|
+
throw new Error(`frame range ${range.start}-${range.end} is beyond the recording (${frames} frames)`);
|
|
28
|
+
}
|
|
29
|
+
return range;
|
|
30
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface Fps {
|
|
2
|
+
num: number;
|
|
3
|
+
den: number;
|
|
4
|
+
value: number;
|
|
5
|
+
str: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function parseRate(str: string): {
|
|
8
|
+
num: number;
|
|
9
|
+
den?: number;
|
|
10
|
+
} | null;
|
|
11
|
+
export declare function parseFpsRate(str?: string): Fps | null;
|
|
12
|
+
export declare function formatFps(rate: string): string;
|
|
13
|
+
export declare function timeAt(frame: number, fps: number): number;
|
|
14
|
+
export declare function framesForSeconds(fps: number, seconds: number): number;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function parseRate(str) {
|
|
2
|
+
const [num, den] = str.split('/').map(Number);
|
|
3
|
+
return Number.isFinite(num) ? { num, den } : null;
|
|
4
|
+
}
|
|
5
|
+
export function parseFpsRate(str) {
|
|
6
|
+
const rate = str ? parseRate(str) : null;
|
|
7
|
+
if (!rate || !Number.isFinite(rate.den) || rate.den === 0 || rate.num === 0) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const { num } = rate;
|
|
11
|
+
const den = rate.den;
|
|
12
|
+
return {
|
|
13
|
+
num,
|
|
14
|
+
den,
|
|
15
|
+
value: num / den,
|
|
16
|
+
str: den === 1 ? String(num) : `${num}/${den}`,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function formatFps(rate) {
|
|
20
|
+
const ratio = /^(\d+)\/(\d+)$/.exec(rate);
|
|
21
|
+
const num = ratio ? Number(ratio[1]) : Number(rate);
|
|
22
|
+
const den = ratio ? Number(ratio[2]) : 1;
|
|
23
|
+
if (!(num > 0) || !(den > 0))
|
|
24
|
+
return rate;
|
|
25
|
+
return `${Math.round((num / den) * 100) / 100} fps`;
|
|
26
|
+
}
|
|
27
|
+
export function timeAt(frame, fps) {
|
|
28
|
+
return fps > 0 ? frame / fps : 0;
|
|
29
|
+
}
|
|
30
|
+
export function framesForSeconds(fps, seconds) {
|
|
31
|
+
return Math.round(fps * seconds);
|
|
32
|
+
}
|
package/dist/geo.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface LatLon {
|
|
2
|
+
lat: number;
|
|
3
|
+
lon: number;
|
|
4
|
+
}
|
|
5
|
+
export interface Bbox {
|
|
6
|
+
minLat: number;
|
|
7
|
+
maxLat: number;
|
|
8
|
+
minLon: number;
|
|
9
|
+
maxLon: number;
|
|
10
|
+
}
|
|
11
|
+
export interface MercatorPixel {
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function isGpsFix(lat: number, lon: number): boolean;
|
|
16
|
+
export declare function gpsFixOrNull(lat: number | null, lon: number | null): LatLon | null;
|
|
17
|
+
export declare function haversineM(a: LatLon, b: LatLon): number;
|
|
18
|
+
export declare function bearingDeg(a: LatLon, b: LatLon): number;
|
|
19
|
+
export declare function circularMeanDeg(degList: number[]): number;
|
|
20
|
+
export declare function project(lon: number, lat: number, zoom: number): MercatorPixel;
|
|
21
|
+
export declare function unproject(x: number, y: number, zoom: number): LatLon;
|
package/dist/geo.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const EARTH_RADIUS_M = 6371008.8;
|
|
2
|
+
const DEG_TO_RAD = Math.PI / 180;
|
|
3
|
+
const RAD_TO_DEG = 180 / Math.PI;
|
|
4
|
+
// (0, 0) is DJI's "no GPS lock" placeholder before the first fix, not a real fix.
|
|
5
|
+
export function isGpsFix(lat, lon) {
|
|
6
|
+
return (Number.isFinite(lat) &&
|
|
7
|
+
Number.isFinite(lon) &&
|
|
8
|
+
lat > -90 &&
|
|
9
|
+
lat < 90 &&
|
|
10
|
+
lon > -180 &&
|
|
11
|
+
lon < 180 &&
|
|
12
|
+
!(lat === 0 && lon === 0));
|
|
13
|
+
}
|
|
14
|
+
export function gpsFixOrNull(lat, lon) {
|
|
15
|
+
return lat !== null && lon !== null && isGpsFix(lat, lon) ? { lat, lon } : null;
|
|
16
|
+
}
|
|
17
|
+
export function haversineM(a, b) {
|
|
18
|
+
const deltaLatRad = (b.lat - a.lat) * DEG_TO_RAD;
|
|
19
|
+
const deltaLonRad = (b.lon - a.lon) * DEG_TO_RAD;
|
|
20
|
+
const latARad = a.lat * DEG_TO_RAD;
|
|
21
|
+
const latBRad = b.lat * DEG_TO_RAD;
|
|
22
|
+
const haversine = Math.sin(deltaLatRad / 2) ** 2 +
|
|
23
|
+
Math.cos(latARad) * Math.cos(latBRad) * Math.sin(deltaLonRad / 2) ** 2;
|
|
24
|
+
return 2 * EARTH_RADIUS_M * Math.asin(Math.min(1, Math.sqrt(haversine)));
|
|
25
|
+
}
|
|
26
|
+
export function bearingDeg(a, b) {
|
|
27
|
+
const latARad = a.lat * DEG_TO_RAD;
|
|
28
|
+
const latBRad = b.lat * DEG_TO_RAD;
|
|
29
|
+
const deltaLonRad = (b.lon - a.lon) * DEG_TO_RAD;
|
|
30
|
+
const y = Math.sin(deltaLonRad) * Math.cos(latBRad);
|
|
31
|
+
const x = Math.cos(latARad) * Math.sin(latBRad) -
|
|
32
|
+
Math.sin(latARad) * Math.cos(latBRad) * Math.cos(deltaLonRad);
|
|
33
|
+
return (Math.atan2(y, x) * RAD_TO_DEG + 360) % 360;
|
|
34
|
+
}
|
|
35
|
+
export function circularMeanDeg(degList) {
|
|
36
|
+
let sumCos = 0;
|
|
37
|
+
let sumSin = 0;
|
|
38
|
+
for (const deg of degList) {
|
|
39
|
+
sumCos += Math.cos(deg * DEG_TO_RAD);
|
|
40
|
+
sumSin += Math.sin(deg * DEG_TO_RAD);
|
|
41
|
+
}
|
|
42
|
+
if (sumCos === 0 && sumSin === 0)
|
|
43
|
+
return degList[0] ?? 0;
|
|
44
|
+
return (Math.atan2(sumSin, sumCos) * RAD_TO_DEG + 360) % 360;
|
|
45
|
+
}
|
|
46
|
+
// Web Mercator (EPSG:3857) global pixels, 256 px tiles, NW origin (Leaflet convention).
|
|
47
|
+
export function project(lon, lat, zoom) {
|
|
48
|
+
const scale = 256 * 2 ** zoom;
|
|
49
|
+
const x = ((lon + 180) / 360) * scale;
|
|
50
|
+
const sinLat = Math.sin(Math.max(-85.05112878, Math.min(85.05112878, lat)) * DEG_TO_RAD);
|
|
51
|
+
const y = (0.5 - Math.log((1 + sinLat) / (1 - sinLat)) / (4 * Math.PI)) * scale;
|
|
52
|
+
return { x, y };
|
|
53
|
+
}
|
|
54
|
+
export function unproject(x, y, zoom) {
|
|
55
|
+
const scale = 256 * 2 ** zoom;
|
|
56
|
+
const lon = (x / scale) * 360 - 180;
|
|
57
|
+
const mercatorN = Math.PI - 2 * Math.PI * (y / scale);
|
|
58
|
+
const lat = RAD_TO_DEG * Math.atan(Math.sinh(mercatorN));
|
|
59
|
+
return { lat, lon };
|
|
60
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface ProtoField {
|
|
2
|
+
wireType: number;
|
|
3
|
+
fieldNumber: number;
|
|
4
|
+
varintValue?: bigint;
|
|
5
|
+
doubleValue?: number;
|
|
6
|
+
floatValue?: number;
|
|
7
|
+
bytesSpan?: [number, number];
|
|
8
|
+
}
|
|
9
|
+
export type ProtoFieldMap = Map<number, ProtoField[]>;
|
|
10
|
+
export declare function parseProtoFields(buf: Buffer, start: number, end: number): ProtoFieldMap;
|
|
11
|
+
export declare function firstSubMessage(buf: Buffer, fields: ProtoFieldMap, fieldNumber: number): ProtoFieldMap | null;
|
|
12
|
+
export declare function firstVarintField(fields: ProtoFieldMap, fieldNumber: number): number | null;
|
|
13
|
+
export declare function firstFloatField(fields: ProtoFieldMap, fieldNumber: number): number | null;
|
|
14
|
+
export declare function firstDoubleField(fields: ProtoFieldMap, fieldNumber: number): number | null;
|
|
15
|
+
export declare function firstStringField(buf: Buffer, fields: ProtoFieldMap, fieldNumber: number): string | null;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const MAX_VARINT_BYTES = 10;
|
|
2
|
+
function readVarint(buf, pos, end) {
|
|
3
|
+
let value = 0n;
|
|
4
|
+
let shift = 0n;
|
|
5
|
+
let i = pos;
|
|
6
|
+
while (i < end && i - pos < MAX_VARINT_BYTES) {
|
|
7
|
+
const byte = buf[i++];
|
|
8
|
+
value |= BigInt(byte & 0x7f) << shift;
|
|
9
|
+
if ((byte & 0x80) === 0)
|
|
10
|
+
return { value, next: i };
|
|
11
|
+
shift += 7n;
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
function signedVarint(value) {
|
|
16
|
+
return Number(BigInt.asIntN(64, value));
|
|
17
|
+
}
|
|
18
|
+
export function parseProtoFields(buf, start, end) {
|
|
19
|
+
const fields = new Map();
|
|
20
|
+
let pos = start;
|
|
21
|
+
while (pos < end) {
|
|
22
|
+
const key = readVarint(buf, pos, end);
|
|
23
|
+
if (!key)
|
|
24
|
+
break;
|
|
25
|
+
const fieldNumber = Number(key.value >> 3n);
|
|
26
|
+
const wireType = Number(key.value & 7n);
|
|
27
|
+
pos = key.next;
|
|
28
|
+
const field = readValue(buf, pos, end, wireType, fieldNumber);
|
|
29
|
+
if (!field)
|
|
30
|
+
break; // groups / unknown wire types / truncation: stop this level
|
|
31
|
+
pos = field.next;
|
|
32
|
+
pushField(fields, fieldNumber, field.protoField);
|
|
33
|
+
}
|
|
34
|
+
return fields;
|
|
35
|
+
}
|
|
36
|
+
function readValue(buf, pos, end, wireType, fieldNumber) {
|
|
37
|
+
const base = { wireType, fieldNumber };
|
|
38
|
+
if (wireType === 0) {
|
|
39
|
+
const v = readVarint(buf, pos, end);
|
|
40
|
+
return v && { protoField: { ...base, varintValue: v.value }, next: v.next };
|
|
41
|
+
}
|
|
42
|
+
if (wireType === 1) {
|
|
43
|
+
if (pos + 8 > end)
|
|
44
|
+
return null;
|
|
45
|
+
return {
|
|
46
|
+
protoField: { ...base, doubleValue: buf.readDoubleLE(pos) },
|
|
47
|
+
next: pos + 8,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (wireType === 2) {
|
|
51
|
+
const len = readVarint(buf, pos, end);
|
|
52
|
+
if (!len)
|
|
53
|
+
return null;
|
|
54
|
+
const payloadStart = len.next;
|
|
55
|
+
const n = Number(len.value);
|
|
56
|
+
if (payloadStart + n > end)
|
|
57
|
+
return null;
|
|
58
|
+
return {
|
|
59
|
+
protoField: { ...base, bytesSpan: [payloadStart, payloadStart + n] },
|
|
60
|
+
next: payloadStart + n,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (wireType === 5) {
|
|
64
|
+
if (pos + 4 > end)
|
|
65
|
+
return null;
|
|
66
|
+
return {
|
|
67
|
+
protoField: { ...base, floatValue: buf.readFloatLE(pos) },
|
|
68
|
+
next: pos + 4,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
function pushField(fields, fieldNumber, field) {
|
|
74
|
+
const existing = fields.get(fieldNumber);
|
|
75
|
+
if (existing)
|
|
76
|
+
existing.push(field);
|
|
77
|
+
else
|
|
78
|
+
fields.set(fieldNumber, [field]);
|
|
79
|
+
}
|
|
80
|
+
export function firstSubMessage(buf, fields, fieldNumber) {
|
|
81
|
+
const field = fields.get(fieldNumber)?.[0];
|
|
82
|
+
if (!field?.bytesSpan)
|
|
83
|
+
return null;
|
|
84
|
+
const [start, end] = field.bytesSpan;
|
|
85
|
+
return parseProtoFields(buf, start, end);
|
|
86
|
+
}
|
|
87
|
+
export function firstVarintField(fields, fieldNumber) {
|
|
88
|
+
const value = fields.get(fieldNumber)?.[0]?.varintValue;
|
|
89
|
+
return value !== undefined ? signedVarint(value) : null;
|
|
90
|
+
}
|
|
91
|
+
export function firstFloatField(fields, fieldNumber) {
|
|
92
|
+
const value = fields.get(fieldNumber)?.[0]?.floatValue;
|
|
93
|
+
return value ?? null;
|
|
94
|
+
}
|
|
95
|
+
export function firstDoubleField(fields, fieldNumber) {
|
|
96
|
+
const value = fields.get(fieldNumber)?.[0]?.doubleValue;
|
|
97
|
+
return value ?? null;
|
|
98
|
+
}
|
|
99
|
+
export function firstStringField(buf, fields, fieldNumber) {
|
|
100
|
+
const span = fields.get(fieldNumber)?.[0]?.bytesSpan;
|
|
101
|
+
return span ? buf.toString('utf8', span[0], span[1]) : null;
|
|
102
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function findSrtSidecar(videoPath: string): string | null;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
export function findSrtSidecar(videoPath) {
|
|
4
|
+
const dir = path.dirname(videoPath);
|
|
5
|
+
const base = path.basename(videoPath).replace(/\.[^.]+$/, '');
|
|
6
|
+
const sidecarName = `${base.toLowerCase()}.srt`;
|
|
7
|
+
for (const fileName of fs.readdirSync(dir)) {
|
|
8
|
+
if (fileName.toLowerCase() === sidecarName) {
|
|
9
|
+
return path.join(dir, fileName);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
}
|
package/dist/srt.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { LatLon } from './geo.js';
|
|
2
|
+
export interface SrtCue {
|
|
3
|
+
tSec: number;
|
|
4
|
+
fix: LatLon | null;
|
|
5
|
+
relAlt: number | null;
|
|
6
|
+
absAlt: number | null;
|
|
7
|
+
iso: number | null;
|
|
8
|
+
shutterTime: number | null;
|
|
9
|
+
whiteBalanceCctK: number | null;
|
|
10
|
+
fNumber: number | null;
|
|
11
|
+
}
|
|
12
|
+
interface SrtParseResult {
|
|
13
|
+
cues: SrtCue[];
|
|
14
|
+
noLock: number;
|
|
15
|
+
count: number;
|
|
16
|
+
}
|
|
17
|
+
export declare function parseSrtText(text: string): SrtParseResult;
|
|
18
|
+
export declare function parseSrt(path: string): Promise<SrtParseResult>;
|
|
19
|
+
export {};
|
package/dist/srt.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { gpsFixOrNull } from './geo.js';
|
|
3
|
+
const RX_TIMING = /^(\d{2}):(\d{2}):(\d{2})[,.](\d{3})\s*-->/;
|
|
4
|
+
const RX_LATLON = /\[latitude:\s*(-?\d+\.?\d*)\]\s*\[longitude:\s*(-?\d+\.?\d*)\]/;
|
|
5
|
+
const RX_ALT = /\[rel_alt:\s*(-?\d+\.?\d*)\s+abs_alt:\s*(-?\d+\.?\d*)\]/;
|
|
6
|
+
const RX_ISO = /\[iso:\s*(-?\d+\.?\d*)\]/;
|
|
7
|
+
const RX_SHUTTER = /\[shutter:\s*([^\]]+)\]/;
|
|
8
|
+
const RX_CT = /\[ct:\s*(-?\d+\.?\d*)\]/;
|
|
9
|
+
const RX_FNUM = /\[fnum:\s*(-?\d+\.?\d*)\]/;
|
|
10
|
+
const RX_FRACTION = /^(\d+\.?\d*)\s*\/\s*(\d+\.?\d*)$/;
|
|
11
|
+
function parseNumber(text) {
|
|
12
|
+
const value = Number.parseFloat(text);
|
|
13
|
+
return Number.isFinite(value) ? value : null;
|
|
14
|
+
}
|
|
15
|
+
function parseShutter(text) {
|
|
16
|
+
const fraction = RX_FRACTION.exec(text.trim());
|
|
17
|
+
if (fraction) {
|
|
18
|
+
const denominator = parseNumber(fraction[2]);
|
|
19
|
+
const numerator = parseNumber(fraction[1]);
|
|
20
|
+
return denominator && numerator !== null ? numerator / denominator : null;
|
|
21
|
+
}
|
|
22
|
+
return parseNumber(text);
|
|
23
|
+
}
|
|
24
|
+
function stripFontTags(line) {
|
|
25
|
+
return line.replace(/<[^>]*>/g, '').trim();
|
|
26
|
+
}
|
|
27
|
+
function timingLineToMs(match) {
|
|
28
|
+
return (Number(match[1]) * 3600000 +
|
|
29
|
+
Number(match[2]) * 60000 +
|
|
30
|
+
Number(match[3]) * 1000 +
|
|
31
|
+
Number(match[4]));
|
|
32
|
+
}
|
|
33
|
+
function newCue(tSec) {
|
|
34
|
+
return {
|
|
35
|
+
tSec,
|
|
36
|
+
fix: null,
|
|
37
|
+
relAlt: null,
|
|
38
|
+
absAlt: null,
|
|
39
|
+
iso: null,
|
|
40
|
+
shutterTime: null,
|
|
41
|
+
whiteBalanceCctK: null,
|
|
42
|
+
fNumber: null,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export function parseSrtText(text) {
|
|
46
|
+
const cues = [];
|
|
47
|
+
let noLock = 0;
|
|
48
|
+
let baseMs = null;
|
|
49
|
+
let cueSawLatLon = false;
|
|
50
|
+
for (const rawLine of text.split(/\r?\n/)) {
|
|
51
|
+
const line = stripFontTags(rawLine);
|
|
52
|
+
if (!line)
|
|
53
|
+
continue;
|
|
54
|
+
const timing = RX_TIMING.exec(line);
|
|
55
|
+
if (timing) {
|
|
56
|
+
const ms = timingLineToMs(timing);
|
|
57
|
+
baseMs ??= ms;
|
|
58
|
+
cues.push(newCue((ms - baseMs) / 1000));
|
|
59
|
+
cueSawLatLon = false;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (cues.length === 0)
|
|
63
|
+
continue;
|
|
64
|
+
const cue = cues[cues.length - 1];
|
|
65
|
+
const latLon = RX_LATLON.exec(line);
|
|
66
|
+
if (latLon && !cueSawLatLon) {
|
|
67
|
+
cueSawLatLon = true;
|
|
68
|
+
const fix = gpsFixOrNull(parseNumber(latLon[1]), parseNumber(latLon[2]));
|
|
69
|
+
if (fix !== null) {
|
|
70
|
+
cue.fix = fix;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
noLock++;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const alt = RX_ALT.exec(line);
|
|
77
|
+
if (alt && cue.relAlt === null) {
|
|
78
|
+
cue.relAlt = parseNumber(alt[1]);
|
|
79
|
+
cue.absAlt = parseNumber(alt[2]);
|
|
80
|
+
}
|
|
81
|
+
const iso = cue.iso === null ? RX_ISO.exec(line) : null;
|
|
82
|
+
if (iso)
|
|
83
|
+
cue.iso = parseNumber(iso[1]);
|
|
84
|
+
const shutter = cue.shutterTime === null ? RX_SHUTTER.exec(line) : null;
|
|
85
|
+
if (shutter)
|
|
86
|
+
cue.shutterTime = parseShutter(shutter[1]);
|
|
87
|
+
const ct = cue.whiteBalanceCctK === null ? RX_CT.exec(line) : null;
|
|
88
|
+
if (ct)
|
|
89
|
+
cue.whiteBalanceCctK = parseNumber(ct[1]);
|
|
90
|
+
const fnum = cue.fNumber === null ? RX_FNUM.exec(line) : null;
|
|
91
|
+
if (fnum)
|
|
92
|
+
cue.fNumber = parseNumber(fnum[1]);
|
|
93
|
+
}
|
|
94
|
+
return { cues, noLock, count: cues.length };
|
|
95
|
+
}
|
|
96
|
+
export async function parseSrt(path) {
|
|
97
|
+
return parseSrtText(await readFile(path, 'utf8'));
|
|
98
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { FrameRange } from './frame-range.js';
|
|
3
|
+
import type { HeadingMode, TelemetrySource } from './telemetry-math.js';
|
|
4
|
+
export declare const TELEMETRY_JSON_SCHEMA_VERSION = 1;
|
|
5
|
+
declare const GimbalDeviceSchema: z.ZodObject<{
|
|
6
|
+
id: z.ZodInt;
|
|
7
|
+
type: z.ZodInt;
|
|
8
|
+
subType: z.ZodInt;
|
|
9
|
+
name: z.ZodString;
|
|
10
|
+
frequency: z.ZodNumber;
|
|
11
|
+
}, z.core.$strict>;
|
|
12
|
+
declare const TelemetryRecordSchema: z.ZodObject<{
|
|
13
|
+
frame: z.ZodInt;
|
|
14
|
+
gimbalDeviceTimestampMs: z.ZodOptional<z.ZodInt>;
|
|
15
|
+
lat: z.ZodOptional<z.ZodNumber>;
|
|
16
|
+
lng: z.ZodOptional<z.ZodNumber>;
|
|
17
|
+
altAbs: z.ZodOptional<z.ZodNumber>;
|
|
18
|
+
altRel: z.ZodOptional<z.ZodNumber>;
|
|
19
|
+
gpsAltMm: z.ZodOptional<z.ZodInt>;
|
|
20
|
+
pitchDeg: z.ZodOptional<z.ZodNumber>;
|
|
21
|
+
rollDeg: z.ZodOptional<z.ZodNumber>;
|
|
22
|
+
yawDeg: z.ZodOptional<z.ZodNumber>;
|
|
23
|
+
gimbalPitchDeg: z.ZodOptional<z.ZodNumber>;
|
|
24
|
+
gimbalRollDeg: z.ZodOptional<z.ZodNumber>;
|
|
25
|
+
gimbalYawDeg: z.ZodOptional<z.ZodNumber>;
|
|
26
|
+
headingDeg: z.ZodNumber;
|
|
27
|
+
headingSource: z.ZodEnum<{
|
|
28
|
+
yaw: "yaw";
|
|
29
|
+
cog: "cog";
|
|
30
|
+
}>;
|
|
31
|
+
iso: z.ZodOptional<z.ZodInt>;
|
|
32
|
+
shutterTime: z.ZodOptional<z.ZodNumber>;
|
|
33
|
+
whiteBalanceCctK: z.ZodOptional<z.ZodInt>;
|
|
34
|
+
fNumber: z.ZodOptional<z.ZodNumber>;
|
|
35
|
+
}, z.core.$strict>;
|
|
36
|
+
export declare const TelemetryDocumentSchema: z.ZodObject<{
|
|
37
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
38
|
+
protoFile: z.ZodOptional<z.ZodString>;
|
|
39
|
+
source: z.ZodEnum<{
|
|
40
|
+
srt: "srt";
|
|
41
|
+
cammeta: "cammeta";
|
|
42
|
+
}>;
|
|
43
|
+
headingMode: z.ZodEnum<{
|
|
44
|
+
yaw: "yaw";
|
|
45
|
+
cog: "cog";
|
|
46
|
+
}>;
|
|
47
|
+
frameCount: z.ZodInt;
|
|
48
|
+
gimbalDevice: z.ZodOptional<z.ZodObject<{
|
|
49
|
+
id: z.ZodInt;
|
|
50
|
+
type: z.ZodInt;
|
|
51
|
+
subType: z.ZodInt;
|
|
52
|
+
name: z.ZodString;
|
|
53
|
+
frequency: z.ZodNumber;
|
|
54
|
+
}, z.core.$strict>>;
|
|
55
|
+
frames: z.ZodArray<z.ZodObject<{
|
|
56
|
+
frame: z.ZodInt;
|
|
57
|
+
gimbalDeviceTimestampMs: z.ZodOptional<z.ZodInt>;
|
|
58
|
+
lat: z.ZodOptional<z.ZodNumber>;
|
|
59
|
+
lng: z.ZodOptional<z.ZodNumber>;
|
|
60
|
+
altAbs: z.ZodOptional<z.ZodNumber>;
|
|
61
|
+
altRel: z.ZodOptional<z.ZodNumber>;
|
|
62
|
+
gpsAltMm: z.ZodOptional<z.ZodInt>;
|
|
63
|
+
pitchDeg: z.ZodOptional<z.ZodNumber>;
|
|
64
|
+
rollDeg: z.ZodOptional<z.ZodNumber>;
|
|
65
|
+
yawDeg: z.ZodOptional<z.ZodNumber>;
|
|
66
|
+
gimbalPitchDeg: z.ZodOptional<z.ZodNumber>;
|
|
67
|
+
gimbalRollDeg: z.ZodOptional<z.ZodNumber>;
|
|
68
|
+
gimbalYawDeg: z.ZodOptional<z.ZodNumber>;
|
|
69
|
+
headingDeg: z.ZodNumber;
|
|
70
|
+
headingSource: z.ZodEnum<{
|
|
71
|
+
yaw: "yaw";
|
|
72
|
+
cog: "cog";
|
|
73
|
+
}>;
|
|
74
|
+
iso: z.ZodOptional<z.ZodInt>;
|
|
75
|
+
shutterTime: z.ZodOptional<z.ZodNumber>;
|
|
76
|
+
whiteBalanceCctK: z.ZodOptional<z.ZodInt>;
|
|
77
|
+
fNumber: z.ZodOptional<z.ZodNumber>;
|
|
78
|
+
}, z.core.$strict>>;
|
|
79
|
+
}, z.core.$strict>;
|
|
80
|
+
type TelemetryRecord = z.infer<typeof TelemetryRecordSchema>;
|
|
81
|
+
type TelemetryGimbalDevice = z.infer<typeof GimbalDeviceSchema>;
|
|
82
|
+
type TelemetryDocument = z.infer<typeof TelemetryDocumentSchema>;
|
|
83
|
+
type TelemetryValueKey = Exclude<keyof TelemetryRecord, 'frame' | 'headingDeg' | 'headingSource'>;
|
|
84
|
+
export type TelemetryFrameInput = {
|
|
85
|
+
[K in TelemetryValueKey]?: number | null;
|
|
86
|
+
};
|
|
87
|
+
export interface TelemetryDocumentInput {
|
|
88
|
+
protoFile?: string | null;
|
|
89
|
+
source: TelemetrySource;
|
|
90
|
+
headingMode: HeadingMode;
|
|
91
|
+
frameCount: number;
|
|
92
|
+
frameStart?: number;
|
|
93
|
+
gimbalDevice?: TelemetryGimbalDevice | null;
|
|
94
|
+
frames: readonly TelemetryFrameInput[];
|
|
95
|
+
headingDeg: readonly number[];
|
|
96
|
+
}
|
|
97
|
+
export declare function segmentTelemetryInput(input: TelemetryDocumentInput, range: FrameRange | null): TelemetryDocumentInput;
|
|
98
|
+
export declare function buildTelemetryDocument(input: TelemetryDocumentInput): TelemetryDocument;
|
|
99
|
+
export declare function serializeTelemetryDocument(doc: TelemetryDocument): string;
|
|
100
|
+
export declare function writeTelemetryDocument(file: string, input: TelemetryDocumentInput): void;
|
|
101
|
+
export declare function generateTelemetryJsonSchema(): string;
|
|
102
|
+
export {};
|