1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-04-21 12:16:59 +02:00
pigallery2/src/backend/model/threading/MetadataLoader.ts

299 lines
12 KiB
TypeScript
Raw Normal View History

2018-12-20 23:02:49 +01:00
import {VideoMetadata} from '../../../common/entities/VideoDTO';
2019-01-12 16:41:45 +01:00
import {FaceRegion, PhotoMetadata} from '../../../common/entities/PhotoDTO';
2018-12-20 23:02:49 +01:00
import {Config} from '../../../common/config/private/Config';
import {Logger} from '../../Logger';
import * as fs from 'fs';
2019-12-07 20:21:23 +01:00
import {imageSize} from 'image-size';
// @ts-ignore
import * as ExifReader from 'exifreader';
2019-01-13 20:18:18 +01:00
import {ExifParserFactory, OrientationTypes} from 'ts-exif-parser';
2018-12-20 23:02:49 +01:00
import {IptcParser} from 'ts-node-iptc';
import {FFmpegFactory} from '../FFmpegFactory';
import {FfprobeData} from 'fluent-ffmpeg';
2019-02-02 22:22:51 -05:00
import {Utils} from '../../../common/Utils';
2018-12-20 23:02:49 +01:00
2019-01-12 16:41:45 +01:00
2018-12-20 23:02:49 +01:00
const LOG_TAG = '[MetadataLoader]';
const ffmpeg = FFmpegFactory.get();
export class MetadataLoader {
public static loadVideoMetadata(fullPath: string): Promise<VideoMetadata> {
return new Promise<VideoMetadata>((resolve) => {
2018-12-20 23:02:49 +01:00
const metadata: VideoMetadata = {
size: {
width: 1,
height: 1
},
bitRate: 0,
duration: 0,
creationDate: 0,
fileSize: 0,
fps: 0
2018-12-20 23:02:49 +01:00
};
try {
const stat = fs.statSync(fullPath);
metadata.fileSize = stat.size;
metadata.creationDate = stat.mtime.getTime();
2018-12-20 23:02:49 +01:00
} catch (err) {
}
try {
ffmpeg(fullPath).ffprobe((err: any, data: FfprobeData) => {
if (!!err || data === null || !data.streams[0]) {
return resolve(metadata);
}
2018-12-20 23:02:49 +01:00
try {
for (let i = 0; i < data.streams.length; i++) {
if (data.streams[i].width) {
metadata.size.width = data.streams[i].width;
metadata.size.height = data.streams[i].height;
2018-12-20 23:02:49 +01:00
if (Utils.isInt32(parseInt(data.streams[i].rotation, 10)) &&
(Math.abs(parseInt(data.streams[i].rotation, 10)) / 90) % 2 === 1) {
// noinspection JSSuspiciousNameCombination
metadata.size.width = data.streams[i].height;
// noinspection JSSuspiciousNameCombination
metadata.size.height = data.streams[i].width;
}
2019-12-07 20:21:23 +01:00
if (Utils.isInt32(Math.floor(parseFloat(data.streams[i].duration) * 1000))) {
metadata.duration = Math.floor(parseFloat(data.streams[i].duration) * 1000);
}
2019-02-02 22:22:51 -05:00
if (Utils.isInt32(parseInt(data.streams[i].bit_rate, 10))) {
2019-02-15 16:19:30 -05:00
metadata.bitRate = parseInt(data.streams[i].bit_rate, 10) || null;
}
if (Utils.isInt32(parseInt(data.streams[i].avg_frame_rate, 10))) {
metadata.fps = parseInt(data.streams[i].avg_frame_rate, 10) || null;
}
metadata.creationDate = Date.parse(data.streams[i].tags.creation_time) || metadata.creationDate;
break;
2019-02-02 22:22:51 -05:00
}
2018-12-20 23:02:49 +01:00
}
} catch (err) {
}
metadata.creationDate = metadata.creationDate || 0;
2018-12-20 23:02:49 +01:00
return resolve(metadata);
});
} catch (e) {
2018-12-20 23:02:49 +01:00
return resolve(metadata);
}
2018-12-20 23:02:49 +01:00
});
}
public static loadPhotoMetadata(fullPath: string): Promise<PhotoMetadata> {
return new Promise<PhotoMetadata>((resolve, reject) => {
const fd = fs.openSync(fullPath, 'r');
const data = Buffer.allocUnsafe(Config.Server.photoMetadataSize);
fs.read(fd, data, 0, Config.Server.photoMetadataSize, 0, (err) => {
2018-12-22 00:09:07 +01:00
fs.closeSync(fd);
2018-12-20 23:02:49 +01:00
if (err) {
return reject({file: fullPath, error: err});
}
const metadata: PhotoMetadata = {
size: {width: 1, height: 1},
orientation: OrientationTypes.TOP_LEFT,
creationDate: 0,
fileSize: 0
};
try {
try {
const stat = fs.statSync(fullPath);
metadata.fileSize = stat.size;
metadata.creationDate = stat.mtime.getTime();
2018-12-20 23:02:49 +01:00
} catch (err) {
}
try {
const exif = ExifParserFactory.create(data).parse();
if (exif.tags.ISO || exif.tags.Model ||
exif.tags.Make || exif.tags.FNumber ||
exif.tags.ExposureTime || exif.tags.FocalLength ||
exif.tags.LensModel) {
metadata.cameraData = {
model: exif.tags.Model,
make: exif.tags.Make,
2019-02-02 22:22:51 -05:00
lens: exif.tags.LensModel
2018-12-20 23:02:49 +01:00
};
2019-02-02 22:22:51 -05:00
if (Utils.isUInt32(exif.tags.ISO)) {
metadata.cameraData.ISO = exif.tags.ISO;
}
if (Utils.isFloat32(exif.tags.ISO)) {
metadata.cameraData.focalLength = exif.tags.FocalLength;
}
if (Utils.isFloat32(exif.tags.ExposureTime)) {
metadata.cameraData.exposure = exif.tags.ExposureTime;
}
if (Utils.isFloat32(exif.tags.FNumber)) {
metadata.cameraData.fStop = exif.tags.FNumber;
}
2018-12-20 23:02:49 +01:00
}
if (!isNaN(exif.tags.GPSLatitude) || exif.tags.GPSLongitude || exif.tags.GPSAltitude) {
metadata.positionData = metadata.positionData || {};
2019-02-02 22:22:51 -05:00
metadata.positionData.GPSData = {};
if (Utils.isFloat32(exif.tags.GPSLongitude)) {
metadata.positionData.GPSData.longitude = exif.tags.GPSLongitude;
}
if (Utils.isFloat32(exif.tags.GPSLatitude)) {
metadata.positionData.GPSData.latitude = exif.tags.GPSLatitude;
}
if (Utils.isInt32(exif.tags.GPSAltitude)) {
metadata.positionData.GPSData.altitude = exif.tags.GPSAltitude;
}
2018-12-20 23:02:49 +01:00
}
if (exif.tags.CreateDate || exif.tags.DateTimeOriginal || exif.tags.ModifyDate) {
2020-02-08 16:31:29 +01:00
metadata.creationDate = (exif.tags.DateTimeOriginal || exif.tags.CreateDate || exif.tags.ModifyDate) * 1000;
2018-12-20 23:02:49 +01:00
}
if (exif.tags.Orientation) {
metadata.orientation = exif.tags.Orientation;
}
if (exif.imageSize) {
metadata.size = {width: exif.imageSize.width, height: exif.imageSize.height};
} else if (exif.tags.RelatedImageWidth && exif.tags.RelatedImageHeight) {
metadata.size = {width: exif.tags.RelatedImageWidth, height: exif.tags.RelatedImageHeight};
} else {
2019-12-07 20:21:23 +01:00
const info = imageSize(fullPath);
2018-12-20 23:02:49 +01:00
metadata.size = {width: info.width, height: info.height};
}
if (OrientationTypes.BOTTOM_LEFT < metadata.orientation &&
metadata.orientation <= OrientationTypes.LEFT_BOTTOM &&
metadata.size.width > metadata.size.height) {
// noinspection JSSuspiciousNameCombination
const height = metadata.size.width;
// noinspection JSSuspiciousNameCombination
metadata.size.width = metadata.size.height;
metadata.size.height = height;
}
2018-12-20 23:02:49 +01:00
} catch (err) {
Logger.debug(LOG_TAG, 'Error parsing exif', fullPath, err);
try {
2019-12-07 20:21:23 +01:00
const info = imageSize(fullPath);
2018-12-20 23:02:49 +01:00
metadata.size = {width: info.width, height: info.height};
} catch (e) {
metadata.size = {width: 1, height: 1};
}
}
try {
const iptcData = IptcParser.parse(data);
2019-01-13 20:18:18 +01:00
if (iptcData.country_or_primary_location_name) {
2018-12-20 23:02:49 +01:00
metadata.positionData = metadata.positionData || {};
metadata.positionData.country = iptcData.country_or_primary_location_name.replace(/\0/g, '').trim();
2019-01-13 20:18:18 +01:00
}
if (iptcData.province_or_state) {
metadata.positionData = metadata.positionData || {};
2018-12-20 23:02:49 +01:00
metadata.positionData.state = iptcData.province_or_state.replace(/\0/g, '').trim();
2019-01-13 20:18:18 +01:00
}
if (iptcData.city) {
metadata.positionData = metadata.positionData || {};
2018-12-20 23:02:49 +01:00
metadata.positionData.city = iptcData.city.replace(/\0/g, '').trim();
}
if (iptcData.caption) {
metadata.caption = iptcData.caption.replace(/\0/g, '').trim();
}
metadata.keywords = iptcData.keywords || [];
2019-01-19 10:07:06 +01:00
2018-12-20 23:02:49 +01:00
metadata.creationDate = <number>(iptcData.date_time ? iptcData.date_time.getTime() : metadata.creationDate);
} catch (err) {
2019-01-19 10:07:06 +01:00
// Logger.debug(LOG_TAG, 'Error parsing iptc data', fullPath, err);
2018-12-20 23:02:49 +01:00
}
metadata.creationDate = metadata.creationDate || 0;
try {
// TODO: clean up the three different exif readers,
// and keep the minimum amount only
const exif = ExifReader.load(data);
if (exif.Rating) {
metadata.rating = <any>parseInt(exif.Rating.value, 10);
}
if (Config.Client.Faces.enabled) {
2019-02-04 17:46:27 -05:00
const faces: FaceRegion[] = [];
if (exif.Regions && exif.Regions.value.RegionList && exif.Regions.value.RegionList.value) {
for (let i = 0; i < exif.Regions.value.RegionList.value.length; i++) {
let type, name, box;
const regionRoot = exif.Regions.value.RegionList.value[i] as any;
const createFaceBox = (w: string, h: string, x: string, y: string) => {
return {
width: Math.round(parseFloat(w) * metadata.size.width),
height: Math.round(parseFloat(h) * metadata.size.height),
left: Math.round(parseFloat(x) * metadata.size.width),
top: Math.round(parseFloat(y) * metadata.size.height)
2020-10-28 14:02:59 +01:00
};
};
/* Adobe Lightroom based face region structure*/
if (regionRoot.value &&
regionRoot.value['rdf:Description'] &&
regionRoot.value['rdf:Description'].value &&
regionRoot.value['rdf:Description'].value['mwg-rs:Area']) {
const region = regionRoot.value['rdf:Description'];
const regionBox = region.value['mwg-rs:Area'].attributes;
name = region.attributes['mwg-rs:Name'];
type = region.attributes['mwg-rs:Type'];
box = createFaceBox(regionBox['stArea:w'],
regionBox['stArea:h'],
regionBox['stArea:x'],
regionBox['stArea:y']);
/* Load exiftool edited face region structure, see github issue #191 */
} else if (regionRoot.Area && regionRoot.Name && regionRoot.Type) {
const regionBox = regionRoot.Area.value;
name = regionRoot.Name.value;
type = regionRoot.Type.value;
box = createFaceBox(regionBox.w.value,
regionBox.h.value,
regionBox.x.value,
regionBox.y.value);
}
if (type !== 'Face' || !name) {
continue;
2019-02-04 17:46:27 -05:00
}
// convert center base box to corner based box
box.left = Math.max(0, box.left - box.width / 2);
box.top = Math.max(0, box.top - box.height / 2);
faces.push({name: name, box: box});
2019-01-12 16:41:45 +01:00
}
}
2019-02-04 17:46:27 -05:00
if (Config.Client.Faces.keywordsToPersons && faces.length > 0) {
metadata.faces = faces; // save faces
// remove faces from keywords
metadata.faces.forEach(f => {
const index = metadata.keywords.indexOf(f.name);
if (index !== -1) {
metadata.keywords.splice(index, 1);
}
});
}
2019-01-12 16:41:45 +01:00
}
} catch (err) {
2019-01-12 16:41:45 +01:00
}
2018-12-20 23:02:49 +01:00
return resolve(metadata);
} catch (err) {
return reject({file: fullPath, error: err});
}
});
}
);
}
2019-02-02 22:22:51 -05:00
2018-12-20 23:02:49 +01:00
}