From 6bc2c822d4b881dbb62833bf4d13dee553d81960 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Thu, 28 Nov 2019 18:35:38 +0100 Subject: [PATCH] Allow custom order of actions in cmd line convert + same action multiple times related to #3854 --- src/Forms/BatchConvert.cs | 2 +- .../CommandLineConverter.cs | 130 +++++++++--------- 2 files changed, 65 insertions(+), 67 deletions(-) diff --git a/src/Forms/BatchConvert.cs b/src/Forms/BatchConvert.cs index 7d8a85ebb..469f311c0 100644 --- a/src/Forms/BatchConvert.cs +++ b/src/Forms/BatchConvert.cs @@ -1370,7 +1370,7 @@ namespace Nikse.SubtitleEdit.Forms { dir = Path.GetDirectoryName(p.FileName); } - var success = CommandLineConverter.BatchConvertSave(targetFormat, TimeSpan.Zero, GetCurrentEncoding(), dir, _count, ref _converted, ref _errors, _allFormats, p.FileName, p.Subtitle, p.SourceFormat, binaryParagraphs, overwrite, -1, null, null, CommandLineConverter.BatchAction.None, null, false, progressCallback); + var success = CommandLineConverter.BatchConvertSave(targetFormat, TimeSpan.Zero, GetCurrentEncoding(), dir, _count, ref _converted, ref _errors, _allFormats, p.FileName, p.Subtitle, p.SourceFormat, binaryParagraphs, overwrite, -1, null, null, null, null, false, progressCallback); if (success) { p.Item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Converted; diff --git a/src/Logic/CommandLineConvert/CommandLineConverter.cs b/src/Logic/CommandLineConvert/CommandLineConverter.cs index 58cff712d..34cfcbd03 100644 --- a/src/Logic/CommandLineConvert/CommandLineConverter.cs +++ b/src/Logic/CommandLineConvert/CommandLineConverter.cs @@ -27,15 +27,13 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert public delegate void BatchConvertProgress(string progress); - [Flags] internal enum BatchAction { - None = 0, - FixCommonErrors = 1, - RemoveTextForHI = 2, - RemoveFormatting = 4, - ReDoCasing = 8, - ReverseRtlStartEnd = 16 + FixCommonErrors, + RemoveTextForHI, + RemoveFormatting, + ReDoCasing, + ReverseRtlStartEnd } internal static void ConvertOrReturn(string productIdentifier, string[] commandLineArguments) @@ -729,7 +727,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert return null; } - private static void ConvertBluRaySubtitle(string fileName, string targetFormat, TimeSpan offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IEnumerable formats, bool overwrite, int pacCodePage, double? targetFrameRate, ICollection multipleReplaceImportFiles, BatchAction actions, bool forcedOnly, Point? resolution) + private static void ConvertBluRaySubtitle(string fileName, string targetFormat, TimeSpan offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IEnumerable formats, bool overwrite, int pacCodePage, double? targetFrameRate, ICollection multipleReplaceImportFiles, List actions, bool forcedOnly, Point? resolution) { var format = Utilities.GetSubtitleFormatByFriendlyName(targetFormat) ?? new SubRip(); @@ -758,7 +756,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert } } - private static void ConvertVobSubSubtitle(string fileName, string targetFormat, TimeSpan offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IEnumerable formats, bool overwrite, int pacCodePage, double? targetFrameRate, ICollection multipleReplaceImportFiles, BatchAction actions, bool forcedOnly) + private static void ConvertVobSubSubtitle(string fileName, string targetFormat, TimeSpan offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IEnumerable formats, bool overwrite, int pacCodePage, double? targetFrameRate, ICollection multipleReplaceImportFiles, List actions, bool forcedOnly) { var format = Utilities.GetSubtitleFormatByFriendlyName(targetFormat) ?? new SubRip(); @@ -784,7 +782,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert } } - private static void ConvertImageListSubtitle(string fileName, Subtitle subtitle, string targetFormat, TimeSpan offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, List formats, bool overwrite, int pacCodePage, double? targetFrameRate, ICollection multipleReplaceImportFiles, BatchAction actions) + private static void ConvertImageListSubtitle(string fileName, Subtitle subtitle, string targetFormat, TimeSpan offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, List formats, bool overwrite, int pacCodePage, double? targetFrameRate, ICollection multipleReplaceImportFiles, List actions) { var format = Utilities.GetSubtitleFormatByFriendlyName(targetFormat) ?? new SubRip(); @@ -919,40 +917,40 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert return null; } - private static BatchAction GetArgumentActions(List args) + private static List GetArgumentActions(List args) { - var actions = BatchAction.None; + var actions = new List(); for (int i = args.Count - 1; i >= 0; i--) { var argument = args[i]; if (argument.StartsWith("/fixcommonerrors", StringComparison.OrdinalIgnoreCase) || argument.StartsWith("-fixcommonerrors", StringComparison.OrdinalIgnoreCase)) { - actions |= BatchAction.FixCommonErrors; + actions.Add(BatchAction.FixCommonErrors); args.RemoveAt(i); } else if (argument.StartsWith("/reversertlstartend", StringComparison.OrdinalIgnoreCase) || argument.StartsWith("-reversertlstartend", StringComparison.OrdinalIgnoreCase)) { - actions |= BatchAction.ReverseRtlStartEnd; + actions.Add(BatchAction.ReverseRtlStartEnd); args.RemoveAt(i); } else if (argument.StartsWith("/redocasing", StringComparison.OrdinalIgnoreCase) || argument.StartsWith("-redocasing", StringComparison.OrdinalIgnoreCase)) { - actions |= BatchAction.ReDoCasing; + actions.Add(BatchAction.ReDoCasing); args.RemoveAt(i); } else if (argument.StartsWith("/removetextforhi", StringComparison.OrdinalIgnoreCase) || argument.StartsWith("-removetextforhi", StringComparison.OrdinalIgnoreCase)) { - actions |= BatchAction.RemoveTextForHI; + actions.Add(BatchAction.RemoveTextForHI); args.RemoveAt(i); } else if (argument.StartsWith("/removeformatting", StringComparison.OrdinalIgnoreCase) || argument.StartsWith("-removeformatting", StringComparison.OrdinalIgnoreCase)) { - actions |= BatchAction.RemoveFormatting; + actions.Add(BatchAction.RemoveFormatting); args.RemoveAt(i); } } @@ -1025,10 +1023,9 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert } } - internal static bool BatchConvertSave(string targetFormat, TimeSpan offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IEnumerable formats, string fileName, Subtitle sub, SubtitleFormat format, List binaryParagraphs, bool overwrite, int pacCodePage, - double? targetFrameRate, ICollection multipleReplaceImportFiles, BatchAction actions = BatchAction.None, + double? targetFrameRate, ICollection multipleReplaceImportFiles, List actions = null, Point? resolution = null, bool autoDetectLanguage = false, BatchConvertProgress progressCallback = null) { double oldFrameRate = Configuration.Settings.General.CurrentFrameRate; @@ -1049,58 +1046,59 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert Configuration.Settings.General.CurrentFrameRate = targetFrameRate.Value; } - if (actions != BatchAction.None) + if (actions != null) { - if ((actions & BatchAction.RemoveTextForHI) == BatchAction.RemoveTextForHI) + foreach (var action in actions) { - var hiSettings = new Core.Forms.RemoveTextForHISettings(sub); - var hiLib = new Core.Forms.RemoveTextForHI(hiSettings); - foreach (var p in sub.Paragraphs) + switch (action) { - p.Text = hiLib.RemoveTextFromHearImpaired(p.Text); - } - } - if ((actions & BatchAction.RemoveFormatting) == BatchAction.RemoveFormatting) - { - foreach (var p in sub.Paragraphs) - { - p.Text = HtmlUtil.RemoveHtmlTags(p.Text, true).Trim(); - } - } - if ((actions & BatchAction.FixCommonErrors) == BatchAction.FixCommonErrors) - { - using (var fce = new FixCommonErrors { BatchMode = true }) - { - for (int i = 0; i < 3; i++) - { - var language = Configuration.Settings.Tools.BatchConvertLanguage; - if (string.IsNullOrEmpty(language) || autoDetectLanguage) + case BatchAction.FixCommonErrors: + using (var fce = new FixCommonErrors { BatchMode = true }) { - language = LanguageAutoDetect.AutoDetectGoogleLanguage(sub); - } + for (int i = 0; i < 3; i++) + { + var language = Configuration.Settings.Tools.BatchConvertLanguage; + if (string.IsNullOrEmpty(language) || autoDetectLanguage) + { + language = LanguageAutoDetect.AutoDetectGoogleLanguage(sub); + } - fce.RunBatch(sub, format, targetEncoding, language); - sub = fce.FixedSubtitle; - } - } - } - if ((actions & BatchAction.ReDoCasing) == BatchAction.ReDoCasing) - { - using (var changeCasing = new ChangeCasing()) - { - changeCasing.FixCasing(sub, LanguageAutoDetect.AutoDetectGoogleLanguage(sub)); - } - using (var changeCasingNames = new ChangeCasingNames()) - { - changeCasingNames.Initialize(sub); - changeCasingNames.FixCasing(); - } - } - if ((actions & BatchAction.ReverseRtlStartEnd) == BatchAction.ReverseRtlStartEnd) - { - foreach (var p in sub.Paragraphs) - { - p.Text = Utilities.ReverseStartAndEndingForRightToLeft(p.Text); + fce.RunBatch(sub, format, targetEncoding, language); + sub = fce.FixedSubtitle; + } + } + break; + case BatchAction.RemoveTextForHI: + var hiSettings = new Core.Forms.RemoveTextForHISettings(sub); + var hiLib = new Core.Forms.RemoveTextForHI(hiSettings); + foreach (var p in sub.Paragraphs) + { + p.Text = hiLib.RemoveTextFromHearImpaired(p.Text); + } + break; + case BatchAction.RemoveFormatting: + foreach (var p in sub.Paragraphs) + { + p.Text = HtmlUtil.RemoveHtmlTags(p.Text, true).Trim(); + } + break; + case BatchAction.ReDoCasing: + using (var changeCasing = new ChangeCasing()) + { + changeCasing.FixCasing(sub, LanguageAutoDetect.AutoDetectGoogleLanguage(sub)); + } + using (var changeCasingNames = new ChangeCasingNames()) + { + changeCasingNames.Initialize(sub); + changeCasingNames.FixCasing(); + } + break; + case BatchAction.ReverseRtlStartEnd: + foreach (var p in sub.Paragraphs) + { + p.Text = Utilities.ReverseStartAndEndingForRightToLeft(p.Text); + } + break; } } }