2024-10-02 10:54:35 -04:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2024-04-19 11:50:13 -04:00
|
|
|
import { dirname } from 'node:path';
|
2024-09-28 13:47:24 -04:00
|
|
|
import { StorageCore } from 'src/cores/storage.core';
|
2024-09-27 10:28:42 -04:00
|
|
|
import { SystemConfigFFmpegDto } from 'src/dtos/system-config.dto';
|
|
|
|
|
import { AssetEntity } from 'src/entities/asset.entity';
|
2024-03-20 19:32:04 +01:00
|
|
|
import {
|
2024-09-27 10:28:42 -04:00
|
|
|
AssetFileType,
|
|
|
|
|
AssetPathType,
|
|
|
|
|
AssetType,
|
2024-03-20 19:32:04 +01:00
|
|
|
AudioCodec,
|
|
|
|
|
Colorspace,
|
2024-09-27 18:10:39 -04:00
|
|
|
LogLevel,
|
2024-09-27 10:28:42 -04:00
|
|
|
StorageFolder,
|
2024-03-20 19:32:04 +01:00
|
|
|
TranscodeHWAccel,
|
|
|
|
|
TranscodePolicy,
|
|
|
|
|
TranscodeTarget,
|
|
|
|
|
VideoCodec,
|
2024-07-21 17:14:23 -04:00
|
|
|
VideoContainer,
|
2024-09-27 10:28:42 -04:00
|
|
|
} from 'src/enum';
|
2024-10-02 10:54:35 -04:00
|
|
|
import { UpsertFileOptions, WithoutProperty } from 'src/interfaces/asset.interface';
|
2024-03-20 22:15:09 -05:00
|
|
|
import {
|
|
|
|
|
IBaseJob,
|
|
|
|
|
IEntityJob,
|
|
|
|
|
JOBS_ASSET_PAGINATION_SIZE,
|
|
|
|
|
JobItem,
|
|
|
|
|
JobName,
|
|
|
|
|
JobStatus,
|
|
|
|
|
QueueName,
|
2024-03-21 12:59:49 +01:00
|
|
|
} from 'src/interfaces/job.interface';
|
2024-10-02 10:54:35 -04:00
|
|
|
import { AudioStreamInfo, TranscodeCommand, VideoFormat, VideoStreamInfo } from 'src/interfaces/media.interface';
|
2024-09-30 17:31:21 -04:00
|
|
|
import { BaseService } from 'src/services/base.service';
|
2024-08-19 20:03:33 -04:00
|
|
|
import { getAssetFiles } from 'src/utils/asset.util';
|
2024-05-27 15:20:07 -04:00
|
|
|
import { BaseConfig, ThumbnailConfig } from 'src/utils/media';
|
2024-04-19 11:50:13 -04:00
|
|
|
import { mimeTypes } from 'src/utils/mime-types';
|
2024-03-20 22:15:09 -05:00
|
|
|
import { usePagination } from 'src/utils/pagination';
|
2023-09-08 08:49:43 +02:00
|
|
|
|
2023-02-25 09:12:03 -05:00
|
|
|
@Injectable()
|
2024-09-30 17:31:21 -04:00
|
|
|
export class MediaService extends BaseService {
|
2024-05-27 15:20:07 -04:00
|
|
|
private maliOpenCL?: boolean;
|
|
|
|
|
private devices?: string[];
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
async handleQueueGenerateThumbnails({ force }: IBaseJob): Promise<JobStatus> {
|
2023-05-26 15:43:24 -04:00
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => {
|
|
|
|
|
return force
|
2024-08-19 20:03:33 -04:00
|
|
|
? this.assetRepository.getAll(pagination, {
|
|
|
|
|
isVisible: true,
|
|
|
|
|
withDeleted: true,
|
|
|
|
|
withArchived: true,
|
|
|
|
|
})
|
2023-05-26 15:43:24 -04:00
|
|
|
: this.assetRepository.getWithout(pagination, WithoutProperty.THUMBNAIL);
|
|
|
|
|
});
|
2023-03-20 11:55:28 -04:00
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
for await (const assets of assetPagination) {
|
2024-01-01 15:45:42 -05:00
|
|
|
const jobs: JobItem[] = [];
|
|
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
for (const asset of assets) {
|
2024-08-19 20:03:33 -04:00
|
|
|
const { previewFile, thumbnailFile } = getAssetFiles(asset.files);
|
|
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
if (!previewFile || !thumbnailFile || !asset.thumbhash || force) {
|
|
|
|
|
jobs.push({ name: JobName.GENERATE_THUMBNAILS, data: { id: asset.id } });
|
2023-06-17 23:22:31 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-03-20 11:55:28 -04:00
|
|
|
}
|
2024-01-01 15:45:42 -05:00
|
|
|
|
|
|
|
|
await this.jobRepository.queueAll(jobs);
|
2023-03-20 11:55:28 -04:00
|
|
|
}
|
|
|
|
|
|
2024-01-01 15:45:42 -05:00
|
|
|
const jobs: JobItem[] = [];
|
2024-01-18 00:08:48 -05:00
|
|
|
const personPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) =>
|
|
|
|
|
this.personRepository.getAll(pagination, { where: force ? undefined : { thumbnailPath: '' } }),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for await (const people of personPagination) {
|
|
|
|
|
for (const person of people) {
|
|
|
|
|
if (!person.faceAssetId) {
|
|
|
|
|
const face = await this.personRepository.getRandomFace(person.id);
|
|
|
|
|
if (!face) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 09:48:29 -04:00
|
|
|
await this.personRepository.update({ id: person.id, faceAssetId: face.id });
|
2023-09-26 03:03:22 -04:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 00:08:48 -05:00
|
|
|
jobs.push({ name: JobName.GENERATE_PERSON_THUMBNAIL, data: { id: person.id } });
|
2023-09-08 08:49:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-01 15:45:42 -05:00
|
|
|
await this.jobRepository.queueAll(jobs);
|
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-05-26 15:43:24 -04:00
|
|
|
}
|
2023-04-11 20:28:25 -05:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
async handleQueueMigration(): Promise<JobStatus> {
|
2023-09-25 17:07:21 +02:00
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) =>
|
|
|
|
|
this.assetRepository.getAll(pagination),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const { active, waiting } = await this.jobRepository.getJobCounts(QueueName.MIGRATION);
|
|
|
|
|
if (active === 1 && waiting === 0) {
|
|
|
|
|
await this.storageCore.removeEmptyDirs(StorageFolder.THUMBNAILS);
|
|
|
|
|
await this.storageCore.removeEmptyDirs(StorageFolder.ENCODED_VIDEO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for await (const assets of assetPagination) {
|
2024-01-01 15:45:42 -05:00
|
|
|
await this.jobRepository.queueAll(
|
|
|
|
|
assets.map((asset) => ({ name: JobName.MIGRATE_ASSET, data: { id: asset.id } })),
|
|
|
|
|
);
|
2023-09-25 17:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 00:08:48 -05:00
|
|
|
const personPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) =>
|
|
|
|
|
this.personRepository.getAll(pagination),
|
2024-01-01 15:45:42 -05:00
|
|
|
);
|
2023-09-25 17:07:21 +02:00
|
|
|
|
2024-01-18 00:08:48 -05:00
|
|
|
for await (const people of personPagination) {
|
|
|
|
|
await this.jobRepository.queueAll(
|
|
|
|
|
people.map((person) => ({ name: JobName.MIGRATE_PERSON, data: { id: person.id } })),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-09-25 17:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
async handleAssetMigration({ id }: IEntityJob): Promise<JobStatus> {
|
2024-09-30 17:31:21 -04:00
|
|
|
const { image } = await this.getConfig({ withCache: true });
|
2024-08-19 20:03:33 -04:00
|
|
|
const [asset] = await this.assetRepository.getByIds([id], { files: true });
|
2023-09-25 17:07:21 +02:00
|
|
|
if (!asset) {
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.FAILED;
|
2023-09-25 17:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-28 02:01:04 -04:00
|
|
|
await this.storageCore.moveAssetImage(asset, AssetPathType.PREVIEW, image.preview.format);
|
|
|
|
|
await this.storageCore.moveAssetImage(asset, AssetPathType.THUMBNAIL, image.thumbnail.format);
|
2024-04-02 00:56:56 -04:00
|
|
|
await this.storageCore.moveAssetVideo(asset);
|
2023-09-25 17:07:21 +02:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-09-25 17:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
async handleGenerateThumbnails({ id }: IEntityJob): Promise<JobStatus> {
|
|
|
|
|
const asset = await this.assetRepository.getById(id, { exifInfo: true, files: true });
|
2023-04-11 20:28:25 -05:00
|
|
|
if (!asset) {
|
2024-09-28 13:47:24 -04:00
|
|
|
this.logger.warn(`Thumbnail generation failed for asset ${id}: not found`);
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.FAILED;
|
2023-04-11 20:28:25 -05:00
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2024-04-18 21:37:55 -04:00
|
|
|
if (!asset.isVisible) {
|
2024-09-28 13:47:24 -04:00
|
|
|
this.logger.verbose(`Thumbnail generation skipped for asset ${id}: not visible`);
|
2024-04-18 21:37:55 -04:00
|
|
|
return JobStatus.SKIPPED;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
let generated: { previewPath: string; thumbnailPath: string; thumbhash: Buffer };
|
|
|
|
|
if (asset.type === AssetType.IMAGE) {
|
|
|
|
|
generated = await this.generateImageThumbnails(asset);
|
|
|
|
|
} else if (asset.type === AssetType.VIDEO) {
|
|
|
|
|
generated = await this.generateVideoThumbnails(asset);
|
|
|
|
|
} else {
|
|
|
|
|
this.logger.warn(`Skipping thumbnail generation for asset ${id}: ${asset.type} is not an image or video`);
|
2024-08-19 13:50:00 -04:00
|
|
|
return JobStatus.SKIPPED;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
const { previewFile, thumbnailFile } = getAssetFiles(asset.files);
|
|
|
|
|
const toUpsert: UpsertFileOptions[] = [];
|
|
|
|
|
if (previewFile?.path !== generated.previewPath) {
|
|
|
|
|
toUpsert.push({ assetId: asset.id, path: generated.previewPath, type: AssetFileType.PREVIEW });
|
2024-04-27 18:43:05 -04:00
|
|
|
}
|
2024-08-19 13:50:00 -04:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
if (thumbnailFile?.path !== generated.thumbnailPath) {
|
|
|
|
|
toUpsert.push({ assetId: asset.id, path: generated.thumbnailPath, type: AssetFileType.THUMBNAIL });
|
2023-05-26 15:43:24 -04:00
|
|
|
}
|
2024-08-30 00:16:12 +02:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
if (toUpsert.length > 0) {
|
|
|
|
|
await this.assetRepository.upsertFiles(toUpsert);
|
|
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
const pathsToDelete = [];
|
|
|
|
|
if (previewFile && previewFile.path !== generated.previewPath) {
|
|
|
|
|
this.logger.debug(`Deleting old preview for asset ${asset.id}`);
|
|
|
|
|
pathsToDelete.push(previewFile.path);
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
if (thumbnailFile && thumbnailFile.path !== generated.thumbnailPath) {
|
|
|
|
|
this.logger.debug(`Deleting old thumbnail for asset ${asset.id}`);
|
|
|
|
|
pathsToDelete.push(thumbnailFile.path);
|
2024-04-18 21:37:55 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
if (pathsToDelete.length > 0) {
|
|
|
|
|
await Promise.all(pathsToDelete.map((path) => this.storageRepository.unlink(path)));
|
2024-08-19 13:50:00 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
if (asset.thumbhash != generated.thumbhash) {
|
|
|
|
|
await this.assetRepository.update({ id: asset.id, thumbhash: generated.thumbhash });
|
2024-04-27 18:43:05 -04:00
|
|
|
}
|
2024-08-19 13:50:00 -04:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
await this.assetRepository.upsertJobStatus({ assetId: asset.id, previewAt: new Date(), thumbnailAt: new Date() });
|
2024-08-19 13:50:00 -04:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|
2023-04-04 10:48:02 -04:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
private async generateImageThumbnails(asset: AssetEntity) {
|
2024-09-30 17:31:21 -04:00
|
|
|
const { image } = await this.getConfig({ withCache: true });
|
2024-09-28 13:47:24 -04:00
|
|
|
const previewPath = StorageCore.getImagePath(asset, AssetPathType.PREVIEW, image.preview.format);
|
|
|
|
|
const thumbnailPath = StorageCore.getImagePath(asset, AssetPathType.THUMBNAIL, image.thumbnail.format);
|
|
|
|
|
this.storageCore.ensureFolders(previewPath);
|
|
|
|
|
|
|
|
|
|
const shouldExtract = image.extractEmbedded && mimeTypes.isRaw(asset.originalPath);
|
|
|
|
|
const extractedPath = StorageCore.getTempPathInDir(dirname(previewPath));
|
|
|
|
|
const didExtract = shouldExtract && (await this.mediaRepository.extract(asset.originalPath, extractedPath));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const useExtracted = didExtract && (await this.shouldUseExtractedImage(extractedPath, image.preview.size));
|
|
|
|
|
const inputPath = useExtracted ? extractedPath : asset.originalPath;
|
|
|
|
|
const colorspace = this.isSRGB(asset) ? Colorspace.SRGB : image.colorspace;
|
|
|
|
|
const processInvalidImages = process.env.IMMICH_PROCESS_INVALID_IMAGES === 'true';
|
|
|
|
|
|
|
|
|
|
const decodeOptions = { colorspace, processInvalidImages, size: image.preview.size };
|
|
|
|
|
const { data, info } = await this.mediaRepository.decodeImage(inputPath, decodeOptions);
|
|
|
|
|
|
|
|
|
|
const options = { colorspace, processInvalidImages, raw: info };
|
|
|
|
|
const outputs = await Promise.all([
|
|
|
|
|
this.mediaRepository.generateThumbnail(data, { ...image.thumbnail, ...options }, thumbnailPath),
|
|
|
|
|
this.mediaRepository.generateThumbnail(data, { ...image.preview, ...options }, previewPath),
|
|
|
|
|
this.mediaRepository.generateThumbhash(data, options),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return { previewPath, thumbnailPath, thumbhash: outputs[2] };
|
|
|
|
|
} finally {
|
|
|
|
|
if (didExtract) {
|
|
|
|
|
await this.storageRepository.unlink(extractedPath);
|
|
|
|
|
}
|
2024-04-18 21:37:55 -04:00
|
|
|
}
|
2024-09-28 13:47:24 -04:00
|
|
|
}
|
2024-04-18 21:37:55 -04:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
private async generateVideoThumbnails(asset: AssetEntity) {
|
2024-09-30 17:31:21 -04:00
|
|
|
const { image, ffmpeg } = await this.getConfig({ withCache: true });
|
2024-09-28 13:47:24 -04:00
|
|
|
const previewPath = StorageCore.getImagePath(asset, AssetPathType.PREVIEW, image.preview.format);
|
|
|
|
|
const thumbnailPath = StorageCore.getImagePath(asset, AssetPathType.THUMBNAIL, image.thumbnail.format);
|
|
|
|
|
this.storageCore.ensureFolders(previewPath);
|
|
|
|
|
|
|
|
|
|
const { audioStreams, videoStreams } = await this.mediaRepository.probe(asset.originalPath);
|
|
|
|
|
const mainVideoStream = this.getMainStream(videoStreams);
|
|
|
|
|
if (!mainVideoStream) {
|
|
|
|
|
throw new Error(`No video streams found for asset ${asset.id}`);
|
2024-04-18 21:37:55 -04:00
|
|
|
}
|
2024-09-28 13:47:24 -04:00
|
|
|
const mainAudioStream = this.getMainStream(audioStreams);
|
2024-04-18 21:37:55 -04:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
const previewConfig = ThumbnailConfig.create({ ...ffmpeg, targetResolution: image.preview.size.toString() });
|
|
|
|
|
const thumbnailConfig = ThumbnailConfig.create({ ...ffmpeg, targetResolution: image.thumbnail.size.toString() });
|
2023-06-17 23:22:31 -04:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
const previewOptions = previewConfig.getCommand(TranscodeTarget.VIDEO, mainVideoStream, mainAudioStream);
|
|
|
|
|
const thumbnailOptions = thumbnailConfig.getCommand(TranscodeTarget.VIDEO, mainVideoStream, mainAudioStream);
|
|
|
|
|
await this.mediaRepository.transcode(asset.originalPath, previewPath, previewOptions);
|
|
|
|
|
await this.mediaRepository.transcode(asset.originalPath, thumbnailPath, thumbnailOptions);
|
2023-06-17 23:22:31 -04:00
|
|
|
|
2024-09-28 13:47:24 -04:00
|
|
|
const thumbhash = await this.mediaRepository.generateThumbhash(previewPath, {
|
|
|
|
|
colorspace: image.colorspace,
|
|
|
|
|
processInvalidImages: process.env.IMMICH_PROCESS_INVALID_IMAGES === 'true',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { previewPath, thumbnailPath, thumbhash };
|
2023-06-17 23:22:31 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
async handleQueueVideoConversion(job: IBaseJob): Promise<JobStatus> {
|
2023-04-04 10:48:02 -04:00
|
|
|
const { force } = job;
|
|
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => {
|
|
|
|
|
return force
|
|
|
|
|
? this.assetRepository.getAll(pagination, { type: AssetType.VIDEO })
|
|
|
|
|
: this.assetRepository.getWithout(pagination, WithoutProperty.ENCODED_VIDEO);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for await (const assets of assetPagination) {
|
2024-01-01 15:45:42 -05:00
|
|
|
await this.jobRepository.queueAll(
|
|
|
|
|
assets.map((asset) => ({ name: JobName.VIDEO_CONVERSION, data: { id: asset.id } })),
|
|
|
|
|
);
|
2023-04-04 10:48:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-05-26 15:43:24 -04:00
|
|
|
}
|
2023-04-11 08:56:52 -05:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
async handleVideoConversion({ id }: IEntityJob): Promise<JobStatus> {
|
2023-05-26 15:43:24 -04:00
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
2023-07-05 01:36:16 -04:00
|
|
|
if (!asset || asset.type !== AssetType.VIDEO) {
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.FAILED;
|
2023-04-11 08:56:52 -05:00
|
|
|
}
|
2023-04-04 10:48:02 -04:00
|
|
|
|
2023-05-26 15:43:24 -04:00
|
|
|
const input = asset.originalPath;
|
2023-10-23 17:52:21 +02:00
|
|
|
const output = StorageCore.getEncodedVideoPath(asset);
|
2023-10-11 04:14:44 +02:00
|
|
|
this.storageCore.ensureFolders(output);
|
2023-05-26 15:43:24 -04:00
|
|
|
|
2024-09-27 18:10:39 -04:00
|
|
|
const { videoStreams, audioStreams, format } = await this.mediaRepository.probe(input, {
|
|
|
|
|
countFrames: this.logger.isLevelEnabled(LogLevel.DEBUG), // makes frame count more reliable for progress logs
|
|
|
|
|
});
|
2023-08-29 05:01:42 -04:00
|
|
|
const mainVideoStream = this.getMainStream(videoStreams);
|
|
|
|
|
const mainAudioStream = this.getMainStream(audioStreams);
|
2024-07-21 17:14:23 -04:00
|
|
|
if (!mainVideoStream || !format.formatName) {
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.FAILED;
|
2023-05-26 15:43:24 -04:00
|
|
|
}
|
2023-04-04 10:48:02 -04:00
|
|
|
|
2023-12-28 00:34:00 -05:00
|
|
|
if (!mainVideoStream.height || !mainVideoStream.width) {
|
|
|
|
|
this.logger.warn(`Skipped transcoding for asset ${asset.id}: no video streams found`);
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.FAILED;
|
2023-12-28 00:34:00 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-30 17:31:21 -04:00
|
|
|
const { ffmpeg } = await this.getConfig({ withCache: true });
|
2024-05-27 15:20:07 -04:00
|
|
|
const target = this.getTranscodeTarget(ffmpeg, mainVideoStream, mainAudioStream);
|
2024-07-21 17:14:23 -04:00
|
|
|
if (target === TranscodeTarget.NONE && !this.isRemuxRequired(ffmpeg, format)) {
|
2023-12-28 00:34:00 -05:00
|
|
|
if (asset.encodedVideoPath) {
|
|
|
|
|
this.logger.log(`Transcoded video exists for asset ${asset.id}, but is no longer required. Deleting...`);
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.DELETE_FILES, data: { files: [asset.encodedVideoPath] } });
|
2024-03-19 22:42:10 -04:00
|
|
|
await this.assetRepository.update({ id: asset.id, encodedVideoPath: null });
|
2024-09-27 18:10:39 -04:00
|
|
|
} else {
|
|
|
|
|
this.logger.verbose(`Asset ${asset.id} does not require transcoding based on current policy, skipping`);
|
2023-12-28 00:34:00 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SKIPPED;
|
2023-05-26 15:43:24 -04:00
|
|
|
}
|
2023-04-04 10:48:02 -04:00
|
|
|
|
2024-09-27 18:10:39 -04:00
|
|
|
let command: TranscodeCommand;
|
2023-07-08 22:43:11 -04:00
|
|
|
try {
|
2024-05-27 15:20:07 -04:00
|
|
|
const config = BaseConfig.create(ffmpeg, await this.getDevices(), await this.hasMaliOpenCL());
|
|
|
|
|
command = config.getCommand(target, mainVideoStream, mainAudioStream);
|
2024-02-02 04:18:00 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
this.logger.error(`An error occurred while configuring transcoding options: ${error}`);
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.FAILED;
|
2023-07-08 22:43:11 -04:00
|
|
|
}
|
2023-04-06 04:32:59 +01:00
|
|
|
|
2024-09-27 18:10:39 -04:00
|
|
|
if (ffmpeg.accel === TranscodeHWAccel.DISABLED) {
|
|
|
|
|
this.logger.log(`Encoding video ${asset.id} without hardware acceleration`);
|
|
|
|
|
} else {
|
|
|
|
|
this.logger.log(`Encoding video ${asset.id} with ${ffmpeg.accel.toUpperCase()} acceleration`);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 21:56:10 -04:00
|
|
|
try {
|
2024-05-27 15:20:07 -04:00
|
|
|
await this.mediaRepository.transcode(input, output, command);
|
2024-09-27 18:10:39 -04:00
|
|
|
} catch (error: any) {
|
|
|
|
|
this.logger.error(`Error occurred during transcoding: ${error.message}`);
|
|
|
|
|
if (ffmpeg.accel === TranscodeHWAccel.DISABLED) {
|
|
|
|
|
return JobStatus.FAILED;
|
2023-08-01 21:56:10 -04:00
|
|
|
}
|
2024-09-27 18:10:39 -04:00
|
|
|
this.logger.error(`Retrying with ${ffmpeg.accel.toUpperCase()} acceleration disabled`);
|
2024-05-27 15:20:07 -04:00
|
|
|
const config = BaseConfig.create({ ...ffmpeg, accel: TranscodeHWAccel.DISABLED });
|
|
|
|
|
command = config.getCommand(target, mainVideoStream, mainAudioStream);
|
|
|
|
|
await this.mediaRepository.transcode(input, output, command);
|
2023-08-01 21:56:10 -04:00
|
|
|
}
|
2023-04-04 10:48:02 -04:00
|
|
|
|
2024-02-14 11:24:39 -05:00
|
|
|
this.logger.log(`Successfully encoded ${asset.id}`);
|
2023-04-04 10:48:02 -04:00
|
|
|
|
2024-03-19 22:42:10 -04:00
|
|
|
await this.assetRepository.update({ id: asset.id, encodedVideoPath: output });
|
2023-05-26 15:43:24 -04:00
|
|
|
|
2024-03-15 14:16:54 +01:00
|
|
|
return JobStatus.SUCCESS;
|
2023-04-04 10:48:02 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-29 05:01:42 -04:00
|
|
|
private getMainStream<T extends VideoStreamInfo | AudioStreamInfo>(streams: T[]): T {
|
2023-04-06 04:32:59 +01:00
|
|
|
return streams.sort((stream1, stream2) => stream2.frameCount - stream1.frameCount)[0];
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 11:24:39 -05:00
|
|
|
private getTranscodeTarget(
|
|
|
|
|
config: SystemConfigFFmpegDto,
|
2024-05-27 15:20:07 -04:00
|
|
|
videoStream?: VideoStreamInfo,
|
|
|
|
|
audioStream?: AudioStreamInfo,
|
2024-02-14 11:24:39 -05:00
|
|
|
): TranscodeTarget {
|
2024-05-27 15:20:07 -04:00
|
|
|
if (!videoStream && !audioStream) {
|
2024-02-14 11:24:39 -05:00
|
|
|
return TranscodeTarget.NONE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isAudioTranscodeRequired = this.isAudioTranscodeRequired(config, audioStream);
|
|
|
|
|
const isVideoTranscodeRequired = this.isVideoTranscodeRequired(config, videoStream);
|
|
|
|
|
|
|
|
|
|
if (isAudioTranscodeRequired && isVideoTranscodeRequired) {
|
|
|
|
|
return TranscodeTarget.ALL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isAudioTranscodeRequired) {
|
|
|
|
|
return TranscodeTarget.AUDIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isVideoTranscodeRequired) {
|
|
|
|
|
return TranscodeTarget.VIDEO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TranscodeTarget.NONE;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 15:20:07 -04:00
|
|
|
private isAudioTranscodeRequired(ffmpegConfig: SystemConfigFFmpegDto, stream?: AudioStreamInfo): boolean {
|
|
|
|
|
if (!stream) {
|
2024-02-14 11:24:39 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (ffmpegConfig.transcode) {
|
|
|
|
|
case TranscodePolicy.DISABLED: {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
case TranscodePolicy.ALL: {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case TranscodePolicy.REQUIRED:
|
|
|
|
|
case TranscodePolicy.OPTIMAL:
|
|
|
|
|
case TranscodePolicy.BITRATE: {
|
|
|
|
|
return !ffmpegConfig.acceptedAudioCodecs.includes(stream.codecName as AudioCodec);
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
throw new Error(`Unsupported transcode policy: ${ffmpegConfig.transcode}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 15:20:07 -04:00
|
|
|
private isVideoTranscodeRequired(ffmpegConfig: SystemConfigFFmpegDto, stream?: VideoStreamInfo): boolean {
|
|
|
|
|
if (!stream) {
|
2024-02-14 11:24:39 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-04-06 04:32:59 +01:00
|
|
|
|
2023-06-10 00:15:12 -04:00
|
|
|
const scalingEnabled = ffmpegConfig.targetResolution !== 'original';
|
|
|
|
|
const targetRes = Number.parseInt(ffmpegConfig.targetResolution);
|
2024-02-14 11:24:39 -05:00
|
|
|
const isLargerThanTargetRes = scalingEnabled && Math.min(stream.height, stream.width) > targetRes;
|
|
|
|
|
const isLargerThanTargetBitrate = stream.bitrate > this.parseBitrateToBps(ffmpegConfig.maxBitrate);
|
|
|
|
|
|
|
|
|
|
const isTargetVideoCodec = ffmpegConfig.acceptedVideoCodecs.includes(stream.codecName as VideoCodec);
|
|
|
|
|
const isRequired = !isTargetVideoCodec || stream.isHDR;
|
2023-04-04 10:48:02 -04:00
|
|
|
|
|
|
|
|
switch (ffmpegConfig.transcode) {
|
2024-02-02 04:18:00 +01:00
|
|
|
case TranscodePolicy.DISABLED: {
|
2023-04-06 04:32:59 +01:00
|
|
|
return false;
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
|
case TranscodePolicy.ALL: {
|
2023-04-04 10:48:02 -04:00
|
|
|
return true;
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
|
case TranscodePolicy.REQUIRED: {
|
2024-02-14 11:24:39 -05:00
|
|
|
return isRequired;
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
|
case TranscodePolicy.OPTIMAL: {
|
2024-02-14 11:24:39 -05:00
|
|
|
return isRequired || isLargerThanTargetRes;
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
|
case TranscodePolicy.BITRATE: {
|
2024-02-14 11:24:39 -05:00
|
|
|
return isRequired || isLargerThanTargetBitrate;
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
|
default: {
|
2024-02-14 11:24:39 -05:00
|
|
|
throw new Error(`Unsupported transcode policy: ${ffmpegConfig.transcode}`);
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
2023-04-04 10:48:02 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-21 17:14:23 -04:00
|
|
|
private isRemuxRequired(ffmpegConfig: SystemConfigFFmpegDto, { formatName, formatLongName }: VideoFormat): boolean {
|
|
|
|
|
if (ffmpegConfig.transcode === TranscodePolicy.DISABLED) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const name = formatLongName === 'QuickTime / MOV' ? VideoContainer.MOV : (formatName as VideoContainer);
|
|
|
|
|
return name !== VideoContainer.MP4 && !ffmpegConfig.acceptedContainers.includes(name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 19:18:47 -04:00
|
|
|
isSRGB(asset: AssetEntity): boolean {
|
|
|
|
|
const { colorspace, profileDescription, bitsPerSample } = asset.exifInfo ?? {};
|
|
|
|
|
if (colorspace || profileDescription) {
|
|
|
|
|
return [colorspace, profileDescription].some((s) => s?.toLowerCase().includes('srgb'));
|
|
|
|
|
} else if (bitsPerSample) {
|
|
|
|
|
// assume sRGB for 8-bit images with no color profile or colorspace metadata
|
|
|
|
|
return bitsPerSample === 8;
|
|
|
|
|
} else {
|
|
|
|
|
// assume sRGB for images with no relevant metadata
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-31 02:25:07 +01:00
|
|
|
|
2024-04-19 11:50:13 -04:00
|
|
|
private parseBitrateToBps(bitrateString: string) {
|
2024-01-31 02:25:07 +01:00
|
|
|
const bitrateValue = Number.parseInt(bitrateString);
|
|
|
|
|
|
2024-02-02 04:18:00 +01:00
|
|
|
if (Number.isNaN(bitrateValue)) {
|
2024-01-31 02:25:07 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bitrateString.toLowerCase().endsWith('k')) {
|
|
|
|
|
return bitrateValue * 1000; // Kilobits per second to bits per second
|
|
|
|
|
} else if (bitrateString.toLowerCase().endsWith('m')) {
|
2024-02-02 04:18:00 +01:00
|
|
|
return bitrateValue * 1_000_000; // Megabits per second to bits per second
|
2024-01-31 02:25:07 +01:00
|
|
|
} else {
|
|
|
|
|
return bitrateValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-19 11:50:13 -04:00
|
|
|
|
|
|
|
|
private async shouldUseExtractedImage(extractedPath: string, targetSize: number) {
|
|
|
|
|
const { width, height } = await this.mediaRepository.getImageDimensions(extractedPath);
|
|
|
|
|
const extractedSize = Math.min(width, height);
|
|
|
|
|
|
|
|
|
|
return extractedSize >= targetSize;
|
|
|
|
|
}
|
2024-05-10 15:03:47 -04:00
|
|
|
|
|
|
|
|
private async getDevices() {
|
|
|
|
|
if (!this.devices) {
|
2024-05-27 15:20:07 -04:00
|
|
|
try {
|
|
|
|
|
this.devices = await this.storageRepository.readdir('/dev/dri');
|
|
|
|
|
} catch {
|
|
|
|
|
this.logger.debug('No devices found in /dev/dri.');
|
|
|
|
|
this.devices = [];
|
|
|
|
|
}
|
2024-05-10 15:03:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.devices;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 15:20:07 -04:00
|
|
|
private async hasMaliOpenCL() {
|
|
|
|
|
if (this.maliOpenCL === undefined) {
|
2024-05-10 15:03:47 -04:00
|
|
|
try {
|
|
|
|
|
const maliIcdStat = await this.storageRepository.stat('/etc/OpenCL/vendors/mali.icd');
|
|
|
|
|
const maliDeviceStat = await this.storageRepository.stat('/dev/mali0');
|
2024-05-27 15:20:07 -04:00
|
|
|
this.maliOpenCL = maliIcdStat.isFile() && maliDeviceStat.isCharacterDevice();
|
2024-05-10 15:03:47 -04:00
|
|
|
} catch {
|
2024-09-27 18:10:39 -04:00
|
|
|
this.logger.debug('OpenCL not available for transcoding, so RKMPP acceleration will use CPU decoding');
|
2024-05-27 15:20:07 -04:00
|
|
|
this.maliOpenCL = false;
|
2024-05-10 15:03:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 15:20:07 -04:00
|
|
|
return this.maliOpenCL;
|
2024-05-10 15:03:47 -04:00
|
|
|
}
|
2023-02-25 09:12:03 -05:00
|
|
|
}
|