From 97d9b80baacf14f100dbebb98a65160d9b5b15da Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Jul 2022 13:44:43 -0500 Subject: [PATCH] Added creation date for video from ffmpeg.prob (#303) --- .../processors/asset-uploaded.processor.ts | 6 +-- .../metadata-extraction.processor.ts | 44 +++++++++++++------ .../job/src/constants/job-name.constant.ts | 2 +- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/server/apps/microservices/src/processors/asset-uploaded.processor.ts b/server/apps/microservices/src/processors/asset-uploaded.processor.ts index 7172c43e4d..2aa5735d23 100644 --- a/server/apps/microservices/src/processors/asset-uploaded.processor.ts +++ b/server/apps/microservices/src/processors/asset-uploaded.processor.ts @@ -15,7 +15,7 @@ import { exifExtractionProcessorName, generateJPEGThumbnailProcessorName, mp4ConversionProcessorName, - videoLengthExtractionProcessorName, + videoMetadataExtractionProcessorName, } from '@app/job'; @Processor(assetUploadedQueueName) @@ -62,9 +62,9 @@ export class AssetUploadedProcessor { ); } - // Extract video duration if uploaded from the web + // Extract video duration if uploaded from the web & CLI if (asset.type == AssetType.VIDEO && asset.duration == '0:00:00.000000') { - await this.metadataExtractionQueue.add(videoLengthExtractionProcessorName, { asset }, { jobId: randomUUID() }); + await this.metadataExtractionQueue.add(videoMetadataExtractionProcessorName, { asset }, { jobId: randomUUID() }); } } } diff --git a/server/apps/microservices/src/processors/metadata-extraction.processor.ts b/server/apps/microservices/src/processors/metadata-extraction.processor.ts index f03a8e1b0c..a74f77e9be 100644 --- a/server/apps/microservices/src/processors/metadata-extraction.processor.ts +++ b/server/apps/microservices/src/processors/metadata-extraction.processor.ts @@ -19,7 +19,7 @@ import { exifExtractionProcessorName, imageTaggingProcessorName, objectDetectionProcessorName, - videoLengthExtractionProcessorName, + videoMetadataExtractionProcessorName, metadataExtractionQueueName, } from '@app/job'; @@ -140,26 +140,42 @@ export class MetadataExtractionProcessor { } } - @Process({ name: videoLengthExtractionProcessorName, concurrency: 2 }) - async extractVideoLength(job: Job) { + @Process({ name: videoMetadataExtractionProcessorName, concurrency: 2 }) + async extractVideoMetadata(job: Job) { const { asset } = job.data; ffmpeg.ffprobe(asset.originalPath, async (err, data) => { if (!err) { + let durationString = asset.duration; + let createdAt = asset.createdAt; + if (data.format.duration) { - const videoDurationInSecond = parseInt(data.format.duration.toString(), 0); - - const hours = Math.floor(videoDurationInSecond / 3600); - const minutes = Math.floor((videoDurationInSecond - hours * 3600) / 60); - const seconds = videoDurationInSecond - hours * 3600 - minutes * 60; - - const durationString = `${hours}:${minutes < 10 ? '0' + minutes.toString() : minutes}:${ - seconds < 10 ? '0' + seconds.toString() : seconds - }.000000`; - - await this.assetRepository.update({ id: asset.id }, { duration: durationString }); + durationString = this.extractDuration(data.format.duration); } + + const videoTags = data.format.tags; + if (videoTags) { + if (videoTags['com.apple.quicktime.creationdate']) { + createdAt = String(videoTags['com.apple.quicktime.creationdate']); + } else { + createdAt = String(videoTags['creation_time']); + } + } + + await this.assetRepository.update({ id: asset.id }, { duration: durationString, createdAt: createdAt }); } }); } + + private extractDuration(duration: number) { + const videoDurationInSecond = parseInt(duration.toString(), 0); + + const hours = Math.floor(videoDurationInSecond / 3600); + const minutes = Math.floor((videoDurationInSecond - hours * 3600) / 60); + const seconds = videoDurationInSecond - hours * 3600 - minutes * 60; + + return `${hours}:${minutes < 10 ? '0' + minutes.toString() : minutes}:${ + seconds < 10 ? '0' + seconds.toString() : seconds + }.000000`; + } } diff --git a/server/libs/job/src/constants/job-name.constant.ts b/server/libs/job/src/constants/job-name.constant.ts index a58f97e5ef..2cb6796ff2 100644 --- a/server/libs/job/src/constants/job-name.constant.ts +++ b/server/libs/job/src/constants/job-name.constant.ts @@ -18,6 +18,6 @@ export const generateWEBPThumbnailProcessorName = 'generate-webp-thumbnail'; * Metadata Extraction Queue Jobs */ export const exifExtractionProcessorName = 'exif-extraction'; -export const videoLengthExtractionProcessorName = 'extract-video-length'; +export const videoMetadataExtractionProcessorName = 'extract-video-metadata'; export const objectDetectionProcessorName = 'detect-object'; export const imageTaggingProcessorName = 'tag-image';