Skip to content

Commit

Permalink
refactor project structure, namespaces and naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias.haimerl committed Nov 20, 2018
1 parent 2724d3c commit 8b2228a
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 72 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Install [FFmpeg.NET](https://github.com/cmxl/FFmpeg.NET) from nuget.org Package
## Samples

- [Grab thumbnail from a video](#grab-thumbnail-from-a-video)
- [Retrieve metadata](#retrieve-metadata)
- [Retrieve metadata](#retrieve-metadata) new Engine
- [Perform basic video conversions](#basic-conversion)
- [Convert from FLV to DVD](#convert-flash-video-to-dvd)
- [Convert FLV to MP4 using various transcoding options](#transcoding-options-flv-to-mp4)
Expand All @@ -70,7 +70,7 @@ Install [FFmpeg.NET](https://github.com/cmxl/FFmpeg.NET) from nuget.org Package
var inputFile = new MediaFile (@"C:\Path\To_Video.flv");
var outputFile = new MediaFile (@"C:\Path\To_Save_Image.jpg");

var ffmpeg = new FFmpeg.NET.Engine.FFmpeg("C:\\ffmpeg\\ffmpeg.exe");
var ffmpeg = new Engine("C:\\ffmpeg\\ffmpeg.exe");
// Saves the frame located on the 15th second of the video.
var options = new ConversionOptions { Seek = TimeSpan.FromSeconds(15) };
await ffmpeg.GetThumbnailAsync(inputFile, outputFile, options);
Expand All @@ -81,7 +81,7 @@ await ffmpeg.GetThumbnailAsync(inputFile, outputFile, options);
```csharp
var inputFile = new MediaFile (@"C:\Path\To_Video.flv");

var ffmpeg = new FFmpeg.NET.Engine.FFmpeg("C:\\ffmpeg\\ffmpeg.exe");
var ffmpeg = new Engine("C:\\ffmpeg\\ffmpeg.exe");
var metadata = await ffmpeg.GetMetadataAsync(inputFile);

Console.WriteLine(metadata.Duration);
Expand All @@ -93,7 +93,7 @@ Console.WriteLine(metadata.Duration);
var inputFile = new MediaFile (@"C:\Path\To_Video.flv");
var outputFile = new MediaFile (@"C:\Path\To_Save_New_Video.mp4");

var ffmpeg = new FFmpeg.NET.Engine.FFmpeg("C:\\ffmpeg\\ffmpeg.exe");
var ffmpeg = new Engine("C:\\ffmpeg\\ffmpeg.exe");
await ffmpeg.ConvertAsync(inputFile, outputFile);
```

Expand All @@ -109,7 +109,7 @@ var conversionOptions = new ConversionOptions
TargetStandard = TargetStandard.PAL
};

var ffmpeg = new FFmpeg.NET.Engine.FFmpeg("C:\\ffmpeg\\ffmpeg.exe");
var ffmpeg = new Engine("C:\\ffmpeg\\ffmpeg.exe");
await ffmpeg.ConvertAsync(inputFile, outputFile, conversionOptions);
```

Expand All @@ -127,7 +127,7 @@ var conversionOptions = new ConversionOptions
AudioSampleRate = AudioSampleRate.Hz44100
};

var ffmpeg = new FFmpeg.NET.Engine.FFmpeg("C:\\ffmpeg\\ffmpeg.exe");
var ffmpeg = new Engine("C:\\ffmpeg\\ffmpeg.exe");
await ffmpeg.ConvertAsync(inputFile, outputFile, conversionOptions);
```

Expand All @@ -137,7 +137,7 @@ await ffmpeg.ConvertAsync(inputFile, outputFile, conversionOptions);
var inputFile = new MediaFile (@"C:\Path\To_Video.flv");
var outputFile = new MediaFile (@"C:\Path\To_Save_ExtractedVideo.flv");

var ffmpeg = new FFmpeg.NET.Engine.FFmpeg("C:\\ffmpeg\\ffmpeg.exe");
var ffmpeg = new Engine("C:\\ffmpeg\\ffmpeg.exe");
var options = new ConversionOptions();

// This example will create a 25 second video, starting from the
Expand All @@ -156,7 +156,7 @@ public async Task StartConverting()
var inputFile = new MediaFile (@"C:\Path\To_Video.flv");
var outputFile = new MediaFile (@"C:\Path\To_Save_New_Video.mp4");

var ffmpeg = new FFmpeg.NET.Engine.FFmpeg("C:\\ffmpeg\\ffmpeg.exe");
var ffmpeg = new Engine("C:\\ffmpeg\\ffmpeg.exe");
ffmpeg.Progress += OnProgress;
ffmpeg.Data += OnData;
ffmpeg.Error += OnError;
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2.1.0
version: 3.0.0

environment:
nuget_token:
Expand Down
3 changes: 1 addition & 2 deletions samples/FFmpeg.NET.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using FFmpeg.NET.Events;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace FFmpeg.NET.Sample
Expand All @@ -14,7 +13,7 @@ private static async Task Main(string[] args)
var inputFile = new MediaFile(@"..\..\..\..\..\tests\FFmpeg.NET.Tests\MediaFiles\SampleVideo_1280x720_1mb.mp4");
var outputFile = new MediaFile(@"output.mkv");

var ffmpeg = new Engine.FFmpeg(@"..\..\..\..\..\lib\ffmpeg\v4\ffmpeg.exe");
var ffmpeg = new Engine(@"..\..\..\..\..\lib\ffmpeg\v4\ffmpeg.exe");
ffmpeg.Progress += OnProgress;
ffmpeg.Data += OnData;
ffmpeg.Error += OnError;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using FFmpeg.NET.Enums;

namespace FFmpeg.NET.Engine
namespace FFmpeg.NET
{
public class ConversionOptions
{
Expand Down
6 changes: 3 additions & 3 deletions src/FFmpeg.NET/Engine/FFmpeg.cs → src/FFmpeg.NET/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
using System.Threading;
using System.Threading.Tasks;

namespace FFmpeg.NET.Engine
namespace FFmpeg.NET
{
public sealed class FFmpeg
public sealed class Engine
{
private readonly string _ffmpegPath;

public FFmpeg(string ffmpegPath)
public Engine(string ffmpegPath)
{
_ffmpegPath = ffmpegPath ?? throw new ArgumentNullException(ffmpegPath, "FFmpeg executable path needs to be provided.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/FFmpeg.NET/Events/ConversionProgressEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FFmpeg.NET.Engine.Models;
using FFmpeg.NET.Models;
using System;

namespace FFmpeg.NET.Events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Globalization;
using System.Text;

namespace FFmpeg.NET.Engine
namespace FFmpeg.NET
{
internal class FFmpegArgumentBuilder
{
Expand Down Expand Up @@ -47,7 +47,7 @@ private static string Convert(MediaFile inputFile, MediaFile outputFile, Convers

// Default conversion
if (conversionOptions == null)
return commandBuilder.AppendFormat(" -i \"{0}\" \"{1}\" ", inputFile.FileInfo.FullName, outputFile.FileInfo.FullName).ToString();
return commandBuilder.AppendFormat(" -i \"{0}\" \"{1}\" ", inputFile.FileInfo.FullName, outputFile.FileInfo.FullName).ToString();

// Media seek position
if (conversionOptions.Seek != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FFmpeg.NET.Engine
namespace FFmpeg.NET
{
internal class FFmpegParameters
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading;
using System.Threading.Tasks;

namespace FFmpeg.NET.Engine
namespace FFmpeg.NET
{
internal sealed class FFmpegProcess
{
Expand Down Expand Up @@ -68,7 +68,7 @@ private void FFmpegProcessOnErrorDataReceived(DataReceivedEventArgs e, FFmpegPar
RegexEngine.TestVideo(e.Data, parameters);
RegexEngine.TestAudio(e.Data, parameters);

var matchDuration = RegexEngine.Index[RegexEngine.Find.Duration].Match(e.Data);
var matchDuration = RegexEngine._index[RegexEngine.Find.Duration].Match(e.Data);
if (matchDuration.Success)
{
if (parameters.InputFile.MetaData == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FFmpeg.NET.Engine
namespace FFmpeg.NET
{
internal enum FFmpegTask
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;

namespace FFmpeg.NET.Engine.Models
namespace FFmpeg.NET.Models
{
internal class ProgressData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using FFmpeg.NET.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using FFmpeg.NET.Engine.Models;
using FFmpeg.NET.Events;

namespace FFmpeg.NET.Engine
namespace FFmpeg.NET
{
/// <summary>
/// Contains all Regex tasks
Expand All @@ -15,7 +14,7 @@ internal static class RegexEngine
/// <summary>
/// Dictionary containing every Regex test.
/// </summary>
internal static Dictionary<Find, Regex> Index = new Dictionary<Find, Regex>
internal static readonly Dictionary<Find, Regex> _index = new Dictionary<Find, Regex>
{
{Find.BitRate, new Regex(@"([0-9]*)\s*kb/s")},
{Find.Duration, new Regex(@"Duration: ([^,]*), ")},
Expand Down Expand Up @@ -45,11 +44,11 @@ internal static bool IsProgressData(string data, out ProgressData progressData)
{
progressData = null;

var matchFrame = Index[Find.ConvertProgressFrame].Match(data);
var matchFps = Index[Find.ConvertProgressFps].Match(data);
var matchSize = Index[Find.ConvertProgressSize].Match(data);
var matchTime = Index[Find.ConvertProgressTime].Match(data);
var matchBitrate = Index[Find.ConvertProgressBitrate].Match(data);
var matchFrame = _index[Find.ConvertProgressFrame].Match(data);
var matchFps = _index[Find.ConvertProgressFps].Match(data);
var matchSize = _index[Find.ConvertProgressSize].Match(data);
var matchTime = _index[Find.ConvertProgressTime].Match(data);
var matchBitrate = _index[Find.ConvertProgressBitrate].Match(data);

if (!matchSize.Success || !matchTime.Success || !matchBitrate.Success)
return false;
Expand All @@ -67,40 +66,32 @@ internal static bool IsProgressData(string data, out ProgressData progressData)
}

private static long? GetLongValue(Match match)
{
if (long.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
return result;

return null;
}
=> long.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)
? result
: (long?)null;

private static double? GetDoubleValue(Match match)
{
if (double.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
return result;

return null;
}
=> double.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)
? result
: (double?)null;

private static int? GetIntValue(Match match)
{
if (int.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
return result;

return null;
}
=> int.TryParse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)
? result
: (int?)null;

internal static void TestVideo(string data, FFmpegParameters engine)
{
var matchMetaVideo = Index[Find.MetaVideo].Match(data);
var matchMetaVideo = _index[Find.MetaVideo].Match(data);

if (!matchMetaVideo.Success) return;
if (!matchMetaVideo.Success)
return;

var fullMetadata = matchMetaVideo.Groups[1].ToString();

var matchVideoFormatColorSize = Index[Find.VideoFormatColorSize].Match(fullMetadata).Groups;
var matchVideoFps = Index[Find.VideoFps].Match(fullMetadata).Groups;
var matchVideoBitRate = Index[Find.BitRate].Match(fullMetadata);
var matchVideoFormatColorSize = _index[Find.VideoFormatColorSize].Match(fullMetadata).Groups;
var matchVideoFps = _index[Find.VideoFps].Match(fullMetadata).Groups;
var matchVideoBitRate = _index[Find.BitRate].Match(fullMetadata);

if (engine.InputFile.MetaData == null)
engine.InputFile.MetaData = new MetaData();
Expand All @@ -114,21 +105,22 @@ internal static void TestVideo(string data, FFmpegParameters engine)
Fps = matchVideoFps[1].Success && !string.IsNullOrEmpty(matchVideoFps[1].ToString()) ? Convert.ToDouble(matchVideoFps[1].ToString(), new CultureInfo("en-US")) : 0,
BitRateKbs =
matchVideoBitRate.Success
? (int?) Convert.ToInt32(matchVideoBitRate.Groups[1].ToString())
? (int?)Convert.ToInt32(matchVideoBitRate.Groups[1].ToString())
: null
};
}

internal static void TestAudio(string data, FFmpegParameters engine)
{
var matchMetaAudio = Index[Find.MetaAudio].Match(data);
var matchMetaAudio = _index[Find.MetaAudio].Match(data);

if (!matchMetaAudio.Success) return;
if (!matchMetaAudio.Success)
return;

var fullMetadata = matchMetaAudio.Groups[1].ToString();

var matchAudioFormatHzChannel = Index[Find.AudioFormatHzChannel].Match(fullMetadata).Groups;
var matchAudioBitRate = Index[Find.BitRate].Match(fullMetadata).Groups;
var matchAudioFormatHzChannel = _index[Find.AudioFormatHzChannel].Match(fullMetadata).Groups;
var matchAudioBitRate = _index[Find.BitRate].Match(fullMetadata).Groups;

if (engine.InputFile.MetaData == null)
engine.InputFile.MetaData = new MetaData();
Expand Down
4 changes: 2 additions & 2 deletions tests/FFmpeg.NET.Tests/ConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ConversionTests(MediaFileFixture fixture, ITestOutputHelper outputHelper)
public async Task FFmpeg_Invokes_ConversionCompleteEvent()
{
var output = new MediaFile(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"MediaFiles\conversionTest.mp4")));
var ffmpeg = new Engine.FFmpeg(_fixture.FFmpegPath);
var ffmpeg = new Engine(_fixture.FFmpegPath);

var e = await Assert.RaisesAsync<ConversionCompleteEventArgs>(
x => ffmpeg.Complete += x,
Expand All @@ -44,7 +44,7 @@ public async Task FFmpeg_Invokes_ConversionCompleteEvent()
[Fact]
public async Task FFmpeg_Should_Throw_Exception_On_Invalid_OutputFile()
{
var ffmpeg = new Engine.FFmpeg(_fixture.FFmpegPath);
var ffmpeg = new Engine(_fixture.FFmpegPath);
var output = new MediaFile("test.txt");
var input = _fixture.VideoFile;

Expand Down
6 changes: 3 additions & 3 deletions tests/FFmpeg.NET.Tests/MetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public MetaDataTests(MediaFileFixture fixture)
[Fact]
public async Task FFmpeg_Can_Read_Audio_Metadata()
{
var ffmpeg = new Engine.FFmpeg(_fixture.FFmpegPath);
var ffmpeg = new Engine(_fixture.FFmpegPath);

var audioFile = _fixture.AudioFile;
var metaData = await ffmpeg.GetMetaDataAsync(audioFile);
Expand All @@ -37,7 +37,7 @@ public async Task FFmpeg_Can_Read_Audio_Metadata()
[Fact]
public async Task FFmpeg_Can_Read_Video_Metadata()
{
var ffmpeg = new Engine.FFmpeg(_fixture.FFmpegPath);
var ffmpeg = new Engine(_fixture.FFmpegPath);

var videoFile = _fixture.VideoFile;
var metaData = await ffmpeg.GetMetaDataAsync(videoFile);
Expand All @@ -61,7 +61,7 @@ public async Task FFmpeg_Can_Read_Video_Metadata()
[Fact]
public async Task CustomParameters()
{
var ffmpeg = new Engine.FFmpeg(_fixture.FFmpegPath);
var ffmpeg = new Engine(_fixture.FFmpegPath);
await ffmpeg.ExecuteAsync($"-i \"{_fixture.VideoFile.FileInfo.FullName}\" -f ffmetadata -");
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/FFmpeg.NET.Tests/MultithreadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Converting_Multiple_Files_Simultaneously()
for (var i = 0; i < Environment.ProcessorCount; i++)
outputFiles.Add(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"MediaFiles\output{i}.mp4")));

var ffmpeg = new Engine.FFmpeg(_fixture.FFmpegPath);
var ffmpeg = new Engine(_fixture.FFmpegPath);
ffmpeg.Complete += (sender, args) => { _output.WriteLine("Complete: [{0} => {1}]", args.Input.FileInfo.Name, args.Output.FileInfo.Name); };
ffmpeg.Progress += (sender, args) => { _output.WriteLine("Progress: {0}", args); };
ffmpeg.Error += (sender, args) => { _output.WriteLine("Error: [{0} => {1}] ExitCode: {2}\n{3}", args.Input.FileInfo.Name, args.Output.FileInfo.Name, args.Exception.ExitCode, args.Exception); };
Expand All @@ -50,9 +50,9 @@ public void Converting_Multiple_Files_Simultaneously()
[Fact]
public void Multiple_FFmpeg_Instances_At_Once_Do_Not_Throw_Exception()
{
var task1 = new Engine.FFmpeg(_fixture.FFmpegPath).GetMetaDataAsync(_fixture.VideoFile);
var task2 = new Engine.FFmpeg(_fixture.FFmpegPath).GetMetaDataAsync(_fixture.VideoFile);
var task3 = new Engine.FFmpeg(_fixture.FFmpegPath).GetMetaDataAsync(_fixture.VideoFile);
var task1 = new Engine(_fixture.FFmpegPath).GetMetaDataAsync(_fixture.VideoFile);
var task2 = new Engine(_fixture.FFmpegPath).GetMetaDataAsync(_fixture.VideoFile);
var task3 = new Engine(_fixture.FFmpegPath).GetMetaDataAsync(_fixture.VideoFile);
Task.WaitAll(task1, task2, task3);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/FFmpeg.NET.Tests/PlaylistCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public PlaylistCreatorTests(MediaFileFixture fixture, ITestOutputHelper output)
[Fact]
public async Task M3uPlaylistCreator_Creates_Valid_m3u8_Content()
{
var ffmpeg = new Engine.FFmpeg(_fixture.FFmpegPath);
var ffmpeg = new Engine(_fixture.FFmpegPath);
var meta1 = await ffmpeg.GetMetaDataAsync(_fixture.VideoFile);
var meta2 = await ffmpeg.GetMetaDataAsync(_fixture.AudioFile);

Expand Down Expand Up @@ -52,7 +52,7 @@ public async Task M3uPlaylistCreator_Creates_Valid_m3u8_Content()
[Fact]
public async Task XspfPlaylistCreator_Creates_Valid_Xml()
{
var ffmpeg = new Engine.FFmpeg(_fixture.FFmpegPath);
var ffmpeg = new Engine(_fixture.FFmpegPath);
var meta1 = await ffmpeg.GetMetaDataAsync(_fixture.VideoFile);
var meta2 = await ffmpeg.GetMetaDataAsync(_fixture.AudioFile);

Expand Down

0 comments on commit 8b2228a

Please sign in to comment.