Check ffmpeg/ffprobe availability #75

Former-commit-id: 0136d49edf
This commit is contained in:
Malte Rosenbjerg 2020-10-24 22:18:16 +02:00
parent fee7d6f450
commit af6480e1d9
4 changed files with 29 additions and 5 deletions

View file

@ -126,6 +126,7 @@ await Task.WhenAll(instance.FinishedRunning().ContinueWith(t =>
private Instance PrepareInstance(out CancellationTokenSource cancellationTokenSource) private Instance PrepareInstance(out CancellationTokenSource cancellationTokenSource)
{ {
FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory);
FFMpegHelper.VerifyFFMpegExists();
var instance = new Instance(FFMpegOptions.Options.FFmpegBinary(), _ffMpegArguments.Text); var instance = new Instance(FFMpegOptions.Options.FFmpegBinary(), _ffMpegArguments.Text);
instance.DataReceived += OutputData; instance.DataReceived += OutputData;
cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource = new CancellationTokenSource();

View file

@ -87,6 +87,7 @@ private static Instance PrepareInstance(string filePath, int outputCapacity)
{ {
FFProbeHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); FFProbeHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory);
var ffprobe = FFMpegOptions.Options.FFProbeBinary(); var ffprobe = FFMpegOptions.Options.FFProbeBinary();
FFProbeHelper.VerifyFFProbeExists();
var arguments = $"-print_format json -show_format -sexagesimal -show_streams \"{filePath}\""; var arguments = $"-print_format json -show_format -sexagesimal -show_streams \"{filePath}\"";
var instance = new Instance(ffprobe, arguments) {DataBufferCapacity = outputCapacity}; var instance = new Instance(ffprobe, arguments) {DataBufferCapacity = outputCapacity};
return instance; return instance;

View file

@ -2,11 +2,14 @@
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using FFMpegCore.Exceptions; using FFMpegCore.Exceptions;
using Instances;
namespace FFMpegCore.Helpers namespace FFMpegCore.Helpers
{ {
public static class FFMpegHelper public static class FFMpegHelper
{ {
private static bool _ffmpegVerified;
public static void ConversionSizeExceptionCheck(Image image) public static void ConversionSizeExceptionCheck(Image image)
{ {
ConversionSizeExceptionCheck(image.Size); ConversionSizeExceptionCheck(image.Size);
@ -32,11 +35,19 @@ public static void ExtensionExceptionCheck(string filename, string extension)
$"Invalid output file. File extension should be '{extension}' required."); $"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, throw new FFMpegException(FFMpegExceptionType.Dependency,
"FFMpeg root is not configured in app config. Missing key 'ffmpegRoot'."); "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");
}
} }
} }

View file

@ -1,9 +1,12 @@
using FFMpegCore.Exceptions; using FFMpegCore.Exceptions;
using Instances;
namespace FFMpegCore.Helpers namespace FFMpegCore.Helpers
{ {
public class FFProbeHelper public class FFProbeHelper
{ {
private static bool _ffprobeVerified;
public static int Gcd(int first, int second) public static int Gcd(int first, int second)
{ {
while (first != 0 && second != 0) while (first != 0 && second != 0)
@ -15,12 +18,20 @@ public static int Gcd(int first, int second)
return first == 0 ? second : first; 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, throw new FFMpegException(FFMpegExceptionType.Dependency,
"FFProbe root is not configured in app config. Missing key 'ffmpegRoot'."); "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");
}
} }
} }