From 4f2e566ab8a0b333ec6a1355cbb5a8bfa2c0911a Mon Sep 17 00:00:00 2001 From: routersys Date: Tue, 15 Sep 2026 20:11:02 +0900 Subject: [PATCH] =?UTF-8?q?ffmpeg=E3=81=8C=E7=95=B0=E5=B8=B8=E7=B5=82?= =?UTF-8?q?=E4=BA=86=E3=81=97=E3=81=9F=E6=99=82=E3=81=AF=E5=85=A5=E5=8A=9B?= =?UTF-8?q?=E3=81=AE=E6=9B=B8=E3=81=8D=E8=BE=BC=E3=81=BF=E3=81=AE=E5=A4=B1?= =?UTF-8?q?=E6=95=97=E3=82=88=E3=82=8A=E7=B5=82=E4=BA=86=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E3=82=92=E8=BF=94=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyForge.Tests/ProxyEncoderTests.cs | 52 ++++++++++++++++++++++ ProxyForge/Encoding/FFmpegProcessRunner.cs | 9 ++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/ProxyForge.Tests/ProxyEncoderTests.cs b/ProxyForge.Tests/ProxyEncoderTests.cs index da6d5b7..b4b12a6 100644 --- a/ProxyForge.Tests/ProxyEncoderTests.cs +++ b/ProxyForge.Tests/ProxyEncoderTests.cs @@ -187,6 +187,20 @@ public sealed class ProxyEncoderTests Assert.False(File.Exists(workspace.OutputPath)); } + [Fact] + public async Task AnOutputThatCannotBeOpenedIsReportedAsAnFFmpegFailure() + { + Ymm4TestEnvironment.Require(); + using var workspace = new Workspace(); + var encoder = workspace.CreateEncoder(640, 360, 30, 1, 10); + using var session = await encoder.OpenAsync(workspace.Request(50), TestContext.Current.CancellationToken); + var missing = Path.Combine(workspace.Path, "missing", "proxy.mp4"); + + var exception = await Assert.ThrowsAsync(() => session.EncodeAsync(0, 10, missing, null, TestContext.Current.CancellationToken)); + + Assert.Equal(ProxyEncodeFailure.FFmpegFailed, exception.Failure); + } + [Fact] public async Task ASoftEdgeIsNotMistakenForTransparency() { @@ -474,6 +488,44 @@ public sealed class ProxyEncoderTests Assert.IsType(failure.InnerException); } + [Fact] + public async Task RunAsyncReportsTheExitOfFFmpegThatStoppedReadingTheInput() + { + Ymm4TestEnvironment.Require(); + using var workspace = new Workspace(); + var missing = Path.Combine(workspace.Path, "missing", "proxy.mp4"); + Exception? failure = null; + + var result = await FFmpegProcessRunner.RunAsync( + Ymm4TestEnvironment.FFmpegPath, + arguments => + { + var request = new FFmpegEncodeRequest(missing, 32, 32, new FrameRate(30, 1), 100_000, 1, SoftwareEncoder); + FFmpegArguments.WriteEncode(arguments, in request); + }, + workspace.Path, + (destination, cancellationToken) => Task.Run(() => + { + var frame = new byte[32 * 32 * 4]; + try + { + for (var index = 0; index < 1024; index++) + destination.Write(frame); + } + catch (IOException exception) + { + failure = exception; + throw; + } + }, cancellationToken), + ProcessPriorityClass.BelowNormal, + TestContext.Current.CancellationToken); + + Assert.IsType(failure); + Assert.False(result.IsSuccess); + Assert.Contains(missing, result.Diagnostics); + } + [Fact] public async Task RunAsyncRethrowsOtherFailuresOfTheInputWriterUnchanged() { diff --git a/ProxyForge/Encoding/FFmpegProcessRunner.cs b/ProxyForge/Encoding/FFmpegProcessRunner.cs index e72c645..96516ad 100644 --- a/ProxyForge/Encoding/FFmpegProcessRunner.cs +++ b/ProxyForge/Encoding/FFmpegProcessRunner.cs @@ -133,12 +133,13 @@ internal static class FFmpegProcessRunner cancellationToken.ThrowIfCancellationRequested(); - if (inputFailure is IOException) - throw new InvalidOperationException(string.Concat("Failed to send frames to FFmpeg. ", diagnostics.ToString()), inputFailure); - if (inputFailure is not null) + var result = new FFmpegProcessResult(process.ExitCode, diagnostics.ToString()); + if (inputFailure is IOException && result.IsSuccess) + throw new InvalidOperationException(string.Concat("Failed to send frames to FFmpeg. ", result.Diagnostics), inputFailure); + if (inputFailure is not (null or IOException)) ExceptionDispatchInfo.Throw(inputFailure); - return new FFmpegProcessResult(process.ExitCode, diagnostics.ToString()); + return result; } static async Task WriteStandardInputAsync(Process process, FFmpegInputWriter writer, CancellationToken cancellationToken)