-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGPMFMetersGenerator.cpp
290 lines (261 loc) · 6.75 KB
/
GPMFMetersGenerator.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* GPMFMetersGenerator
*
* Generate images sequence from GPMF' meters
*
* 26/01/2022 - Starting development
* 28/02/2022 - Handle multipart video
* 07/03/2022 - Add GPX generation
* 16/04/2022 - Bump to v1.0 (CAUTION, verbose and debug options changed !)
*
* 09/05/2022 - Switch to v2.0
*
* 23/05/2022 - Switch to v3.0 : adding stories support
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>
#include "datalib/Context.h"
#include "datalib/GPVideo.h"
#include "datalib/GPX.h"
#include "gfxlib/SpeedGfx.h"
#include "gfxlib/AltitudeGfx.h"
#include "gfxlib/PathGfx.h"
#include "gfxlib/SpeedTrkGfx.h"
#include "gfxlib/Export.h"
#include "gfxlib/QualityGfx.h"
#include "gfxlib/TrkStatGfx.h"
/* Configuration */
#include "Version.h"
/* Which gfx to generate */
static char gfx_speed = 0; /* 0,2,3,b */
static char gfx_altitude = 0;
static bool gfx_path = false;
static char gfx_strk = 0; /* 0,2,3 */
static bool gfx_GPX = false;
static bool gfx_KML = false;
static bool gfx_quality = false;
static TrekkingStatGfx::gfxtype gfx_trkstt = TrekkingStatGfx::NONE; /* 0: none, 1: HH:MM, 2: HH:MM:SS */
GPX *hiking = NULL; // full hiking trace
int main(int argc, char *argv[]){
bool force = false;
/* Reading arguments */
int opt;
while(( opt = getopt(argc, argv, ":vdhFs:aApk:VXKG:S:qtTQ")) != -1) {
switch(opt){
case 'F':
force = true;
break;
case 'd': // debug implies verbose
debug = true;
case 'v':
verbose = true;
break;
case 'V':
genvideo = false;
break;
case 's':
switch(*optarg){
case '2':
case '3':
case 'b':
gfx_speed = *optarg;
break;
default :
fputs("*E* Only 2, 3, b are recognized as -s option\n", stderr);
exit(EXIT_FAILURE);
}
break;
case 'a':
gfx_altitude = 'a';
break;
case 'A':
gfx_altitude = 'A';
break;
case 'p':
gfx_path = true;
break;
case 't':
gfx_trkstt = TrekkingStatGfx::HM;
break;
case 'T':
gfx_trkstt = TrekkingStatGfx::HMS;
break;
case 'k':
switch(*optarg){
case '2':
case '3':
gfx_strk = *optarg;
break;
default :
fputs("*E* Only 2, 3 are recognized as -k option\n", stderr);
exit(EXIT_FAILURE);
}
break;
case 'q':
gfx_quality = true;
break;
case 'Q':
enfquality = true;
break;
case 'X':
gfx_GPX = true;
break;
case 'K':
gfx_KML = true;
break;
case 'G':
if(hiking){
fputs("*F* Only one GPX or Story file can be loaded at a time\n", stderr);
exit(EXIT_FAILURE);
}
hiking = new GPX(optarg);
hiking->Dump();
break;
case 'S':
if(hiking){
fputs("*F* Only one GPX or Story file can be loaded at a time\n", stderr);
exit(EXIT_FAILURE);
}
hiking = new GPX(optarg, true);
hiking->Dump();
break;
case '?': // unknown option
fprintf(stderr, "unknown option: -%c\n", optopt);
case 'h':
case ':': // no argument provided (or missing argument)
if(optopt == 's'){
gfx_speed = '2';
break;
} else if(optopt == 'k'){
gfx_strk = '2';
break;
}
puts(
"GPMFMetersGenerator v" VERSION "\n"
"(c) L.Faillie (destroyedlolo) 2022-23\n"
"\nGPMFMetersGenerator [-options] Video.mp4\n"
"\nKnown options :\n"
"-s[2|3|b] : enable speed gfx (default 2d, 3: 3d, b: both)\n"
"-k[2|3] : enable speed tracker gfx (default 2d, 3: 3d)\n"
"-a : enable altitude gfx and draw curve from GoPro video\n"
"-A : enable altitude gfx and draw curve from GPX/story\n"
"-p : enable path gfx\n"
"-q : enable quality gfx\n"
"-t : enable Trekking statistics (HH:MM)\n"
"-T : enable Trekking statistics (HH:MM:SS)\n"
"\n"
"-X : export telemetry as GPX file\n"
"-K : export telemetry as KML file\n"
"\n"
"-G<file> : load a GPX file\n"
"-S<file> : load a story file\n"
"\tOnly a GPX or a story can be loadded, not both\n"
"\n"
"-V : Don't generate video, keep PNG files\n"
"-Q : enforce quality by removing samples where GoP is > 500\n"
"-F : don't fail if the target directory exists\n"
"-v : turn verbose on\n"
"-d : turn debugging messages on\n"
);
exit(EXIT_FAILURE);
break;
}
}
/* Handle first videos */
if(optind >= argc){
fputs("*F* No Video provided\n",stderr);
exit(EXIT_FAILURE);
}
if(optind != argc-1){
fputs("*F* Only one video is expected\n",stderr);
exit(EXIT_FAILURE);
}
/* Determine target directory name from the 1st video */
size_t len = strlen(argv[optind]) + 4;
if( strlen("img0123456.png") > len )
len = strlen("img0123456.png");
len += strlen(argv[optind]) + 1;
char targetDir[ len ]; /* Where files will be created */
strcpy(targetDir, argv[optind]);
char *targetFile = strrchr(targetDir, '.'); /* target file name */
if(!targetFile){
puts("*F* Video filename doesn't have extension");
exit(EXIT_FAILURE);
}
/* Create the target directory */
*targetFile = 0;
if(!force){
DIR* dir = opendir(targetDir);
if(dir){
closedir(dir);
printf("*F* '%s' alreay exists\n", targetDir);
exit(EXIT_FAILURE);
} else if(errno != ENOENT){
perror(targetDir);
exit(EXIT_FAILURE);
}
}
if(mkdir(targetDir, S_IRWXU | S_IRWXG | S_IRWXO) == -1){
if((verbose) && !force)
perror(targetDir);
if(!force)
exit(EXIT_FAILURE);
}
/* Keep video name */
char *fname = basename(targetDir);
char vname[strlen(fname) + 1];
strcpy(vname, fname);
/* done with directory */
*(targetFile++) = '/';
*targetFile = 0;
if(verbose)
printf("*I* images will be generated in '%s'\n", targetDir);
GPVideo video(argv[optind]);
video.Dump();
if(hiking && hiking->isStory()) // Specify the current video
if(!hiking->currentVideo(basename(argv[optind])))
fprintf(stderr, "*W* '%s' is not part of loaded story\n", basename(argv[optind]));
/* Generate videos */
if(gfx_speed){
SpeedGfx gfx( video, hiking, gfx_speed );
gfx.GenerateAllGfx(targetDir, targetFile);
}
if(gfx_altitude){
if(gfx_altitude=='A' and !hiking){
fputs("*F* a GPX file or a story is needed with '-A'\n", stderr);
exit(EXIT_FAILURE);
}
AltitudeGfx gfx( video, hiking, gfx_altitude=='A' );
gfx.GenerateAllGfx(targetDir, targetFile);
}
if(gfx_quality){
QualityGfx gfx( video, hiking );
gfx.GenerateAllGfx(targetDir, targetFile);
}
if(gfx_path){
PathGfx gfx( video, hiking );
gfx.GenerateAllGfx(targetDir, targetFile);
}
if(gfx_strk){
SpeedTrkGfx gfx( video, hiking, gfx_strk );
gfx.GenerateAllGfx(targetDir, targetFile);
}
if(gfx_trkstt){
TrekkingStatGfx gfx( video, hiking, gfx_trkstt );
gfx.GenerateAllGfx(targetDir, targetFile);
}
if(gfx_GPX){
Export gfx( video );
gfx.generateGPX(targetDir, targetFile, vname);
}
if(gfx_KML){
Export gfx( video );
gfx.generateKML(targetDir, targetFile, vname);
}
}