-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpxcat
executable file
·111 lines (99 loc) · 3.32 KB
/
gpxcat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
import argparse
import logging
import sys
import traceback
import gpxlib
#
# Assumptions:
#
# - A GoPro creates .mp4 files in TimeWarp mode with a constant speedup factor S.
# - Time passed between the end of one .MP4 file and the start of the next.
# (Presumably, to replace batteries.)
# - Using https://github.com/NetworkAndSoftware/gopro2gpx, these MP4 files were turned into GPX files.
# - There are n of these GPX files: file[0], file[1], ..., file[n-1].
# - Each GPX file consists of a sorted array of (at least) {lat, lng, time} tuples.
#
# We observe that the following is true for each for each of these files with respect to the time:
#
# - time[0] is always the correct wallclock time, in other words, the GoPro
# timestamps the start of the video with the correct real-world time.
#
# - The GoPro *incorrectly* timestamps subsequent tuples, making it appear the
# speed was factor S higher than the actual speed. For example, if the speedup
# factor S was 15, then the GoPro will make it seem like 15 real seconds took
# only 1 second, which would give a bike of about 300 km/h. Of course that
# makes total sense, since you'll be watching a video that was 15x accelerated;
# to the viewer the bike is actually going 15x as fast.
#
# The goal of this script, then, is to create a GPX file that correctl
#
# - Remove gaps between GPX files, pretending that battery replacement is
# instant.
#
# - Fix timestamps to the correct wallclock time so that tools like
#
# Illustration:
#
# file[0].time[0], file[0].time[1], ..., file[0].time[n], { gap[0] }
# file[1].time[0], file[1].time[1], ..., file[1].time[n], { gap[1] }
# file[2].time[0], file[2].time[1], ..., file[2].time[n], { gap[1] }
#
#
def main():
parser = argparse.ArgumentParser(
description="A tool to concatenate and time-correct one or more GPX files"
)
parser.add_argument(
"-v",
"--verbose",
help="Increase output verbosity (thus rendering output XML unusable)",
action="store_true",
)
parser.add_argument(
"-s",
"--stretch",
help="Time expansion factor",
type=int,
default=gpxlib.DEFAULT_CAT_STRETCH,
)
parser.add_argument(
"-k",
"--killgap",
help="Removes time gap between GPX files",
action="store_true",
)
parser.add_argument(
"-g",
"--gaplength",
help="When combined with -k replaces time gap with fixed # of seconds",
type=int,
default=gpxlib.DEFAULT_CAT_GAPLENGTH,
)
parser.add_argument("-o", "--output", help="Write output to OUTPUT")
parser.add_argument("files", nargs="*")
args = parser.parse_args()
if not args.files:
parser.error("At least one one input file expected")
points_list = []
for f in args.files:
gpx_in, points = gpxlib.read(f)
points_list.append(points)
gpx_out, segment = gpxlib.create(gpx_in)
try:
segment.points = gpxlib.gpxcat(
points_list,
stretch=args.stretch,
killgap=args.killgap,
gaplength=args.gaplength,
)
except Exception:
sys.exit(traceback.format_exc())
s = gpx_out.to_xml()
if args.output:
with open(args.output, "w") as f:
f.write(s)
else:
print(s)
if __name__ == "__main__":
main()