From 7547c48927e5613a39003c8bab7282ec5ca24e1d Mon Sep 17 00:00:00 2001 From: Vlad Jerca Date: Mon, 4 Mar 2019 22:56:37 +0200 Subject: [PATCH] FFMpegCore: Rethink options configuration Former-commit-id: a0de4c1cf66695d0a27699d57773a5457cdfc6eb --- FFMpegCore.Test/FFMpegOptions.cs | 50 ++++++++++++++++++++++++++++++ FFMpegCore.Test/FFMpegTest.cs | 25 --------------- FFMpegCore/FFMPEG/FFBase.cs | 24 +------------- FFMpegCore/FFMPEG/FFMpeg.cs | 15 +++------ FFMpegCore/FFMPEG/FFMpegOptions.cs | 25 +++++++++++++-- FFMpegCore/FFMPEG/FFProbe.cs | 7 +++-- FFMpegCore/FFMpegCore.csproj | 6 ++-- README.md | 18 ++++++++++- 8 files changed, 103 insertions(+), 67 deletions(-) create mode 100644 FFMpegCore.Test/FFMpegOptions.cs diff --git a/FFMpegCore.Test/FFMpegOptions.cs b/FFMpegCore.Test/FFMpegOptions.cs new file mode 100644 index 0000000..7928ce3 --- /dev/null +++ b/FFMpegCore.Test/FFMpegOptions.cs @@ -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(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); + } + } + } +} diff --git a/FFMpegCore.Test/FFMpegTest.cs b/FFMpegCore.Test/FFMpegTest.cs index 51ee6b0..ac1abf1 100644 --- a/FFMpegCore.Test/FFMpegTest.cs +++ b/FFMpegCore.Test/FFMpegTest.cs @@ -1,5 +1,4 @@ using FFMpegCore.FFMPEG; -using FFMpegCore.FFMPEG.Exceptions; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FFMpegCore.Test @@ -16,29 +15,5 @@ public void CTOR_Default() Assert.IsNotNull(encoder); 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" }); - } } } diff --git a/FFMpegCore/FFMPEG/FFBase.cs b/FFMpegCore/FFMPEG/FFBase.cs index d38c8ca..fe90fd5 100644 --- a/FFMpegCore/FFMPEG/FFBase.cs +++ b/FFMpegCore/FFMPEG/FFBase.cs @@ -9,32 +9,10 @@ namespace FFMpegCore.FFMPEG { 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 FFBase(FFMpegOptions opts = null) + protected FFBase() { - var options = opts; - - if ( - opts == null && - File.Exists(_ConfigFile) - ) - { - options = JsonConvert.DeserializeObject(File.ReadAllText(_ConfigFile)); - } - - if (options == null) - { - options = new FFMpegOptions - { - RootDirectory = _DefaultRoot - }; - } - - ConfiguredRoot = options.RootDirectory; } /// diff --git a/FFMpegCore/FFMPEG/FFMpeg.cs b/FFMpegCore/FFMPEG/FFMpeg.cs index b6660b0..ae428a4 100644 --- a/FFMpegCore/FFMPEG/FFMpeg.cs +++ b/FFMpegCore/FFMPEG/FFMpeg.cs @@ -25,20 +25,15 @@ public class FFMpeg : FFBase /// /// Intializes the FFMPEG encoder. /// - public FFMpeg(FFMpegOptions opts = null): base(opts) + public FFMpeg(): base() { - _Init(); - ArgumentBuilder = new FFArgumentBuilder(); - } - - private void _Init() - { - FFMpegHelper.RootExceptionCheck(ConfiguredRoot); - FFProbeHelper.RootExceptionCheck(ConfiguredRoot); + FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); var target = Environment.Is64BitProcess ? "x64" : "x86"; - _ffmpegPath = ConfiguredRoot + $"\\{target}\\ffmpeg.exe"; + _ffmpegPath = $"{FFMpegOptions.Options.RootDirectory}\\{target}\\ffmpeg.exe"; + + ArgumentBuilder = new FFArgumentBuilder(); } /// diff --git a/FFMpegCore/FFMPEG/FFMpegOptions.cs b/FFMpegCore/FFMPEG/FFMpegOptions.cs index 5030e39..c7f46d6 100644 --- a/FFMpegCore/FFMPEG/FFMpegOptions.cs +++ b/FFMpegCore/FFMPEG/FFMpegOptions.cs @@ -1,7 +1,28 @@ -namespace FFMpegCore.FFMPEG +using Newtonsoft.Json; +using System.IO; + +namespace FFMpegCore.FFMPEG { 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(File.ReadAllText(_ConfigFile)); + } + } + + public string RootDirectory { get; set; } = _DefaultRoot; } } diff --git a/FFMpegCore/FFMPEG/FFProbe.cs b/FFMpegCore/FFMPEG/FFProbe.cs index 421f29d..d285186 100644 --- a/FFMpegCore/FFMPEG/FFProbe.cs +++ b/FFMpegCore/FFMPEG/FFProbe.cs @@ -3,18 +3,19 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; namespace FFMpegCore.FFMPEG { 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"; - _ffprobePath = ConfiguredRoot + $"\\{target}\\ffprobe.exe"; + _ffprobePath = $"{FFMpegOptions.Options.RootDirectory}\\{target}\\ffprobe.exe"; } /// diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index 73a5ff2..aa85e3d 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -7,9 +7,9 @@ https://github.com/vladjerca/FFMpegCore Vlad Jerca 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. - 1.0.4 - 1.0.4.0 - 1.0.4.0 + 1.0.5 + 1.0.5.0 + 1.0.5.0 diff --git a/README.md b/README.md index d29af45..4259189 100644 --- a/README.md +++ b/README.md @@ -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. -### 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.