2022-08-23 21:34:21 +07:00
|
|
|
import { APP_UPLOAD_LOCATION } from '@app/common/constants';
|
|
|
|
|
import { AssetEntity } from '@app/database/entities/asset.entity';
|
2022-10-06 11:25:54 -05:00
|
|
|
import { QueueNameEnum } from '@app/job';
|
2022-07-02 21:06:36 -05:00
|
|
|
import { mp4ConversionProcessorName } from '@app/job/constants/job-name.constant';
|
|
|
|
|
import { IMp4ConversionProcessor } from '@app/job/interfaces/video-transcode.interface';
|
2022-06-11 16:12:06 -05:00
|
|
|
import { Process, Processor } from '@nestjs/bull';
|
|
|
|
|
import { Logger } from '@nestjs/common';
|
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
import { Job } from 'bull';
|
|
|
|
|
import ffmpeg from 'fluent-ffmpeg';
|
|
|
|
|
import { existsSync, mkdirSync } from 'fs';
|
2022-11-14 23:39:32 -05:00
|
|
|
import { ImmichConfigService } from 'libs/immich-config/src';
|
2022-06-11 16:12:06 -05:00
|
|
|
import { Repository } from 'typeorm';
|
|
|
|
|
|
2022-10-06 11:25:54 -05:00
|
|
|
@Processor(QueueNameEnum.VIDEO_CONVERSION)
|
2022-06-11 16:12:06 -05:00
|
|
|
export class VideoTranscodeProcessor {
|
|
|
|
|
constructor(
|
|
|
|
|
@InjectRepository(AssetEntity)
|
|
|
|
|
private assetRepository: Repository<AssetEntity>,
|
2022-11-14 23:39:32 -05:00
|
|
|
private immichConfigService: ImmichConfigService,
|
2022-06-11 16:12:06 -05:00
|
|
|
) {}
|
|
|
|
|
|
2022-11-18 23:12:54 -06:00
|
|
|
@Process({ name: mp4ConversionProcessorName, concurrency: 2 })
|
2022-07-02 21:06:36 -05:00
|
|
|
async mp4Conversion(job: Job<IMp4ConversionProcessor>) {
|
|
|
|
|
const { asset } = job.data;
|
2022-06-11 16:12:06 -05:00
|
|
|
|
|
|
|
|
if (asset.mimeType != 'video/mp4') {
|
|
|
|
|
const basePath = APP_UPLOAD_LOCATION;
|
|
|
|
|
const encodedVideoPath = `${basePath}/${asset.userId}/encoded-video`;
|
|
|
|
|
|
|
|
|
|
if (!existsSync(encodedVideoPath)) {
|
|
|
|
|
mkdirSync(encodedVideoPath, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const savedEncodedPath = encodedVideoPath + '/' + asset.id + '.mp4';
|
|
|
|
|
|
|
|
|
|
if (asset.encodedVideoPath == '' || !asset.encodedVideoPath) {
|
|
|
|
|
// Put the processing into its own async function to prevent the job exist right away
|
|
|
|
|
await this.runFFMPEGPipeLine(asset, savedEncodedPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async runFFMPEGPipeLine(asset: AssetEntity, savedEncodedPath: string): Promise<void> {
|
2022-12-09 15:51:42 -05:00
|
|
|
const config = await this.immichConfigService.getConfig();
|
2022-11-14 23:39:32 -05:00
|
|
|
|
2022-06-11 16:12:06 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
ffmpeg(asset.originalPath)
|
2022-11-14 23:39:32 -05:00
|
|
|
.outputOptions([
|
2022-12-09 15:51:42 -05:00
|
|
|
`-crf ${config.ffmpeg.crf}`,
|
|
|
|
|
`-preset ${config.ffmpeg.preset}`,
|
|
|
|
|
`-vcodec ${config.ffmpeg.targetVideoCodec}`,
|
|
|
|
|
`-acodec ${config.ffmpeg.targetAudioCodec}`,
|
|
|
|
|
`-vf scale=${config.ffmpeg.targetScaling}`,
|
2022-11-14 23:39:32 -05:00
|
|
|
])
|
2022-06-11 16:12:06 -05:00
|
|
|
.output(savedEncodedPath)
|
|
|
|
|
.on('start', () => {
|
2022-06-19 08:16:35 -05:00
|
|
|
Logger.log('Start Converting Video', 'mp4Conversion');
|
2022-06-11 16:12:06 -05:00
|
|
|
})
|
2022-06-25 19:53:06 +02:00
|
|
|
.on('error', (error) => {
|
2022-06-11 16:12:06 -05:00
|
|
|
Logger.error(`Cannot Convert Video ${error}`, 'mp4Conversion');
|
|
|
|
|
reject();
|
|
|
|
|
})
|
|
|
|
|
.on('end', async () => {
|
|
|
|
|
Logger.log(`Converting Success ${asset.id}`, 'mp4Conversion');
|
|
|
|
|
await this.assetRepository.update({ id: asset.id }, { encodedVideoPath: savedEncodedPath });
|
|
|
|
|
resolve();
|
|
|
|
|
})
|
|
|
|
|
.run();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|