2023-07-09 04:43:11 +02:00
|
|
|
import { AssetEntity, AssetType, TranscodePolicy, VideoCodec } from '@app/infra/entities';
|
|
|
|
import { Inject, Injectable, Logger, UnsupportedMediaTypeException } from '@nestjs/common';
|
2023-02-25 15:12:03 +01:00
|
|
|
import { join } from 'path';
|
2023-05-26 21:43:24 +02:00
|
|
|
import { IAssetRepository, WithoutProperty } from '../asset';
|
2023-05-22 20:05:06 +02:00
|
|
|
import { usePagination } from '../domain.util';
|
2023-05-26 21:43:24 +02:00
|
|
|
import { IBaseJob, IEntityJob, IJobRepository, JobName, JOBS_ASSET_PAGINATION_SIZE } from '../job';
|
2023-03-25 15:50:57 +01:00
|
|
|
import { IStorageRepository, StorageCore, StorageFolder } from '../storage';
|
2023-04-04 16:48:02 +02:00
|
|
|
import { ISystemConfigRepository, SystemConfigFFmpegDto } from '../system-config';
|
|
|
|
import { SystemConfigCore } from '../system-config/system-config.core';
|
2023-05-17 19:07:17 +02:00
|
|
|
import { JPEG_THUMBNAIL_SIZE, WEBP_THUMBNAIL_SIZE } from './media.constant';
|
2023-04-06 05:32:59 +02:00
|
|
|
import { AudioStreamInfo, IMediaRepository, VideoStreamInfo } from './media.repository';
|
2023-07-09 04:43:11 +02:00
|
|
|
import { H264Config, HEVCConfig, VP9Config } from './media.util';
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MediaService {
|
|
|
|
private logger = new Logger(MediaService.name);
|
2023-03-25 15:50:57 +01:00
|
|
|
private storageCore = new StorageCore();
|
2023-04-04 16:48:02 +02:00
|
|
|
private configCore: SystemConfigCore;
|
2023-02-25 15:12:03 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
|
|
|
@Inject(IMediaRepository) private mediaRepository: IMediaRepository,
|
|
|
|
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
2023-04-04 16:48:02 +02:00
|
|
|
@Inject(ISystemConfigRepository) systemConfig: ISystemConfigRepository,
|
|
|
|
) {
|
|
|
|
this.configCore = new SystemConfigCore(systemConfig);
|
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
async handleQueueGenerateThumbnails(job: IBaseJob) {
|
|
|
|
const { force } = job;
|
2023-03-20 16:55:28 +01:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) => {
|
|
|
|
return force
|
|
|
|
? this.assetRepository.getAll(pagination)
|
|
|
|
: this.assetRepository.getWithout(pagination, WithoutProperty.THUMBNAIL);
|
|
|
|
});
|
2023-03-20 16:55:28 +01:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
for await (const assets of assetPagination) {
|
|
|
|
for (const asset of assets) {
|
2023-06-18 05:22:31 +02:00
|
|
|
if (!asset.resizePath || force) {
|
|
|
|
await this.jobRepository.queue({ name: JobName.GENERATE_JPEG_THUMBNAIL, data: { id: asset.id } });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!asset.webpPath) {
|
|
|
|
await this.jobRepository.queue({ name: JobName.GENERATE_WEBP_THUMBNAIL, data: { id: asset.id } });
|
|
|
|
}
|
|
|
|
if (!asset.thumbhash) {
|
|
|
|
await this.jobRepository.queue({ name: JobName.GENERATE_THUMBHASH_THUMBNAIL, data: { id: asset.id } });
|
|
|
|
}
|
2023-03-20 16:55:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-04-12 03:28:25 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
async handleGenerateJpegThumbnail({ id }: IEntityJob) {
|
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
2023-04-12 03:28:25 +02:00
|
|
|
if (!asset) {
|
2023-05-26 21:43:24 +02:00
|
|
|
return false;
|
2023-04-12 03:28:25 +02:00
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
const resizePath = this.storageCore.getFolderLocation(StorageFolder.THUMBNAILS, asset.ownerId);
|
|
|
|
this.storageRepository.mkdirSync(resizePath);
|
|
|
|
const jpegThumbnailPath = join(resizePath, `${asset.id}.jpeg`);
|
|
|
|
|
2023-06-15 05:42:35 +02:00
|
|
|
switch (asset.type) {
|
|
|
|
case AssetType.IMAGE:
|
2023-05-26 21:43:24 +02:00
|
|
|
await this.mediaRepository.resize(asset.originalPath, jpegThumbnailPath, {
|
|
|
|
size: JPEG_THUMBNAIL_SIZE,
|
|
|
|
format: 'jpeg',
|
|
|
|
});
|
2023-06-15 05:42:35 +02:00
|
|
|
break;
|
|
|
|
case AssetType.VIDEO:
|
|
|
|
this.logger.log('Generating video thumbnail');
|
|
|
|
await this.mediaRepository.extractVideoThumbnail(asset.originalPath, jpegThumbnailPath, JPEG_THUMBNAIL_SIZE);
|
|
|
|
this.logger.log(`Successfully generated video thumbnail ${asset.id}`);
|
|
|
|
break;
|
2023-05-26 21:43:24 +02:00
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
await this.assetRepository.save({ id: asset.id, resizePath: jpegThumbnailPath });
|
2023-02-25 15:12:03 +01:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
return true;
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
|
|
|
|
2023-07-09 04:43:11 +02:00
|
|
|
async handleGenerateWebpThumbnail({ id }: IEntityJob) {
|
2023-05-26 21:43:24 +02:00
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
|
|
|
if (!asset || !asset.resizePath) {
|
|
|
|
return false;
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
|
|
|
|
2023-07-10 19:56:45 +02:00
|
|
|
const webpPath = asset.resizePath.replace('jpeg', 'webp').replace('jpg', 'webp');
|
2023-02-25 15:12:03 +01:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
await this.mediaRepository.resize(asset.resizePath, webpPath, { size: WEBP_THUMBNAIL_SIZE, format: 'webp' });
|
2023-07-10 19:56:45 +02:00
|
|
|
await this.assetRepository.save({ id: asset.id, webpPath });
|
2023-05-26 21:43:24 +02:00
|
|
|
|
|
|
|
return true;
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
2023-04-04 16:48:02 +02:00
|
|
|
|
2023-06-18 05:22:31 +02:00
|
|
|
async handleGenerateThumbhashThumbnail({ id }: IEntityJob): Promise<boolean> {
|
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
|
|
|
if (!asset?.resizePath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const thumbhash = await this.mediaRepository.generateThumbhash(asset.resizePath);
|
|
|
|
await this.assetRepository.save({ id: asset.id, thumbhash });
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-04-04 16:48:02 +02:00
|
|
|
async handleQueueVideoConversion(job: IBaseJob) {
|
|
|
|
const { force } = job;
|
|
|
|
|
2023-05-26 21:43:24 +02: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) {
|
|
|
|
for (const asset of assets) {
|
|
|
|
await this.jobRepository.queue({ name: JobName.VIDEO_CONVERSION, data: { id: asset.id } });
|
2023-04-04 16:48:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-04-11 15:56:52 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
async handleVideoConversion({ id }: IEntityJob) {
|
|
|
|
const [asset] = await this.assetRepository.getByIds([id]);
|
2023-07-05 07:36:16 +02:00
|
|
|
if (!asset || asset.type !== AssetType.VIDEO) {
|
2023-05-26 21:43:24 +02:00
|
|
|
return false;
|
2023-04-11 15:56:52 +02:00
|
|
|
}
|
2023-04-04 16:48:02 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
const input = asset.originalPath;
|
|
|
|
const outputFolder = this.storageCore.getFolderLocation(StorageFolder.ENCODED_VIDEO, asset.ownerId);
|
|
|
|
const output = join(outputFolder, `${asset.id}.mp4`);
|
|
|
|
this.storageRepository.mkdirSync(outputFolder);
|
|
|
|
|
|
|
|
const { videoStreams, audioStreams, format } = await this.mediaRepository.probe(input);
|
|
|
|
const mainVideoStream = this.getMainVideoStream(videoStreams);
|
|
|
|
const mainAudioStream = this.getMainAudioStream(audioStreams);
|
|
|
|
const containerExtension = format.formatName;
|
2023-07-09 22:15:34 +02:00
|
|
|
if (!mainVideoStream || !containerExtension) {
|
2023-05-26 21:43:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-04-04 16:48:02 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
const { ffmpeg: config } = await this.configCore.getConfig();
|
2023-04-04 16:48:02 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
const required = this.isTranscodeRequired(asset, mainVideoStream, mainAudioStream, containerExtension, config);
|
|
|
|
if (!required) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-04 16:48:02 +02:00
|
|
|
|
2023-07-09 04:43:11 +02:00
|
|
|
let transcodeOptions;
|
|
|
|
try {
|
|
|
|
transcodeOptions = this.getCodecConfig(config).getOptions(mainVideoStream);
|
|
|
|
} catch (err) {
|
|
|
|
this.logger.error(`An error occurred while configuring transcoding options: ${err}`);
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-06 05:32:59 +02:00
|
|
|
|
2023-07-09 04:43:11 +02:00
|
|
|
this.logger.log(`Start encoding video ${asset.id} ${JSON.stringify(transcodeOptions)}`);
|
|
|
|
await this.mediaRepository.transcode(input, output, transcodeOptions);
|
2023-04-04 16:48:02 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
this.logger.log(`Encoding success ${asset.id}`);
|
2023-04-04 16:48:02 +02:00
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
await this.assetRepository.save({ id: asset.id, encodedVideoPath: output });
|
|
|
|
|
|
|
|
return true;
|
2023-04-04 16:48:02 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 05:32:59 +02:00
|
|
|
private getMainVideoStream(streams: VideoStreamInfo[]): VideoStreamInfo | null {
|
|
|
|
return streams.sort((stream1, stream2) => stream2.frameCount - stream1.frameCount)[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
private getMainAudioStream(streams: AudioStreamInfo[]): AudioStreamInfo | null {
|
|
|
|
return streams[0];
|
2023-04-04 16:48:02 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 05:32:59 +02:00
|
|
|
private isTranscodeRequired(
|
2023-04-09 03:35:54 +02:00
|
|
|
asset: AssetEntity,
|
2023-04-06 05:32:59 +02:00
|
|
|
videoStream: VideoStreamInfo,
|
2023-07-09 22:15:34 +02:00
|
|
|
audioStream: AudioStreamInfo | null,
|
2023-04-06 05:32:59 +02:00
|
|
|
containerExtension: string,
|
|
|
|
ffmpegConfig: SystemConfigFFmpegDto,
|
|
|
|
): boolean {
|
|
|
|
if (!videoStream.height || !videoStream.width) {
|
2023-04-04 16:48:02 +02:00
|
|
|
this.logger.error('Skipping transcode, height or width undefined for video stream');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-06 05:32:59 +02:00
|
|
|
const isTargetVideoCodec = videoStream.codecName === ffmpegConfig.targetVideoCodec;
|
|
|
|
const isTargetContainer = ['mov,mp4,m4a,3gp,3g2,mj2', 'mp4', 'mov'].includes(containerExtension);
|
2023-07-09 22:15:34 +02:00
|
|
|
const isTargetAudioCodec = audioStream == null || audioStream.codecName === ffmpegConfig.targetAudioCodec;
|
|
|
|
|
|
|
|
if (audioStream != null) {
|
|
|
|
this.logger.verbose(
|
|
|
|
`${asset.id}: AudioCodecName ${audioStream.codecName}, AudioStreamCodecType ${audioStream.codecType}, containerExtension ${containerExtension}`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.logger.verbose(
|
|
|
|
`${asset.id}: AudioCodecName None, AudioStreamCodecType None, containerExtension ${containerExtension}`,
|
|
|
|
);
|
|
|
|
}
|
2023-04-06 05:32:59 +02:00
|
|
|
|
|
|
|
const allTargetsMatching = isTargetVideoCodec && isTargetAudioCodec && isTargetContainer;
|
2023-06-10 06:15:12 +02:00
|
|
|
const scalingEnabled = ffmpegConfig.targetResolution !== 'original';
|
|
|
|
const targetRes = Number.parseInt(ffmpegConfig.targetResolution);
|
|
|
|
const isLargerThanTargetRes = scalingEnabled && Math.min(videoStream.height, videoStream.width) > targetRes;
|
2023-04-04 16:48:02 +02:00
|
|
|
|
|
|
|
switch (ffmpegConfig.transcode) {
|
2023-07-09 04:43:11 +02:00
|
|
|
case TranscodePolicy.DISABLED:
|
2023-04-06 05:32:59 +02:00
|
|
|
return false;
|
|
|
|
|
2023-07-09 04:43:11 +02:00
|
|
|
case TranscodePolicy.ALL:
|
2023-04-04 16:48:02 +02:00
|
|
|
return true;
|
|
|
|
|
2023-07-09 04:43:11 +02:00
|
|
|
case TranscodePolicy.REQUIRED:
|
2023-04-06 05:32:59 +02:00
|
|
|
return !allTargetsMatching;
|
2023-04-04 16:48:02 +02:00
|
|
|
|
2023-07-09 04:43:11 +02:00
|
|
|
case TranscodePolicy.OPTIMAL:
|
2023-06-10 06:15:12 +02:00
|
|
|
return !allTargetsMatching || isLargerThanTargetRes;
|
2023-04-04 16:48:02 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 04:43:11 +02:00
|
|
|
private getCodecConfig(config: SystemConfigFFmpegDto) {
|
|
|
|
switch (config.targetVideoCodec) {
|
|
|
|
case VideoCodec.H264:
|
|
|
|
return new H264Config(config);
|
|
|
|
case VideoCodec.HEVC:
|
|
|
|
return new HEVCConfig(config);
|
|
|
|
case VideoCodec.VP9:
|
|
|
|
return new VP9Config(config);
|
|
|
|
default:
|
|
|
|
throw new UnsupportedMediaTypeException(`Codec '${config.targetVideoCodec}' is unsupported`);
|
2023-05-22 20:07:43 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|