From 095687087567d35e842c2e14c59a10d278798e96 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Thu, 16 Oct 2025 15:14:14 +0200 Subject: [PATCH] Run code cleanup --- .../FFMpeg/Arguments/OutputSegmentArgument.cs | 178 ++++++++++-------- .../FFMpeg/Arguments/SegmentCustomArgument.cs | 22 +-- .../SegmentResetTimestampsArgument.cs | 29 +-- .../Arguments/SegmentStrftimeArgument.cs | 31 +-- .../FFMpeg/Arguments/SegmentTimeArgument.cs | 29 +-- .../FFMpeg/Arguments/SegmentWrapArgument.cs | 29 +-- FFMpegCore/FFOptions.cs | 113 +++++------ 7 files changed, 232 insertions(+), 199 deletions(-) diff --git a/FFMpegCore/FFMpeg/Arguments/OutputSegmentArgument.cs b/FFMpegCore/FFMpeg/Arguments/OutputSegmentArgument.cs index 73b63f7..a3f5843 100644 --- a/FFMpegCore/FFMpeg/Arguments/OutputSegmentArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/OutputSegmentArgument.cs @@ -1,88 +1,114 @@ using FFMpegCore.Exceptions; -namespace FFMpegCore.Arguments +namespace FFMpegCore.Arguments; + +/// +/// Represents output parameter +/// +public class OutputSegmentArgument : IOutputArgument { - /// - /// Represents output parameter - /// - public class OutputSegmentArgument : IOutputArgument + public readonly SegmentArgumentOptions Options; + public readonly bool Overwrite; + public readonly string SegmentPattern; + + public OutputSegmentArgument(SegmentArgument segmentArgument) { - public readonly string SegmentPattern; - public readonly bool Overwrite; - public readonly SegmentArgumentOptions Options; - public OutputSegmentArgument(SegmentArgument segmentArgument) + SegmentPattern = segmentArgument.SegmentPattern; + Overwrite = segmentArgument.Overwrite; + var segmentArgumentobj = new SegmentArgumentOptions(); + segmentArgument.Options?.Invoke(segmentArgumentobj); + Options = segmentArgumentobj; + } + + public void Pre() + { + if (int.TryParse(Options.Arguments.FirstOrDefault(x => x.Key == "segment_time").Value, out var result) && result < 1) { - SegmentPattern = segmentArgument.SegmentPattern; - Overwrite = segmentArgument.Overwrite; - var segmentArgumentobj = new SegmentArgumentOptions(); - segmentArgument.Options?.Invoke(segmentArgumentobj); - Options = segmentArgumentobj; + throw new FFMpegException(FFMpegExceptionType.Process, "Parameter SegmentTime cannot be negative or equal to zero"); } - public void Pre() + if (Options.Arguments.FirstOrDefault(x => x.Key == "segment_time").Value == "0") { - if (int.TryParse(Options.Arguments.FirstOrDefault(x => x.Key == "segment_time").Value, out var result) && result < 1) + throw new FFMpegException(FFMpegExceptionType.Process, "Parameter SegmentWrap cannot equal to zero"); + } + } + + public Task During(CancellationToken cancellationToken = default) + { + return Task.CompletedTask; + } + + public void Post() + { + } + + public string Text => GetText(); + + private string GetText() + { + var arguments = Options.Arguments + .Where(arg => !string.IsNullOrWhiteSpace(arg.Value) && !string.IsNullOrWhiteSpace(arg.Key)) + .Select(arg => { - throw new FFMpegException(FFMpegExceptionType.Process, "Parameter SegmentTime cannot be negative or equal to zero"); - } + return arg.Value; + }); - if (Options.Arguments.FirstOrDefault(x => x.Key == "segment_time").Value == "0") - { - throw new FFMpegException(FFMpegExceptionType.Process, "Parameter SegmentWrap cannot equal to zero"); - } - } - public Task During(CancellationToken cancellationToken = default) => Task.CompletedTask; - public void Post() - { - } - - public string Text => GetText(); - private string GetText() - { - var arguments = Options.Arguments - .Where(arg => !string.IsNullOrWhiteSpace(arg.Value) && !string.IsNullOrWhiteSpace(arg.Key)) - .Select(arg => - { - return arg.Value; - }); - - return $"-f segment {string.Join(" ", arguments)} \"{SegmentPattern}\"{(Overwrite ? " -y" : string.Empty)}"; - } - } - - public interface ISegmentArgument - { - public string Key { get; } - public string Value { get; } - } - - public class SegmentArgumentOptions - { - public List Arguments { get; } = new(); - - public SegmentArgumentOptions ResetTimeStamps(bool resetTimestamps = true) => WithArgument(new SegmentResetTimeStampsArgument(resetTimestamps)); - public SegmentArgumentOptions Strftime(bool enable = false) => WithArgument(new SegmentStrftimeArgument(enable)); - public SegmentArgumentOptions Time(int time = 60) => WithArgument(new SegmentTimeArgument(time)); - public SegmentArgumentOptions Wrap(int limit = -1) => WithArgument(new SegmentWrapArgument(limit)); - public SegmentArgumentOptions WithCustomArgument(string argument) => WithArgument(new SegmentCustomArgument(argument)); - private SegmentArgumentOptions WithArgument(ISegmentArgument argument) - { - Arguments.Add(argument); - return this; - } - } - - public class SegmentArgument - { - public readonly string SegmentPattern; - public readonly bool Overwrite; - public readonly Action Options; - - public SegmentArgument(string segmentPattern, bool overwrite, Action options) - { - SegmentPattern = segmentPattern; - Overwrite = overwrite; - Options = options; - } + return $"-f segment {string.Join(" ", arguments)} \"{SegmentPattern}\"{(Overwrite ? " -y" : string.Empty)}"; + } +} + +public interface ISegmentArgument +{ + public string Key { get; } + public string Value { get; } +} + +public class SegmentArgumentOptions +{ + public List Arguments { get; } = new(); + + public SegmentArgumentOptions ResetTimeStamps(bool resetTimestamps = true) + { + return WithArgument(new SegmentResetTimeStampsArgument(resetTimestamps)); + } + + public SegmentArgumentOptions Strftime(bool enable = false) + { + return WithArgument(new SegmentStrftimeArgument(enable)); + } + + public SegmentArgumentOptions Time(int time = 60) + { + return WithArgument(new SegmentTimeArgument(time)); + } + + public SegmentArgumentOptions Wrap(int limit = -1) + { + return WithArgument(new SegmentWrapArgument(limit)); + } + + public SegmentArgumentOptions WithCustomArgument(string argument) + { + return WithArgument(new SegmentCustomArgument(argument)); + } + + private SegmentArgumentOptions WithArgument(ISegmentArgument argument) + { + Arguments.Add(argument); + return this; + } +} + +public class SegmentArgument +{ + public readonly Action Options; + public readonly bool Overwrite; + public readonly string SegmentPattern; + + public SegmentArgument(string segmentPattern, bool overwrite, Action options) + { + SegmentPattern = segmentPattern; + Overwrite = overwrite; + Options = options; } } diff --git a/FFMpegCore/FFMpeg/Arguments/SegmentCustomArgument.cs b/FFMpegCore/FFMpeg/Arguments/SegmentCustomArgument.cs index 359e529..f90503c 100644 --- a/FFMpegCore/FFMpeg/Arguments/SegmentCustomArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/SegmentCustomArgument.cs @@ -1,14 +1,14 @@ -namespace FFMpegCore.Arguments -{ - public class SegmentCustomArgument : ISegmentArgument - { - public readonly string Argument; +namespace FFMpegCore.Arguments; - public SegmentCustomArgument(string argument) - { - Argument = argument; - } - public string Key => "custom"; - public string Value => Argument ?? string.Empty; +public class SegmentCustomArgument : ISegmentArgument +{ + public readonly string Argument; + + public SegmentCustomArgument(string argument) + { + Argument = argument; } + + public string Key => "custom"; + public string Value => Argument ?? string.Empty; } diff --git a/FFMpegCore/FFMpeg/Arguments/SegmentResetTimestampsArgument.cs b/FFMpegCore/FFMpeg/Arguments/SegmentResetTimestampsArgument.cs index f9a048f..34a95e7 100644 --- a/FFMpegCore/FFMpeg/Arguments/SegmentResetTimestampsArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/SegmentResetTimestampsArgument.cs @@ -1,20 +1,21 @@ -namespace FFMpegCore.Arguments +namespace FFMpegCore.Arguments; + +/// +/// Represents reset_timestamps parameter +/// +public class SegmentResetTimeStampsArgument : ISegmentArgument { + public readonly bool ResetTimestamps; + /// - /// Represents reset_timestamps parameter + /// Represents reset_timestamps parameter /// - public class SegmentResetTimeStampsArgument : ISegmentArgument + /// true if files timestamps are to be reset + public SegmentResetTimeStampsArgument(bool resetTimestamps) { - public readonly bool ResetTimestamps; - /// - /// Represents reset_timestamps parameter - /// - /// true if files timestamps are to be reset - public SegmentResetTimeStampsArgument(bool resetTimestamps) - { - ResetTimestamps = resetTimestamps; - } - public string Key { get; } = "reset_timestamps"; - public string Value => ResetTimestamps ? $"-reset_timestamps 1" : string.Empty; + ResetTimestamps = resetTimestamps; } + + public string Key { get; } = "reset_timestamps"; + public string Value => ResetTimestamps ? "-reset_timestamps 1" : string.Empty; } diff --git a/FFMpegCore/FFMpeg/Arguments/SegmentStrftimeArgument.cs b/FFMpegCore/FFMpeg/Arguments/SegmentStrftimeArgument.cs index b50552a..57dfc9d 100644 --- a/FFMpegCore/FFMpeg/Arguments/SegmentStrftimeArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/SegmentStrftimeArgument.cs @@ -1,20 +1,23 @@ -namespace FFMpegCore.Arguments +namespace FFMpegCore.Arguments; + +/// +/// Use the strftime function to define the name of the new segments to write. If this is selected, the output segment name must contain a +/// strftime function template. Default value is 0. +/// +public class SegmentStrftimeArgument : ISegmentArgument { + public readonly bool Enable; + /// - /// Use the strftime function to define the name of the new segments to write. If this is selected, the output segment name must contain a strftime function template. Default value is 0. + /// Use the strftime function to define the name of the new segments to write. If this is selected, the output segment name must contain a + /// strftime function template. Default value is 0. /// - public class SegmentStrftimeArgument : ISegmentArgument + /// true to enable strftime + public SegmentStrftimeArgument(bool enable) { - public readonly bool Enable; - /// - /// Use the strftime function to define the name of the new segments to write. If this is selected, the output segment name must contain a strftime function template. Default value is 0. - /// - /// true to enable strftime - public SegmentStrftimeArgument(bool enable) - { - Enable = enable; - } - public string Key { get; } = "strftime"; - public string Value => Enable ? $"-strftime 1" : string.Empty; + Enable = enable; } + + public string Key { get; } = "strftime"; + public string Value => Enable ? "-strftime 1" : string.Empty; } diff --git a/FFMpegCore/FFMpeg/Arguments/SegmentTimeArgument.cs b/FFMpegCore/FFMpeg/Arguments/SegmentTimeArgument.cs index 5a42e29..c791f5f 100644 --- a/FFMpegCore/FFMpeg/Arguments/SegmentTimeArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/SegmentTimeArgument.cs @@ -1,20 +1,21 @@ -namespace FFMpegCore.Arguments +namespace FFMpegCore.Arguments; + +/// +/// Represents segment_time parameter +/// +public class SegmentTimeArgument : ISegmentArgument { + public readonly int Time; + /// - /// Represents segment_time parameter + /// Represents segment_time parameter /// - public class SegmentTimeArgument : ISegmentArgument + /// time in seconds of the segment + public SegmentTimeArgument(int time) { - public readonly int Time; - /// - /// Represents segment_time parameter - /// - /// time in seconds of the segment - public SegmentTimeArgument(int time) - { - Time = time; - } - public string Key { get; } = "segment_time"; - public string Value => Time <= 0 ? string.Empty : $"-segment_time {Time}"; + Time = time; } + + public string Key { get; } = "segment_time"; + public string Value => Time <= 0 ? string.Empty : $"-segment_time {Time}"; } diff --git a/FFMpegCore/FFMpeg/Arguments/SegmentWrapArgument.cs b/FFMpegCore/FFMpeg/Arguments/SegmentWrapArgument.cs index be38208..811e47c 100644 --- a/FFMpegCore/FFMpeg/Arguments/SegmentWrapArgument.cs +++ b/FFMpegCore/FFMpeg/Arguments/SegmentWrapArgument.cs @@ -1,20 +1,21 @@ -namespace FFMpegCore.Arguments +namespace FFMpegCore.Arguments; + +/// +/// Represents segment_wrap parameter +/// +public class SegmentWrapArgument : ISegmentArgument { + public readonly int Limit; + /// - /// Represents segment_wrap parameter + /// Represents segment_wrap parameter /// - public class SegmentWrapArgument : ISegmentArgument + /// limit value after which segment index will wrap around + public SegmentWrapArgument(int limit) { - public readonly int Limit; - /// - /// Represents segment_wrap parameter - /// - /// limit value after which segment index will wrap around - public SegmentWrapArgument(int limit) - { - Limit = limit; - } - public string Key { get; } = "segment_wrap"; - public string Value => Limit <= 0 ? string.Empty : $"-segment_wrap {Limit}"; + Limit = limit; } + + public string Key { get; } = "segment_wrap"; + public string Value => Limit <= 0 ? string.Empty : $"-segment_wrap {Limit}"; } diff --git a/FFMpegCore/FFOptions.cs b/FFMpegCore/FFOptions.cs index 2d4e4c9..ea784e5 100644 --- a/FFMpegCore/FFOptions.cs +++ b/FFMpegCore/FFOptions.cs @@ -2,68 +2,69 @@ using System.Text.Json.Serialization; using FFMpegCore.Enums; -namespace FFMpegCore +namespace FFMpegCore; + +public class FFOptions : ICloneable { - public class FFOptions : ICloneable + /// + /// Working directory for the ffmpeg/ffprobe instance + /// + public string WorkingDirectory { get; set; } = string.Empty; + + /// + /// Folder container ffmpeg and ffprobe binaries. Leave empty if ffmpeg and ffprobe are present in PATH + /// + public string BinaryFolder { get; set; } = string.Empty; + + /// + /// Folder used for temporary files necessary for static methods on FFMpeg class + /// + public string TemporaryFilesFolder { get; set; } = Path.GetTempPath(); + + /// + /// Encoding web name used to persist encoding + /// + public string EncodingWebName { get; set; } = Encoding.Default.WebName; + + /// + /// Encoding used for parsing stdout/stderr on ffmpeg and ffprobe processes + /// + [JsonIgnore] + public Encoding Encoding { - /// - /// Working directory for the ffmpeg/ffprobe instance - /// - public string WorkingDirectory { get; set; } = string.Empty; + get => Encoding.GetEncoding(EncodingWebName); + set => EncodingWebName = value?.WebName ?? Encoding.Default.WebName; + } - /// - /// Folder container ffmpeg and ffprobe binaries. Leave empty if ffmpeg and ffprobe are present in PATH - /// - public string BinaryFolder { get; set; } = string.Empty; + /// + /// The log level to use when calling of the ffmpeg executable. + /// + /// This option can be overridden before an execution of a Process command + /// to set the log level for that command. + /// + /// + public FFMpegLogLevel? LogLevel { get; set; } - /// - /// Folder used for temporary files necessary for static methods on FFMpeg class - /// - public string TemporaryFilesFolder { get; set; } = Path.GetTempPath(); + /// + /// + public Dictionary ExtensionOverrides { get; set; } = new() { { "mpegts", ".ts" } }; - /// - /// Encoding web name used to persist encoding - /// - public string EncodingWebName { get; set; } = Encoding.Default.WebName; + /// + /// Whether to cache calls to get ffmpeg codec, pixel- and container-formats + /// + public bool UseCache { get; set; } = true; - /// - /// Encoding used for parsing stdout/stderr on ffmpeg and ffprobe processes - /// - [JsonIgnore] - public Encoding Encoding - { - get => Encoding.GetEncoding(EncodingWebName); - set => EncodingWebName = value?.WebName ?? Encoding.Default.WebName; - } + /// + object ICloneable.Clone() + { + return Clone(); + } - /// - /// The log level to use when calling of the ffmpeg executable. - /// - /// This option can be overridden before an execution of a Process command - /// to set the log level for that command. - /// - /// - public FFMpegLogLevel? LogLevel { get; set; } - - /// - /// - /// - public Dictionary ExtensionOverrides { get; set; } = new() - { - { "mpegts", ".ts" }, - }; - - /// - /// Whether to cache calls to get ffmpeg codec, pixel- and container-formats - /// - public bool UseCache { get; set; } = true; - - /// - object ICloneable.Clone() => Clone(); - - /// - /// Creates a new object that is a copy of the current instance. - /// - public FFOptions Clone() => (FFOptions)MemberwiseClone(); + /// + /// Creates a new object that is a copy of the current instance. + /// + public FFOptions Clone() + { + return (FFOptions)MemberwiseClone(); } }