1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-01 08:31:59 +00:00

chore: refactor transcode config routing (#9800)

* chore: refactor transcode config

* rename parameter

* handle no /dev/dri

* prefer undefined
This commit is contained in:
Mert 2024-05-27 15:20:07 -04:00 committed by GitHub
parent 21bd20fd75
commit dca420ef70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 115 additions and 123 deletions

View file

@ -53,7 +53,7 @@ export interface VideoInfo {
audioStreams: AudioStreamInfo[]; audioStreams: AudioStreamInfo[];
} }
export interface TranscodeOptions { export interface TranscodeCommand {
inputOptions: string[]; inputOptions: string[];
outputOptions: string[]; outputOptions: string[];
twoPass: boolean; twoPass: boolean;
@ -67,7 +67,7 @@ export interface BitrateDistribution {
} }
export interface VideoCodecSWConfig { export interface VideoCodecSWConfig {
getOptions(target: TranscodeTarget, videoStream: VideoStreamInfo, audioStream: AudioStreamInfo): TranscodeOptions; getCommand(target: TranscodeTarget, videoStream: VideoStreamInfo, audioStream: AudioStreamInfo): TranscodeCommand;
} }
export interface VideoCodecHWConfig extends VideoCodecSWConfig { export interface VideoCodecHWConfig extends VideoCodecSWConfig {
@ -83,5 +83,5 @@ export interface IMediaRepository {
// video // video
probe(input: string): Promise<VideoInfo>; probe(input: string): Promise<VideoInfo>;
transcode(input: string, output: string | Writable, options: TranscodeOptions): Promise<void>; transcode(input: string, output: string | Writable, command: TranscodeCommand): Promise<void>;
} }

View file

@ -11,7 +11,7 @@ import {
IMediaRepository, IMediaRepository,
ImageDimensions, ImageDimensions,
ThumbnailOptions, ThumbnailOptions,
TranscodeOptions, TranscodeCommand,
VideoInfo, VideoInfo,
} from 'src/interfaces/media.interface'; } from 'src/interfaces/media.interface';
import { Instrumentation } from 'src/utils/instrumentation'; import { Instrumentation } from 'src/utils/instrumentation';
@ -97,7 +97,7 @@ export class MediaRepository implements IMediaRepository {
}; };
} }
transcode(input: string, output: string | Writable, options: TranscodeOptions): Promise<void> { transcode(input: string, output: string | Writable, options: TranscodeCommand): Promise<void> {
if (!options.twoPass) { if (!options.twoPass) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.configureFfmpegCall(input, output, options).on('error', reject).on('end', resolve).run(); this.configureFfmpegCall(input, output, options).on('error', reject).on('end', resolve).run();
@ -150,7 +150,7 @@ export class MediaRepository implements IMediaRepository {
return { width, height }; return { width, height };
} }
private configureFfmpegCall(input: string, output: string | Writable, options: TranscodeOptions) { private configureFfmpegCall(input: string, output: string | Writable, options: TranscodeCommand) {
return ffmpeg(input, { niceness: 10 }) return ffmpeg(input, { niceness: 10 })
.inputOptions(options.inputOptions) .inputOptions(options.inputOptions)
.outputOptions(options.outputOptions) .outputOptions(options.outputOptions)

View file

@ -27,25 +27,12 @@ import {
QueueName, QueueName,
} from 'src/interfaces/job.interface'; } from 'src/interfaces/job.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface'; import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { AudioStreamInfo, IMediaRepository, VideoCodecHWConfig, VideoStreamInfo } from 'src/interfaces/media.interface'; import { AudioStreamInfo, IMediaRepository, VideoStreamInfo } from 'src/interfaces/media.interface';
import { IMoveRepository } from 'src/interfaces/move.interface'; import { IMoveRepository } from 'src/interfaces/move.interface';
import { IPersonRepository } from 'src/interfaces/person.interface'; import { IPersonRepository } from 'src/interfaces/person.interface';
import { IStorageRepository } from 'src/interfaces/storage.interface'; import { IStorageRepository } from 'src/interfaces/storage.interface';
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface'; import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
import { import { BaseConfig, ThumbnailConfig } from 'src/utils/media';
AV1Config,
H264Config,
HEVCConfig,
NvencHwDecodeConfig,
NvencSwDecodeConfig,
QsvHwDecodeConfig,
QsvSwDecodeConfig,
RkmppHwDecodeConfig,
RkmppSwDecodeConfig,
ThumbnailConfig,
VAAPIConfig,
VP9Config,
} from 'src/utils/media';
import { mimeTypes } from 'src/utils/mime-types'; import { mimeTypes } from 'src/utils/mime-types';
import { usePagination } from 'src/utils/pagination'; import { usePagination } from 'src/utils/pagination';
@ -53,8 +40,8 @@ import { usePagination } from 'src/utils/pagination';
export class MediaService { export class MediaService {
private configCore: SystemConfigCore; private configCore: SystemConfigCore;
private storageCore: StorageCore; private storageCore: StorageCore;
private openCL: boolean | null = null; private maliOpenCL?: boolean;
private devices: string[] | null = null; private devices?: string[];
constructor( constructor(
@Inject(IAssetRepository) private assetRepository: IAssetRepository, @Inject(IAssetRepository) private assetRepository: IAssetRepository,
@ -232,8 +219,8 @@ export class MediaService {
return; return;
} }
const mainAudioStream = this.getMainStream(audioStreams); const mainAudioStream = this.getMainStream(audioStreams);
const config = { ...ffmpeg, targetResolution: size.toString() }; const config = ThumbnailConfig.create({ ...ffmpeg, targetResolution: size.toString() });
const options = new ThumbnailConfig(config).getOptions(TranscodeTarget.VIDEO, mainVideoStream, mainAudioStream); const options = config.getCommand(TranscodeTarget.VIDEO, mainVideoStream, mainAudioStream);
await this.mediaRepository.transcode(asset.originalPath, path, options); await this.mediaRepository.transcode(asset.originalPath, path, options);
break; break;
} }
@ -331,8 +318,8 @@ export class MediaService {
return JobStatus.FAILED; return JobStatus.FAILED;
} }
const { ffmpeg: config } = await this.configCore.getConfig(); const { ffmpeg } = await this.configCore.getConfig();
const target = this.getTranscodeTarget(config, mainVideoStream, mainAudioStream); const target = this.getTranscodeTarget(ffmpeg, mainVideoStream, mainAudioStream);
if (target === TranscodeTarget.NONE) { if (target === TranscodeTarget.NONE) {
if (asset.encodedVideoPath) { if (asset.encodedVideoPath) {
this.logger.log(`Transcoded video exists for asset ${asset.id}, but is no longer required. Deleting...`); this.logger.log(`Transcoded video exists for asset ${asset.id}, but is no longer required. Deleting...`);
@ -343,30 +330,28 @@ export class MediaService {
return JobStatus.SKIPPED; return JobStatus.SKIPPED;
} }
let transcodeOptions; let command;
try { try {
transcodeOptions = await this.getCodecConfig(config).then((c) => const config = BaseConfig.create(ffmpeg, await this.getDevices(), await this.hasMaliOpenCL());
c.getOptions(target, mainVideoStream, mainAudioStream), command = config.getCommand(target, mainVideoStream, mainAudioStream);
);
} catch (error) { } catch (error) {
this.logger.error(`An error occurred while configuring transcoding options: ${error}`); this.logger.error(`An error occurred while configuring transcoding options: ${error}`);
return JobStatus.FAILED; return JobStatus.FAILED;
} }
this.logger.log(`Started encoding video ${asset.id} ${JSON.stringify(transcodeOptions)}`); this.logger.log(`Started encoding video ${asset.id} ${JSON.stringify(command)}`);
try { try {
await this.mediaRepository.transcode(input, output, transcodeOptions); await this.mediaRepository.transcode(input, output, command);
} catch (error) { } catch (error) {
this.logger.error(error); this.logger.error(error);
if (config.accel !== TranscodeHWAccel.DISABLED) { if (ffmpeg.accel !== TranscodeHWAccel.DISABLED) {
this.logger.error( this.logger.error(
`Error occurred during transcoding. Retrying with ${config.accel.toUpperCase()} acceleration disabled.`, `Error occurred during transcoding. Retrying with ${ffmpeg.accel.toUpperCase()} acceleration disabled.`,
); );
} }
transcodeOptions = await this.getCodecConfig({ ...config, accel: TranscodeHWAccel.DISABLED }).then((c) => const config = BaseConfig.create({ ...ffmpeg, accel: TranscodeHWAccel.DISABLED });
c.getOptions(target, mainVideoStream, mainAudioStream), command = config.getCommand(target, mainVideoStream, mainAudioStream);
); await this.mediaRepository.transcode(input, output, command);
await this.mediaRepository.transcode(input, output, transcodeOptions);
} }
this.logger.log(`Successfully encoded ${asset.id}`); this.logger.log(`Successfully encoded ${asset.id}`);
@ -382,10 +367,10 @@ export class MediaService {
private getTranscodeTarget( private getTranscodeTarget(
config: SystemConfigFFmpegDto, config: SystemConfigFFmpegDto,
videoStream: VideoStreamInfo | null, videoStream?: VideoStreamInfo,
audioStream: AudioStreamInfo | null, audioStream?: AudioStreamInfo,
): TranscodeTarget { ): TranscodeTarget {
if (videoStream == null && audioStream == null) { if (!videoStream && !audioStream) {
return TranscodeTarget.NONE; return TranscodeTarget.NONE;
} }
@ -407,8 +392,8 @@ export class MediaService {
return TranscodeTarget.NONE; return TranscodeTarget.NONE;
} }
private isAudioTranscodeRequired(ffmpegConfig: SystemConfigFFmpegDto, stream: AudioStreamInfo | null): boolean { private isAudioTranscodeRequired(ffmpegConfig: SystemConfigFFmpegDto, stream?: AudioStreamInfo): boolean {
if (stream == null) { if (!stream) {
return false; return false;
} }
@ -430,8 +415,8 @@ export class MediaService {
} }
} }
private isVideoTranscodeRequired(ffmpegConfig: SystemConfigFFmpegDto, stream: VideoStreamInfo | null): boolean { private isVideoTranscodeRequired(ffmpegConfig: SystemConfigFFmpegDto, stream?: VideoStreamInfo): boolean {
if (stream == null) { if (!stream) {
return false; return false;
} }
@ -465,70 +450,6 @@ export class MediaService {
} }
} }
async getCodecConfig(config: SystemConfigFFmpegDto) {
if (config.accel === TranscodeHWAccel.DISABLED) {
return this.getSWCodecConfig(config);
}
return this.getHWCodecConfig(config);
}
private getSWCodecConfig(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);
}
case VideoCodec.AV1: {
return new AV1Config(config);
}
default: {
throw new UnsupportedMediaTypeException(`Codec '${config.targetVideoCodec}' is unsupported`);
}
}
}
private async getHWCodecConfig(config: SystemConfigFFmpegDto) {
let handler: VideoCodecHWConfig;
switch (config.accel) {
case TranscodeHWAccel.NVENC: {
handler = config.accelDecode ? new NvencHwDecodeConfig(config) : new NvencSwDecodeConfig(config);
break;
}
case TranscodeHWAccel.QSV: {
handler = config.accelDecode
? new QsvHwDecodeConfig(config, await this.getDevices())
: new QsvSwDecodeConfig(config, await this.getDevices());
break;
}
case TranscodeHWAccel.VAAPI: {
handler = new VAAPIConfig(config, await this.getDevices());
break;
}
case TranscodeHWAccel.RKMPP: {
handler =
config.accelDecode && (await this.hasOpenCL())
? new RkmppHwDecodeConfig(config, await this.getDevices())
: new RkmppSwDecodeConfig(config, await this.getDevices());
break;
}
default: {
throw new UnsupportedMediaTypeException(`${config.accel.toUpperCase()} acceleration is unsupported`);
}
}
if (!handler.getSupportedCodecs().includes(config.targetVideoCodec)) {
throw new UnsupportedMediaTypeException(
`${config.accel.toUpperCase()} acceleration does not support codec '${config.targetVideoCodec.toUpperCase()}'. Supported codecs: ${handler.getSupportedCodecs()}`,
);
}
return handler;
}
isSRGB(asset: AssetEntity): boolean { isSRGB(asset: AssetEntity): boolean {
const { colorspace, profileDescription, bitsPerSample } = asset.exifInfo ?? {}; const { colorspace, profileDescription, bitsPerSample } = asset.exifInfo ?? {};
if (colorspace || profileDescription) { if (colorspace || profileDescription) {
@ -567,24 +488,29 @@ export class MediaService {
private async getDevices() { private async getDevices() {
if (!this.devices) { if (!this.devices) {
this.devices = await this.storageRepository.readdir('/dev/dri'); try {
this.devices = await this.storageRepository.readdir('/dev/dri');
} catch {
this.logger.debug('No devices found in /dev/dri.');
this.devices = [];
}
} }
return this.devices; return this.devices;
} }
private async hasOpenCL() { private async hasMaliOpenCL() {
if (this.openCL === null) { if (this.maliOpenCL === undefined) {
try { try {
const maliIcdStat = await this.storageRepository.stat('/etc/OpenCL/vendors/mali.icd'); const maliIcdStat = await this.storageRepository.stat('/etc/OpenCL/vendors/mali.icd');
const maliDeviceStat = await this.storageRepository.stat('/dev/mali0'); const maliDeviceStat = await this.storageRepository.stat('/dev/mali0');
this.openCL = maliIcdStat.isFile() && maliDeviceStat.isCharacterDevice(); this.maliOpenCL = maliIcdStat.isFile() && maliDeviceStat.isCharacterDevice();
} catch { } catch {
this.logger.warn('OpenCL not available for transcoding, using CPU instead.'); this.logger.debug('OpenCL not available for transcoding, using CPU decoding instead.');
this.openCL = false; this.maliOpenCL = false;
} }
} }
return this.openCL; return this.maliOpenCL;
} }
} }

View file

@ -3,22 +3,84 @@ import { SystemConfigFFmpegDto } from 'src/dtos/system-config.dto';
import { import {
AudioStreamInfo, AudioStreamInfo,
BitrateDistribution, BitrateDistribution,
TranscodeOptions, TranscodeCommand,
VideoCodecHWConfig, VideoCodecHWConfig,
VideoCodecSWConfig, VideoCodecSWConfig,
VideoStreamInfo, VideoStreamInfo,
} from 'src/interfaces/media.interface'; } from 'src/interfaces/media.interface';
class BaseConfig implements VideoCodecSWConfig { export class BaseConfig implements VideoCodecSWConfig {
presets = ['veryslow', 'slower', 'slow', 'medium', 'fast', 'faster', 'veryfast', 'superfast', 'ultrafast']; readonly presets = ['veryslow', 'slower', 'slow', 'medium', 'fast', 'faster', 'veryfast', 'superfast', 'ultrafast'];
constructor(protected config: SystemConfigFFmpegDto) {} protected constructor(protected config: SystemConfigFFmpegDto) {}
getOptions(target: TranscodeTarget, videoStream: VideoStreamInfo, audioStream?: AudioStreamInfo) { static create(config: SystemConfigFFmpegDto, devices: string[] = [], hasMaliOpenCL = false): VideoCodecSWConfig {
if (config.accel === TranscodeHWAccel.DISABLED) {
return this.getSWCodecConfig(config);
}
return this.getHWCodecConfig(config, devices, hasMaliOpenCL);
}
private static getSWCodecConfig(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);
}
case VideoCodec.AV1: {
return new AV1Config(config);
}
default: {
throw new Error(`Codec '${config.targetVideoCodec}' is unsupported`);
}
}
}
private static getHWCodecConfig(config: SystemConfigFFmpegDto, devices: string[] = [], hasMaliOpenCL = false) {
let handler: VideoCodecHWConfig;
switch (config.accel) {
case TranscodeHWAccel.NVENC: {
handler = config.accelDecode ? new NvencHwDecodeConfig(config) : new NvencSwDecodeConfig(config);
break;
}
case TranscodeHWAccel.QSV: {
handler = config.accelDecode ? new QsvHwDecodeConfig(config, devices) : new QsvSwDecodeConfig(config, devices);
break;
}
case TranscodeHWAccel.VAAPI: {
handler = new VAAPIConfig(config, devices);
break;
}
case TranscodeHWAccel.RKMPP: {
handler =
config.accelDecode && hasMaliOpenCL
? new RkmppHwDecodeConfig(config, devices)
: new RkmppSwDecodeConfig(config, devices);
break;
}
default: {
throw new Error(`${config.accel.toUpperCase()} acceleration is unsupported`);
}
}
if (!handler.getSupportedCodecs().includes(config.targetVideoCodec)) {
throw new Error(
`${config.accel.toUpperCase()} acceleration does not support codec '${config.targetVideoCodec.toUpperCase()}'. Supported codecs: ${handler.getSupportedCodecs()}`,
);
}
return handler;
}
getCommand(target: TranscodeTarget, videoStream: VideoStreamInfo, audioStream?: AudioStreamInfo) {
const options = { const options = {
inputOptions: this.getBaseInputOptions(videoStream), inputOptions: this.getBaseInputOptions(videoStream),
outputOptions: [...this.getBaseOutputOptions(target, videoStream, audioStream), '-v verbose'], outputOptions: [...this.getBaseOutputOptions(target, videoStream, audioStream), '-v verbose'],
twoPass: this.eligibleForTwoPass(), twoPass: this.eligibleForTwoPass(),
} as TranscodeOptions; } as TranscodeCommand;
if ([TranscodeTarget.ALL, TranscodeTarget.VIDEO].includes(target)) { if ([TranscodeTarget.ALL, TranscodeTarget.VIDEO].includes(target)) {
const filters = this.getFilterOptions(videoStream); const filters = this.getFilterOptions(videoStream);
if (filters.length > 0) { if (filters.length > 0) {
@ -318,6 +380,10 @@ export class BaseHWConfig extends BaseConfig implements VideoCodecHWConfig {
} }
export class ThumbnailConfig extends BaseConfig { export class ThumbnailConfig extends BaseConfig {
static create(config: SystemConfigFFmpegDto): VideoCodecSWConfig {
return new ThumbnailConfig(config);
}
getBaseInputOptions(): string[] { getBaseInputOptions(): string[] {
return ['-skip_frame nokey', '-sws_flags accurate_rnd+full_chroma_int']; return ['-skip_frame nokey', '-sws_flags accurate_rnd+full_chroma_int'];
} }