1111using MediaBrowser . Controller . Lyrics ;
1212using MediaBrowser . Model . Entities ;
1313using MediaBrowser . Model . Globalization ;
14+ using MediaBrowser . Model . Lyrics ;
1415using MediaBrowser . Model . Tasks ;
1516using 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+ }
0 commit comments