mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-12-08 20:29:05 +00:00
feat(server): add transcode presets (#2084)
* feat: add transcode presets * Add migration * chore: generate api * refactor: use enum type instead of string for transcode option * chore: generate api * refactor: enhance readability of runVideoEncode method * refactor: reuse SettingSelect for transcoding presets * refactor: simplify return statement * chore: regenerate api * fix: correct label attribute * Update import * fix test --------- Co-authored-by: Alex <alex.tran1502@gmail.com>
This commit is contained in:
@@ -8,10 +8,11 @@ import {
|
||||
QueueName,
|
||||
StorageCore,
|
||||
StorageFolder,
|
||||
SystemConfigFFmpegDto,
|
||||
SystemConfigService,
|
||||
WithoutProperty,
|
||||
} from '@app/domain';
|
||||
import { AssetEntity, AssetType } from '@app/infra/db/entities';
|
||||
import { AssetEntity, AssetType, TranscodePreset } from '@app/infra/db/entities';
|
||||
import { Process, Processor } from '@nestjs/bull';
|
||||
import { Inject, Logger } from '@nestjs/common';
|
||||
import { Job } from 'bull';
|
||||
@@ -74,10 +75,41 @@ export class VideoTranscodeProcessor {
|
||||
async runVideoEncode(asset: AssetEntity, savedEncodedPath: string): Promise<void> {
|
||||
const config = await this.systemConfigService.getConfig();
|
||||
|
||||
if (config.ffmpeg.transcodeAll) {
|
||||
const transcode = await this.needsTranscoding(asset, config.ffmpeg);
|
||||
if (transcode) {
|
||||
//TODO: If video or audio are already the correct format, don't re-encode, copy the stream
|
||||
return this.runFFMPEGPipeLine(asset, savedEncodedPath);
|
||||
}
|
||||
}
|
||||
|
||||
async needsTranscoding(asset: AssetEntity, ffmpegConfig: SystemConfigFFmpegDto): Promise<boolean> {
|
||||
switch (ffmpegConfig.transcode) {
|
||||
case TranscodePreset.ALL:
|
||||
return true;
|
||||
|
||||
case TranscodePreset.REQUIRED:
|
||||
{
|
||||
const videoStream = await this.getVideoStream(asset);
|
||||
if (videoStream.codec_name !== ffmpegConfig.targetVideoCodec) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TranscodePreset.OPTIMAL: {
|
||||
const videoStream = await this.getVideoStream(asset);
|
||||
if (videoStream.codec_name !== ffmpegConfig.targetVideoCodec) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const videoHeightThreshold = 1080;
|
||||
return !videoStream.height || videoStream.height > videoHeightThreshold;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async getVideoStream(asset: AssetEntity): Promise<ffmpeg.FfprobeStream> {
|
||||
const videoInfo = await this.runFFProbePipeline(asset);
|
||||
|
||||
const videoStreams = videoInfo.streams.filter((stream) => {
|
||||
@@ -90,10 +122,7 @@ export class VideoTranscodeProcessor {
|
||||
return stream2Frames - stream1Frames;
|
||||
})[0];
|
||||
|
||||
//TODO: If video or audio are already the correct format, don't re-encode, copy the stream
|
||||
if (longestVideoStream.codec_name !== config.ffmpeg.targetVideoCodec) {
|
||||
return this.runFFMPEGPipeLine(asset, savedEncodedPath);
|
||||
}
|
||||
return longestVideoStream;
|
||||
}
|
||||
|
||||
async runFFMPEGPipeLine(asset: AssetEntity, savedEncodedPath: string): Promise<void> {
|
||||
|
||||
@@ -4601,8 +4601,13 @@
|
||||
"targetScaling": {
|
||||
"type": "string"
|
||||
},
|
||||
"transcodeAll": {
|
||||
"type": "boolean"
|
||||
"transcode": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"all",
|
||||
"optimal",
|
||||
"required"
|
||||
]
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
@@ -4611,7 +4616,7 @@
|
||||
"targetVideoCodec",
|
||||
"targetAudioCodec",
|
||||
"targetScaling",
|
||||
"transcodeAll"
|
||||
"transcode"
|
||||
]
|
||||
},
|
||||
"SystemConfigOAuthDto": {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { IsBoolean, IsString } from 'class-validator';
|
||||
import { IsEnum, IsString } from 'class-validator';
|
||||
import { TranscodePreset } from '@app/infra/db/entities';
|
||||
|
||||
export class SystemConfigFFmpegDto {
|
||||
@IsString()
|
||||
@@ -16,6 +17,6 @@ export class SystemConfigFFmpegDto {
|
||||
@IsString()
|
||||
targetScaling!: string;
|
||||
|
||||
@IsBoolean()
|
||||
transcodeAll!: boolean;
|
||||
@IsEnum(TranscodePreset)
|
||||
transcode!: TranscodePreset;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { SystemConfig, SystemConfigEntity, SystemConfigKey } from '@app/infra/db/entities';
|
||||
import { SystemConfig, SystemConfigEntity, SystemConfigKey, TranscodePreset } from '@app/infra/db/entities';
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import * as _ from 'lodash';
|
||||
import { Subject } from 'rxjs';
|
||||
@@ -14,7 +14,7 @@ const defaults: SystemConfig = Object.freeze({
|
||||
targetVideoCodec: 'h264',
|
||||
targetAudioCodec: 'aac',
|
||||
targetScaling: '1280:-2',
|
||||
transcodeAll: false,
|
||||
transcode: TranscodePreset.REQUIRED,
|
||||
},
|
||||
oauth: {
|
||||
enabled: false,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { SystemConfigEntity, SystemConfigKey } from '@app/infra/db/entities';
|
||||
import { SystemConfigEntity, SystemConfigKey, TranscodePreset } from '@app/infra/db/entities';
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
import { newJobRepositoryMock, newSystemConfigRepositoryMock, systemConfigStub } from '../../test';
|
||||
import { IJobRepository, JobName } from '../job';
|
||||
@@ -18,7 +18,7 @@ const updatedConfig = Object.freeze({
|
||||
targetAudioCodec: 'aac',
|
||||
targetScaling: '1280:-2',
|
||||
targetVideoCodec: 'h264',
|
||||
transcodeAll: false,
|
||||
transcode: TranscodePreset.REQUIRED,
|
||||
},
|
||||
oauth: {
|
||||
autoLaunch: true,
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
SharedLinkEntity,
|
||||
SharedLinkType,
|
||||
SystemConfig,
|
||||
TranscodePreset,
|
||||
UserEntity,
|
||||
UserTokenEntity,
|
||||
} from '@app/infra/db/entities';
|
||||
@@ -401,7 +402,7 @@ export const systemConfigStub = {
|
||||
targetAudioCodec: 'aac',
|
||||
targetScaling: '1280:-2',
|
||||
targetVideoCodec: 'h264',
|
||||
transcodeAll: false,
|
||||
transcode: TranscodePreset.REQUIRED,
|
||||
},
|
||||
oauth: {
|
||||
autoLaunch: false,
|
||||
|
||||
@@ -18,7 +18,7 @@ export enum SystemConfigKey {
|
||||
FFMPEG_TARGET_VIDEO_CODEC = 'ffmpeg.targetVideoCodec',
|
||||
FFMPEG_TARGET_AUDIO_CODEC = 'ffmpeg.targetAudioCodec',
|
||||
FFMPEG_TARGET_SCALING = 'ffmpeg.targetScaling',
|
||||
FFMPEG_TRANSCODE_ALL = 'ffmpeg.transcodeAll',
|
||||
FFMPEG_TRANSCODE = 'ffmpeg.transcode',
|
||||
OAUTH_ENABLED = 'oauth.enabled',
|
||||
OAUTH_ISSUER_URL = 'oauth.issuerUrl',
|
||||
OAUTH_CLIENT_ID = 'oauth.clientId',
|
||||
@@ -33,6 +33,12 @@ export enum SystemConfigKey {
|
||||
STORAGE_TEMPLATE = 'storageTemplate.template',
|
||||
}
|
||||
|
||||
export enum TranscodePreset {
|
||||
ALL = 'all',
|
||||
OPTIMAL = 'optimal',
|
||||
REQUIRED = 'required',
|
||||
}
|
||||
|
||||
export interface SystemConfig {
|
||||
ffmpeg: {
|
||||
crf: string;
|
||||
@@ -40,7 +46,7 @@ export interface SystemConfig {
|
||||
targetVideoCodec: string;
|
||||
targetAudioCodec: string;
|
||||
targetScaling: string;
|
||||
transcodeAll: boolean;
|
||||
transcode: TranscodePreset;
|
||||
};
|
||||
oauth: {
|
||||
enabled: boolean;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class UpdateTranscodeOption1679751316282 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
UPDATE system_config
|
||||
SET
|
||||
key = 'ffmpeg.transcode',
|
||||
value = '"all"'
|
||||
WHERE
|
||||
key = 'ffmpeg.transcodeAll' AND value = 'true'
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
UPDATE system_config
|
||||
SET
|
||||
key = 'ffmpeg.transcodeAll',
|
||||
value = 'true'
|
||||
WHERE
|
||||
key = 'ffmpeg.transcode' AND value = '"all"'
|
||||
`);
|
||||
|
||||
await queryRunner.query(`DELETE FROM "system_config" WHERE key = 'ffmpeg.transcode'`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user