diff --git a/Wasari.Daemon/Handlers/DownloadRequestHandler.cs b/Wasari.Daemon/Handlers/DownloadRequestHandler.cs index 8f5b15d..c84f40a 100644 --- a/Wasari.Daemon/Handlers/DownloadRequestHandler.cs +++ b/Wasari.Daemon/Handlers/DownloadRequestHandler.cs @@ -5,6 +5,7 @@ using Wasari.App.Abstractions; using Wasari.Daemon.Models; using Wasari.Daemon.Options; +using Wasari.FFmpeg; using Wolverine; namespace Wasari.Daemon.Handlers; @@ -13,10 +14,8 @@ public class DownloadRequestHandler { public async ValueTask Handle(DownloadRequest request, ILogger logger, - DownloadServiceSolver downloadServiceSolver, IServiceProvider serviceProvider, IOptions daemonOptions, - IOptions downloadOptions, IMessageBus messageBus) { if (daemonOptions.Value.RedisLockEnabled) @@ -31,18 +30,32 @@ public async ValueTask Handle(DownloadRequest request, return; } - await DownloadEpisode(request, logger, downloadServiceSolver, serviceProvider, daemonOptions, downloadOptions, messageBus); + await DownloadEpisode(request, logger, serviceProvider, messageBus); } else { - await DownloadEpisode(request, logger, downloadServiceSolver, serviceProvider, daemonOptions, downloadOptions, messageBus); + await DownloadEpisode(request, logger, serviceProvider, messageBus); } } - private static async ValueTask DownloadEpisode(DownloadRequest request, ILogger logger, DownloadServiceSolver downloadServiceSolver, IServiceProvider serviceProvider, IOptions daemonOptions, IOptions downloadOptions, IMessageBus messageBus) + private static async ValueTask DownloadEpisode(DownloadRequest request, ILogger logger, IServiceProvider serviceProvider, IMessageBus messageBus) { + await using var serviceScope = serviceProvider.CreateAsyncScope(); + logger.LogInformation("Starting download of {Url}", request.Url); + var downloadOptions = serviceScope.ServiceProvider.GetRequiredService>(); + var daemonOptions = serviceScope.ServiceProvider.GetRequiredService>(); + var downloadServiceSolver = serviceScope.ServiceProvider.GetRequiredService(); + + if (request.HevcOptions != null) + { + var ffmpegOptions = serviceScope.ServiceProvider.GetRequiredService>(); + ffmpegOptions.Value.HevcProfile = request.HevcOptions.Profile; + ffmpegOptions.Value.HevcQualityMin = request.HevcOptions.Qmin; + ffmpegOptions.Value.HevcQualityMax = request.HevcOptions.Qmax; + } + var downloadService = downloadServiceSolver.GetService(request.Url); var episodesRange = new Ranges(request.EpisodeNumber, request.EpisodeNumber); var seasonsRange = new Ranges(request.SeasonNumber, request.SeasonNumber); @@ -57,8 +70,8 @@ private static async ValueTask DownloadEpisode(DownloadRequest request, ILogger { case DownloadedEpisodeStatus.Downloaded: logger.LogInformation("Downloaded {Episode}", downloadedEpisode); - - if (downloadedEpisode.FilePath != null && daemonOptions.Value.CheckVideoIntegrityAfterDownload) + + if (downloadedEpisode.FilePath != null && daemonOptions.Value.CheckVideoIntegrityAfterDownload) await messageBus.PublishAsync(new CheckVideoIntegrityRequest(downloadedEpisode.FilePath, true)); break; case DownloadedEpisodeStatus.AlreadyExists: diff --git a/Wasari.Daemon/Models/DownloadRequest.cs b/Wasari.Daemon/Models/DownloadRequest.cs index 393c34d..069b1ff 100644 --- a/Wasari.Daemon/Models/DownloadRequest.cs +++ b/Wasari.Daemon/Models/DownloadRequest.cs @@ -1,3 +1,7 @@ -namespace Wasari.Daemon.Models; +using Wasari.FFmpeg; -public record DownloadRequest(Uri Url, int EpisodeNumber, int SeasonNumber, string? SeriesNameOverride); \ No newline at end of file +namespace Wasari.Daemon.Models; + +public record DownloadRequestHevcOptions(HevcProfile Profile, int? Qmin, int? Qmax); + +public record DownloadRequest(Uri Url, int EpisodeNumber, int SeasonNumber, string? SeriesNameOverride, DownloadRequestHevcOptions? HevcOptions); \ No newline at end of file diff --git a/Wasari.Daemon/Validators/DownloadRequestValidator.cs b/Wasari.Daemon/Validators/DownloadRequestValidator.cs index 1ec7aa8..11a9514 100644 --- a/Wasari.Daemon/Validators/DownloadRequestValidator.cs +++ b/Wasari.Daemon/Validators/DownloadRequestValidator.cs @@ -1,5 +1,6 @@ using FluentValidation; using Wasari.Daemon.Models; +using Wasari.FFmpeg; // ReSharper disable UnusedType.Global @@ -15,5 +16,13 @@ public DownloadRequestValidator() .WithMessage("Url must be an absolute uri"); RuleFor(i => i.EpisodeNumber).GreaterThanOrEqualTo(0); RuleFor(i => i.SeasonNumber).GreaterThan(0); + RuleFor(i => i.HevcOptions) + .Must(i => i is null or + {Profile: HevcProfile.High, Qmax: null,Qmin: null} + or {Profile: HevcProfile.Medium, Qmax: null, Qmin: null} + or {Profile: HevcProfile.Low, Qmax: null, Qmin: null} + or {Profile: HevcProfile.Custom, Qmax: >= 1 and <= 51, Qmin: >= 1 and <= 51} + ) + .WithMessage("Invalid HEVC options"); } } \ No newline at end of file