Skip to content

Commit 03eb6f5

Browse files
committed
feat: replace non time synced lyrics with synced ones
1 parent 5d3bfcd commit 03eb6f5

2 files changed

Lines changed: 51 additions & 6 deletions

File tree

Jellyfin.Plugin.Lyrics/LyricDownloadTask.cs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using MediaBrowser.Controller.Lyrics;
1212
using MediaBrowser.Model.Entities;
1313
using MediaBrowser.Model.Globalization;
14+
using MediaBrowser.Model.Lyrics;
1415
using MediaBrowser.Model.Tasks;
1516
using Microsoft.Extensions.Logging;
1617

@@ -53,13 +54,13 @@ public LyricDownloadTask(
5354
}
5455

5556
/// <inheritdoc />
56-
public string Name => "Download missing lyrics (new)";
57+
public string Name => "Download and upgrade lyrics (new)";
5758

5859
/// <inheritdoc />
5960
public string Key => "DLLyrics";
6061

6162
/// <inheritdoc />
62-
public string Description => "Task to download missing lyrics from lrclib.net";
63+
public string Description => "Task to download missing lyrics and upgrade plain lyrics to synced lyrics from lrclib.net";
6364

6465
/// <inheritdoc />
6566
public string Category => _localizationManager.GetLocalizedString("TasksLibraryCategory");
@@ -82,6 +83,11 @@ public async Task ExecuteAsync(IProgress<double> progress, CancellationToken can
8283

8384
var startIndex = 0;
8485
var completed = 0;
86+
var missingDownloadedCount = 0;
87+
var upgradedToSyncedCount = 0;
88+
var alreadySyncedSkippedCount = 0;
89+
var plainNoSyncedFoundCount = 0;
90+
var errorsCount = 0;
8591

8692
while (startIndex < totalCount)
8793
{
@@ -94,7 +100,6 @@ public async Task ExecuteAsync(IProgress<double> progress, CancellationToken can
94100

95101
try
96102
{
97-
// Check if lyrics already exist for this item
98103
var existingLyrics = await _lyricManager.GetLyricsAsync(audioItem, cancellationToken).ConfigureAwait(false);
99104

100105
if (existingLyrics is null)
@@ -105,12 +110,34 @@ public async Task ExecuteAsync(IProgress<double> progress, CancellationToken can
105110
{
106111
_logger.LogDebug("Saving lyrics for {Path}", audioItem.Path);
107112
await _lyricManager.DownloadLyricsAsync(audioItem, lyricResults[0].Id, cancellationToken).ConfigureAwait(false);
113+
missingDownloadedCount++;
114+
}
115+
}
116+
else if (HasSyncedLyrics(existingLyrics))
117+
{
118+
alreadySyncedSkippedCount++;
119+
}
120+
else
121+
{
122+
_logger.LogDebug("Checking upgrade to synced lyrics for {Path}", audioItem.Path);
123+
var lyricResults = await _lyricManager.SearchLyricsAsync(audioItem, true, cancellationToken).ConfigureAwait(false);
124+
var syncedCandidate = SelectBestSyncedCandidate(lyricResults);
125+
if (syncedCandidate is not null)
126+
{
127+
_logger.LogDebug("Upgrading to synced lyrics for {Path}", audioItem.Path);
128+
await _lyricManager.DownloadLyricsAsync(audioItem, syncedCandidate.Id, cancellationToken).ConfigureAwait(false);
129+
upgradedToSyncedCount++;
130+
}
131+
else
132+
{
133+
plainNoSyncedFoundCount++;
108134
}
109135
}
110136
}
111137
catch (Exception ex)
112138
{
113-
_logger.LogError(ex, "Error downloading lyrics for {Path}", audioItem.Path);
139+
errorsCount++;
140+
_logger.LogError(ex, "Error processing lyrics for {Path}", audioItem.Path);
114141
}
115142

116143
completed++;
@@ -120,6 +147,14 @@ public async Task ExecuteAsync(IProgress<double> progress, CancellationToken can
120147
startIndex += QueryPageLimit;
121148
}
122149

150+
_logger.LogInformation(
151+
"Lyrics task complete. Missing downloaded: {MissingDownloadedCount}, upgraded to synced: {UpgradedToSyncedCount}, already synced skipped: {AlreadySyncedSkippedCount}, plain with no synced found: {PlainNoSyncedFoundCount}, errors: {ErrorsCount}",
152+
missingDownloadedCount,
153+
upgradedToSyncedCount,
154+
alreadySyncedSkippedCount,
155+
plainNoSyncedFoundCount,
156+
errorsCount);
157+
123158
progress.Report(100);
124159
}
125160

@@ -135,4 +170,14 @@ public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
135170
}
136171
];
137172
}
138-
}
173+
174+
private static bool HasSyncedLyrics(LyricDto existingLyrics)
175+
{
176+
return existingLyrics.Metadata?.IsSynced == true;
177+
}
178+
179+
private static RemoteLyricInfoDto? SelectBestSyncedCandidate(IReadOnlyList<RemoteLyricInfoDto> lyricResults)
180+
{
181+
return lyricResults.FirstOrDefault(static x => x.Lyrics?.Metadata?.IsSynced == true);
182+
}
183+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Looking for **v10.10.7 support**? -> https://github.com/Felitendo/jellyfin-plugi
2727
6. Restart Jellyfin
2828
7. Search for the Plugin "LrcLib" (is sometimes pre-installed) and uninstall it (if it's not installed then skip this step)
2929
8. Restart Jellyfin again
30-
9. Go to **Scheduled Tasks** and run **"Download missing lyrics (new)"**
30+
9. Go to **Scheduled Tasks** and run **"Download and upgrade lyrics (new)"**
3131
10. Go to **Libraries** and click on **Scan all Libraries**
3232

3333
---

0 commit comments

Comments
 (0)