From 0edb30eba8c9f88911055615260232d9f7ef98cf Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Sat, 19 Oct 2024 20:28:53 +0100 Subject: [PATCH] Refactor frame-to-milliseconds conversion Replaced direct frame rate based conversions with reusable utility methods across multiple subtitle formats. This improves consistency, readability, and maintainability of the codebase. Signed-off-by: Ivandro Jao --- src/libse/Common/TimeCode.cs | 2 +- src/libse/SubtitleFormats/AQTitle.cs | 7 +------ src/libse/SubtitleFormats/CaptionAssistant.cs | 9 +-------- src/libse/SubtitleFormats/Cavena890.cs | 7 +++---- src/libse/SubtitleFormats/ElrPrint.cs | 2 +- src/libse/SubtitleFormats/NciCaption.cs | 9 +-------- src/libse/SubtitleFormats/UtxFrames.cs | 13 ++----------- src/ui/Forms/Main.cs | 2 +- 8 files changed, 11 insertions(+), 40 deletions(-) diff --git a/src/libse/Common/TimeCode.cs b/src/libse/Common/TimeCode.cs index c520a893d..cce2fdcab 100644 --- a/src/libse/Common/TimeCode.cs +++ b/src/libse/Common/TimeCode.cs @@ -354,7 +354,7 @@ namespace Nikse.SubtitleEdit.Core.Common public TimeCode AlignToFrame() { var ts = TimeSpan.FromMilliseconds(Math.Round(TotalMilliseconds, MidpointRounding.AwayFromZero)); - var frames = Math.Round(ts.Milliseconds / (TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate)); + var frames = SubtitleFormat.MillisecondsToFrames(ts.Milliseconds); TimeSpan ts2; if (frames >= Configuration.Settings.General.CurrentFrameRate - 0.001) { diff --git a/src/libse/SubtitleFormats/AQTitle.cs b/src/libse/SubtitleFormats/AQTitle.cs index 039a3f3a1..465cfa8c3 100644 --- a/src/libse/SubtitleFormats/AQTitle.cs +++ b/src/libse/SubtitleFormats/AQTitle.cs @@ -131,11 +131,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats return frames.ToString(CultureInfo.InvariantCulture); } - private static TimeCode DecodeTimeCode(string timePart) - { - int milliseconds = (int)Math.Round(TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate * int.Parse(timePart)); - return new TimeCode(milliseconds); - } - + private static TimeCode DecodeTimeCode(string timePart) => new TimeCode(FramesToMilliseconds(int.Parse(timePart))); } } diff --git a/src/libse/SubtitleFormats/CaptionAssistant.cs b/src/libse/SubtitleFormats/CaptionAssistant.cs index df3027ff6..dace62199 100644 --- a/src/libse/SubtitleFormats/CaptionAssistant.cs +++ b/src/libse/SubtitleFormats/CaptionAssistant.cs @@ -24,14 +24,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var minutes = int.Parse(parts[1]); var seconds = int.Parse(parts[2]); var frames = int.Parse(parts[3]); - - int milliseconds = (int)Math.Round(((TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate) * frames)); - if (milliseconds > 999) - { - milliseconds = 999; - } - - return new TimeCode(hour, minutes, seconds, milliseconds); + return new TimeCode(hour, minutes, seconds, FramesToMillisecondsMax999(frames)); } public override string ToText(Subtitle subtitle, string title) diff --git a/src/libse/SubtitleFormats/Cavena890.cs b/src/libse/SubtitleFormats/Cavena890.cs index fb5595d96..bf446c97a 100644 --- a/src/libse/SubtitleFormats/Cavena890.cs +++ b/src/libse/SubtitleFormats/Cavena890.cs @@ -1522,8 +1522,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats private static void WriteTime(Stream fs, TimeCode timeCode) { - double totalMilliseconds = timeCode.TotalMilliseconds; - int frames = (int)Math.Round(totalMilliseconds / (TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate)); + int frames = MillisecondsToFrames(timeCode.TotalMilliseconds); fs.WriteByte((byte)(frames / 256 / 256)); fs.WriteByte((byte)(frames / 256)); fs.WriteByte((byte)(frames % 256)); @@ -1644,8 +1643,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats else { subtitle.Paragraphs.Add(p); - p.StartTime.TotalMilliseconds = (TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate) * startFrame; - p.EndTime.TotalMilliseconds = (TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate) * endFrame; + p.StartTime.TotalMilliseconds = FramesToMilliseconds(startFrame); + p.EndTime.TotalMilliseconds = FramesToMilliseconds(endFrame); p.Text = string.Join(Environment.NewLine, new[] { line1, line2 }.Select(l => l.TrimEnd()).Where(l => !string.IsNullOrWhiteSpace(l))); } if (boxType >= 0xa0 && boxType <= 0xa9 && !string.IsNullOrEmpty(p.Text)) // box diff --git a/src/libse/SubtitleFormats/ElrPrint.cs b/src/libse/SubtitleFormats/ElrPrint.cs index 454b85a97..3d678ccc1 100644 --- a/src/libse/SubtitleFormats/ElrPrint.cs +++ b/src/libse/SubtitleFormats/ElrPrint.cs @@ -48,7 +48,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { string s; var ts = p.Duration.TimeSpan; - var frames = Math.Round(ts.Milliseconds / (TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate)); + var frames = MillisecondsToFrames(ts.Milliseconds); if (frames >= Configuration.Settings.General.CurrentFrameRate - 0.001) { s = $"{ts.Seconds + 1:00}:{0:00}"; diff --git a/src/libse/SubtitleFormats/NciCaption.cs b/src/libse/SubtitleFormats/NciCaption.cs index cde4c17f5..2746f50c9 100644 --- a/src/libse/SubtitleFormats/NciCaption.cs +++ b/src/libse/SubtitleFormats/NciCaption.cs @@ -62,14 +62,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats int minutes = buffer[index + 1]; int seconds = buffer[index + 2]; int frames = buffer[index + 3]; - - int milliseconds = (int)Math.Round(TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate * frames); - if (milliseconds > 999) - { - milliseconds = 999; - } - - return new TimeCode(hour, minutes, seconds, milliseconds); + return new TimeCode(hour, minutes, seconds, FramesToMillisecondsMax999(frames)); } public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) diff --git a/src/libse/SubtitleFormats/UtxFrames.cs b/src/libse/SubtitleFormats/UtxFrames.cs index 2fdef5c8f..861fe1374 100644 --- a/src/libse/SubtitleFormats/UtxFrames.cs +++ b/src/libse/SubtitleFormats/UtxFrames.cs @@ -72,17 +72,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats subtitle.Renumber(); } - private static string EncodeTimeCode(TimeCode time) - { - long frames = (long)(time.TotalMilliseconds / (TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate)); - return frames.ToString(); - } - - private static TimeCode DecodeTimeCode(string timePart) - { - int milliseconds = (int)Math.Round(TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate * int.Parse(timePart)); - return new TimeCode(milliseconds); - } + private static string EncodeTimeCode(TimeCode time) => MillisecondsToFrames(time.TotalMilliseconds).ToString(); + private static TimeCode DecodeTimeCode(string timePart) => new TimeCode(FramesToMilliseconds(int.Parse(timePart))); } } diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index 049954f76..8e6cea989 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -13408,7 +13408,7 @@ namespace Nikse.SubtitleEdit.Forms { var seconds = (int)numericUpDownDuration.Value; var frames = (int)Math.Round((Convert.ToDouble(numericUpDownDuration.Value) % 1.0 * 100.0)); - return seconds * TimeCode.BaseUnit + frames * (TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate); + return seconds * TimeCode.BaseUnit + SubtitleFormat.FramesToMilliseconds(frames); } return ((double)numericUpDownDuration.Value * TimeCode.BaseUnit);