fix: add title and artist as the first line lyrics

This commit is contained in:
Zhe Fang
2025-06-20 21:03:43 -04:00
parent 9e8bc3b7df
commit bc8fd4de31
6 changed files with 56 additions and 23 deletions

View File

@@ -28,6 +28,7 @@
<PackageReference Include="CommunityToolkit.WinUI.Extensions" Version="8.2.250402" />
<PackageReference Include="CommunityToolkit.WinUI.Helpers" Version="8.2.250402" />
<PackageReference Include="CommunityToolkit.WinUI.Media" Version="8.2.250402" />
<PackageReference Include="Dubya.WindowsMediaController" Version="2.5.5" />
<PackageReference Include="Lyricify.Lyrics.Helper" Version="0.1.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.6" />

View File

@@ -24,7 +24,7 @@ namespace BetterLyrics.WinUI3.Helper
switch (lyricsFormat)
{
case LyricsFormat.Lrc:
ParseLyricsFromLrc(raw);
ParseLyricsFromLrc(raw, durationMs);
break;
case LyricsFormat.DecryptedQrc:
ParseLyricsFromQrc(raw, durationMs);
@@ -34,9 +34,7 @@ namespace BetterLyrics.WinUI3.Helper
}
if (
title != null
&& artist != null
&& _lyricsLines != null
_lyricsLines != null
&& _lyricsLines.Count > 0
&& _lyricsLines[0].StartTimestampMs > 0
)
@@ -47,7 +45,7 @@ namespace BetterLyrics.WinUI3.Helper
{
StartTimestampMs = 0,
EndTimestampMs = _lyricsLines[0].StartTimestampMs,
Texts = [$"{artist} - {title}"],
Texts = [artist != null && title != null ? $"{artist} - {title}" : ""],
}
);
}
@@ -59,7 +57,7 @@ namespace BetterLyrics.WinUI3.Helper
/// </summary>
/// <param name="track"></param>
/// <param name="raw"></param>
private void ParseLyricsFromLrc(string raw)
private void ParseLyricsFromLrc(string raw, int durationMs)
{
Track track = new() { Lyrics = new() };
track.Lyrics.ParseLRC(raw);
@@ -81,7 +79,7 @@ namespace BetterLyrics.WinUI3.Helper
}
else
{
endTimestampMs = (int)track.DurationMs;
endTimestampMs = durationMs;
}
lyricsLine ??= new LyricsLine { StartTimestampMs = startTimestampMs };

View File

@@ -6,17 +6,6 @@ namespace BetterLyrics.WinUI3.Services
{
public interface IMusicSearchService
{
/// <summary>
///
/// </summary>
/// <param name="title"></param>
/// <param name="artist"></param>
/// <param name="album"></param>
/// <param name="durationMs"></param>
/// <param name="matchMode"></param>
/// <param name="targetProps"></param>
/// <param name="searchProviders"></param>
/// <returns>Return a tuple (raw lyrics, lyrics format, album art)</returns>
Task<(string?, LyricsFormat?)> SearchLyricsAsync(
string title,
string artist,
@@ -24,5 +13,7 @@ namespace BetterLyrics.WinUI3.Services
double durationMs = 0.0,
MusicSearchMatchMode matchMode = MusicSearchMatchMode.TitleAndArtist
);
byte[]? SearchAlbumArtAsync(string title, string artist);
}
}

View File

@@ -30,6 +30,36 @@ namespace BetterLyrics.WinUI3.Services
);
}
public byte[]? SearchAlbumArtAsync(string title, string artist)
{
foreach (var path in _settingsService.MusicLibraries)
{
if (Directory.Exists(path))
{
foreach (
var file in Directory.GetFiles(path, $"*.*", SearchOption.AllDirectories)
)
{
if (file.Contains(title) && file.Contains(artist))
{
Track track = new(file);
if (track.Lyrics.SynchronizedLyrics.Count > 0)
{
// Get synchronized lyrics from the track (metadata)
var bytes = track.EmbeddedPictures.FirstOrDefault()?.PictureData;
if (bytes != null)
{
return bytes;
}
}
}
}
}
}
return null;
}
public async Task<(string?, LyricsFormat?)> SearchLyricsAsync(
string title,
string artist,

View File

@@ -58,6 +58,13 @@ namespace BetterLyrics.WinUI3.Services
{
SongInfo.AlbumArt = await ImageHelper.ToByteArrayAsync(streamReference);
}
else
{
SongInfo.AlbumArt = _musicSearchService.SearchAlbumArtAsync(
SongInfo.Title,
SongInfo.Artist
);
}
SongInfoChanged?.Invoke(this, new SongInfoChangedEventArgs(SongInfo));
}

View File

@@ -238,10 +238,10 @@ namespace BetterLyrics.WinUI3.ViewModels
_isRelayoutNeeded = true;
LyricsStatus = LyricsStatus.Loading;
(var lyricsRaw, var lyricsFormat) = await _musicSearchService.SearchLyricsAsync(
SongInfo.Title,
SongInfo.Artist,
SongInfo.Album,
SongInfo.DurationMs ?? 0
SongInfo?.Title ?? "",
SongInfo?.Artist ?? "",
SongInfo?.Album ?? "",
SongInfo?.DurationMs ?? 0
);
if (lyricsRaw == null)
@@ -250,7 +250,13 @@ namespace BetterLyrics.WinUI3.ViewModels
}
else
{
_lyrics = new LyricsParser().Parse(lyricsRaw, lyricsFormat);
_lyrics = new LyricsParser().Parse(
lyricsRaw,
lyricsFormat,
SongInfo.Title,
SongInfo.Artist,
(int)(SongInfo.DurationMs)
);
_isRelayoutNeeded = true;
LyricsStatus = LyricsStatus.Found;
}