From 8ee92a40ab7687a9167a3312b143c20eb1e3c5f7 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Tue, 11 Feb 2020 22:45:02 +0100 Subject: [PATCH] Cleanup begun --- FFMpegCore/FFMPEG/Argument/Argument.cs | 8 +- .../FFMPEG/Argument/ArgumentContainer.cs | 3 - .../FFMPEG/Argument/ArgumentStringifier.cs | 31 ------ .../Argument/Atoms/AudioCodecArgument.cs | 5 - .../Argument/Atoms/BitStreamFilterArgument.cs | 5 - .../FFMPEG/Argument/Atoms/ConcatArgument.cs | 6 +- .../FFMPEG/Argument/Atoms/CopyArgument.cs | 5 - .../FFMPEG/Argument/Atoms/CpuSpeedArgument.cs | 8 +- .../Argument/Atoms/ForceFormatArgument.cs | 5 - .../Atoms/FrameOutputCountArgument.cs | 8 +- .../Argument/Atoms/FrameRateArgument.cs | 8 +- .../FFMPEG/Argument/Atoms/InputArgument.cs | 3 - .../FFMPEG/Argument/Atoms/LoopArgument.cs | 8 +- .../FFMPEG/Argument/Atoms/OutputArgument.cs | 4 - .../FFMPEG/Argument/Atoms/OverrideArgument.cs | 8 +- .../FFMPEG/Argument/Atoms/ScaleArgument.cs | 7 +- .../FFMPEG/Argument/Atoms/SeekArgument.cs | 4 - .../FFMPEG/Argument/Atoms/ShortestArgument.cs | 8 +- .../FFMPEG/Argument/Atoms/SizeArgument.cs | 9 +- .../FFMPEG/Argument/Atoms/SpeedArgument.cs | 5 - .../Argument/Atoms/StartNumberArgument.cs | 8 +- .../FFMPEG/Argument/Atoms/ThreadsArgument.cs | 4 - .../Argument/Atoms/VideoCodecArgument.cs | 5 - .../FFMPEG/Argument/IArgumentBuilder.cs | 10 +- FFMpegCore/FFMPEG/FFBase.cs | 22 ++++- FFMpegCore/FFMPEG/FFMpeg.cs | 99 ++++++++++--------- FFMpegCore/FFMPEG/FFMpegOptions.cs | 63 +++++------- FFMpegCore/FFMPEG/FFProbe.cs | 3 - FFMpegCore/FFMpegCore.csproj | 2 + FFMpegCore/Helpers/FFMpegHelper.cs | 1 - FFMpegCore/Helpers/FFProbeHelper.cs | 5 +- FFMpegCore/ImageInfo.cs | 2 - README.md | 2 +- 33 files changed, 110 insertions(+), 264 deletions(-) diff --git a/FFMpegCore/FFMPEG/Argument/Argument.cs b/FFMpegCore/FFMPEG/Argument/Argument.cs index bad0d57..f911ea3 100644 --- a/FFMpegCore/FFMPEG/Argument/Argument.cs +++ b/FFMpegCore/FFMPEG/Argument/Argument.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { /// /// Abstract class implements basic functionality of ffmpeg arguments diff --git a/FFMpegCore/FFMPEG/Argument/ArgumentContainer.cs b/FFMpegCore/FFMPEG/Argument/ArgumentContainer.cs index 7587101..25f6bde 100644 --- a/FFMpegCore/FFMPEG/Argument/ArgumentContainer.cs +++ b/FFMpegCore/FFMPEG/Argument/ArgumentContainer.cs @@ -1,9 +1,6 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/ArgumentStringifier.cs b/FFMpegCore/FFMPEG/Argument/ArgumentStringifier.cs index 30d2001..0a723ef 100644 --- a/FFMpegCore/FFMPEG/Argument/ArgumentStringifier.cs +++ b/FFMpegCore/FFMPEG/Argument/ArgumentStringifier.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Drawing; -using System.IO; namespace FFMpegCore.FFMPEG.Argument { @@ -18,11 +17,6 @@ internal static string Speed(int cpu) return $"-quality good -cpu-used {cpu} -deadline realtime "; } - internal static string Audio(AudioCodec codec, AudioQuality bitrate) - { - return Audio(codec) + Audio(bitrate); - } - internal static string Audio(AudioCodec codec, int bitrate) { return Audio(codec) + Audio(bitrate); @@ -69,11 +63,6 @@ internal static string Threads(int threads) return $"-threads {threads} "; } - internal static string Input(Uri uri) - { - return Input(uri.AbsolutePath); - } - internal static string Disable(Channel type) { switch (type) @@ -87,21 +76,6 @@ internal static string Disable(Channel type) } } - internal static string Input(VideoInfo input) - { - return $"-i \"{input.FullName}\" "; - } - - internal static string Input(FileInfo input) - { - return $"-i \"{input.FullName}\" "; - } - - internal static string Output(FileInfo output) - { - return $"\"{output.FullName}\""; - } - internal static string Output(string output) { return $"\"{output}\""; @@ -122,11 +96,6 @@ internal static string Scale(int width, int height) return $"-vf scale={width}:{height} "; } - internal static string Scale(Size size) - { - return Scale(size.Width, size.Height); - } - internal static string Size(Size? size) { if (!size.HasValue) return string.Empty; diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/AudioCodecArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/AudioCodecArgument.cs index a053f7d..66195f9 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/AudioCodecArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/AudioCodecArgument.cs @@ -1,9 +1,4 @@ using FFMpegCore.FFMPEG.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/BitStreamFilterArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/BitStreamFilterArgument.cs index d950903..553072b 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/BitStreamFilterArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/BitStreamFilterArgument.cs @@ -1,9 +1,4 @@ using FFMpegCore.FFMPEG.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/ConcatArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/ConcatArgument.cs index b37d6a1..cbdbb51 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/ConcatArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/ConcatArgument.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections; +using System.Collections; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/CopyArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/CopyArgument.cs index 586e3f2..cd94e69 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/CopyArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/CopyArgument.cs @@ -1,9 +1,4 @@ using FFMpegCore.FFMPEG.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/CpuSpeedArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/CpuSpeedArgument.cs index c3e8f47..53c4014 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/CpuSpeedArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/CpuSpeedArgument.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { /// /// Represents cpu speed parameter diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/ForceFormatArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/ForceFormatArgument.cs index 0180f59..b9c3a4d 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/ForceFormatArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/ForceFormatArgument.cs @@ -1,9 +1,4 @@ using FFMpegCore.FFMPEG.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/FrameOutputCountArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/FrameOutputCountArgument.cs index b6c8a8d..3facb87 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/FrameOutputCountArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/FrameOutputCountArgument.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { /// /// Represents frame output count parameter diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/FrameRateArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/FrameRateArgument.cs index 9966dfc..f6e0f01 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/FrameRateArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/FrameRateArgument.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { /// /// Represents frame rate parameter diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/InputArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/InputArgument.cs index a04f980..e4a370f 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/InputArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/InputArgument.cs @@ -1,9 +1,6 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/LoopArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/LoopArgument.cs index f8a3f8a..b09b2dd 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/LoopArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/LoopArgument.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { /// /// Represents loop parameter diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/OutputArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/OutputArgument.cs index 9d70148..5049b0b 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/OutputArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/OutputArgument.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/OverrideArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/OverrideArgument.cs index 91f84e4..ec5ad73 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/OverrideArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/OverrideArgument.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { /// /// Represents override parameter diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/ScaleArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/ScaleArgument.cs index 50f59ca..a0b4712 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/ScaleArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/ScaleArgument.cs @@ -1,10 +1,5 @@ using FFMpegCore.FFMPEG.Enums; -using System; -using System.Collections.Generic; using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { @@ -36,7 +31,7 @@ public ScaleArgument(VideoSize videosize) /// String representation of the argument public override string GetStringValue() { - return ArgumentStringifier.Scale(Value); + return ArgumentStringifier.Scale(Value.Width, Value.Height); } } } diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/SeekArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/SeekArgument.cs index f6a8410..edc0a6e 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/SeekArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/SeekArgument.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/ShortestArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/ShortestArgument.cs index 2a3e7e5..c25b0ad 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/ShortestArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/ShortestArgument.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { /// /// Represents shortest parameter diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/SizeArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/SizeArgument.cs index 14f592e..f70dc7d 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/SizeArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/SizeArgument.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Drawing; using FFMpegCore.FFMPEG.Enums; namespace FFMpegCore.FFMPEG.Argument @@ -17,7 +12,7 @@ public SizeArgument() { } - public SizeArgument(Size? value) : base(value.HasValue ? value.Value : new Size()) + public SizeArgument(Size? value) : base(value ?? new Size()) { } diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/SpeedArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/SpeedArgument.cs index 70690da..6d8f5cd 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/SpeedArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/SpeedArgument.cs @@ -1,9 +1,4 @@ using FFMpegCore.FFMPEG.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/StartNumberArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/StartNumberArgument.cs index 832117e..7a8201d 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/StartNumberArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/StartNumberArgument.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { /// /// Represents start number parameter diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/ThreadsArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/ThreadsArgument.cs index ecfad9b..5c95a4a 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/ThreadsArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/ThreadsArgument.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/VideoCodecArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/VideoCodecArgument.cs index 2357341..eaf5d9b 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/VideoCodecArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/VideoCodecArgument.cs @@ -1,9 +1,4 @@ using FFMpegCore.FFMPEG.Enums; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/IArgumentBuilder.cs b/FFMpegCore/FFMPEG/Argument/IArgumentBuilder.cs index 1398c31..0248c36 100644 --- a/FFMpegCore/FFMPEG/Argument/IArgumentBuilder.cs +++ b/FFMpegCore/FFMPEG/Argument/IArgumentBuilder.cs @@ -1,12 +1,4 @@ -using FFMpegCore.FFMPEG.Enums; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FFMpegCore.FFMPEG.Argument +namespace FFMpegCore.FFMPEG.Argument { public interface IArgumentBuilder { diff --git a/FFMpegCore/FFMPEG/FFBase.cs b/FFMpegCore/FFMPEG/FFBase.cs index 6ac3d80..554f36d 100644 --- a/FFMpegCore/FFMPEG/FFBase.cs +++ b/FFMpegCore/FFMPEG/FFBase.cs @@ -1,8 +1,5 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; +using System; using System.Diagnostics; -using System.IO; using System.Linq; using System.Threading.Tasks; using RunProcessAsTask; @@ -86,4 +83,21 @@ protected async Task RunProcessAsync(string filePath, string arguments) return string.Join("", result.StandardOutput); } } + + public static class ProcessHelpers + { + public static void RunProcess(string filePath, string arguments) + { + + } + public static async Task RunProcessAsync(string fileName, string arguments) + { + var startInfo = new ProcessStartInfo(fileName, arguments); + startInfo.RedirectStandardOutput = true; + startInfo.RedirectStandardError = true; + + var result = await ProcessEx.RunAsync(startInfo); + return string.Join("", result.StandardOutput); + } + } } \ No newline at end of file diff --git a/FFMpegCore/FFMPEG/FFMpeg.cs b/FFMpegCore/FFMPEG/FFMpeg.cs index de44268..5fe7689 100644 --- a/FFMpegCore/FFMPEG/FFMpeg.cs +++ b/FFMpegCore/FFMPEG/FFMpeg.cs @@ -11,7 +11,6 @@ using System.Globalization; using System.IO; using System.Linq; -using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; @@ -26,7 +25,7 @@ public class FFMpeg : FFBase /// /// Intializes the FFMPEG encoder. /// - public FFMpeg(): base() + public FFMpeg() : base() { FFMpegHelper.RootExceptionCheck(FFMpegOptions.Options.RootDirectory); @@ -49,7 +48,8 @@ public FFMpeg(): base() /// Thumbnail size. If width or height equal 0, the other will be computed automatically. /// By default, it deletes the created image on disk. If set to true, it won't delete the image /// Bitmap with the requested snapshot. - public Bitmap Snapshot(VideoInfo source, FileInfo output, Size? size = null, TimeSpan? captureTime = null, bool persistSnapshotOnFileSystem = false) + public Bitmap Snapshot(VideoInfo source, FileInfo output, Size? size = null, TimeSpan? captureTime = null, + bool persistSnapshotOnFileSystem = false) { if (captureTime == null) captureTime = TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3); @@ -66,28 +66,28 @@ public Bitmap Snapshot(VideoInfo source, FileInfo output, Size? size = null, Tim { if (size.Value.Width == 0) { - var ratio = source.Width / (double)size.Value.Width; + var ratio = source.Width / (double) size.Value.Width; - size = new Size((int)(source.Width * ratio), (int)(source.Height * ratio)); + size = new Size((int) (source.Width * ratio), (int) (source.Height * ratio)); } if (size.Value.Height == 0) { - var ratio = source.Height / (double)size.Value.Height; + var ratio = source.Height / (double) size.Value.Height; - size = new Size((int)(source.Width * ratio), (int)(source.Height * ratio)); + size = new Size((int) (source.Width * ratio), (int) (source.Height * ratio)); } } FFMpegHelper.ConversionExceptionCheck(source.ToFileInfo(), output); var container = new ArgumentContainer( - new InputArgument(source), - new VideoCodecArgument(VideoCodec.Png), - new FrameOutputCountArgument(1), - new SeekArgument(captureTime), - new SizeArgument(size), - new OutputArgument(output) - ); + new InputArgument(source), + new VideoCodecArgument(VideoCodec.Png), + new FrameOutputCountArgument(1), + new SeekArgument(captureTime), + new SizeArgument(size), + new OutputArgument(output) + ); if (!RunProcess(container, output)) { @@ -97,7 +97,7 @@ public Bitmap Snapshot(VideoInfo source, FileInfo output, Size? size = null, Tim output.Refresh(); Bitmap result; - using (var bmp = (Bitmap)Image.FromFile(output.FullName)) + using (var bmp = (Bitmap) Image.FromFile(output.FullName)) { using (var ms = new MemoryStream()) { @@ -129,10 +129,8 @@ public VideoInfo Convert( VideoInfo source, FileInfo output, VideoType type = VideoType.Mp4, - Speed speed = - Speed.SuperFast, - VideoSize size = - VideoSize.Original, + Speed speed = Speed.SuperFast, + VideoSize size = VideoSize.Original, AudioQuality audioQuality = AudioQuality.Normal, bool multithreaded = false) { @@ -142,13 +140,12 @@ public VideoInfo Convert( _totalTime = source.Duration; - var scale = VideoSize.Original == size ? 1 : - (double)source.Height / (int)size; + var scale = VideoSize.Original == size ? 1 : (double) source.Height / (int) size; var outputSize = new Size( - (int)(source.Width / scale), - (int)(source.Height / scale) - ); + (int) (source.Width / scale), + (int) (source.Height / scale) + ); if (outputSize.Width % 2 != 0) { @@ -194,7 +191,8 @@ public VideoInfo Convert( if (!RunProcess(container, output)) { - throw new FFMpegException(FFMpegExceptionType.Conversion, $"The video could not be converted to {Enum.GetName(typeof(VideoType), type)}"); + throw new FFMpegException(FFMpegExceptionType.Conversion, + $"The video could not be converted to {Enum.GetName(typeof(VideoType), type)}"); } _totalTime = TimeSpan.MinValue; @@ -226,7 +224,8 @@ public VideoInfo PosterWithAudio(FileInfo image, FileInfo audio, FileInfo output if (!RunProcess(container, output)) { - throw new FFMpegException(FFMpegExceptionType.Operation, "An error occured while adding the audio file to the image."); + throw new FFMpegException(FFMpegExceptionType.Operation, + "An error occured while adding the audio file to the image."); } return new VideoInfo(output); @@ -248,10 +247,10 @@ public VideoInfo Join(FileInfo output, params VideoInfo[] videos) FFMpegHelper.ConversionSizeExceptionCheck(video); var destinationPath = video.FullName.Replace(video.Extension, FileExtension.Ts); Convert( - video, - new FileInfo(destinationPath), - VideoType.Ts - ); + video, + new FileInfo(destinationPath), + VideoType.Ts + ); return destinationPath; }).ToList(); @@ -266,10 +265,11 @@ public VideoInfo Join(FileInfo output, params VideoInfo[] videos) { if (!RunProcess(container, output)) { - throw new FFMpegException(FFMpegExceptionType.Operation, "Could not join the provided video files."); + throw new FFMpegException(FFMpegExceptionType.Operation, + "Could not join the provided video files."); } - return new VideoInfo(output); + return new VideoInfo(output); } finally { @@ -289,7 +289,8 @@ public VideoInfo JoinImageSequence(FileInfo output, double frameRate = 30, param var temporaryImageFiles = images.Select((image, index) => { FFMpegHelper.ConversionSizeExceptionCheck(Image.FromFile(image.FullName)); - var destinationPath = image.FullName.Replace(image.Name, $"{index.ToString().PadLeft(9, '0')}{image.Extension}"); + var destinationPath = + image.FullName.Replace(image.Name, $"{index.ToString().PadLeft(9, '0')}{image.Extension}"); File.Copy(image.FullName, destinationPath); return destinationPath; @@ -311,7 +312,8 @@ public VideoInfo JoinImageSequence(FileInfo output, double frameRate = 30, param { if (!RunProcess(container, output)) { - throw new FFMpegException(FFMpegExceptionType.Operation, "Could not join the provided image sequence."); + throw new FFMpegException(FFMpegExceptionType.Operation, + "Could not join the provided image sequence."); } return new VideoInfo(output); @@ -341,11 +343,13 @@ public VideoInfo SaveM3U8Stream(Uri uri, FileInfo output) if (!RunProcess(container, output)) { - throw new FFMpegException(FFMpegExceptionType.Operation, $"Saving the ${uri.AbsoluteUri} stream failed."); + throw new FFMpegException(FFMpegExceptionType.Operation, + $"Saving the ${uri.AbsoluteUri} stream failed."); } return new VideoInfo(output); } + throw new ArgumentException($"Uri: {uri.AbsoluteUri}, does not point to a valid http(s) stream."); } @@ -395,7 +399,8 @@ public FileInfo ExtractAudio(VideoInfo source, FileInfo output) if (!RunProcess(container, output)) { - throw new FFMpegException(FFMpegExceptionType.Operation, "Could not extract the audio from the requested video."); + throw new FFMpegException(FFMpegExceptionType.Operation, + "Could not extract the audio from the requested video."); } output.Refresh(); @@ -436,8 +441,8 @@ public VideoInfo ReplaceAudio(VideoInfo source, FileInfo audio, FileInfo output, public VideoInfo Convert(ArgumentContainer arguments) { - var output = ((OutputArgument)arguments[typeof(OutputArgument)]).GetAsFileInfo(); - var sources = ((InputArgument)arguments[typeof(InputArgument)]).GetAsVideoInfo(); + var output = ((OutputArgument) arguments[typeof(OutputArgument)]).GetAsFileInfo(); + var sources = ((InputArgument) arguments[typeof(InputArgument)]).GetAsVideoInfo(); // Sum duration of all sources _totalTime = TimeSpan.Zero; @@ -475,8 +480,8 @@ public void Stop() private bool RunProcess(ArgumentContainer container, FileInfo output) { var successState = true; - - CreateProcess(this.ArgumentBuilder.BuildArguments(container), _ffmpegPath, true, rStandardError: true); + + CreateProcess(ArgumentBuilder.BuildArguments(container), _ffmpegPath, true, rStandardError: true); try { @@ -506,6 +511,7 @@ private bool RunProcess(ArgumentContainer container, FileInfo output) throw new FFMpegException(FFMpegExceptionType.Process, _errorOutput); } } + return successState; } @@ -520,6 +526,7 @@ private void Cleanup(IEnumerable pathList) } } + private static Regex _progressRegex = new Regex(@"\w\w:\w\w:\w\w", RegexOptions.Compiled); private void OutputData(object sender, DataReceivedEventArgs e) { if (e.Data == null) @@ -532,17 +539,17 @@ private void OutputData(object sender, DataReceivedEventArgs e) if (OnProgress == null || !IsWorking) return; - var r = new Regex(@"\w\w:\w\w:\w\w"); - var m = r.Match(e.Data); if (!e.Data.Contains("frame")) return; - if (!m.Success) return; + + var match = _progressRegex.Match(e.Data); + if (!match.Success) return; - var t = TimeSpan.Parse(m.Value, CultureInfo.InvariantCulture); - var percentage = Math.Round(t.TotalSeconds / _totalTime.TotalSeconds * 100, 2); + var processed = TimeSpan.Parse(match.Value, CultureInfo.InvariantCulture); + var percentage = Math.Round(processed.TotalSeconds / _totalTime.TotalSeconds * 100, 2); OnProgress(percentage); } #endregion } -} +} \ No newline at end of file diff --git a/FFMpegCore/FFMPEG/FFMpegOptions.cs b/FFMpegCore/FFMPEG/FFMpegOptions.cs index f59e632..7555620 100644 --- a/FFMpegCore/FFMPEG/FFMpegOptions.cs +++ b/FFMpegCore/FFMPEG/FFMpegOptions.cs @@ -8,8 +8,8 @@ namespace FFMpegCore.FFMPEG { public class FFMpegOptions { - private static string _ConfigFile = Path.Combine(".", "ffmpeg.config.json"); - private static string _DefaultRoot = Path.Combine(".", "FFMPEG", "bin"); + private static readonly string ConfigFile = Path.Combine(".", "ffmpeg.config.json"); + private static readonly string DefaultRoot = Path.Combine(".", "FFMPEG", "bin"); public static FFMpegOptions Options { get; private set; } = new FFMpegOptions(); @@ -20,56 +20,37 @@ public static void Configure(FFMpegOptions options) static FFMpegOptions() { - if (File.Exists(_ConfigFile)) + if (File.Exists(ConfigFile)) { - Options = JsonConvert.DeserializeObject(File.ReadAllText(_ConfigFile)); + Options = JsonConvert.DeserializeObject(File.ReadAllText(ConfigFile)); } } - public string RootDirectory { get; set; } = _DefaultRoot; + public string RootDirectory { get; set; } = DefaultRoot; - public string FFmpegBinary + public string FFmpegBinary => FFBinary("FFMpeg"); + + public string FFProbeBinary => FFBinary("FFProbe"); + + private static string FFBinary(string name) { - get + var ffName = name.ToLowerInvariant(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + ffName += ".exe"; + + var target = Environment.Is64BitProcess ? "x64" : "x86"; + if (Directory.Exists(Path.Combine(Options.RootDirectory, target))) { - var target = Environment.Is64BitProcess ? "x64" : "x86"; - var progName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "ffmpeg.exe" : "ffmpeg"; - - if (Directory.Exists(Path.Combine(Options.RootDirectory, target))) - { - progName = Path.Combine(target, progName); - } - - var path = Path.Combine(Options.RootDirectory, progName); - - if (!File.Exists(path)) - throw new FFMpegException(FFMpegExceptionType.Dependency, - $"FFMpeg cannot be found @ {path}"); - - return path; + ffName = Path.Combine(target, ffName); } - } - public string FFProbeBinary - { - get - { - var target = Environment.Is64BitProcess ? "x64" : "x86"; - var progName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "ffprobe.exe" : "ffprobe"; + var path = Path.Combine(Options.RootDirectory, ffName); - if (Directory.Exists(Path.Combine(Options.RootDirectory, target))) - { - progName = Path.Combine(target, progName); - } + if (!File.Exists(path)) + throw new FFMpegException(FFMpegExceptionType.Dependency, + $"{name} cannot be found @ {path}"); - var path = Path.Combine(Options.RootDirectory, progName); - - if (!File.Exists(path)) - throw new FFMpegException(FFMpegExceptionType.Dependency, - $"FFProbe cannot be found @ {path}"); - - return path; - } + return path; } } } diff --git a/FFMpegCore/FFMPEG/FFProbe.cs b/FFMpegCore/FFMPEG/FFProbe.cs index 095a7d2..0e084cd 100644 --- a/FFMpegCore/FFMPEG/FFProbe.cs +++ b/FFMpegCore/FFMPEG/FFProbe.cs @@ -3,10 +3,7 @@ using Newtonsoft.Json; using System; using System.Globalization; -using System.IO; -using System.Runtime.InteropServices; using System.Threading.Tasks; -using RunProcessAsTask; namespace FFMpegCore.FFMPEG { diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index b4b271f..a72cabc 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -11,6 +11,7 @@ 1.0.12.0 1.0.12.0 + 8 @@ -128,6 +129,7 @@ + diff --git a/FFMpegCore/Helpers/FFMpegHelper.cs b/FFMpegCore/Helpers/FFMpegHelper.cs index f0c65c3..9804869 100644 --- a/FFMpegCore/Helpers/FFMpegHelper.cs +++ b/FFMpegCore/Helpers/FFMpegHelper.cs @@ -1,7 +1,6 @@ using System; using System.Drawing; using System.IO; -using System.Runtime.InteropServices; using FFMpegCore.FFMPEG.Exceptions; namespace FFMpegCore.Helpers diff --git a/FFMpegCore/Helpers/FFProbeHelper.cs b/FFMpegCore/Helpers/FFProbeHelper.cs index 3efcda9..5b6b423 100644 --- a/FFMpegCore/Helpers/FFProbeHelper.cs +++ b/FFMpegCore/Helpers/FFProbeHelper.cs @@ -1,7 +1,4 @@ -using System; -using System.IO; -using System.Runtime.InteropServices; -using FFMpegCore.FFMPEG.Exceptions; +using FFMpegCore.FFMPEG.Exceptions; namespace FFMpegCore.Helpers { diff --git a/FFMpegCore/ImageInfo.cs b/FFMpegCore/ImageInfo.cs index 3550b25..9c9c044 100644 --- a/FFMpegCore/ImageInfo.cs +++ b/FFMpegCore/ImageInfo.cs @@ -1,10 +1,8 @@ using FFMpegCore.Enums; -using FFMpegCore.FFMPEG; using FFMpegCore.Helpers; using System; using System.Drawing; using System.IO; -using System.Linq; namespace FFMpegCore { diff --git a/README.md b/README.md index 7543cd6..0c87ad1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # FFMpegCore [![NuGet Badge](https://buildstats.info/nuget/FFMpegCore)](https://www.nuget.org/packages/FFMpegCore/) -[![Build Status](https://travis-ci.org/vladjerca/FFMpegCore.svg?branch=master)](https://travis-ci.org/vladjerca/FFMpegCore) +[![Build Status](https://travis-ci.org/rosenbjerg/FFMpegCore.svg)](https://travis-ci.org/vladjerca/FFMpegCore) # Setup