Default to throwing on errors

Former-commit-id: 3855215000
This commit is contained in:
Malte Rosenbjerg 2020-05-12 17:55:31 +02:00
parent 02c05d1b29
commit e0b7d652d9
4 changed files with 62 additions and 31 deletions

View file

@ -297,28 +297,22 @@ public void Video_ToMP4_Args_StreamPipe()
[TestMethod, Timeout(10000)]
public void Video_ToMP4_Args_StreamOutputPipe_Async_Failure()
{
using var ms = new MemoryStream();
Assert.ThrowsExceptionAsync<FFMpegException>(async () =>
{
await using var ms = new MemoryStream();
var pipeSource = new StreamPipeDataReader(ms);
var result = FFMpegArguments
await FFMpegArguments
.FromInputFiles(VideoLibrary.LocalVideo)
.ForceFormat("mkv")
.OutputToPipe(pipeSource)
.ProcessAsynchronously()
.WaitForResult();
Assert.IsFalse(result);
.ProcessAsynchronously();
});
}
[TestMethod, Timeout(10000)]
public void Video_ToMP4_Args_StreamOutputPipe_Failure()
{
using var ms = new MemoryStream();
var pipeSource = new StreamPipeDataReader(ms);
var result = FFMpegArguments
.FromInputFiles(VideoLibrary.LocalVideo)
.ForceFormat("mkv")
.OutputToPipe(pipeSource)
.ProcessSynchronously();
Assert.IsFalse(result);
Assert.ThrowsException<FFMpegException>(() => ConvertToStreamPipe(new ForceFormatArgument("mkv")));
}

View file

@ -13,14 +13,15 @@ public enum FFMpegExceptionType
public class FFMpegException : Exception
{
public FFMpegException(FFMpegExceptionType type, string message): this(type, message, null) { }
public FFMpegException(FFMpegExceptionType type, string? message = null, Exception? innerException = null)
public FFMpegException(FFMpegExceptionType type, string? message = null, Exception? innerException = null, string ffMpegErrorOutput = "")
: base(message, innerException)
{
FFMpegErrorOutput = ffMpegErrorOutput;
Type = type;
}
public FFMpegExceptionType Type { get; }
public string FFMpegErrorOutput { get; }
}
}

View file

@ -187,6 +187,7 @@ public static bool Join(string output, params MediaAnalysis[] videos)
{
FFMpegHelper.ConversionSizeExceptionCheck(video);
var destinationPath = Path.Combine(FFMpegOptions.Options.TempDirectory, $"{Path.GetFileNameWithoutExtension(video.Path)}{FileExtension.Ts}");
Directory.CreateDirectory(FFMpegOptions.Options.TempDirectory);
Convert(video, destinationPath, VideoType.Ts);
return destinationPath;
}).ToArray();
@ -219,6 +220,7 @@ public static bool JoinImageSequence(string output, double frameRate = 30, param
{
FFMpegHelper.ConversionSizeExceptionCheck(Image.FromFile(image.FullName));
var destinationPath = Path.Combine(FFMpegOptions.Options.TempDirectory, $"{index.ToString().PadLeft(9, '0')}{image.Extension}");
Directory.CreateDirectory(FFMpegOptions.Options.TempDirectory);
File.Copy(image.FullName, destinationPath);
return destinationPath;
}).ToArray();

View file

@ -4,6 +4,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using FFMpegCore.Exceptions;
using FFMpegCore.Helpers;
using Instances;
@ -36,7 +37,7 @@ public FFMpegArgumentProcessor NotifyOnProgress(Action<TimeSpan>? onTimeProgress
_onTimeProgress = onTimeProgress;
return this;
}
public bool ProcessSynchronously()
public bool ProcessSynchronously(bool throwOnError = true)
{
FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory);
using var instance = new Instance(FFMpegOptions.Options.FFmpegBinary(), _ffMpegArguments.Text);
@ -46,18 +47,34 @@ public bool ProcessSynchronously()
_ffMpegArguments.Pre();
var cancellationTokenSource = new CancellationTokenSource();
try
{
Task.WaitAll(instance.FinishedRunning().ContinueWith(t =>
{
errorCode = t.Result;
cancellationTokenSource.Cancel();
}), _ffMpegArguments.During(cancellationTokenSource.Token));
}
catch (Exception e)
{
if (!throwOnError)
return false;
throw new FFMpegException(FFMpegExceptionType.Process, "Exception thrown during processing", e,
string.Join("\n", instance.ErrorData));
}
finally
{
_ffMpegArguments.Post();
}
if (throwOnError && errorCode != 0)
throw new FFMpegException(FFMpegExceptionType.Conversion, string.Join("\n", instance.ErrorData));
return errorCode == 0;
}
public async Task<bool> ProcessAsynchronously()
public async Task<bool> ProcessAsynchronously(bool throwOnError = true)
{
FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory);
using var instance = new Instance(FFMpegOptions.Options.FFmpegBinary(), _ffMpegArguments.Text);
@ -68,12 +85,29 @@ public async Task<bool> ProcessAsynchronously()
_ffMpegArguments.Pre();
var cancellationTokenSource = new CancellationTokenSource();
try
{
await Task.WhenAll(instance.FinishedRunning().ContinueWith(t =>
{
errorCode = t.Result;
cancellationTokenSource.Cancel();
}), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false);
}
catch (Exception e)
{
if (!throwOnError)
return false;
throw new FFMpegException(FFMpegExceptionType.Process, "Exception thrown during processing", e,
string.Join("\n", instance.ErrorData));
}
finally
{
_ffMpegArguments.Post();
}
if (throwOnError && errorCode != 0)
throw new FFMpegException(FFMpegExceptionType.Conversion, string.Join("\n", instance.ErrorData));
return errorCode == 0;
}