From 31075e48e16b7fc2e4956d32b5dd1a5e24c1b14d Mon Sep 17 00:00:00 2001 From: ema Date: Sun, 15 Dec 2024 02:42:32 +0800 Subject: [PATCH] Fix error on audio with multiple covers --- .../Extensions/StringExtension.cs | 74 +++++++++++++++++++ .../ViewerPanel.xaml.cs | 19 +++-- QuickLook/QuickLook.csproj | 3 + 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Extensions/StringExtension.cs diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Extensions/StringExtension.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Extensions/StringExtension.cs new file mode 100644 index 000000000..d79b33e38 --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/Extensions/StringExtension.cs @@ -0,0 +1,74 @@ +// Copyright © 2024 ema +// +// This file is part of QuickLook program. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using System; +using System.Collections.Generic; + +#pragma warning disable IDE0130 // Namespace does not match folder structure + +namespace QuickLook.Plugin.VideoViewer; + +#pragma warning restore IDE0130 // Namespace does not match folder structure + +internal static class StringExtension +{ + /// + /// Splits a ReadOnlySpan into an array of strings using the specified separator. + /// + /// The input ReadOnlySpan to split. + /// The ReadOnlySpan separator to use for splitting. + /// An array of strings that are the result of splitting the input span. + /// + /// - If the separator is not found, the entire input span will be returned as a single element. + /// - If the input is empty, the method will return an empty array. + /// - This method avoids allocating intermediate substrings during processing. + /// + public static string[] Split(this string input, string separator) + { + if (input == null) + { + return [input]; + } + + ReadOnlySpan @in = input.AsSpan(); + ReadOnlySpan sep = separator.AsSpan(); + + List result = []; + int start = 0; + + // Continue splitting until no separator is found + while (true) + { + // Find the next occurrence of the separator + int index = @in.Slice(start).IndexOf(sep); + if (index == -1) + { + // No more separators; add the remaining substring + result.Add(@in.Slice(start).ToString()); + break; + } + + // Add the substring before the separator to the result list + result.Add(@in.Slice(start, index).ToString()); + + // Move the start position past the separator + start += index + sep.Length; + } + + return [.. result]; + } +} diff --git a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs index fddd520ee..530c9ae58 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.VideoViewer/ViewerPanel.xaml.cs @@ -259,12 +259,19 @@ private void UpdateMeta(string path, MediaInfo.MediaInfo info) metaArtists.Text = artist; metaAlbum.Text = album; - var cs = info.Get(StreamKind.General, 0, "Cover_Data"); - if (!string.IsNullOrEmpty(cs)) - using (var ms = new MemoryStream(Convert.FromBase64String(cs))) - { - CoverArt = BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); - } + var coverData = info.Get(StreamKind.General, 0, "Cover_Data"); + if (!string.IsNullOrEmpty(coverData)) + { + var coverBytes = Convert.FromBase64String + ( + coverData.Length % 4 == 0 // MediaInfo may will return multiple covers + ? coverData + : coverData.Split(" / ")[0] // Get the first cover only + ); + using var ms = new MemoryStream(coverBytes); + + CoverArt = BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + } } catch (Exception) { diff --git a/QuickLook/QuickLook.csproj b/QuickLook/QuickLook.csproj index 5696416ee..d3a2a3f78 100644 --- a/QuickLook/QuickLook.csproj +++ b/QuickLook/QuickLook.csproj @@ -108,6 +108,9 @@ all + + all +