From 46bc75c6d1f46253a89d8325696d08e2fb6ca63b Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Thu, 27 Feb 2020 21:12:48 +0100 Subject: [PATCH] Cleanup --- FFMpegCore.Test/ArgumentBuilderTest.cs | 10 +-- FFMpegCore.Test/VideoTest.cs | 68 ++++++++++--------- FFMpegCore/FFMPEG/Argument/Argument.cs | 8 +-- .../FFMPEG/Argument/ArgumentContainer.cs | 4 +- .../Argument/Atoms/BitStreamFilterArgument.cs | 3 +- .../FFMPEG/Argument/Atoms/CopyArgument.cs | 2 +- .../FFMPEG/Argument/FFArgumentBuilder.cs | 5 +- .../FFMPEG/Exceptions/FFMpegException.cs | 6 +- FFMpegCore/FFMPEG/FFMpeg.cs | 1 - FFMpegCore/FFMPEG/FFMpegOptions.cs | 4 +- FFMpegCore/FFMPEG/FFProbe.cs | 8 +-- FFMpegCore/ImageInfo.cs | 10 +-- 12 files changed, 59 insertions(+), 70 deletions(-) diff --git a/FFMpegCore.Test/ArgumentBuilderTest.cs b/FFMpegCore.Test/ArgumentBuilderTest.cs index df791d8..cde8da2 100644 --- a/FFMpegCore.Test/ArgumentBuilderTest.cs +++ b/FFMpegCore.Test/ArgumentBuilderTest.cs @@ -3,7 +3,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; -using FFMpegCore.FFMPEG; namespace FFMpegCore.Test { @@ -22,8 +21,7 @@ public ArgumentBuilderTest() : base() private string GetArgumentsString(params Argument[] args) { - var container = new ArgumentContainer(); - container.Add(new InputArgument("input.mp4")); + var container = new ArgumentContainer {new InputArgument("input.mp4")}; foreach (var a in args) { container.Add(a); @@ -68,10 +66,8 @@ public void Builder_BuildString_BitStream() [TestMethod] public void Builder_BuildString_Concat() { - var container = new ArgumentContainer(); - - container.Add(new ConcatArgument(concatFiles)); - container.Add(new OutputArgument("output.mp4")); + var container = new ArgumentContainer {new ConcatArgument(concatFiles), new OutputArgument("output.mp4")}; + var str = builder.BuildArguments(container); diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 819fb3a..cef729f 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -70,8 +70,7 @@ public void Convert(VideoType type, ArgumentContainer container) { var input = VideoInfo.FromFileInfo(Input); - var arguments = new ArgumentContainer(); - arguments.Add(new InputArgument(input)); + var arguments = new ArgumentContainer {new InputArgument(input)}; foreach (var arg in container) { arguments.Add(arg.Value); @@ -124,8 +123,7 @@ public void Video_ToMP4() [TestMethod] public void Video_ToMP4_Args() { - var container = new ArgumentContainer(); - container.Add(new VideoCodecArgument(VideoCodec.LibX264)); + var container = new ArgumentContainer {new VideoCodecArgument(VideoCodec.LibX264)}; Convert(VideoType.Mp4, container); } @@ -138,10 +136,12 @@ public void Video_ToTS() [TestMethod] public void Video_ToTS_Args() { - var container = new ArgumentContainer(); - container.Add(new CopyArgument()); - container.Add(new BitStreamFilterArgument(Channel.Video, Filter.H264_Mp4ToAnnexB)); - container.Add(new ForceFormatArgument(VideoCodec.MpegTs)); + var container = new ArgumentContainer + { + new CopyArgument(), + new BitStreamFilterArgument(Channel.Video, Filter.H264_Mp4ToAnnexB), + new ForceFormatArgument(VideoCodec.MpegTs) + }; Convert(VideoType.Ts, container); } @@ -155,9 +155,11 @@ public void Video_ToOGV_Resize() [TestMethod] public void Video_ToOGV_Resize_Args() { - var container = new ArgumentContainer(); - container.Add(new ScaleArgument(VideoSize.Ed)); - container.Add(new VideoCodecArgument(VideoCodec.LibTheora)); + var container = new ArgumentContainer + { + new ScaleArgument(VideoSize.Ed), + new VideoCodecArgument(VideoCodec.LibTheora) + }; Convert(VideoType.Ogv, container); } @@ -170,9 +172,11 @@ public void Video_ToMP4_Resize() [TestMethod] public void Video_ToMP4_Resize_Args() { - var container = new ArgumentContainer(); - container.Add(new ScaleArgument(VideoSize.Ld)); - container.Add(new VideoCodecArgument(VideoCodec.LibX264)); + var container = new ArgumentContainer + { + new ScaleArgument(VideoSize.Ld), + new VideoCodecArgument(VideoCodec.LibX264) + }; Convert(VideoType.Mp4, container); } @@ -209,12 +213,10 @@ public void Video_Snapshot() { var input = VideoInfo.FromFileInfo(Input); - using (var bitmap = Encoder.Snapshot(input, output)) - { - Assert.AreEqual(input.Width, bitmap.Width); - Assert.AreEqual(input.Height, bitmap.Height); - Assert.AreEqual(bitmap.RawFormat, ImageFormat.Png); - } + using var bitmap = Encoder.Snapshot(input, output); + Assert.AreEqual(input.Width, bitmap.Width); + Assert.AreEqual(input.Height, bitmap.Height); + Assert.AreEqual(bitmap.RawFormat, ImageFormat.Png); } finally { @@ -231,13 +233,11 @@ public void Video_Snapshot_PersistSnapshot() { var input = VideoInfo.FromFileInfo(Input); - using (var bitmap = Encoder.Snapshot(input, output, persistSnapshotOnFileSystem: true)) - { - Assert.AreEqual(input.Width, bitmap.Width); - Assert.AreEqual(input.Height, bitmap.Height); - Assert.AreEqual(bitmap.RawFormat, ImageFormat.Png); - Assert.IsTrue(File.Exists(output.FullName)); - } + using var bitmap = Encoder.Snapshot(input, output, persistSnapshotOnFileSystem: true); + Assert.AreEqual(input.Width, bitmap.Width); + Assert.AreEqual(input.Height, bitmap.Height); + Assert.AreEqual(bitmap.RawFormat, ImageFormat.Png); + Assert.IsTrue(File.Exists(output.FullName)); } finally { @@ -260,7 +260,7 @@ public void Video_Join() var result = Encoder.Join(output, input, input2); Assert.IsTrue(File.Exists(output.FullName)); - TimeSpan expectedDuration = input.Duration * 2; + var expectedDuration = input.Duration * 2; Assert.AreEqual(expectedDuration.Days, result.Duration.Days); Assert.AreEqual(expectedDuration.Hours, result.Duration.Hours); Assert.AreEqual(expectedDuration.Minutes, result.Duration.Minutes); @@ -289,7 +289,7 @@ public void Video_Join_Image_Sequence() .ToList() .ForEach(file => { - for (int i = 0; i < 15; i++) + for (var i = 0; i < 15; i++) { imageSet.Add(new ImageInfo(file)); } @@ -329,10 +329,12 @@ public void Video_Duration() { var video = VideoInfo.FromFileInfo(VideoLibrary.LocalVideo); var output = Input.OutputLocation(VideoType.Mp4); - var arguments = new ArgumentContainer(); - arguments.Add(new InputArgument(VideoLibrary.LocalVideo)); - arguments.Add(new DurationArgument(TimeSpan.FromSeconds(video.Duration.TotalSeconds - 5))); - arguments.Add(new OutputArgument(output)); + var arguments = new ArgumentContainer + { + new InputArgument(VideoLibrary.LocalVideo), + new DurationArgument(TimeSpan.FromSeconds(video.Duration.TotalSeconds - 5)), + new OutputArgument(output) + }; try { Encoder.Convert(arguments); diff --git a/FFMpegCore/FFMPEG/Argument/Argument.cs b/FFMpegCore/FFMPEG/Argument/Argument.cs index e187afe..353aaeb 100644 --- a/FFMpegCore/FFMPEG/Argument/Argument.cs +++ b/FFMpegCore/FFMPEG/Argument/Argument.cs @@ -41,14 +41,14 @@ public Argument(T value) public abstract class Argument : Argument { /// - /// First value type of + /// First value type of /// - public T1 First { get; set; } + public T1 First { get; } /// - /// Second value type of + /// Second value type of /// - public T2 Second { get; set; } + public T2 Second { get; } public Argument() { } diff --git a/FFMpegCore/FFMPEG/Argument/ArgumentContainer.cs b/FFMpegCore/FFMPEG/Argument/ArgumentContainer.cs index 25f6bde..a543324 100644 --- a/FFMpegCore/FFMPEG/Argument/ArgumentContainer.cs +++ b/FFMpegCore/FFMPEG/Argument/ArgumentContainer.cs @@ -17,7 +17,7 @@ public ArgumentContainer(params Argument[] arguments) foreach(var argument in arguments) { - this.Add(argument); + Add(argument); } } @@ -50,7 +50,7 @@ public void Add(Type key, Argument value) [Obsolete] public void Add(KeyValuePair item) { - this.Add(item.Value); + Add(item.Value); } /// diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/BitStreamFilterArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/BitStreamFilterArgument.cs index 4e321ea..6b6d484 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/BitStreamFilterArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/BitStreamFilterArgument.cs @@ -1,5 +1,4 @@ -using System; -using FFMpegCore.FFMPEG.Enums; +using FFMpegCore.FFMPEG.Enums; namespace FFMpegCore.FFMPEG.Argument { diff --git a/FFMpegCore/FFMPEG/Argument/Atoms/CopyArgument.cs b/FFMpegCore/FFMPEG/Argument/Atoms/CopyArgument.cs index 41a0c30..43153ef 100644 --- a/FFMpegCore/FFMPEG/Argument/Atoms/CopyArgument.cs +++ b/FFMpegCore/FFMPEG/Argument/Atoms/CopyArgument.cs @@ -18,7 +18,7 @@ public override string GetStringValue() { Channel.Audio => "-c:a copy", Channel.Video => "-c:v copy", - Channel.Both => "-c copy" + _ => "-c copy" }; } } diff --git a/FFMpegCore/FFMPEG/Argument/FFArgumentBuilder.cs b/FFMpegCore/FFMPEG/Argument/FFArgumentBuilder.cs index 4707bf3..13932fc 100644 --- a/FFMpegCore/FFMPEG/Argument/FFArgumentBuilder.cs +++ b/FFMpegCore/FFMPEG/Argument/FFArgumentBuilder.cs @@ -1,7 +1,4 @@ -using FFMpegCore.Enums; -using FFMpegCore.Helpers; -using System; -using System.IO; +using System; using System.Linq; namespace FFMpegCore.FFMPEG.Argument diff --git a/FFMpegCore/FFMPEG/Exceptions/FFMpegException.cs b/FFMpegCore/FFMPEG/Exceptions/FFMpegException.cs index ca927d0..0895d02 100644 --- a/FFMpegCore/FFMPEG/Exceptions/FFMpegException.cs +++ b/FFMpegCore/FFMPEG/Exceptions/FFMpegException.cs @@ -14,18 +14,16 @@ public enum FFMpegExceptionType public class FFMpegException : Exception { - public FFMpegException(FFMpegExceptionType type): this(type, null, null) { } - public FFMpegException(FFMpegExceptionType type, StringBuilder sb): this(type, sb.ToString(), null) { } public FFMpegException(FFMpegExceptionType type, string message): this(type, message, null) { } - public FFMpegException(FFMpegExceptionType type, string message, FFMpegException innerException) + public FFMpegException(FFMpegExceptionType type, string message = null, Exception innerException = null) : base(message, innerException) { Type = type; } - public FFMpegExceptionType Type { get; set; } + public FFMpegExceptionType Type { get; } } } \ No newline at end of file diff --git a/FFMpegCore/FFMPEG/FFMpeg.cs b/FFMpegCore/FFMPEG/FFMpeg.cs index be5412d..745d140 100644 --- a/FFMpegCore/FFMPEG/FFMpeg.cs +++ b/FFMpegCore/FFMPEG/FFMpeg.cs @@ -5,7 +5,6 @@ using FFMpegCore.Helpers; using System; using System.Collections.Generic; -using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; using System.Globalization; diff --git a/FFMpegCore/FFMPEG/FFMpegOptions.cs b/FFMpegCore/FFMPEG/FFMpegOptions.cs index 23f7559..f8fe390 100644 --- a/FFMpegCore/FFMPEG/FFMpegOptions.cs +++ b/FFMpegCore/FFMPEG/FFMpegOptions.cs @@ -1,9 +1,7 @@ -using FFMpegCore.FFMPEG.Exceptions; -using Newtonsoft.Json; +using Newtonsoft.Json; using System; using System.IO; using System.Runtime.InteropServices; -using Instances; namespace FFMpegCore.FFMPEG { diff --git a/FFMpegCore/FFMPEG/FFProbe.cs b/FFMpegCore/FFMPEG/FFProbe.cs index 1329b3c..a965f71 100644 --- a/FFMpegCore/FFMPEG/FFProbe.cs +++ b/FFMpegCore/FFMPEG/FFProbe.cs @@ -78,11 +78,11 @@ private VideoInfo ParseVideoInfoInternal(VideoInfo info, string probeOutput) var video = metadata.Streams.Find(s => s.CodecType == "video"); var audio = metadata.Streams.Find(s => s.CodecType == "audio"); - double videoSize = 0d; - double audioSize = 0d; + var videoSize = 0d; + var audioSize = 0d; - string sDuration = (video ?? audio).Duration; - TimeSpan duration = TimeSpan.Zero; + var sDuration = (video ?? audio).Duration; + var duration = TimeSpan.Zero; if (sDuration != null) { duration = TimeSpan.FromSeconds(double.TryParse(sDuration, NumberStyles.Any, CultureInfo.InvariantCulture, out var output) ? output : 0); diff --git a/FFMpegCore/ImageInfo.cs b/FFMpegCore/ImageInfo.cs index 3c1d947..dcf0e28 100644 --- a/FFMpegCore/ImageInfo.cs +++ b/FFMpegCore/ImageInfo.cs @@ -23,14 +23,14 @@ public ImageInfo(FileInfo fileInfo) fileInfo.Refresh(); - this.Size = fileInfo.Length / (1024 * 1024); + Size = fileInfo.Length / (1024 * 1024); using (var image = Image.FromFile(fileInfo.FullName)) { - this.Width = image.Width; - this.Height = image.Height; - var cd = FFProbeHelper.Gcd(this.Width, this.Height); - this.Ratio = $"{this.Width / cd}:{this.Height / cd}"; + Width = image.Width; + Height = image.Height; + var cd = FFProbeHelper.Gcd(Width, Height); + Ratio = $"{Width / cd}:{Height / cd}"; }