From af6480e1d9be1efe3e9c25a009732dfcaa037358 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Sat, 24 Oct 2020 22:18:16 +0200 Subject: [PATCH] Check ffmpeg/ffprobe availability #75 Former-commit-id: 0136d49edf1aa1a203d46adf1e388db6335b6d80 --- FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs | 1 + FFMpegCore/FFProbe/FFProbe.cs | 1 + FFMpegCore/Helpers/FFMpegHelper.cs | 15 +++++++++++++-- FFMpegCore/Helpers/FFProbeHelper.cs | 17 ++++++++++++++--- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs index e0262d4..e931eaf 100644 --- a/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs +++ b/FFMpegCore/FFMpeg/FFMpegArgumentProcessor.cs @@ -126,6 +126,7 @@ await Task.WhenAll(instance.FinishedRunning().ContinueWith(t => private Instance PrepareInstance(out CancellationTokenSource cancellationTokenSource) { FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); + FFMpegHelper.VerifyFFMpegExists(); var instance = new Instance(FFMpegOptions.Options.FFmpegBinary(), _ffMpegArguments.Text); instance.DataReceived += OutputData; cancellationTokenSource = new CancellationTokenSource(); diff --git a/FFMpegCore/FFProbe/FFProbe.cs b/FFMpegCore/FFProbe/FFProbe.cs index 131f465..12dead6 100644 --- a/FFMpegCore/FFProbe/FFProbe.cs +++ b/FFMpegCore/FFProbe/FFProbe.cs @@ -87,6 +87,7 @@ private static Instance PrepareInstance(string filePath, int outputCapacity) { FFProbeHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); var ffprobe = FFMpegOptions.Options.FFProbeBinary(); + FFProbeHelper.VerifyFFProbeExists(); var arguments = $"-print_format json -show_format -sexagesimal -show_streams \"{filePath}\""; var instance = new Instance(ffprobe, arguments) {DataBufferCapacity = outputCapacity}; return instance; diff --git a/FFMpegCore/Helpers/FFMpegHelper.cs b/FFMpegCore/Helpers/FFMpegHelper.cs index 8553573..f2a214e 100644 --- a/FFMpegCore/Helpers/FFMpegHelper.cs +++ b/FFMpegCore/Helpers/FFMpegHelper.cs @@ -2,11 +2,14 @@ using System.Drawing; using System.IO; using FFMpegCore.Exceptions; +using Instances; namespace FFMpegCore.Helpers { public static class FFMpegHelper { + private static bool _ffmpegVerified; + public static void ConversionSizeExceptionCheck(Image image) { ConversionSizeExceptionCheck(image.Size); @@ -32,11 +35,19 @@ public static void ExtensionExceptionCheck(string filename, string extension) $"Invalid output file. File extension should be '{extension}' required."); } - public static void RootExceptionCheck(string root) + public static void RootExceptionCheck() { - if (root == null) + if (FFMpegOptions.Options.RootDirectory == null) throw new FFMpegException(FFMpegExceptionType.Dependency, "FFMpeg root is not configured in app config. Missing key 'ffmpegRoot'."); } + + public static void VerifyFFMpegExists() + { + if (_ffmpegVerified) return; + var (exitCode, _) = Instance.Finish(FFMpegOptions.Options.FFmpegBinary(), "-version"); + _ffmpegVerified = exitCode == 0; + if (!_ffmpegVerified) throw new FFMpegException(FFMpegExceptionType.Operation, "ffmpeg was not found on your system"); + } } } diff --git a/FFMpegCore/Helpers/FFProbeHelper.cs b/FFMpegCore/Helpers/FFProbeHelper.cs index eed1b7a..1e833e0 100644 --- a/FFMpegCore/Helpers/FFProbeHelper.cs +++ b/FFMpegCore/Helpers/FFProbeHelper.cs @@ -1,9 +1,12 @@ using FFMpegCore.Exceptions; +using Instances; namespace FFMpegCore.Helpers { public class FFProbeHelper { + private static bool _ffprobeVerified; + public static int Gcd(int first, int second) { while (first != 0 && second != 0) @@ -15,12 +18,20 @@ public static int Gcd(int first, int second) return first == 0 ? second : first; } - public static void RootExceptionCheck(string root) + public static void RootExceptionCheck() { - if (root == null) + if (FFMpegOptions.Options.RootDirectory == null) throw new FFMpegException(FFMpegExceptionType.Dependency, "FFProbe root is not configured in app config. Missing key 'ffmpegRoot'."); - + + } + + public static void VerifyFFProbeExists() + { + if (_ffprobeVerified) return; + var (exitCode, _) = Instance.Finish(FFMpegOptions.Options.FFProbeBinary(), "-version"); + _ffprobeVerified = exitCode == 0; + if (!_ffprobeVerified) throw new FFMpegException(FFMpegExceptionType.Operation, "ffprobe was not found on your system"); } } }