Resolved ffmpeg remaining as a zombie process

This commit is contained in:
Samuele Lorefice
2025-12-15 20:39:38 +01:00
parent aafc3dab99
commit 24df58056c
4 changed files with 66 additions and 46 deletions

View File

@@ -39,7 +39,7 @@ public class EncoderService : BackgroundService, IEncoderService {
if (JobQueue.Count > 0) { if (JobQueue.Count > 0) {
// Grab a reference to the next job in queue // Grab a reference to the next job in queue
var job = JobQueue.Peek(); var job = JobQueue.Peek();
ProcessJob(job); // Process the job ProcessJob(job, stoppingToken).Wait(stoppingToken); // Process the job
JobQueue.Dequeue(); // Remove it from the queue JobQueue.Dequeue(); // Remove it from the queue
Jobs.Add(job); // Add it to the completed jobs list Jobs.Add(job); // Add it to the completed jobs list
} }
@@ -61,19 +61,21 @@ public class EncoderService : BackgroundService, IEncoderService {
return JobQueue.Concat(Jobs); return JobQueue.Concat(Jobs);
} }
void ProcessJob(EncodingJob job) { async Task ProcessJob(EncodingJob job, CancellationToken cancellationToken) {
job.Status = JobStatus.InProgress; job.Status = JobStatus.InProgress;
var file = job.FilePath; var file = job.FilePath;
var mediaInfo = FFProbe.Analyse(file); var mediaInfo = await FFProbe.AnalyseAsync(file, cancellationToken: cancellationToken);
if (mediaInfo.PrimaryVideoStream == null) { if (mediaInfo.PrimaryVideoStream == null) {
job.Status = JobStatus.Failed; job.Status = JobStatus.Failed;
return; 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)); string outputPath = Path.Combine(options.TemporaryFilesPath, Path.GetFileName(job.FilePath));
int qp = Utils.ToQPValue(W, H); 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 .OutputToFile(outputPath, true, args => args
.CopyChannel(Channel.Audio) .CopyChannel(Channel.Audio)
.CopyChannel(Channel.Subtitle) .CopyChannel(Channel.Subtitle)
@@ -84,7 +86,8 @@ public class EncoderService : BackgroundService, IEncoderService {
.WithArgument(new NvencQPArgument((byte)qp)) .WithArgument(new NvencQPArgument((byte)qp))
.WithFastStart() .WithFastStart()
) )
.NotifyOnProgress(progress => { .NotifyOnProgress(progress =>
{
Logger.Log(LogLevel.Information, Logger.Log(LogLevel.Information,
$""" $"""
Job {job.Id}: {progress / mediaInfo.Duration:P} Job {job.Id}: {progress / mediaInfo.Duration:P}
@@ -94,9 +97,11 @@ public class EncoderService : BackgroundService, IEncoderService {
Output path: {outputPath} Output path: {outputPath}
"""); """);
job.Progress = (float)(progress / mediaInfo.Duration); job.Progress = (float)(progress / mediaInfo.Duration);
}) }).CancellableThrough(cancellationToken)
.ProcessSynchronously(); .ProcessAsynchronously();
if(status) { result.Wait(cancellationToken);
if(result.Result) {
job.Status = JobStatus.Completed; job.Status = JobStatus.Completed;
job.EncodedFilePath = outputPath; job.EncodedFilePath = outputPath;
File.Delete(file); // Clean up original file File.Delete(file); // Clean up original file

View File

@@ -9,8 +9,9 @@ public enum JobStatus {
public enum EncoderType { public enum EncoderType {
H264,
HEVC,
AV1, AV1,
HEVC
} }
public record EncodingJob { public record EncodingJob {

View File

@@ -1,6 +1,7 @@
using System.Net.Http.Headers;
using Encoder; using Encoder;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.WebUtilities;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -30,36 +31,7 @@ app.MapPost("encode", async context => {
// Disable request size limit // Disable request size limit
context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize = null; context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize = null;
var request = context.Request; // Fail fast, if EncoderService is not available don't bother processing the 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>(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);
var encSrv = context.RequestServices.GetService<EncoderService>(); var encSrv = context.RequestServices.GetService<EncoderService>();
if (encSrv == null) { if (encSrv == null) {
context.Response.StatusCode = 500; context.Response.StatusCode = 500;
@@ -67,7 +39,52 @@ app.MapPost("encode", async context => {
return; 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<EncoderType>(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); encSrv.EnqueueJob(job);
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new { JobId = jobGuid }); await context.Response.WriteAsJsonAsync(new { JobId = jobGuid });
}).WithFormOptions(multipartBodyLengthLimit: 1_073_741_824L*64L); }).WithFormOptions(multipartBodyLengthLimit: 1_073_741_824L*64L);

View File

@@ -41,9 +41,6 @@ public static class Utils {
return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.ProcessPath)!, path)); 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) public static string? GetBoundary(string contentType)
{ {
var boundary = HeaderUtilities.RemoveQuotes( var boundary = HeaderUtilities.RemoveQuotes(