Encoder/Lib/EncodeWorker.cs
2026-01-12 11:32:18 +09:00

45 lines
1.2 KiB
C#

using System.Diagnostics;
namespace Encoder.Lib;
public class EncodeWorker
{
private readonly ProcessStartInfo startInfo;
public EncodeWorker(string workingDirectory, string inputFile, string outputFile, string encodeParam)
{
var arguments = $"-y -i \"{inputFile}\" {encodeParam} \"{outputFile}\"";
this.startInfo = new ProcessStartInfo()
{
FileName = "ffmpeg",
WorkingDirectory = workingDirectory,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
};
}
public async ValueTask<int> Run()
{
try
{
Console.WriteLine($"{this.startInfo.FileName} {this.startInfo.Arguments}");
using Process? process = Process.Start(this.startInfo);
if (process is null)
{
throw new FileNotFoundException();
}
await process.StandardError.ReadToEndAsync().ConfigureAwait(false);
await process.WaitForExitAsync().ConfigureAwait(false);
return process.ExitCode;
}
catch
{
return -1;
}
}
}