2023-03-02 21:47:08 -05:00
|
|
|
import {
|
|
|
|
|
AssetCore,
|
2023-05-22 11:26:56 -04:00
|
|
|
IAssetJob,
|
2023-03-02 21:47:08 -05:00
|
|
|
IAssetRepository,
|
2023-03-20 11:55:28 -04:00
|
|
|
IBaseJob,
|
2023-04-04 18:23:07 -04:00
|
|
|
IGeocodingRepository,
|
2023-03-05 15:44:31 -05:00
|
|
|
IJobRepository,
|
2023-03-02 21:47:08 -05:00
|
|
|
JobName,
|
2023-05-22 20:05:06 +02:00
|
|
|
JOBS_ASSET_PAGINATION_SIZE,
|
2023-03-02 21:47:08 -05:00
|
|
|
QueueName,
|
2023-05-22 20:05:06 +02:00
|
|
|
usePagination,
|
2023-03-20 11:55:28 -04:00
|
|
|
WithoutProperty,
|
2023-03-02 21:47:08 -05:00
|
|
|
} from '@app/domain';
|
2023-04-11 08:53:42 -05:00
|
|
|
import { AssetEntity, AssetType, ExifEntity } from '@app/infra/entities';
|
2023-02-25 09:12:03 -05:00
|
|
|
import { Inject, Logger } from '@nestjs/common';
|
2022-09-13 12:09:57 -05:00
|
|
|
import { ConfigService } from '@nestjs/config';
|
2022-08-23 21:34:21 +07:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
2023-04-04 18:23:07 -04:00
|
|
|
import tz_lookup from '@photostructure/tz-lookup';
|
2023-03-02 21:47:08 -05:00
|
|
|
import { ExifDateTime, exiftool, Tags } from 'exiftool-vendored';
|
2023-03-03 17:49:22 -05:00
|
|
|
import ffmpeg, { FfprobeData } from 'fluent-ffmpeg';
|
2023-03-23 15:27:29 +01:00
|
|
|
import { Duration } from 'luxon';
|
2023-03-02 21:47:08 -05:00
|
|
|
import fs from 'node:fs';
|
2022-08-29 03:43:31 +07:00
|
|
|
import sharp from 'sharp';
|
2022-08-23 21:34:21 +07:00
|
|
|
import { Repository } from 'typeorm/repository/Repository';
|
2023-03-03 17:49:22 -05:00
|
|
|
import { promisify } from 'util';
|
|
|
|
|
|
|
|
|
|
const ffprobe = promisify<string, FfprobeData>(ffmpeg.ffprobe);
|
2023-02-16 02:41:51 -05:00
|
|
|
|
|
|
|
|
interface ImmichTags extends Tags {
|
|
|
|
|
ContentIdentifier?: string;
|
|
|
|
|
}
|
2022-07-02 21:06:36 -05:00
|
|
|
|
2022-06-11 16:12:06 -05:00
|
|
|
export class MetadataExtractionProcessor {
|
2023-01-13 09:23:12 -05:00
|
|
|
private logger = new Logger(MetadataExtractionProcessor.name);
|
2023-03-02 21:47:08 -05:00
|
|
|
private assetCore: AssetCore;
|
2023-04-04 18:23:07 -04:00
|
|
|
private reverseGeocodingEnabled: boolean;
|
2023-03-02 21:47:08 -05:00
|
|
|
|
2022-06-11 16:12:06 -05:00
|
|
|
constructor(
|
2023-03-20 11:55:28 -04:00
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
|
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-04-04 18:23:07 -04:00
|
|
|
@Inject(IGeocodingRepository) private geocodingRepository: IGeocodingRepository,
|
|
|
|
|
@InjectRepository(ExifEntity) private exifRepository: Repository<ExifEntity>,
|
2022-06-11 16:12:06 -05:00
|
|
|
|
2023-01-13 09:23:12 -05:00
|
|
|
configService: ConfigService,
|
2022-06-11 16:12:06 -05:00
|
|
|
) {
|
2023-03-05 15:44:31 -05:00
|
|
|
this.assetCore = new AssetCore(assetRepository, jobRepository);
|
2023-04-04 18:23:07 -04:00
|
|
|
this.reverseGeocodingEnabled = !configService.get('DISABLE_REVERSE_GEOCODING');
|
2022-06-11 16:12:06 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-23 21:36:36 -04:00
|
|
|
async init(deleteCache = false) {
|
2023-04-04 18:23:07 -04:00
|
|
|
this.logger.warn(`Reverse geocoding is ${this.reverseGeocodingEnabled ? 'enabled' : 'disabled'}`);
|
|
|
|
|
if (!this.reverseGeocodingEnabled) {
|
|
|
|
|
return;
|
2022-10-07 09:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2023-04-04 18:23:07 -04:00
|
|
|
try {
|
2023-05-23 21:36:36 -04:00
|
|
|
if (deleteCache) {
|
2023-05-20 22:39:12 -04:00
|
|
|
await this.geocodingRepository.deleteCache();
|
|
|
|
|
}
|
2023-04-04 18:23:07 -04:00
|
|
|
this.logger.log('Initializing Reverse Geocoding');
|
2022-10-07 09:15:05 -05:00
|
|
|
|
2023-04-04 18:23:07 -04:00
|
|
|
await this.jobRepository.pause(QueueName.METADATA_EXTRACTION);
|
|
|
|
|
await this.geocodingRepository.init();
|
|
|
|
|
await this.jobRepository.resume(QueueName.METADATA_EXTRACTION);
|
2022-09-23 03:50:05 +01:00
|
|
|
|
2023-04-04 18:23:07 -04:00
|
|
|
this.logger.log('Reverse Geocoding Initialized');
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
this.logger.error(`Unable to initialize reverse geocoding: ${error}`, error?.stack);
|
|
|
|
|
}
|
2022-09-23 03:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
2023-05-26 08:52:52 -04:00
|
|
|
async handleQueueMetadataExtraction(job: IBaseJob) {
|
2023-03-20 11:55:28 -04:00
|
|
|
try {
|
2023-05-26 08:52:52 -04:00
|
|
|
const { force } = job;
|
2023-05-22 20:05:06 +02:00
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => {
|
|
|
|
|
return force
|
|
|
|
|
? this.assetRepository.getAll(pagination)
|
|
|
|
|
: this.assetRepository.getWithout(pagination, WithoutProperty.EXIF);
|
|
|
|
|
});
|
2023-03-20 11:55:28 -04:00
|
|
|
|
2023-05-22 20:05:06 +02:00
|
|
|
for await (const assets of assetPagination) {
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
|
const name = asset.type === AssetType.VIDEO ? JobName.EXTRACT_VIDEO_METADATA : JobName.EXIF_EXTRACTION;
|
|
|
|
|
await this.jobRepository.queue({ name, data: { asset } });
|
|
|
|
|
}
|
2023-03-20 11:55:28 -04:00
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
this.logger.error(`Unable to queue metadata extraction`, error?.stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 08:52:52 -04:00
|
|
|
async extractExifInfo(job: IAssetJob) {
|
|
|
|
|
let asset = job.asset;
|
2023-04-11 08:53:42 -05:00
|
|
|
|
2022-06-11 16:12:06 -05:00
|
|
|
try {
|
feat(server): xmp sidecar metadata (#2466)
* initial commit for XMP sidecar support
* Added support for 'missing' metadata files to include those without sidecar files, now detects sidecar files in the filesystem for media already ingested but the sidecar was created afterwards
* didn't mean to commit default log level during testing
* new sidecar logic for video metadata as well
* Added xml mimetype for sidecars only
* don't need capture group for this regex
* wrong default value reverted
* simplified the move here - keep it in the same try catch since the outcome is to move the media back anyway
* simplified setter logic
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
* simplified logic per suggestions
* sidecar is now its own queue with a discover and sync, updated UI for the new job queueing
* queue a sidecar job for every asset based on discovery or sync, though the logic is almost identical aside from linking the sidecar
* now queue sidecar jobs for each assset, though logic is mostly the same between discovery and sync
* simplified logic of filename extraction and asset instantiation
* not sure how that got deleted..
* updated code per suggestions and comments in the PR
* stat was not being used, removed the variable set
* better type checking, using in-scope variables for exif getter instead of passing in every time
* removed commented out test
* ran and resolved all lints, formats, checks, and tests
* resolved suggested change in PR
* made getExifProperty more dynamic with multiple possible args for fallbacks, fixed typo, used generic in function for better type checking
* better error handling and moving files back to positions on move or save failure
* regenerated api
* format fixes
* Added XMP documentation
* documentation typo
* Merged in main
* missed merge conflict
* more changes due to a merge
* Resolving conflicts
* added icon for sidecar jobs
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-05-24 21:59:30 -04:00
|
|
|
const mediaExifData = await exiftool.read<ImmichTags>(asset.originalPath).catch((error: any) => {
|
2023-04-11 08:53:42 -05:00
|
|
|
this.logger.warn(
|
|
|
|
|
`The exifData parsing failed due to ${error} for asset ${asset.id} at ${asset.originalPath}`,
|
|
|
|
|
error?.stack,
|
|
|
|
|
);
|
2023-01-17 13:41:00 -06:00
|
|
|
return null;
|
2022-08-29 03:43:31 +07:00
|
|
|
});
|
feat(server): xmp sidecar metadata (#2466)
* initial commit for XMP sidecar support
* Added support for 'missing' metadata files to include those without sidecar files, now detects sidecar files in the filesystem for media already ingested but the sidecar was created afterwards
* didn't mean to commit default log level during testing
* new sidecar logic for video metadata as well
* Added xml mimetype for sidecars only
* don't need capture group for this regex
* wrong default value reverted
* simplified the move here - keep it in the same try catch since the outcome is to move the media back anyway
* simplified setter logic
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
* simplified logic per suggestions
* sidecar is now its own queue with a discover and sync, updated UI for the new job queueing
* queue a sidecar job for every asset based on discovery or sync, though the logic is almost identical aside from linking the sidecar
* now queue sidecar jobs for each assset, though logic is mostly the same between discovery and sync
* simplified logic of filename extraction and asset instantiation
* not sure how that got deleted..
* updated code per suggestions and comments in the PR
* stat was not being used, removed the variable set
* better type checking, using in-scope variables for exif getter instead of passing in every time
* removed commented out test
* ran and resolved all lints, formats, checks, and tests
* resolved suggested change in PR
* made getExifProperty more dynamic with multiple possible args for fallbacks, fixed typo, used generic in function for better type checking
* better error handling and moving files back to positions on move or save failure
* regenerated api
* format fixes
* Added XMP documentation
* documentation typo
* Merged in main
* missed merge conflict
* more changes due to a merge
* Resolving conflicts
* added icon for sidecar jobs
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-05-24 21:59:30 -04:00
|
|
|
const sidecarExifData = asset.sidecarPath
|
|
|
|
|
? await exiftool.read<ImmichTags>(asset.sidecarPath).catch((error: any) => {
|
|
|
|
|
this.logger.warn(
|
|
|
|
|
`The exifData parsing failed due to ${error} for asset ${asset.id} at ${asset.originalPath}`,
|
|
|
|
|
error?.stack,
|
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
})
|
|
|
|
|
: {};
|
2022-06-11 16:12:06 -05:00
|
|
|
|
2023-01-17 13:41:00 -06:00
|
|
|
const exifToDate = (exifDate: string | ExifDateTime | undefined) => {
|
|
|
|
|
if (!exifDate) return null;
|
2022-09-22 15:58:17 -05:00
|
|
|
|
2023-01-17 13:41:00 -06:00
|
|
|
if (typeof exifDate === 'string') {
|
|
|
|
|
return new Date(exifDate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return exifDate.toDate();
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-02 21:11:24 +02:00
|
|
|
const exifTimeZone = (exifDate: string | ExifDateTime | undefined) => {
|
|
|
|
|
if (!exifDate) return null;
|
|
|
|
|
|
|
|
|
|
if (typeof exifDate === 'string') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return exifDate.zone ?? null;
|
|
|
|
|
};
|
|
|
|
|
|
feat(server): xmp sidecar metadata (#2466)
* initial commit for XMP sidecar support
* Added support for 'missing' metadata files to include those without sidecar files, now detects sidecar files in the filesystem for media already ingested but the sidecar was created afterwards
* didn't mean to commit default log level during testing
* new sidecar logic for video metadata as well
* Added xml mimetype for sidecars only
* don't need capture group for this regex
* wrong default value reverted
* simplified the move here - keep it in the same try catch since the outcome is to move the media back anyway
* simplified setter logic
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
* simplified logic per suggestions
* sidecar is now its own queue with a discover and sync, updated UI for the new job queueing
* queue a sidecar job for every asset based on discovery or sync, though the logic is almost identical aside from linking the sidecar
* now queue sidecar jobs for each assset, though logic is mostly the same between discovery and sync
* simplified logic of filename extraction and asset instantiation
* not sure how that got deleted..
* updated code per suggestions and comments in the PR
* stat was not being used, removed the variable set
* better type checking, using in-scope variables for exif getter instead of passing in every time
* removed commented out test
* ran and resolved all lints, formats, checks, and tests
* resolved suggested change in PR
* made getExifProperty more dynamic with multiple possible args for fallbacks, fixed typo, used generic in function for better type checking
* better error handling and moving files back to positions on move or save failure
* regenerated api
* format fixes
* Added XMP documentation
* documentation typo
* Merged in main
* missed merge conflict
* more changes due to a merge
* Resolving conflicts
* added icon for sidecar jobs
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-05-24 21:59:30 -04:00
|
|
|
const getExifProperty = <T extends keyof ImmichTags>(...properties: T[]): any | null => {
|
|
|
|
|
for (const property of properties) {
|
|
|
|
|
const value = sidecarExifData?.[property] ?? mediaExifData?.[property];
|
|
|
|
|
if (value !== null && value !== undefined) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const timeZone = exifTimeZone(getExifProperty('DateTimeOriginal', 'CreateDate') ?? asset.fileCreatedAt);
|
|
|
|
|
const fileCreatedAt = exifToDate(getExifProperty('DateTimeOriginal', 'CreateDate') ?? asset.fileCreatedAt);
|
|
|
|
|
const fileModifiedAt = exifToDate(getExifProperty('ModifyDate') ?? asset.fileModifiedAt);
|
2022-09-28 11:41:50 +01:00
|
|
|
const fileStats = fs.statSync(asset.originalPath);
|
|
|
|
|
const fileSizeInBytes = fileStats.size;
|
2023-01-17 13:41:00 -06:00
|
|
|
|
|
|
|
|
const newExif = new ExifEntity();
|
2022-06-11 16:12:06 -05:00
|
|
|
newExif.assetId = asset.id;
|
2023-01-17 13:41:00 -06:00
|
|
|
newExif.fileSizeInByte = fileSizeInBytes;
|
feat(server): xmp sidecar metadata (#2466)
* initial commit for XMP sidecar support
* Added support for 'missing' metadata files to include those without sidecar files, now detects sidecar files in the filesystem for media already ingested but the sidecar was created afterwards
* didn't mean to commit default log level during testing
* new sidecar logic for video metadata as well
* Added xml mimetype for sidecars only
* don't need capture group for this regex
* wrong default value reverted
* simplified the move here - keep it in the same try catch since the outcome is to move the media back anyway
* simplified setter logic
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
* simplified logic per suggestions
* sidecar is now its own queue with a discover and sync, updated UI for the new job queueing
* queue a sidecar job for every asset based on discovery or sync, though the logic is almost identical aside from linking the sidecar
* now queue sidecar jobs for each assset, though logic is mostly the same between discovery and sync
* simplified logic of filename extraction and asset instantiation
* not sure how that got deleted..
* updated code per suggestions and comments in the PR
* stat was not being used, removed the variable set
* better type checking, using in-scope variables for exif getter instead of passing in every time
* removed commented out test
* ran and resolved all lints, formats, checks, and tests
* resolved suggested change in PR
* made getExifProperty more dynamic with multiple possible args for fallbacks, fixed typo, used generic in function for better type checking
* better error handling and moving files back to positions on move or save failure
* regenerated api
* format fixes
* Added XMP documentation
* documentation typo
* Merged in main
* missed merge conflict
* more changes due to a merge
* Resolving conflicts
* added icon for sidecar jobs
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-05-24 21:59:30 -04:00
|
|
|
newExif.make = getExifProperty('Make');
|
|
|
|
|
newExif.model = getExifProperty('Model');
|
|
|
|
|
newExif.exifImageHeight = getExifProperty('ExifImageHeight', 'ImageHeight');
|
|
|
|
|
newExif.exifImageWidth = getExifProperty('ExifImageWidth', 'ImageWidth');
|
|
|
|
|
newExif.exposureTime = getExifProperty('ExposureTime');
|
|
|
|
|
newExif.orientation = getExifProperty('Orientation')?.toString();
|
2023-02-19 16:44:53 +00:00
|
|
|
newExif.dateTimeOriginal = fileCreatedAt;
|
|
|
|
|
newExif.modifyDate = fileModifiedAt;
|
2023-04-02 21:11:24 +02:00
|
|
|
newExif.timeZone = timeZone;
|
feat(server): xmp sidecar metadata (#2466)
* initial commit for XMP sidecar support
* Added support for 'missing' metadata files to include those without sidecar files, now detects sidecar files in the filesystem for media already ingested but the sidecar was created afterwards
* didn't mean to commit default log level during testing
* new sidecar logic for video metadata as well
* Added xml mimetype for sidecars only
* don't need capture group for this regex
* wrong default value reverted
* simplified the move here - keep it in the same try catch since the outcome is to move the media back anyway
* simplified setter logic
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
* simplified logic per suggestions
* sidecar is now its own queue with a discover and sync, updated UI for the new job queueing
* queue a sidecar job for every asset based on discovery or sync, though the logic is almost identical aside from linking the sidecar
* now queue sidecar jobs for each assset, though logic is mostly the same between discovery and sync
* simplified logic of filename extraction and asset instantiation
* not sure how that got deleted..
* updated code per suggestions and comments in the PR
* stat was not being used, removed the variable set
* better type checking, using in-scope variables for exif getter instead of passing in every time
* removed commented out test
* ran and resolved all lints, formats, checks, and tests
* resolved suggested change in PR
* made getExifProperty more dynamic with multiple possible args for fallbacks, fixed typo, used generic in function for better type checking
* better error handling and moving files back to positions on move or save failure
* regenerated api
* format fixes
* Added XMP documentation
* documentation typo
* Merged in main
* missed merge conflict
* more changes due to a merge
* Resolving conflicts
* added icon for sidecar jobs
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-05-24 21:59:30 -04:00
|
|
|
newExif.lensModel = getExifProperty('LensModel');
|
|
|
|
|
newExif.fNumber = getExifProperty('FNumber');
|
|
|
|
|
const focalLength = getExifProperty('FocalLength');
|
|
|
|
|
newExif.focalLength = focalLength ? parseFloat(focalLength) : null;
|
|
|
|
|
// This is unusual - exifData.ISO should return a number, but experienced that sidecar XMP
|
|
|
|
|
// files MAY return an array of numbers instead.
|
|
|
|
|
const iso = getExifProperty('ISO');
|
|
|
|
|
newExif.iso = Array.isArray(iso) ? iso[0] : iso || null;
|
|
|
|
|
newExif.latitude = getExifProperty('GPSLatitude');
|
|
|
|
|
newExif.longitude = getExifProperty('GPSLongitude');
|
|
|
|
|
newExif.livePhotoCID = getExifProperty('MediaGroupUUID');
|
2022-06-11 16:12:06 -05:00
|
|
|
|
2023-02-16 02:41:51 -05:00
|
|
|
if (newExif.livePhotoCID && !asset.livePhotoVideoId) {
|
2023-04-04 00:48:05 -04:00
|
|
|
const motionAsset = await this.assetCore.findLivePhotoMatch({
|
|
|
|
|
livePhotoCID: newExif.livePhotoCID,
|
|
|
|
|
otherAssetId: asset.id,
|
|
|
|
|
ownerId: asset.ownerId,
|
|
|
|
|
type: AssetType.VIDEO,
|
|
|
|
|
});
|
2023-02-16 02:41:51 -05:00
|
|
|
if (motionAsset) {
|
2023-03-02 21:47:08 -05:00
|
|
|
await this.assetCore.save({ id: asset.id, livePhotoVideoId: motionAsset.id });
|
|
|
|
|
await this.assetCore.save({ id: motionAsset.id, isVisible: false });
|
2023-02-16 02:41:51 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 08:53:42 -05:00
|
|
|
await this.applyReverseGeocoding(asset, newExif);
|
2022-06-11 16:12:06 -05:00
|
|
|
|
2022-09-22 15:58:17 -05:00
|
|
|
/**
|
|
|
|
|
* IF the EXIF doesn't contain the width and height of the image,
|
|
|
|
|
* We will use Sharpjs to get the information.
|
|
|
|
|
*/
|
2022-08-29 03:43:31 +07:00
|
|
|
if (!newExif.exifImageHeight || !newExif.exifImageWidth || !newExif.orientation) {
|
|
|
|
|
const metadata = await sharp(asset.originalPath).metadata();
|
|
|
|
|
|
|
|
|
|
if (newExif.exifImageHeight === null) {
|
|
|
|
|
newExif.exifImageHeight = metadata.height || null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newExif.exifImageWidth === null) {
|
|
|
|
|
newExif.exifImageWidth = metadata.width || null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newExif.orientation === null) {
|
|
|
|
|
newExif.orientation = metadata.orientation !== undefined ? `${metadata.orientation}` : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 22:50:22 -06:00
|
|
|
await this.exifRepository.upsert(newExif, { conflictPaths: ['assetId'] });
|
2023-03-28 16:04:11 -04:00
|
|
|
asset = await this.assetCore.save({ id: asset.id, fileCreatedAt: fileCreatedAt?.toISOString() });
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.STORAGE_TEMPLATE_MIGRATION_SINGLE, data: { asset } });
|
2023-01-13 09:23:12 -05:00
|
|
|
} catch (error: any) {
|
2023-04-11 08:53:42 -05:00
|
|
|
this.logger.error(
|
|
|
|
|
`Error extracting EXIF ${error} for assetId ${asset.id} at ${asset.originalPath}`,
|
|
|
|
|
error?.stack,
|
|
|
|
|
);
|
2022-06-11 16:12:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 08:52:52 -04:00
|
|
|
async extractVideoMetadata(job: IAssetJob) {
|
|
|
|
|
let asset = job.asset;
|
2022-06-19 08:16:35 -05:00
|
|
|
|
2023-01-30 11:14:13 -05:00
|
|
|
if (!asset.isVisible) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-21 06:31:37 +07:00
|
|
|
try {
|
2023-03-03 17:49:22 -05:00
|
|
|
const data = await ffprobe(asset.originalPath);
|
|
|
|
|
const durationString = this.extractDuration(data.format.duration || asset.duration);
|
2023-02-19 16:44:53 +00:00
|
|
|
let fileCreatedAt = asset.fileCreatedAt;
|
2022-08-21 06:31:37 +07:00
|
|
|
|
|
|
|
|
const videoTags = data.format.tags;
|
|
|
|
|
if (videoTags) {
|
|
|
|
|
if (videoTags['com.apple.quicktime.creationdate']) {
|
2023-02-19 16:44:53 +00:00
|
|
|
fileCreatedAt = String(videoTags['com.apple.quicktime.creationdate']);
|
2022-08-21 06:31:37 +07:00
|
|
|
} else if (videoTags['creation_time']) {
|
2023-02-19 16:44:53 +00:00
|
|
|
fileCreatedAt = String(videoTags['creation_time']);
|
2022-08-21 06:31:37 +07:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-19 08:16:35 -05:00
|
|
|
|
feat(server): xmp sidecar metadata (#2466)
* initial commit for XMP sidecar support
* Added support for 'missing' metadata files to include those without sidecar files, now detects sidecar files in the filesystem for media already ingested but the sidecar was created afterwards
* didn't mean to commit default log level during testing
* new sidecar logic for video metadata as well
* Added xml mimetype for sidecars only
* don't need capture group for this regex
* wrong default value reverted
* simplified the move here - keep it in the same try catch since the outcome is to move the media back anyway
* simplified setter logic
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
* simplified logic per suggestions
* sidecar is now its own queue with a discover and sync, updated UI for the new job queueing
* queue a sidecar job for every asset based on discovery or sync, though the logic is almost identical aside from linking the sidecar
* now queue sidecar jobs for each assset, though logic is mostly the same between discovery and sync
* simplified logic of filename extraction and asset instantiation
* not sure how that got deleted..
* updated code per suggestions and comments in the PR
* stat was not being used, removed the variable set
* better type checking, using in-scope variables for exif getter instead of passing in every time
* removed commented out test
* ran and resolved all lints, formats, checks, and tests
* resolved suggested change in PR
* made getExifProperty more dynamic with multiple possible args for fallbacks, fixed typo, used generic in function for better type checking
* better error handling and moving files back to positions on move or save failure
* regenerated api
* format fixes
* Added XMP documentation
* documentation typo
* Merged in main
* missed merge conflict
* more changes due to a merge
* Resolving conflicts
* added icon for sidecar jobs
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-05-24 21:59:30 -04:00
|
|
|
const exifData = await exiftool.read<ImmichTags>(asset.sidecarPath || asset.originalPath).catch((error: any) => {
|
2023-04-11 08:53:42 -05:00
|
|
|
this.logger.warn(
|
|
|
|
|
`The exifData parsing failed due to ${error} for asset ${asset.id} at ${asset.originalPath}`,
|
|
|
|
|
error?.stack,
|
|
|
|
|
);
|
2023-02-16 02:41:51 -05:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-21 06:31:37 +07:00
|
|
|
const newExif = new ExifEntity();
|
|
|
|
|
newExif.assetId = asset.id;
|
|
|
|
|
newExif.fileSizeInByte = data.format.size || null;
|
2023-02-19 16:44:53 +00:00
|
|
|
newExif.dateTimeOriginal = fileCreatedAt ? new Date(fileCreatedAt) : null;
|
2022-08-21 06:31:37 +07:00
|
|
|
newExif.modifyDate = null;
|
2023-04-02 21:11:24 +02:00
|
|
|
newExif.timeZone = null;
|
2022-08-21 06:31:37 +07:00
|
|
|
newExif.latitude = null;
|
|
|
|
|
newExif.longitude = null;
|
|
|
|
|
newExif.city = null;
|
|
|
|
|
newExif.state = null;
|
|
|
|
|
newExif.country = null;
|
|
|
|
|
newExif.fps = null;
|
2023-02-16 02:41:51 -05:00
|
|
|
newExif.livePhotoCID = exifData?.ContentIdentifier || null;
|
|
|
|
|
|
|
|
|
|
if (newExif.livePhotoCID) {
|
2023-04-04 00:48:05 -04:00
|
|
|
const photoAsset = await this.assetCore.findLivePhotoMatch({
|
|
|
|
|
livePhotoCID: newExif.livePhotoCID,
|
|
|
|
|
ownerId: asset.ownerId,
|
|
|
|
|
otherAssetId: asset.id,
|
|
|
|
|
type: AssetType.IMAGE,
|
|
|
|
|
});
|
2023-02-16 02:41:51 -05:00
|
|
|
if (photoAsset) {
|
2023-03-02 21:47:08 -05:00
|
|
|
await this.assetCore.save({ id: photoAsset.id, livePhotoVideoId: asset.id });
|
|
|
|
|
await this.assetCore.save({ id: asset.id, isVisible: false });
|
2023-02-16 02:41:51 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-21 06:31:37 +07:00
|
|
|
|
|
|
|
|
if (videoTags && videoTags['location']) {
|
|
|
|
|
const location = videoTags['location'] as string;
|
|
|
|
|
const locationRegex = /([+-][0-9]+\.[0-9]+)([+-][0-9]+\.[0-9]+)\/$/;
|
|
|
|
|
const match = location.match(locationRegex);
|
|
|
|
|
|
|
|
|
|
if (match?.length === 3) {
|
2022-08-21 12:58:47 +07:00
|
|
|
newExif.latitude = parseFloat(match[1]);
|
|
|
|
|
newExif.longitude = parseFloat(match[2]);
|
2022-07-04 13:44:43 -05:00
|
|
|
}
|
2022-08-21 06:31:37 +07:00
|
|
|
} else if (videoTags && videoTags['com.apple.quicktime.location.ISO6709']) {
|
|
|
|
|
const location = videoTags['com.apple.quicktime.location.ISO6709'] as string;
|
|
|
|
|
const locationRegex = /([+-][0-9]+\.[0-9]+)([+-][0-9]+\.[0-9]+)([+-][0-9]+\.[0-9]+)\/$/;
|
|
|
|
|
const match = location.match(locationRegex);
|
2022-08-21 12:58:47 +07:00
|
|
|
|
2022-08-21 06:31:37 +07:00
|
|
|
if (match?.length === 4) {
|
|
|
|
|
newExif.latitude = parseFloat(match[1]);
|
|
|
|
|
newExif.longitude = parseFloat(match[2]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-19 08:16:35 -05:00
|
|
|
|
2023-04-02 21:11:24 +02:00
|
|
|
if (newExif.longitude && newExif.latitude) {
|
|
|
|
|
try {
|
|
|
|
|
newExif.timeZone = tz_lookup(newExif.latitude, newExif.longitude);
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
this.logger.warn(`Error while calculating timezone from gps coordinates: ${error}`, error?.stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 08:53:42 -05:00
|
|
|
await this.applyReverseGeocoding(asset, newExif);
|
2022-08-21 06:31:37 +07:00
|
|
|
|
|
|
|
|
for (const stream of data.streams) {
|
|
|
|
|
if (stream.codec_type === 'video') {
|
|
|
|
|
newExif.exifImageWidth = stream.width || null;
|
|
|
|
|
newExif.exifImageHeight = stream.height || null;
|
|
|
|
|
|
|
|
|
|
if (typeof stream.rotation === 'string') {
|
|
|
|
|
newExif.orientation = stream.rotation;
|
|
|
|
|
} else if (typeof stream.rotation === 'number') {
|
|
|
|
|
newExif.orientation = `${stream.rotation}`;
|
2022-07-12 16:34:43 -05:00
|
|
|
} else {
|
2022-08-21 06:31:37 +07:00
|
|
|
newExif.orientation = null;
|
2022-07-04 13:44:43 -05:00
|
|
|
}
|
|
|
|
|
|
2022-08-21 06:31:37 +07:00
|
|
|
if (stream.r_frame_rate) {
|
2022-09-08 11:07:27 +02:00
|
|
|
const fpsParts = stream.r_frame_rate.split('/');
|
2022-08-21 06:31:37 +07:00
|
|
|
|
|
|
|
|
if (fpsParts.length === 2) {
|
|
|
|
|
newExif.fps = Math.round(parseInt(fpsParts[0]) / parseInt(fpsParts[1]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-19 08:16:35 -05:00
|
|
|
}
|
2022-08-21 06:31:37 +07:00
|
|
|
|
2023-01-26 22:50:22 -06:00
|
|
|
await this.exifRepository.upsert(newExif, { conflictPaths: ['assetId'] });
|
2023-03-28 16:04:11 -04:00
|
|
|
asset = await this.assetCore.save({ id: asset.id, duration: durationString, fileCreatedAt });
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.STORAGE_TEMPLATE_MIGRATION_SINGLE, data: { asset } });
|
2023-04-11 08:53:42 -05:00
|
|
|
} catch (error: any) {
|
|
|
|
|
this.logger.error(
|
|
|
|
|
`Error in video metadata extraction due to ${error} for asset ${asset.id} at ${asset.originalPath}`,
|
|
|
|
|
error?.stack,
|
|
|
|
|
);
|
2022-08-21 06:31:37 +07:00
|
|
|
}
|
2022-06-19 08:16:35 -05:00
|
|
|
}
|
2022-07-04 13:44:43 -05:00
|
|
|
|
2023-04-11 08:53:42 -05:00
|
|
|
private async applyReverseGeocoding(asset: AssetEntity, newExif: ExifEntity) {
|
|
|
|
|
const { latitude, longitude } = newExif;
|
2023-04-04 18:23:07 -04:00
|
|
|
if (this.reverseGeocodingEnabled && longitude && latitude) {
|
|
|
|
|
try {
|
|
|
|
|
const { country, state, city } = await this.geocodingRepository.reverseGeocode({ latitude, longitude });
|
|
|
|
|
newExif.country = country;
|
|
|
|
|
newExif.state = state;
|
|
|
|
|
newExif.city = city;
|
|
|
|
|
} catch (error: any) {
|
2023-04-11 08:53:42 -05:00
|
|
|
this.logger.warn(
|
|
|
|
|
`Unable to run reverse geocoding due to ${error} for asset ${asset.id} at ${asset.originalPath}`,
|
|
|
|
|
error?.stack,
|
|
|
|
|
);
|
2023-04-04 18:23:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 17:49:22 -05:00
|
|
|
private extractDuration(duration: number | string | null) {
|
|
|
|
|
const videoDurationInSecond = Number(duration);
|
|
|
|
|
if (!videoDurationInSecond) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-07-04 13:44:43 -05:00
|
|
|
|
2023-03-23 15:27:29 +01:00
|
|
|
return Duration.fromObject({ seconds: videoDurationInSecond }).toFormat('hh:mm:ss.SSS');
|
2022-07-04 13:44:43 -05:00
|
|
|
}
|
2022-06-11 16:12:06 -05:00
|
|
|
}
|