| | | 1 | | using Anichron.Core.Data.Repository; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Anichron.Worker.Ingestion.Pipeline; |
| | | 4 | | |
| | | 5 | | namespace Anichron.Worker.Ingestion.Proxy; |
| | | 6 | | |
| | 158 | 7 | | internal sealed partial class PairedMovProxyWriter( |
| | 158 | 8 | | IEnumerable<IVideoProxyGenerator> generators, |
| | 158 | 9 | | IProxyDirectoryStrategy proxyDirectoryStrategy, |
| | 158 | 10 | | ProxyStagingWriter stagingWriter, |
| | 158 | 11 | | IMediaAssetRepository repository, |
| | 158 | 12 | | ILogger<PairedMovProxyWriter> logger) |
| | | 13 | | { |
| | | 14 | | internal async Task<bool> WriteMissingAsync(IngestionContext context, MediaAsset mov, CancellationToken ct) |
| | 38 | 15 | | { |
| | 38 | 16 | | var written = context.ProxyFilesOf(mov.Id); |
| | 38 | 17 | | var missing = generators |
| | 38 | 18 | | .Where(generator => !mov.ProxyFiles.Concat(written).Any(proxy => proxy.ProxyType == generator.ProxyType)) |
| | 38 | 19 | | .ToArray(); |
| | 38 | 20 | | if (missing.Length == 0) |
| | 11 | 21 | | return false; |
| | | 22 | | |
| | 27 | 23 | | await WriteAsync(context, mov, mov.ContentHash, missing, ct); |
| | 27 | 24 | | repository.AddProxyFiles(context.ProxyFilesOf(mov.Id)); |
| | 27 | 25 | | return true; |
| | 38 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary>Writes every video proxy of the item's video into its content's folder, for <paramref name="mov"/> to t |
| | | 29 | | /// <param name="context">The item whose video changed content.</param> |
| | | 30 | | /// <param name="mov">The row that takes the content; it owns the proxies.</param> |
| | | 31 | | /// <param name="ct">Cancels the transcode.</param> |
| | | 32 | | /// <returns>The written proxies, not added to the repository.</returns> |
| | | 33 | | internal async Task<IReadOnlyList<ProxyFile>> WriteForNewContentAsync(IngestionContext context, MediaAsset mov, Canc |
| | 11 | 34 | | { |
| | 11 | 35 | | await WriteAsync(context, mov, context.SecondaryHash!, [.. generators], ct); |
| | 11 | 36 | | return context.ProxyFilesOf(mov.Id); |
| | 11 | 37 | | } |
| | | 38 | | |
| | | 39 | | private async Task WriteAsync( |
| | | 40 | | IngestionContext context, MediaAsset mov, string contentHash, IVideoProxyGenerator[] pending, CancellationToken |
| | 38 | 41 | | { |
| | 38 | 42 | | var source = context.Item.SecondaryFile!.AbsolutePath; |
| | 38 | 43 | | var directory = proxyDirectoryStrategy.GetDirectory(context.Config.Id, contentHash); |
| | 190 | 44 | | foreach (var generator in pending) |
| | 38 | 45 | | { |
| | 38 | 46 | | await stagingWriter.WriteAsync( |
| | 38 | 47 | | context, |
| | 38 | 48 | | mov.Id, |
| | 38 | 49 | | $"{directory}/{generator.FileName}", |
| | 38 | 50 | | generator.ProxyType, |
| | 38 | 51 | | (temporaryPath, token) => generator.TranscodeAsync(source, temporaryPath, token), |
| | 38 | 52 | | ct); |
| | 38 | 53 | | } |
| | | 54 | | |
| | 38 | 55 | | context.StageMovProxies(mov.Id); |
| | 38 | 56 | | Log.ProxiesGenerated(logger, pending.Length, context.Item.SecondaryFile.RelativePath); |
| | 38 | 57 | | } |
| | | 58 | | |
| | | 59 | | private static partial class Log |
| | | 60 | | { |
| | | 61 | | [LoggerMessage(Level = LogLevel.Information, Message = "Generated {Count} video proxy file(s) for paired {Relati |
| | | 62 | | public static partial void ProxiesGenerated(ILogger logger, int count, string relativePath); |
| | | 63 | | } |
| | | 64 | | } |