diff --git a/Encoder/EncoderService.cs b/Encoder/EncoderService.cs index 0c51b8a..4b5037c 100644 --- a/Encoder/EncoderService.cs +++ b/Encoder/EncoderService.cs @@ -39,7 +39,7 @@ public class EncoderService : BackgroundService, IEncoderService { if (JobQueue.Count > 0) { // Grab a reference to the next job in queue var job = JobQueue.Peek(); - ProcessJob(job); // Process the job + ProcessJob(job, stoppingToken).Wait(stoppingToken); // Process the job JobQueue.Dequeue(); // Remove it from the queue Jobs.Add(job); // Add it to the completed jobs list } @@ -61,19 +61,21 @@ public class EncoderService : BackgroundService, IEncoderService { return JobQueue.Concat(Jobs); } - void ProcessJob(EncodingJob job) { + async Task ProcessJob(EncodingJob job, CancellationToken cancellationToken) { job.Status = JobStatus.InProgress; var file = job.FilePath; - var mediaInfo = FFProbe.Analyse(file); + var mediaInfo = await FFProbe.AnalyseAsync(file, cancellationToken: cancellationToken); if (mediaInfo.PrimaryVideoStream == null) { job.Status = JobStatus.Failed; return; } - var W = mediaInfo.PrimaryVideoStream.Width; - var H = mediaInfo.PrimaryVideoStream.Height; + + int W = mediaInfo.PrimaryVideoStream.Width; + int H = mediaInfo.PrimaryVideoStream.Height; string outputPath = Path.Combine(options.TemporaryFilesPath, Path.GetFileName(job.FilePath)); int qp = Utils.ToQPValue(W, H); - var status = FFMpegArguments.FromFileInput(file, true, args => args.WithHardwareAcceleration()) + var result = FFMpegArguments + .FromFileInput(file, true, args => args.WithHardwareAcceleration()) .OutputToFile(outputPath, true, args => args .CopyChannel(Channel.Audio) .CopyChannel(Channel.Subtitle) @@ -84,8 +86,9 @@ public class EncoderService : BackgroundService, IEncoderService { .WithArgument(new NvencQPArgument((byte)qp)) .WithFastStart() ) - .NotifyOnProgress(progress => { - Logger.Log(LogLevel.Information, + .NotifyOnProgress(progress => + { + Logger.Log(LogLevel.Information, $""" Job {job.Id}: {progress / mediaInfo.Duration:P} Processed {progress:g} | Total {mediaInfo.Duration:g} @@ -94,9 +97,11 @@ public class EncoderService : BackgroundService, IEncoderService { Output path: {outputPath} """); job.Progress = (float)(progress / mediaInfo.Duration); - }) - .ProcessSynchronously(); - if(status) { + }).CancellableThrough(cancellationToken) + .ProcessAsynchronously(); + result.Wait(cancellationToken); + + if(result.Result) { job.Status = JobStatus.Completed; job.EncodedFilePath = outputPath; File.Delete(file); // Clean up original file diff --git a/Encoder/EncodingJob.cs b/Encoder/EncodingJob.cs index a72e873..b92b25a 100644 --- a/Encoder/EncodingJob.cs +++ b/Encoder/EncodingJob.cs @@ -9,8 +9,9 @@ public enum JobStatus { public enum EncoderType { + H264, + HEVC, AV1, - HEVC } public record EncodingJob { diff --git a/Encoder/Program.cs b/Encoder/Program.cs index e834398..41458ab 100644 --- a/Encoder/Program.cs +++ b/Encoder/Program.cs @@ -1,6 +1,7 @@ +using System.Net.Http.Headers; using Encoder; using Microsoft.AspNetCore.Http.Features; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.WebUtilities; var builder = WebApplication.CreateBuilder(args); @@ -29,37 +30,8 @@ app.UseHttpsRedirection(); app.MapPost("encode", async context => { // Disable request size limit context.Features.Get()?.MaxRequestBodySize = null; - - var request = context.Request; - if (!request.HasFormContentType) { - context.Response.StatusCode = 400; - await context.Response.WriteAsync("Invalid content type. Expected multipart/form-data."); - return; - } - - var form = request.Form; - var encoderType = form["encoder"].FirstOrDefault(); - if(encoderType == null || !Enum.TryParse(encoderType, true, out var parsedEncoderType)) { - context.Response.StatusCode = 400; - await context.Response.WriteAsync("Invalid or missing encoderType parameter."); - return; - } - // Contrary to what it seems, the "name" here is the form field name, not the file name - var file = form.Files.GetFile("video"); - if (file == null) { - context.Response.StatusCode = 400; - await context.Response.WriteAsync("No video file provided."); - return; - } - - // Save the file to a temporary location - var jobGuid = Guid.NewGuid(); - var tempFilePath = Path.GetFullPath(Path.Combine(uploadsPath, jobGuid.ToString("D")+Path.GetExtension(file.FileName))); - await using (var stream = File.Create(tempFilePath)) file.CopyTo(stream); - - // Create and enqueue the encoding job - var job = new EncodingJob(jobGuid, tempFilePath, parsedEncoderType); + // Fail fast, if EncoderService is not available don't bother processing the request var encSrv = context.RequestServices.GetService(); if (encSrv == null) { context.Response.StatusCode = 500; @@ -67,7 +39,52 @@ app.MapPost("encode", async context => { return; } + // Parse multipart form data manually to handle large file uploads + var request = context.Request; + if (!request.HasFormContentType) { + context.Response.StatusCode = 400; + await context.Response.WriteAsync("Invalid content type. Expected multipart/form-data."); + return; + } + + Guid jobGuid = Guid.NewGuid(); + EncoderType encoderType = EncoderType.H264; + string tempFilePath = Path.Combine(uploadsPath, jobGuid.ToString("D")); + + var boundary = Utils.GetBoundary(request.ContentType!); + var reader = new MultipartReader(boundary!, context.Request.Body); + MultipartSection? section; + while ((section = await reader.ReadNextSectionAsync()) != null) { + if(section.ContentDisposition is null) continue; + + var contentDisposition = ContentDispositionHeaderValue.Parse(section.ContentDisposition); + switch (contentDisposition.Name) { + case "encoder": { + string encoderTypeStr = await section.ReadAsStringAsync(); + if (!Enum.TryParse(encoderTypeStr, true, out encoderType)) { + context.Response.StatusCode = 400; + await context.Response.WriteAsync("Invalid or missing encoderType parameter."); + return; + } + continue; + } + case "video" when string.IsNullOrWhiteSpace(contentDisposition.FileName): { + context.Response.StatusCode = 400; + await context.Response.WriteAsync("No video file provided."); + return; + } + case "video": { + tempFilePath += Path.GetExtension(contentDisposition.FileName); + await using var fstream = File.Create(tempFilePath); + await section.Body.CopyToAsync(fstream); + continue; + } + } + } + + var job = new EncodingJob(jobGuid, tempFilePath, encoderType); encSrv.EnqueueJob(job); + context.Response.StatusCode = 200; await context.Response.WriteAsJsonAsync(new { JobId = jobGuid }); }).WithFormOptions(multipartBodyLengthLimit: 1_073_741_824L*64L); diff --git a/Encoder/Utils.cs b/Encoder/Utils.cs index f860896..1199322 100644 --- a/Encoder/Utils.cs +++ b/Encoder/Utils.cs @@ -41,9 +41,6 @@ public static class Utils { return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.ProcessPath)!, path)); } - public static bool IsMultipartContentType(string contentType) - => !string.IsNullOrEmpty(contentType) && contentType.Contains("multipart/form-data"); - public static string? GetBoundary(string contentType) { var boundary = HeaderUtilities.RemoveQuotes(