diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/LyricTrack/LrcHelper.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/LyricTrack/LrcHelper.cs index e0a0986..278605a 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/LyricTrack/LrcHelper.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/LyricTrack/LrcHelper.cs @@ -113,14 +113,38 @@ public static class LrcHelper return lrcList; } - public static LrcLine GetNearestLrc(IEnumerable lrcList, TimeSpan time) + /// + /// Returns the nearest in the list whose timestamp is less than or equal to the specified time. + /// If multiple lines have the same timestamp, their lyrics are merged using the specified separator. + /// + /// The collection of objects to search. + /// The target time to find the nearest lyric line for. + /// The separator used to join lyrics with duplicate timestamps. Default is a newline ("\n"). + /// The nearest at or before the specified time, or null if none found. + public static LrcLine GetNearestLrc(IEnumerable lrcList, TimeSpan time, string separator = "\n") { - LrcLine line = lrcList + IEnumerable lines = lrcList .Where(x => x.LrcTime != null && x.LrcTime <= time) - .OrderByDescending(x => x.LrcTime) - .FirstOrDefault(); + .OrderByDescending(x => x.LrcTime); - return line; + LrcLine first = lines.FirstOrDefault(); + + if (first is not null) + { + LrcLine[] mergingLines = [.. lines.Where(x => x.LrcTime == first.LrcTime)]; + + // If any duplicate timestamps exist, merge them + if (mergingLines.Length > 1) + { + // Create a new LrcLine can keep original ones unchanged + return new LrcLine( + first.LrcTime, + string.Join(separator, lines.Where(x => x.LrcTime == first.LrcTime).Select(x => x.LrcText)) + ); + } + } + + return first; } ///