Skip to content

Commit

Permalink
FFMpegCore: Rethink options configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
vladjerca committed Mar 4, 2019
1 parent 74ea8a4 commit a0de4c1
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 67 deletions.
50 changes: 50 additions & 0 deletions FFMpegCore.Test/FFMpegOptions.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
25 changes: 0 additions & 25 deletions FFMpegCore.Test/FFMpegTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FFMpegCore.FFMPEG;
using FFMpegCore.FFMPEG.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FFMpegCore.Test
Expand All @@ -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" });
}
}
}
24 changes: 1 addition & 23 deletions FFMpegCore/FFMPEG/FFBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FFMpegOptions>(File.ReadAllText(_ConfigFile));
}

if (options == null)
{
options = new FFMpegOptions
{
RootDirectory = _DefaultRoot
};
}

ConfiguredRoot = options.RootDirectory;
}

/// <summary>
Expand Down
15 changes: 5 additions & 10 deletions FFMpegCore/FFMPEG/FFMpeg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ public class FFMpeg : FFBase
/// <summary>
/// Intializes the FFMPEG encoder.
/// </summary>
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();
}

/// <summary>
Expand Down
25 changes: 23 additions & 2 deletions FFMpegCore/FFMPEG/FFMpegOptions.cs
Original file line number Diff line number Diff line change
@@ -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<FFMpegOptions>(File.ReadAllText(_ConfigFile));
}
}

public string RootDirectory { get; set; } = _DefaultRoot;
}
}
7 changes: 4 additions & 3 deletions FFMpegCore/FFMPEG/FFProbe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions FFMpegCore/FFMpegCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<PackageProjectUrl>https://github.com/vladjerca/FFMpegCore</PackageProjectUrl>
<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>
<Version>1.0.4</Version>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<Version>1.0.5</Version>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit a0de4c1

Please sign in to comment.