From d2391b95b3c6d8637a5aa435201f5fc641d75596 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Fri, 18 Mar 2022 09:32:47 +0100 Subject: [PATCH] Add MkvEditCommand --- .../MkvPropEdit/MkvEditCommand.cs | 116 ++++++++++++++++++ MkvToolnixWrapper/Shared/MkvExtensions.cs | 13 ++ 2 files changed, 129 insertions(+) create mode 100644 MkvToolnixWrapper/MkvPropEdit/MkvEditCommand.cs create mode 100644 MkvToolnixWrapper/Shared/MkvExtensions.cs diff --git a/MkvToolnixWrapper/MkvPropEdit/MkvEditCommand.cs b/MkvToolnixWrapper/MkvPropEdit/MkvEditCommand.cs new file mode 100644 index 0000000..4e041ff --- /dev/null +++ b/MkvToolnixWrapper/MkvPropEdit/MkvEditCommand.cs @@ -0,0 +1,116 @@ +using System.Text; +using MkvPropEditWrapper.Shared; + +namespace MkvPropEditWrapper; + +public class MkvEditCommand +{ + public const string CommandName = "mkvpropedit"; + + public class SubCommand + { + private int? _trackNumber; + + private string? _property; + private string? _value; + + public SubCommand() + { + } + + public SubCommand(int trackNumber, string property, string value) + { + _trackNumber = trackNumber; + _property = property; + _value = value; + } + + public SubCommand WithNumber(int number) + { + _trackNumber = number; + + return this; + } + + public SubCommand WithProperty(string property) + { + _property = property; + + return this; + } + + public SubCommand WithValue(string value) + { + _value = value; + + return this; + } + + public void AppendSelf(StringBuilder builder) + { + builder.Append($"track:{_trackNumber} --set {_property}:{_value}"); + } + + public override string ToString() + { + return $"track:{_trackNumber} --set {_property}:{_value}"; + } + } + + private string? _filePath; + private readonly IList _subCommands; + + public MkvEditCommand() + { + _subCommands = new List(); + } + + public MkvEditCommand(string filePath) + { + _filePath = filePath; + + _subCommands = new List(); + } + + public MkvEditCommand WithFilePath(string filePath) + { + _filePath = filePath; + + return this; + } + + public MkvEditCommand AddSubCommand(SubCommand command) + { + _subCommands.Add(command); + + return this; + } + + public override string ToString() + { + var builder = new StringBuilder(); + + builder.Append($"{CommandName} {_filePath}"); + + foreach (var subCommand in _subCommands) + { + builder.Append(" --edit "); + subCommand.AppendSelf(builder); + } + + return builder.ToString(); + } +} + +public static class SubCommandExtensions +{ + public static MkvEditCommand.SubCommand WithFlagDefault(this MkvEditCommand.SubCommand subCommand, bool flagDefault) + { + return subCommand.WithProperty(Flags.Default).WithValue(flagDefault.ToFlagBoolean()); + } + + public static MkvEditCommand.SubCommand WithFlagForced(this MkvEditCommand.SubCommand subCommand, bool flagForced) + { + return subCommand.WithProperty(Flags.Forced).WithValue(flagForced.ToFlagBoolean()); + } +} \ No newline at end of file diff --git a/MkvToolnixWrapper/Shared/MkvExtensions.cs b/MkvToolnixWrapper/Shared/MkvExtensions.cs new file mode 100644 index 0000000..5a57624 --- /dev/null +++ b/MkvToolnixWrapper/Shared/MkvExtensions.cs @@ -0,0 +1,13 @@ +namespace MkvPropEditWrapper.Shared; + +public static class MkvExtensions +{ + public static bool FromFlagBoolean(this string value) => value switch + { + "1" => true, + "0" => false, + _ => throw new ArgumentException("Value must be '0' or '1'", nameof(value)) + }; + + public static string ToFlagBoolean(this bool value) => value ? "1" : "0"; +} \ No newline at end of file