Default to throwing on errors

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

View file

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

View file

@ -13,14 +13,15 @@ public enum FFMpegExceptionType
public class FFMpegException : Exception 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) : base(message, innerException)
{ {
FFMpegErrorOutput = ffMpegErrorOutput;
Type = type; Type = type;
} }
public FFMpegExceptionType Type { get; } 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); FFMpegHelper.ConversionSizeExceptionCheck(video);
var destinationPath = Path.Combine(FFMpegOptions.Options.TempDirectory, $"{Path.GetFileNameWithoutExtension(video.Path)}{FileExtension.Ts}"); var destinationPath = Path.Combine(FFMpegOptions.Options.TempDirectory, $"{Path.GetFileNameWithoutExtension(video.Path)}{FileExtension.Ts}");
Directory.CreateDirectory(FFMpegOptions.Options.TempDirectory);
Convert(video, destinationPath, VideoType.Ts); Convert(video, destinationPath, VideoType.Ts);
return destinationPath; return destinationPath;
}).ToArray(); }).ToArray();
@ -219,6 +220,7 @@ public static bool JoinImageSequence(string output, double frameRate = 30, param
{ {
FFMpegHelper.ConversionSizeExceptionCheck(Image.FromFile(image.FullName)); FFMpegHelper.ConversionSizeExceptionCheck(Image.FromFile(image.FullName));
var destinationPath = Path.Combine(FFMpegOptions.Options.TempDirectory, $"{index.ToString().PadLeft(9, '0')}{image.Extension}"); var destinationPath = Path.Combine(FFMpegOptions.Options.TempDirectory, $"{index.ToString().PadLeft(9, '0')}{image.Extension}");
Directory.CreateDirectory(FFMpegOptions.Options.TempDirectory);
File.Copy(image.FullName, destinationPath); File.Copy(image.FullName, destinationPath);
return destinationPath; return destinationPath;
}).ToArray(); }).ToArray();

View file

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