Resolved ffmpeg remaining as a zombie process
This commit is contained in:
@@ -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,7 +86,8 @@ public class EncoderService : BackgroundService, IEncoderService {
|
||||
.WithArgument(new NvencQPArgument((byte)qp))
|
||||
.WithFastStart()
|
||||
)
|
||||
.NotifyOnProgress(progress => {
|
||||
.NotifyOnProgress(progress =>
|
||||
{
|
||||
Logger.Log(LogLevel.Information,
|
||||
$"""
|
||||
Job {job.Id}: {progress / mediaInfo.Duration:P}
|
||||
@@ -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
|
||||
|
||||
@@ -9,8 +9,9 @@ public enum JobStatus {
|
||||
|
||||
|
||||
public enum EncoderType {
|
||||
H264,
|
||||
HEVC,
|
||||
AV1,
|
||||
HEVC
|
||||
}
|
||||
|
||||
public record EncodingJob {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -30,36 +31,7 @@ app.MapPost("encode", async context => {
|
||||
// Disable request size limit
|
||||
context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.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>(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<EncoderService>();
|
||||
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<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);
|
||||
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsJsonAsync(new { JobId = jobGuid });
|
||||
}).WithFormOptions(multipartBodyLengthLimit: 1_073_741_824L*64L);
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user