diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index bc44f20..e4f750d 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -656,23 +656,33 @@ public void Video_TranscodeInMemory() [TestMethod, Timeout(10000)] public async Task Video_Cancel_Async() { - await using var resStream = new MemoryStream(); - var reader = new StreamPipeSink(resStream); - var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(512, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 128, 128)); - + var output = Input.OutputLocation(VideoType.Mp4); + var task = FFMpegArguments - .FromPipeInput(writer) - .OutputToPipe(reader, opt => opt - .WithVideoCodec("vp9") - .ForceFormat("webm")) + .FromFileInput(VideoLibrary.LocalVideo) + .OutputToFile(output, false, opt => opt + .Resize(new Size(1000, 1000)) + .WithAudioCodec(AudioCodec.Aac) + .WithVideoCodec(VideoCodec.LibX264) + .WithConstantRateFactor(14) + .WithSpeedPreset(Speed.VerySlow) + .Loop(3)) .CancellableThrough(out var cancel) .ProcessAsynchronously(false); - - await Task.Delay(300); - cancel(); - var result = await task; - Assert.IsFalse(result); + try + { + await Task.Delay(300); + cancel(); + + var result = await task; + Assert.IsFalse(result); + } + finally + { + if (File.Exists(output)) + File.Delete(output); + } } } } diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs index 2307787..92fa6fe 100644 --- a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs +++ b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs @@ -51,18 +51,20 @@ public bool ProcessSynchronously(bool throwOnError = true) void OnCancelEvent(object sender, EventArgs args) { - instance?.SendInput("q"); + instance.SendInput("q"); cancellationTokenSource.Cancel(); + instance.Started = false; } CancelEvent += OnCancelEvent; instance.Exited += delegate { cancellationTokenSource.Cancel(); }; - _ffMpegArguments.Pre(); try { + _ffMpegArguments.Pre(); Task.WaitAll(instance.FinishedRunning().ContinueWith(t => { errorCode = t.Result; + cancellationTokenSource.Cancel(); _ffMpegArguments.Post(); }), _ffMpegArguments.During(cancellationTokenSource.Token)); } @@ -98,15 +100,17 @@ void OnCancelEvent(object sender, EventArgs args) { instance?.SendInput("q"); cancellationTokenSource.Cancel(); + instance.Started = false; } CancelEvent += OnCancelEvent; - _ffMpegArguments.Pre(); try { + _ffMpegArguments.Pre(); await Task.WhenAll(instance.FinishedRunning().ContinueWith(t => { errorCode = t.Result; + cancellationTokenSource.Cancel(); _ffMpegArguments.Post(); }), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false); }