FFMpegCore/FFMpegCore/GlobalFFOptions.cs
2021-08-07 16:39:10 +02:00

57 lines
1.8 KiB
C#

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.Json;
namespace FFMpegCore
{
public static class GlobalFFOptions
{
private static readonly string ConfigFile = "ffmpeg.config.json";
private static FFOptions? _current;
public static FFOptions Current
{
get { return _current ??= LoadFFOptions(); }
}
public static void Configure(Action<FFOptions> optionsAction)
{
optionsAction?.Invoke(Current);
}
public static void Configure(FFOptions ffOptions)
{
_current = ffOptions ?? throw new ArgumentNullException(nameof(ffOptions));
}
public static string GetFFMpegBinaryPath(FFOptions? ffOptions = null) => GetFFBinaryPath("FFMpeg", ffOptions ?? Current);
public static string GetFFProbeBinaryPath(FFOptions? ffOptions = null) => GetFFBinaryPath("FFProbe", ffOptions ?? Current);
private static string GetFFBinaryPath(string name, FFOptions ffOptions)
{
var ffName = name.ToLowerInvariant();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
ffName += ".exe";
var target = Environment.Is64BitProcess ? "x64" : "x86";
if (Directory.Exists(Path.Combine(ffOptions.BinaryFolder, target)))
ffName = Path.Combine(target, ffName);
return Path.Combine(ffOptions.BinaryFolder, ffName);
}
private static FFOptions LoadFFOptions()
{
if (File.Exists(ConfigFile))
{
return JsonSerializer.Deserialize<FFOptions>(File.ReadAllText(ConfigFile))!;
}
else
{
return new FFOptions();
}
}
}
}