You've already forked immich
mirror of
https://github.com/immich-app/immich.git
synced 2025-06-28 05:18:38 +02:00
refactor(server): modularize getFfmpegOptions
(#3138)
* refactored `getFfmpegOptions` refactor transcoding, make separate service * fixed enum casing * use `Logger` instead of `console.log` * review suggestions * use enum for `getHandler` * fixed formatting * Update server/src/domain/media/media.util.ts Co-authored-by: Jason Rasmussen <jrasm91@gmail.com> * Update server/src/domain/media/media.util.ts Co-authored-by: Jason Rasmussen <jrasm91@gmail.com> * More specific imports, renamed codec classes * simplified code * removed unused import * added tests * added base implementation for bitrate and threads --------- Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
This commit is contained in:
88
mobile/openapi/lib/model/audio_codec.dart
generated
Normal file
88
mobile/openapi/lib/model/audio_codec.dart
generated
Normal file
@ -0,0 +1,88 @@
|
||||
//
|
||||
// AUTO-GENERATED FILE, DO NOT MODIFY!
|
||||
//
|
||||
// @dart=2.12
|
||||
|
||||
// ignore_for_file: unused_element, unused_import
|
||||
// ignore_for_file: always_put_required_named_parameters_first
|
||||
// ignore_for_file: constant_identifier_names
|
||||
// ignore_for_file: lines_longer_than_80_chars
|
||||
|
||||
part of openapi.api;
|
||||
|
||||
|
||||
class AudioCodec {
|
||||
/// Instantiate a new enum with the provided [value].
|
||||
const AudioCodec._(this.value);
|
||||
|
||||
/// The underlying value of this enum member.
|
||||
final String value;
|
||||
|
||||
@override
|
||||
String toString() => value;
|
||||
|
||||
String toJson() => value;
|
||||
|
||||
static const mp3 = AudioCodec._(r'mp3');
|
||||
static const aac = AudioCodec._(r'aac');
|
||||
static const opus = AudioCodec._(r'opus');
|
||||
|
||||
/// List of all possible values in this [enum][AudioCodec].
|
||||
static const values = <AudioCodec>[
|
||||
mp3,
|
||||
aac,
|
||||
opus,
|
||||
];
|
||||
|
||||
static AudioCodec? fromJson(dynamic value) => AudioCodecTypeTransformer().decode(value);
|
||||
|
||||
static List<AudioCodec>? listFromJson(dynamic json, {bool growable = false,}) {
|
||||
final result = <AudioCodec>[];
|
||||
if (json is List && json.isNotEmpty) {
|
||||
for (final row in json) {
|
||||
final value = AudioCodec.fromJson(row);
|
||||
if (value != null) {
|
||||
result.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toList(growable: growable);
|
||||
}
|
||||
}
|
||||
|
||||
/// Transformation class that can [encode] an instance of [AudioCodec] to String,
|
||||
/// and [decode] dynamic data back to [AudioCodec].
|
||||
class AudioCodecTypeTransformer {
|
||||
factory AudioCodecTypeTransformer() => _instance ??= const AudioCodecTypeTransformer._();
|
||||
|
||||
const AudioCodecTypeTransformer._();
|
||||
|
||||
String encode(AudioCodec data) => data.value;
|
||||
|
||||
/// Decodes a [dynamic value][data] to a AudioCodec.
|
||||
///
|
||||
/// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully,
|
||||
/// then null is returned. However, if [allowNull] is false and the [dynamic value][data]
|
||||
/// cannot be decoded successfully, then an [UnimplementedError] is thrown.
|
||||
///
|
||||
/// The [allowNull] is very handy when an API changes and a new enum value is added or removed,
|
||||
/// and users are still using an old app with the old code.
|
||||
AudioCodec? decode(dynamic data, {bool allowNull = true}) {
|
||||
if (data != null) {
|
||||
switch (data) {
|
||||
case r'mp3': return AudioCodec.mp3;
|
||||
case r'aac': return AudioCodec.aac;
|
||||
case r'opus': return AudioCodec.opus;
|
||||
default:
|
||||
if (!allowNull) {
|
||||
throw ArgumentError('Unknown enum value to decode: $data');
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Singleton [AudioCodecTypeTransformer] instance.
|
||||
static AudioCodecTypeTransformer? _instance;
|
||||
}
|
||||
|
124
mobile/openapi/lib/model/system_config_f_fmpeg_dto.dart
generated
124
mobile/openapi/lib/model/system_config_f_fmpeg_dto.dart
generated
@ -15,72 +15,72 @@ class SystemConfigFFmpegDto {
|
||||
SystemConfigFFmpegDto({
|
||||
required this.crf,
|
||||
required this.threads,
|
||||
required this.preset,
|
||||
required this.targetVideoCodec,
|
||||
required this.targetAudioCodec,
|
||||
required this.transcode,
|
||||
required this.preset,
|
||||
required this.targetResolution,
|
||||
required this.maxBitrate,
|
||||
required this.twoPass,
|
||||
required this.transcode,
|
||||
});
|
||||
|
||||
int crf;
|
||||
|
||||
int threads;
|
||||
|
||||
VideoCodec targetVideoCodec;
|
||||
|
||||
AudioCodec targetAudioCodec;
|
||||
|
||||
TranscodePolicy transcode;
|
||||
|
||||
String preset;
|
||||
|
||||
String targetVideoCodec;
|
||||
|
||||
String targetAudioCodec;
|
||||
|
||||
String targetResolution;
|
||||
|
||||
String maxBitrate;
|
||||
|
||||
bool twoPass;
|
||||
|
||||
SystemConfigFFmpegDtoTranscodeEnum transcode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) => identical(this, other) || other is SystemConfigFFmpegDto &&
|
||||
other.crf == crf &&
|
||||
other.threads == threads &&
|
||||
other.preset == preset &&
|
||||
other.targetVideoCodec == targetVideoCodec &&
|
||||
other.targetAudioCodec == targetAudioCodec &&
|
||||
other.transcode == transcode &&
|
||||
other.preset == preset &&
|
||||
other.targetResolution == targetResolution &&
|
||||
other.maxBitrate == maxBitrate &&
|
||||
other.twoPass == twoPass &&
|
||||
other.transcode == transcode;
|
||||
other.twoPass == twoPass;
|
||||
|
||||
@override
|
||||
int get hashCode =>
|
||||
// ignore: unnecessary_parenthesis
|
||||
(crf.hashCode) +
|
||||
(threads.hashCode) +
|
||||
(preset.hashCode) +
|
||||
(targetVideoCodec.hashCode) +
|
||||
(targetAudioCodec.hashCode) +
|
||||
(transcode.hashCode) +
|
||||
(preset.hashCode) +
|
||||
(targetResolution.hashCode) +
|
||||
(maxBitrate.hashCode) +
|
||||
(twoPass.hashCode) +
|
||||
(transcode.hashCode);
|
||||
(twoPass.hashCode);
|
||||
|
||||
@override
|
||||
String toString() => 'SystemConfigFFmpegDto[crf=$crf, threads=$threads, preset=$preset, targetVideoCodec=$targetVideoCodec, targetAudioCodec=$targetAudioCodec, targetResolution=$targetResolution, maxBitrate=$maxBitrate, twoPass=$twoPass, transcode=$transcode]';
|
||||
String toString() => 'SystemConfigFFmpegDto[crf=$crf, threads=$threads, targetVideoCodec=$targetVideoCodec, targetAudioCodec=$targetAudioCodec, transcode=$transcode, preset=$preset, targetResolution=$targetResolution, maxBitrate=$maxBitrate, twoPass=$twoPass]';
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final json = <String, dynamic>{};
|
||||
json[r'crf'] = this.crf;
|
||||
json[r'threads'] = this.threads;
|
||||
json[r'preset'] = this.preset;
|
||||
json[r'targetVideoCodec'] = this.targetVideoCodec;
|
||||
json[r'targetAudioCodec'] = this.targetAudioCodec;
|
||||
json[r'transcode'] = this.transcode;
|
||||
json[r'preset'] = this.preset;
|
||||
json[r'targetResolution'] = this.targetResolution;
|
||||
json[r'maxBitrate'] = this.maxBitrate;
|
||||
json[r'twoPass'] = this.twoPass;
|
||||
json[r'transcode'] = this.transcode;
|
||||
return json;
|
||||
}
|
||||
|
||||
@ -94,13 +94,13 @@ class SystemConfigFFmpegDto {
|
||||
return SystemConfigFFmpegDto(
|
||||
crf: mapValueOfType<int>(json, r'crf')!,
|
||||
threads: mapValueOfType<int>(json, r'threads')!,
|
||||
targetVideoCodec: VideoCodec.fromJson(json[r'targetVideoCodec'])!,
|
||||
targetAudioCodec: AudioCodec.fromJson(json[r'targetAudioCodec'])!,
|
||||
transcode: TranscodePolicy.fromJson(json[r'transcode'])!,
|
||||
preset: mapValueOfType<String>(json, r'preset')!,
|
||||
targetVideoCodec: mapValueOfType<String>(json, r'targetVideoCodec')!,
|
||||
targetAudioCodec: mapValueOfType<String>(json, r'targetAudioCodec')!,
|
||||
targetResolution: mapValueOfType<String>(json, r'targetResolution')!,
|
||||
maxBitrate: mapValueOfType<String>(json, r'maxBitrate')!,
|
||||
twoPass: mapValueOfType<bool>(json, r'twoPass')!,
|
||||
transcode: SystemConfigFFmpegDtoTranscodeEnum.fromJson(json[r'transcode'])!,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
@ -150,93 +150,13 @@ class SystemConfigFFmpegDto {
|
||||
static const requiredKeys = <String>{
|
||||
'crf',
|
||||
'threads',
|
||||
'preset',
|
||||
'targetVideoCodec',
|
||||
'targetAudioCodec',
|
||||
'transcode',
|
||||
'preset',
|
||||
'targetResolution',
|
||||
'maxBitrate',
|
||||
'twoPass',
|
||||
'transcode',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
class SystemConfigFFmpegDtoTranscodeEnum {
|
||||
/// Instantiate a new enum with the provided [value].
|
||||
const SystemConfigFFmpegDtoTranscodeEnum._(this.value);
|
||||
|
||||
/// The underlying value of this enum member.
|
||||
final String value;
|
||||
|
||||
@override
|
||||
String toString() => value;
|
||||
|
||||
String toJson() => value;
|
||||
|
||||
static const all = SystemConfigFFmpegDtoTranscodeEnum._(r'all');
|
||||
static const optimal = SystemConfigFFmpegDtoTranscodeEnum._(r'optimal');
|
||||
static const required_ = SystemConfigFFmpegDtoTranscodeEnum._(r'required');
|
||||
static const disabled = SystemConfigFFmpegDtoTranscodeEnum._(r'disabled');
|
||||
|
||||
/// List of all possible values in this [enum][SystemConfigFFmpegDtoTranscodeEnum].
|
||||
static const values = <SystemConfigFFmpegDtoTranscodeEnum>[
|
||||
all,
|
||||
optimal,
|
||||
required_,
|
||||
disabled,
|
||||
];
|
||||
|
||||
static SystemConfigFFmpegDtoTranscodeEnum? fromJson(dynamic value) => SystemConfigFFmpegDtoTranscodeEnumTypeTransformer().decode(value);
|
||||
|
||||
static List<SystemConfigFFmpegDtoTranscodeEnum>? listFromJson(dynamic json, {bool growable = false,}) {
|
||||
final result = <SystemConfigFFmpegDtoTranscodeEnum>[];
|
||||
if (json is List && json.isNotEmpty) {
|
||||
for (final row in json) {
|
||||
final value = SystemConfigFFmpegDtoTranscodeEnum.fromJson(row);
|
||||
if (value != null) {
|
||||
result.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toList(growable: growable);
|
||||
}
|
||||
}
|
||||
|
||||
/// Transformation class that can [encode] an instance of [SystemConfigFFmpegDtoTranscodeEnum] to String,
|
||||
/// and [decode] dynamic data back to [SystemConfigFFmpegDtoTranscodeEnum].
|
||||
class SystemConfigFFmpegDtoTranscodeEnumTypeTransformer {
|
||||
factory SystemConfigFFmpegDtoTranscodeEnumTypeTransformer() => _instance ??= const SystemConfigFFmpegDtoTranscodeEnumTypeTransformer._();
|
||||
|
||||
const SystemConfigFFmpegDtoTranscodeEnumTypeTransformer._();
|
||||
|
||||
String encode(SystemConfigFFmpegDtoTranscodeEnum data) => data.value;
|
||||
|
||||
/// Decodes a [dynamic value][data] to a SystemConfigFFmpegDtoTranscodeEnum.
|
||||
///
|
||||
/// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully,
|
||||
/// then null is returned. However, if [allowNull] is false and the [dynamic value][data]
|
||||
/// cannot be decoded successfully, then an [UnimplementedError] is thrown.
|
||||
///
|
||||
/// The [allowNull] is very handy when an API changes and a new enum value is added or removed,
|
||||
/// and users are still using an old app with the old code.
|
||||
SystemConfigFFmpegDtoTranscodeEnum? decode(dynamic data, {bool allowNull = true}) {
|
||||
if (data != null) {
|
||||
switch (data) {
|
||||
case r'all': return SystemConfigFFmpegDtoTranscodeEnum.all;
|
||||
case r'optimal': return SystemConfigFFmpegDtoTranscodeEnum.optimal;
|
||||
case r'required': return SystemConfigFFmpegDtoTranscodeEnum.required_;
|
||||
case r'disabled': return SystemConfigFFmpegDtoTranscodeEnum.disabled;
|
||||
default:
|
||||
if (!allowNull) {
|
||||
throw ArgumentError('Unknown enum value to decode: $data');
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Singleton [SystemConfigFFmpegDtoTranscodeEnumTypeTransformer] instance.
|
||||
static SystemConfigFFmpegDtoTranscodeEnumTypeTransformer? _instance;
|
||||
}
|
||||
|
||||
|
||||
|
91
mobile/openapi/lib/model/transcode_policy.dart
generated
Normal file
91
mobile/openapi/lib/model/transcode_policy.dart
generated
Normal file
@ -0,0 +1,91 @@
|
||||
//
|
||||
// AUTO-GENERATED FILE, DO NOT MODIFY!
|
||||
//
|
||||
// @dart=2.12
|
||||
|
||||
// ignore_for_file: unused_element, unused_import
|
||||
// ignore_for_file: always_put_required_named_parameters_first
|
||||
// ignore_for_file: constant_identifier_names
|
||||
// ignore_for_file: lines_longer_than_80_chars
|
||||
|
||||
part of openapi.api;
|
||||
|
||||
|
||||
class TranscodePolicy {
|
||||
/// Instantiate a new enum with the provided [value].
|
||||
const TranscodePolicy._(this.value);
|
||||
|
||||
/// The underlying value of this enum member.
|
||||
final String value;
|
||||
|
||||
@override
|
||||
String toString() => value;
|
||||
|
||||
String toJson() => value;
|
||||
|
||||
static const all = TranscodePolicy._(r'all');
|
||||
static const optimal = TranscodePolicy._(r'optimal');
|
||||
static const required_ = TranscodePolicy._(r'required');
|
||||
static const disabled = TranscodePolicy._(r'disabled');
|
||||
|
||||
/// List of all possible values in this [enum][TranscodePolicy].
|
||||
static const values = <TranscodePolicy>[
|
||||
all,
|
||||
optimal,
|
||||
required_,
|
||||
disabled,
|
||||
];
|
||||
|
||||
static TranscodePolicy? fromJson(dynamic value) => TranscodePolicyTypeTransformer().decode(value);
|
||||
|
||||
static List<TranscodePolicy>? listFromJson(dynamic json, {bool growable = false,}) {
|
||||
final result = <TranscodePolicy>[];
|
||||
if (json is List && json.isNotEmpty) {
|
||||
for (final row in json) {
|
||||
final value = TranscodePolicy.fromJson(row);
|
||||
if (value != null) {
|
||||
result.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toList(growable: growable);
|
||||
}
|
||||
}
|
||||
|
||||
/// Transformation class that can [encode] an instance of [TranscodePolicy] to String,
|
||||
/// and [decode] dynamic data back to [TranscodePolicy].
|
||||
class TranscodePolicyTypeTransformer {
|
||||
factory TranscodePolicyTypeTransformer() => _instance ??= const TranscodePolicyTypeTransformer._();
|
||||
|
||||
const TranscodePolicyTypeTransformer._();
|
||||
|
||||
String encode(TranscodePolicy data) => data.value;
|
||||
|
||||
/// Decodes a [dynamic value][data] to a TranscodePolicy.
|
||||
///
|
||||
/// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully,
|
||||
/// then null is returned. However, if [allowNull] is false and the [dynamic value][data]
|
||||
/// cannot be decoded successfully, then an [UnimplementedError] is thrown.
|
||||
///
|
||||
/// The [allowNull] is very handy when an API changes and a new enum value is added or removed,
|
||||
/// and users are still using an old app with the old code.
|
||||
TranscodePolicy? decode(dynamic data, {bool allowNull = true}) {
|
||||
if (data != null) {
|
||||
switch (data) {
|
||||
case r'all': return TranscodePolicy.all;
|
||||
case r'optimal': return TranscodePolicy.optimal;
|
||||
case r'required': return TranscodePolicy.required_;
|
||||
case r'disabled': return TranscodePolicy.disabled;
|
||||
default:
|
||||
if (!allowNull) {
|
||||
throw ArgumentError('Unknown enum value to decode: $data');
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Singleton [TranscodePolicyTypeTransformer] instance.
|
||||
static TranscodePolicyTypeTransformer? _instance;
|
||||
}
|
||||
|
88
mobile/openapi/lib/model/video_codec.dart
generated
Normal file
88
mobile/openapi/lib/model/video_codec.dart
generated
Normal file
@ -0,0 +1,88 @@
|
||||
//
|
||||
// AUTO-GENERATED FILE, DO NOT MODIFY!
|
||||
//
|
||||
// @dart=2.12
|
||||
|
||||
// ignore_for_file: unused_element, unused_import
|
||||
// ignore_for_file: always_put_required_named_parameters_first
|
||||
// ignore_for_file: constant_identifier_names
|
||||
// ignore_for_file: lines_longer_than_80_chars
|
||||
|
||||
part of openapi.api;
|
||||
|
||||
|
||||
class VideoCodec {
|
||||
/// Instantiate a new enum with the provided [value].
|
||||
const VideoCodec._(this.value);
|
||||
|
||||
/// The underlying value of this enum member.
|
||||
final String value;
|
||||
|
||||
@override
|
||||
String toString() => value;
|
||||
|
||||
String toJson() => value;
|
||||
|
||||
static const h264 = VideoCodec._(r'h264');
|
||||
static const hevc = VideoCodec._(r'hevc');
|
||||
static const vp9 = VideoCodec._(r'vp9');
|
||||
|
||||
/// List of all possible values in this [enum][VideoCodec].
|
||||
static const values = <VideoCodec>[
|
||||
h264,
|
||||
hevc,
|
||||
vp9,
|
||||
];
|
||||
|
||||
static VideoCodec? fromJson(dynamic value) => VideoCodecTypeTransformer().decode(value);
|
||||
|
||||
static List<VideoCodec>? listFromJson(dynamic json, {bool growable = false,}) {
|
||||
final result = <VideoCodec>[];
|
||||
if (json is List && json.isNotEmpty) {
|
||||
for (final row in json) {
|
||||
final value = VideoCodec.fromJson(row);
|
||||
if (value != null) {
|
||||
result.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toList(growable: growable);
|
||||
}
|
||||
}
|
||||
|
||||
/// Transformation class that can [encode] an instance of [VideoCodec] to String,
|
||||
/// and [decode] dynamic data back to [VideoCodec].
|
||||
class VideoCodecTypeTransformer {
|
||||
factory VideoCodecTypeTransformer() => _instance ??= const VideoCodecTypeTransformer._();
|
||||
|
||||
const VideoCodecTypeTransformer._();
|
||||
|
||||
String encode(VideoCodec data) => data.value;
|
||||
|
||||
/// Decodes a [dynamic value][data] to a VideoCodec.
|
||||
///
|
||||
/// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully,
|
||||
/// then null is returned. However, if [allowNull] is false and the [dynamic value][data]
|
||||
/// cannot be decoded successfully, then an [UnimplementedError] is thrown.
|
||||
///
|
||||
/// The [allowNull] is very handy when an API changes and a new enum value is added or removed,
|
||||
/// and users are still using an old app with the old code.
|
||||
VideoCodec? decode(dynamic data, {bool allowNull = true}) {
|
||||
if (data != null) {
|
||||
switch (data) {
|
||||
case r'h264': return VideoCodec.h264;
|
||||
case r'hevc': return VideoCodec.hevc;
|
||||
case r'vp9': return VideoCodec.vp9;
|
||||
default:
|
||||
if (!allowNull) {
|
||||
throw ArgumentError('Unknown enum value to decode: $data');
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Singleton [VideoCodecTypeTransformer] instance.
|
||||
static VideoCodecTypeTransformer? _instance;
|
||||
}
|
||||
|
Reference in New Issue
Block a user