Add support for embedded lyrics in music file #1847

This commit is contained in:
ema
2026-01-06 03:12:07 +08:00
parent a6210e177b
commit d1383e512e
2 changed files with 30 additions and 2 deletions

View File

@@ -50,6 +50,7 @@
</Reference>
<PackageReference Include="UTF.Unknown" Version="2.6.0" />
<PackageReference Include="Melanchall.DryWetMidi" Version="8.0.3" />
<PackageReference Include="TagLibSharp" Version="2.3.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -27,7 +27,6 @@ using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
@@ -309,12 +308,40 @@ public partial class ViewerPanel : UserControl, IDisposable, INotifyPropertyChan
var lyricPath = Path.ChangeExtension(path, ".lrc");
// Stop previous timer if any.
_lyricTimer?.Stop();
_lyricTimer = null;
_lyricLines = null;
if (File.Exists(lyricPath))
{
var buffer = File.ReadAllBytes(lyricPath);
var encoding = CharsetDetector.DetectFromBytes(buffer).Detected?.Encoding ?? Encoding.Default;
_lyricLines = LrcHelper.ParseText(encoding.GetString(buffer)).ToArray();
_lyricLines = [.. LrcHelper.ParseText(encoding.GetString(buffer))];
}
else
{
// Use embedded lyrics from MediaInfo if present.
// Common tag: General/Lyrics (may contain LRC formatted content).
var embeddedLyrics = info?.Get(StreamKind.General, 0, "Lyrics");
// Only check whether the tag of lyrics is present by MediaInfo
if (!string.IsNullOrWhiteSpace(embeddedLyrics))
{
var file = TagLib.File.Create(path);
embeddedLyrics = file.Tag.Lyrics;
// Check whether the tag of lyrics is present by TagLib#
if (!string.IsNullOrWhiteSpace(embeddedLyrics))
{
_lyricLines = [.. LrcHelper.ParseText(embeddedLyrics)];
}
}
}
if (_lyricLines != null && _lyricLines.Length != 0)
{
_lyricTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
_lyricTimer.Tick += (sender, e) =>
{