Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Adds HevcOptions support to the daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cordeiro committed Dec 29, 2023
1 parent f91692a commit ffd33fe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
27 changes: 20 additions & 7 deletions Wasari.Daemon/Handlers/DownloadRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,10 +14,8 @@ public class DownloadRequestHandler
{
public async ValueTask Handle(DownloadRequest request,
ILogger<DownloadRequestHandler> logger,
DownloadServiceSolver downloadServiceSolver,
IServiceProvider serviceProvider,
IOptions<DaemonOptions> daemonOptions,
IOptions<DownloadOptions> downloadOptions,
IMessageBus messageBus)
{
if (daemonOptions.Value.RedisLockEnabled)
Expand All @@ -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> daemonOptions, IOptions<DownloadOptions> 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<IOptions<DownloadOptions>>();
var daemonOptions = serviceScope.ServiceProvider.GetRequiredService<IOptions<DaemonOptions>>();
var downloadServiceSolver = serviceScope.ServiceProvider.GetRequiredService<DownloadServiceSolver>();

if (request.HevcOptions != null)
{
var ffmpegOptions = serviceScope.ServiceProvider.GetRequiredService<IOptions<FFmpegOptions>>();
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);
Expand All @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions Wasari.Daemon/Models/DownloadRequest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
namespace Wasari.Daemon.Models;
using Wasari.FFmpeg;

public record DownloadRequest(Uri Url, int EpisodeNumber, int SeasonNumber, string? SeriesNameOverride);
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);
9 changes: 9 additions & 0 deletions Wasari.Daemon/Validators/DownloadRequestValidator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentValidation;
using Wasari.Daemon.Models;
using Wasari.FFmpeg;

// ReSharper disable UnusedType.Global

Expand All @@ -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");
}
}

0 comments on commit ffd33fe

Please sign in to comment.