aboutsummaryrefslogtreecommitdiff
blob: 3d04bfbb63e4ddd9936d64bd4238fe562ff06a30 (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import { constants as fsConstants, Dirent } from 'fs';
import { access, mkdir, readdir, readFile, writeFile } from 'fs/promises';
import { promisify } from 'util';
import path from 'path';
import exifReader from 'exif-reader';
import sizeOf from 'image-size';
import sharp from 'sharp';
import ffmpegPath from 'ffmpeg-static';
import ffprobePath from 'ffprobe-static';
import FfmpegCommand, { FfprobeData } from 'fluent-ffmpeg';
import { DOMParser } from 'xmldom';
import { FeatureCollection, LineString } from 'geojson';
import { gpx } from '@tmcw/togeojson';
import gb from 'geojson-bounds';
import { MediaItem, Group, MediaType } from './models';
import { distanceBetween, secondsToTimeString } from './lib/util';

const promisifiedSizeOf = promisify(sizeOf);

interface GroupedFiles {
  photos: Dirent[];
  videos: Dirent[];
  geoData: Dirent[];
}

interface DimensionsTuple {
  width: number;
  height: number;
}

interface FileError {
  file: string;
  error: any;
}

const failedFiles: FileError[] = [];

function setupFfmpeg(): void {
  if (ffmpegPath === null) {
    console.warn(
      'Could not use built-in FFmpeg. Make sure you have FFmpeg installed, if you want to process videos.',
    );
    return;
  }

  FfmpegCommand.setFfmpegPath(ffmpegPath);
  FfmpegCommand.setFfprobePath(ffprobePath.path);
}

function hasExtension(filename: string, acceptedExtensions: string[]): boolean {
  const index = filename.lastIndexOf('.');
  if (index < 0) {
    return false;
  }

  const extension = filename.substring(index + 1);

  return acceptedExtensions.includes(extension);
}

function hasImageExtension(filename: string): boolean {
  const acceptedExtensions = [
    'apng',
    'avif',
    'bmp',
    'dib',
    'gif',
    'jfi',
    'jfif',
    'jif',
    'jpe',
    'jpeg',
    'jpg',
    'png',
    'tif',
    'tiff',
    'webp',
  ];

  return hasExtension(filename, acceptedExtensions);
}

function hasVideoExtension(filename: string): boolean {
  const acceptedExtensions = ['avi', 'mkv', 'mov', 'mp4', 'webm'];

  return hasExtension(filename, acceptedExtensions);
}

function convertCoordinate(coordinate: number[], coordinateRef: string): number | undefined {
  if (coordinate !== undefined && coordinateRef !== undefined) {
    const ref = coordinateRef === 'N' || coordinateRef === 'E' ? 1 : -1;
    return ref * (coordinate[0] + coordinate[1] / 60 + coordinate[2] / 3600);
  }
}

function decodeExifUserComment(buffer: Buffer): string {
  const encoding = buffer.toString('ascii', 0, 7);
  
  let index = 8;
  for (let i = index; i < buffer.length; i++) {
    if (buffer[i] !== 0) {
      index = i;
      break;
    }
  }

  if (encoding === 'ASCII\0\0') {
    return buffer.toString('utf8', index);
  } else if (encoding === 'UNICODE') {
    return buffer.toString('utf16le', index);
  } else {
    return buffer.toString();
  }
}

async function checkIfFileExists(filepath: string): Promise<boolean> {
  try {
    await access(filepath, fsConstants.F_OK);
    return true;
  } catch (err: any) {
    if (err.code === 'ENOENT') {
      return false;
    }

    throw err;
  }
}

function getNormalImageSize({
  width,
  height,
  orientation,
}: {
  width: number;
  height: number;
  orientation: number;
}): DimensionsTuple {
  return orientation >= 5 ? { width: height, height: width } : { width, height };
}

async function resizePhoto(
  photo: sharp.Sharp,
  filepath: string,
  orientation: number,
): Promise<DimensionsTuple> {
  const outputInfo = await photo
    .withMetadata({ orientation })
    .resize({
      width: orientation >= 5 ? 1080 : undefined,
      height: 1080,
      fit: sharp.fit.inside,
      withoutEnlargement: true,
    })
    .toFile(filepath);

  return getNormalImageSize({ ...outputInfo, orientation });
}

async function resizePhotoOrGetSize(
  photo: sharp.Sharp,
  filepath: string,
  orientation: number,
): Promise<DimensionsTuple> {
  const absolutePath = path.resolve(filepath);
  const fileExists = await checkIfFileExists(absolutePath);

  if (!fileExists) {
    console.log(`File "${absolutePath}" doesn't exist, resizing the image...`);
    return await resizePhoto(photo, absolutePath, orientation);
  }

  console.log(`File ${absolutePath} already exists, trying to get dimensions of the image...`);

  try {
    const dimensions = await promisifiedSizeOf(absolutePath);
    if (dimensions?.width !== undefined || dimensions?.height !== undefined) {
      return getNormalImageSize({
        width: dimensions.width as number,
        height: dimensions.height as number,
        orientation,
      });
    }
  } catch (err) {
    console.error(`Could not get the dimensions of the image ${absolutePath}.`);
    console.error(err);
  }

  console.log(`Trying to resize the image ${absolutePath}...`);
  return await resizePhoto(photo, absolutePath, orientation);
}

async function generateThumbnail(
  photo: sharp.Sharp,
  filepath: string,
  orientation?: number,
): Promise<void> {
  const fileExists = await checkIfFileExists(filepath);
  if (!fileExists) {
    await photo
      .withMetadata({ orientation })
      .resize({ width: 36, height: 36, withoutEnlargement: true })
      .toFile(filepath);
  }
}

async function getPhotos(
  inputDir: string,
  outputPath: string,
  serverPath: string,
  relativePath: string,
  files: Dirent[],
): Promise<MediaItem[]> {
  const photos: MediaItem[] = [];

  for (const file of files) {
    const inputFile = `${inputDir}/${file.name}`;
    const outputDir = `${outputPath}/${relativePath}`;

    try {
      // load photo using sharp
      const photo = sharp(inputFile, { failOnError: false });

      // read photo metadata
      const metadata = await photo.metadata();
      const exifData = exifReader(metadata.exif);

      // reformat EXIF coordinates to usable ones
      const latitude = convertCoordinate(exifData.gps?.GPSLatitude, exifData.gps?.GPSLatitudeRef);
      const longitude = convertCoordinate(
        exifData.gps?.GPSLongitude,
        exifData.gps?.GPSLongitudeRef,
      );

      await mkdir(outputDir, { recursive: true });

      // resize the photo or get size of existing
      const outputInfo = await resizePhotoOrGetSize(
        photo,
        `${outputDir}/${file.name}`,
        metadata.orientation ?? 1,
      );

      await generateThumbnail(photo, `${outputDir}/thumb-${file.name}`, metadata.orientation);

      photos.push({
        name: file.name,
        src: `${serverPath}/${relativePath}/${file.name}`,
        type: MediaType.Photo,
        width: outputInfo.width,
        height: outputInfo.height,
        latitude,
        longitude,
        time: exifData.exif?.DateTimeOriginal,
        thumbnail: `${serverPath}/${relativePath}/thumb-${file.name}`,
        caption: decodeExifUserComment(exifData.exif?.UserComment),
      });
    } catch (err) {
      console.error(`Could not process the file ${inputFile}.`);
      console.error(err);
      failedFiles.push({ file: inputFile, error: err });
    }
  }

  return photos;
}

async function getVideoInfo(file: string): Promise<FfprobeData> {
  return await new Promise((resolve, reject) => {
    FfmpegCommand.ffprobe(file, (err, data) => {
      if (err !== undefined && err !== null) {
        return reject(err);
      }

      return resolve(data);
    });
  });
}

async function optimizeVideo(inputFile: string, outputFile: string): Promise<FfprobeData> {
  const command = FfmpegCommand(inputFile);

  return await new Promise((resolve, reject) => {
    command
      .complexFilter(["scale=-2:min'(1080,ih)'"])
      .on('progress', (progress) => {
        console.log(`Processing file ${inputFile}: ${progress.percent as number}% done`);
      })
      .on('error', (err) => reject(err))
      .on('end', () => resolve(getVideoInfo(outputFile)))
      .save(outputFile);
  });
}

async function generateVideoThumbnail(
  inputFile: string,
  outputDir: string,
  outputFileName: string,
): Promise<void> {
  const command = FfmpegCommand(inputFile);

  return await new Promise((resolve, reject) => {
    command
      .screenshots({ size: '36x36', count: 1, folder: outputDir, filename: outputFileName })
      .on('error', (err) => reject(err))
      .on('end', () => resolve());
  });
}

async function optimizeVideoOrGetInfo(inputFile: string, outputFile: string): Promise<FfprobeData> {
  const fileExists = await checkIfFileExists(outputFile);

  if (!fileExists) {
    console.log(`File "${outputFile}" doesn't exist, optimizing the video...`);
    return await optimizeVideo(inputFile, outputFile);
  }

  console.log(`File ${outputFile} already exists, trying to get info about the video...`);

  try {
    return await getVideoInfo(outputFile);
  } catch (err) {
    console.error(`Could not get the info about video ${outputFile}.`);
    console.error(err);
  }

  return await optimizeVideo(inputFile, outputFile);
}

async function processVideo(
  inputDir: string,
  outputPath: string,
  serverPath: string,
  relativePath: string,
  file: Dirent,
): Promise<MediaItem> {
  const inputFile = `${inputDir}/${file.name}`;
  const outputDir = `${outputPath}/${relativePath}`;
  const outputFileName = `${file.name.substring(0, file.name.lastIndexOf('.'))}.webm`;
  const outputFile = path.resolve(`${outputDir}/${outputFileName}`);

  try {
    await mkdir(outputDir, { recursive: true });

    // re-encode video or get info about the existing one
    const info = await optimizeVideoOrGetInfo(inputFile, outputFile);
    const videoStream = info.streams.find((s) => s.codec_type === 'video');

    // generate thumbnail
    const thumbnailFileName = `thumb-${file.name.substring(0, file.name.lastIndexOf('.'))}.jpg`;
    const thumbnailExists = await checkIfFileExists(`${outputDir}/${thumbnailFileName}`);
    if (!thumbnailExists) {
      await generateVideoThumbnail(inputFile, outputDir, thumbnailFileName);
    }

    return {
      name: outputFileName,
      src: `${serverPath}/${relativePath}/${outputFileName}`,
      type: MediaType.Video,
      width: videoStream?.width ?? 0,
      height: videoStream?.height ?? 0,
      thumbnail: `${serverPath}/${relativePath}/${thumbnailFileName}`,
    };
  } catch (err) {
    console.error(`Could not process the file ${inputFile}.`);
    console.error(err);
    failedFiles.push({ file: inputFile, error: err });
    throw err;
  }
}

async function getVideos(
  inputDir: string,
  outputPath: string,
  serverPath: string,
  relativePath: string,
  files: Dirent[],
): Promise<MediaItem[]> {
  const videos: MediaItem[] = [];

  for (const file of files) {
    const video = await processVideo(inputDir, outputPath, serverPath, relativePath, file);
    videos.push(video);
  }

  return videos;
}

async function getMedia(
  inputDir: string,
  outputPath: string,
  serverPath: string,
  relativePath: string,
  files: GroupedFiles,
): Promise<MediaItem[]> {
  const photos = await getPhotos(inputDir, outputPath, serverPath, relativePath, files.photos);
  const videos = await getVideos(inputDir, outputPath, serverPath, relativePath, files.videos);
  const media = photos.concat(videos);

  return media;
}

async function getFiles(inputDir: string): Promise<GroupedFiles> {
  console.log(`Processing files in ${inputDir} directory...`);

  const dirents = await readdir(inputDir, { withFileTypes: true });
  const files = dirents.reduce<GroupedFiles>(
    (prev, dirent) => {
      if (!dirent.isFile()) {
        return prev;
      }

      if (hasImageExtension(dirent.name)) {
        prev.photos.push(dirent);
      } else if (hasVideoExtension(dirent.name)) {
        prev.videos.push(dirent);
      } else if (hasExtension(dirent.name, ['gpx'])) {
        prev.geoData.push(dirent);
      }

      return prev;
    },
    { photos: [], videos: [], geoData: [] },
  );

  return files;
}

async function processGeoData(inputDir: string, files: Dirent[]): Promise<FeatureCollection[]> {
  const geoData: FeatureCollection[] = [];

  for (const file of files) {
    // read file contents
    const fileContents = await readFile(`${inputDir}/${file.name}`, 'utf8');
    const contentsWithoutNS = fileContents.replace(/\sxmlns[^"]+"[^"]+"/g, '');

    // create DOM from string
    const doc = new DOMParser().parseFromString(contentsWithoutNS, 'text/xml');

    // convert GPX to GeoJSON
    const track: FeatureCollection = gpx(doc);

    // add bounding box
    track.bbox = [gb.xMin(track), gb.yMin(track), gb.xMax(track), gb.yMax(track)];

    geoData.push(track);
  }

  return geoData;
}

function createMetadataFromGeoJSON(
  geoData: FeatureCollection,
): { [key: string]: string | number } | undefined {
  if (geoData === undefined) {
    return;
  }

  const coordTimes = geoData.features[0].properties?.coordinateProperties?.times;

  if (coordTimes === undefined || coordTimes?.length === 0) {
    return;
  }

  // time of the first point
  const start = coordTimes[0];

  // time of the last point
  const end = coordTimes[coordTimes.length - 1];

  // total duration in seconds
  const duration = (new Date(end).getTime() - new Date(start).getTime()) / 1000;
  const durationString = secondsToTimeString(duration);

  // distance and speed
  let totalDistance = 0;
  const speeds = [];
  const coords = (geoData.features[0].geometry as LineString).coordinates;

  for (let i = 0; i < coords.length - 1; i += 1) {
    const a = [coords[i][1], coords[i][0]];
    const b = [coords[i + 1][1], coords[i + 1][0]];
    const distance = distanceBetween(a, b);
    if (distance > 0) {
      totalDistance += distance;
      const timeBetween = new Date(coordTimes[i + 1]).getTime() - new Date(coordTimes[i]).getTime();
      speeds.push(distance / timeBetween);
    }
  }

  // total distance in km
  const distance = Math.floor(totalDistance / 10) / 100;

  // average speed in km/h
  const speedMps = speeds.reduce((acc, val) => acc + val) / speeds.length;
  const speed = Math.floor(speedMps * 3600 * 100) / 100;

  return { duration: durationString, distance, speed };
}

async function getGroups(
  inputPath: string,
  outputPath: string,
  serverPath: string,
  relativePath: string,
): Promise<Group[]> {
  const dirs = (await readdir(inputPath, { withFileTypes: true })).filter((dirent) =>
    dirent.isDirectory(),
  );

  const groups: Group[] = [];

  for (const dir of dirs) {
    const name = dir.name;
    const id = name.replaceAll(' ', '-').toLowerCase();

    const inputDir = `${inputPath}/${name}`;
    const relativeGroupPath = `${relativePath}/${id}`;

    const files = await getFiles(inputDir);

    const media = await getMedia(inputDir, outputPath, serverPath, relativeGroupPath, files);
    media.sort((a, b) => (a.name > b.name ? 1 : -1));

    const geoData = await processGeoData(inputDir, files.geoData);

    const metadata = createMetadataFromGeoJSON(geoData[0]);

    const group: Group = { id, name, media, geoData, metadata };

    if (metadata !== undefined) {
      group.description =
        'Total distance: {distance} km\nDuration: {duration}\nAverage speed: {speed} km/h';
    }

    groups.push(group);
  }

  return groups;
}

async function main(): Promise<void> {
  if (process.argv[2] === undefined || process.argv[3] === undefined) {
    console.log(
      'Usage: npx ts-node ./src/generate-data.ts [INPUT_PATH] [OUTPUT_PATH] [SERVER_PATH]',
    );
    return;
  }

  setupFfmpeg();

  const inputPath = process.argv[2];
  const outputPath = process.argv[3];
  const serverPath = process.argv[4] ?? '';

  const name = path.basename(inputPath);
  const id = name.replaceAll(' ', '-').toLowerCase();
  const groups = await getGroups(inputPath, outputPath, serverPath, id);

  const trip = { id, name, groups };
  const tripJson = JSON.stringify(trip);

  const outputDir = `${outputPath}/${id}`;
  const outputFile = `${outputDir}/index.json`;

  console.log(`Writing output to ${outputFile}...`);

  try {
    await mkdir(outputDir, { recursive: true });
    await writeFile(outputFile, tripJson);
  } catch (err) {
    console.error(err);
  }

  if (failedFiles.length > 0) {
    console.error(
      `Failed to process:\n${failedFiles
        .map((f) => `- ${f.file}\n  ${f.error as string}`)
        .join('\n')}`,
    );
  }
}

void main();