Encoding added Requested Encoding to the request, made request async, some refactoring

This commit is contained in:
Samuele Lorefice
2025-12-15 19:00:31 +01:00
parent db20f5d54d
commit db6873c93c
6 changed files with 108 additions and 44 deletions

View File

@@ -14,8 +14,8 @@ public class EncoderService : BackgroundService, IEncoderService {
public EncoderService(ILogger<EncoderService> logger, IOptions<FFmpegOptions> ffmpegOptions) {
Logger = logger;
options = ffmpegOptions.Value;
options.FfmpegPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), options.FfmpegPath));
options.TemporaryFilesPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), options.TemporaryFilesPath));
options.FfmpegPath = options.FfmpegPath.AbsoluteOrProcessPath();
options.TemporaryFilesPath = options.TemporaryFilesPath.AbsoluteOrProcessPath();
Directory.CreateDirectory(options.TemporaryFilesPath); // Ensure the temporary files directory exists
JobQueue = new();
@@ -63,7 +63,7 @@ public class EncoderService : BackgroundService, IEncoderService {
void ProcessJob(EncodingJob job) {
job.Status = JobStatus.InProgress;
var file = job.OrigFilePath;
var file = job.FilePath;
var mediaInfo = FFProbe.Analyse(file);
if (mediaInfo.PrimaryVideoStream == null) {
job.Status = JobStatus.Failed;
@@ -71,7 +71,7 @@ public class EncoderService : BackgroundService, IEncoderService {
}
var W = mediaInfo.PrimaryVideoStream.Width;
var H = mediaInfo.PrimaryVideoStream.Height;
string outputPath = Path.Combine(options.TemporaryFilesPath, Path.GetFileName(job.OrigFilePath));
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())
.OutputToFile(outputPath, true, args => args
@@ -99,8 +99,11 @@ public class EncoderService : BackgroundService, IEncoderService {
if(status) {
job.Status = JobStatus.Completed;
job.EncodedFilePath = outputPath;
File.Delete(file); // Clean up original file
} else {
job.Status = JobStatus.Failed;
// Clean up any partially created output file. Leave original file for retry.
if (File.Exists(outputPath)) File.Delete(outputPath);
}
job.CompletedAt = DateTime.Now;
job.Progress = 1.0f;