FFMpegCore: Rethink options configuration

Former-commit-id: a0de4c1cf6
This commit is contained in:
Vlad Jerca 2019-03-04 22:56:37 +02:00
parent 3abaf36c49
commit 7547c48927
8 changed files with 103 additions and 67 deletions

View file

@ -0,0 +1,50 @@
using FFMpegCore.FFMPEG;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System.IO;
namespace FFMpegCore.Test
{
[TestClass]
public class FFMpegOptionsTest
{
[TestMethod]
public void Options_Initialized()
{
Assert.IsNotNull(FFMpegOptions.Options);
}
[TestMethod]
public void Options_Defaults_Configured()
{
Assert.AreEqual(new FFMpegOptions().RootDirectory, ".\\FFMPEG\\bin");
}
[TestMethod]
public void Options_Loaded_From_File()
{
Assert.AreEqual(
FFMpegOptions.Options.RootDirectory,
JsonConvert.DeserializeObject<FFMpegOptions>(File.ReadAllText(".\\ffmpeg.config.json")).RootDirectory
);
}
[TestMethod]
public void Options_Overrided()
{
var original = FFMpegOptions.Options;
try
{
FFMpegOptions.Configure(new FFMpegOptions { RootDirectory = "Whatever" });
Assert.AreEqual(
FFMpegOptions.Options.RootDirectory,
"Whatever"
);
}
finally
{
FFMpegOptions.Configure(original);
}
}
}
}

View file

@ -1,5 +1,4 @@
using FFMpegCore.FFMPEG; using FFMpegCore.FFMPEG;
using FFMpegCore.FFMPEG.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace FFMpegCore.Test namespace FFMpegCore.Test
@ -16,29 +15,5 @@ public void CTOR_Default()
Assert.IsNotNull(encoder); Assert.IsNotNull(encoder);
Assert.IsNotNull(probe); Assert.IsNotNull(probe);
} }
[TestMethod]
public void CTOR_Options()
{
var encoder = new FFMpeg(new FFMpegOptions { RootDirectory = ".\\FFMPEG\\bin" });
var probe = new FFProbe(new FFMpegOptions { RootDirectory = ".\\FFMPEG\\bin" });
Assert.IsNotNull(encoder);
Assert.IsNotNull(probe);
}
[TestMethod]
[ExpectedException(typeof(FFMpegException))]
public void CTOR_Encoder_Options_Invalid()
{
var encoder = new FFMpeg(new FFMpegOptions { RootDirectory = "INVALID_DIR" });
}
[TestMethod]
[ExpectedException(typeof(FFMpegException))]
public void CTOR_Probe_Options_Invalid()
{
var encoder = new FFProbe(new FFMpegOptions { RootDirectory = "INVALID_DIR" });
}
} }
} }

View file

@ -9,32 +9,10 @@ namespace FFMpegCore.FFMPEG
{ {
public abstract class FFBase : IDisposable public abstract class FFBase : IDisposable
{ {
private static string _ConfigFile = "./ffmpeg.config.json";
private static string _DefaultRoot = ".\\FFMPEG\\bin";
protected string ConfiguredRoot;
protected Process Process; protected Process Process;
protected FFBase(FFMpegOptions opts = null) protected FFBase()
{ {
var options = opts;
if (
opts == null &&
File.Exists(_ConfigFile)
)
{
options = JsonConvert.DeserializeObject<FFMpegOptions>(File.ReadAllText(_ConfigFile));
}
if (options == null)
{
options = new FFMpegOptions
{
RootDirectory = _DefaultRoot
};
}
ConfiguredRoot = options.RootDirectory;
} }
/// <summary> /// <summary>

View file

@ -25,20 +25,15 @@ public class FFMpeg : FFBase
/// <summary> /// <summary>
/// Intializes the FFMPEG encoder. /// Intializes the FFMPEG encoder.
/// </summary> /// </summary>
public FFMpeg(FFMpegOptions opts = null): base(opts) public FFMpeg(): base()
{ {
_Init(); FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory);
ArgumentBuilder = new FFArgumentBuilder();
}
private void _Init()
{
FFMpegHelper.RootExceptionCheck(ConfiguredRoot);
FFProbeHelper.RootExceptionCheck(ConfiguredRoot);
var target = Environment.Is64BitProcess ? "x64" : "x86"; var target = Environment.Is64BitProcess ? "x64" : "x86";
_ffmpegPath = ConfiguredRoot + $"\\{target}\\ffmpeg.exe"; _ffmpegPath = $"{FFMpegOptions.Options.RootDirectory}\\{target}\\ffmpeg.exe";
ArgumentBuilder = new FFArgumentBuilder();
} }
/// <summary> /// <summary>

View file

@ -1,7 +1,28 @@
namespace FFMpegCore.FFMPEG using Newtonsoft.Json;
using System.IO;
namespace FFMpegCore.FFMPEG
{ {
public class FFMpegOptions public class FFMpegOptions
{ {
public string RootDirectory { get; set; } private static string _ConfigFile = ".\\ffmpeg.config.json";
private static string _DefaultRoot = ".\\FFMPEG\\bin";
public static FFMpegOptions Options { get; private set; } = new FFMpegOptions();
public static void Configure(FFMpegOptions options)
{
Options = options;
}
static FFMpegOptions()
{
if (File.Exists(_ConfigFile))
{
Options = JsonConvert.DeserializeObject<FFMpegOptions>(File.ReadAllText(_ConfigFile));
}
}
public string RootDirectory { get; set; } = _DefaultRoot;
} }
} }

View file

@ -3,18 +3,19 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
namespace FFMpegCore.FFMPEG namespace FFMpegCore.FFMPEG
{ {
public sealed class FFProbe : FFBase public sealed class FFProbe : FFBase
{ {
public FFProbe(FFMpegOptions opts = null): base(opts) public FFProbe(): base()
{ {
FFProbeHelper.RootExceptionCheck(ConfiguredRoot); FFProbeHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory);
var target = Environment.Is64BitProcess ? "x64" : "x86"; var target = Environment.Is64BitProcess ? "x64" : "x86";
_ffprobePath = ConfiguredRoot + $"\\{target}\\ffprobe.exe"; _ffprobePath = $"{FFMpegOptions.Options.RootDirectory}\\{target}\\ffprobe.exe";
} }
/// <summary> /// <summary>

View file

@ -7,9 +7,9 @@
<PackageProjectUrl>https://github.com/vladjerca/FFMpegCore</PackageProjectUrl> <PackageProjectUrl>https://github.com/vladjerca/FFMpegCore</PackageProjectUrl>
<Copyright>Vlad Jerca</Copyright> <Copyright>Vlad Jerca</Copyright>
<Description>A great way to use FFMpeg encoding when writing video applications, client-side and server-side. It has wrapper methods that allow conversion to all web formats: MP4, OGV, TS and methods of capturing screens from the videos.</Description> <Description>A great way to use FFMpeg encoding when writing video applications, client-side and server-side. It has wrapper methods that allow conversion to all web formats: MP4, OGV, TS and methods of capturing screens from the videos.</Description>
<Version>1.0.4</Version> <Version>1.0.5</Version>
<AssemblyVersion>1.0.4.0</AssemblyVersion> <AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion> <FileVersion>1.0.5.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View file

@ -12,7 +12,23 @@ Install-Package FFMpegCore
A great way to use FFMpeg encoding when writing video applications, client-side and server-side. It has wrapper methods that allow conversion to all web formats: MP4, OGV, TS and methods of capturing screens from the videos. A great way to use FFMpeg encoding when writing video applications, client-side and server-side. It has wrapper methods that allow conversion to all web formats: MP4, OGV, TS and methods of capturing screens from the videos.
### Configuratoin ### Configuration
By default the `RootDirectory` is set to `"\\FFMPEG\\bin"`.
#### Option 1
The default value can be overwritten via the `FFMpegOptions` class:
```c#
public Startup()
{
FFMpegOptions.Configure(new FFMpegOptions { RootDirectory = "\\My_Binary\\Path" });
}
```
#### Option 2
The root directory for the ffmpeg binaries can be configured via the `ffmpeg.config.json` file. The root directory for the ffmpeg binaries can be configured via the `ffmpeg.config.json` file.