Skip to content

Commit 98efbc7

Browse files
committed
fix mp3 tagging issues
1 parent ad43993 commit 98efbc7

1 file changed

Lines changed: 73 additions & 2 deletions

File tree

lib/api/download_service_dart.dart

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,11 @@ class DownloadThread {
631631
// Download cover art
632632
await _downloadCoverArt(outFile, track);
633633

634+
// Download LRC lyrics if enabled
635+
if (settings.downloadLyrics) {
636+
await _downloadLrcLyrics(outFile, track);
637+
}
638+
634639
// Tag file with metadata
635640
await _tagFile(outFile, track);
636641
}
@@ -893,6 +898,67 @@ class DownloadThread {
893898
}
894899
}
895900

901+
/// Download LRC lyrics file
902+
Future<void> _downloadLrcLyrics(File audioFile, Track track) async {
903+
try {
904+
// Check if track has synced lyrics
905+
if (track.lyrics == null ||
906+
track.lyrics!.syncedLyrics == null ||
907+
track.lyrics!.syncedLyrics!.isEmpty) {
908+
logger.log('No synced lyrics for track, skipping lyrics file');
909+
return;
910+
}
911+
912+
// Generate LRC filename
913+
final lrcPath =
914+
audioFile.path.substring(0, audioFile.path.lastIndexOf('.')) + '.lrc';
915+
final lrcFile = File(lrcPath);
916+
917+
// Generate LRC content
918+
final lrcData = _generateLRC(track);
919+
920+
// Write to file
921+
await lrcFile.writeAsString(lrcData);
922+
923+
logger.log('Downloaded LRC lyrics: ${lrcFile.path}');
924+
} catch (e) {
925+
logger.error(
926+
'Error downloading lyrics! $e',
927+
DownloadInfo(trackId: download.trackId, id: download.id),
928+
);
929+
}
930+
}
931+
932+
/// Generate LRC format from lyrics data
933+
String _generateLRC(Track track) {
934+
final output = StringBuffer();
935+
936+
// Write metadata
937+
if (track.artists != null && track.artists!.isNotEmpty) {
938+
final artists = track.artists!.map((a) => a.name).join(', ');
939+
output.write('[ar:$artists]\r\n');
940+
}
941+
942+
if (track.album?.title != null) {
943+
output.write('[al:${track.album!.title}]\r\n');
944+
}
945+
946+
if (track.title != null) {
947+
output.write('[ti:${track.title}]\r\n');
948+
}
949+
950+
// Write synced lyrics
951+
if (track.lyrics?.syncedLyrics != null) {
952+
for (var lyric in track.lyrics!.syncedLyrics!) {
953+
if (lyric.lrcTimestamp != null && lyric.text != null) {
954+
output.write('[${lyric.lrcTimestamp}]${lyric.text}\r\n');
955+
}
956+
}
957+
}
958+
959+
return output.toString();
960+
}
961+
896962
/// Tag file with metadata
897963
Future<void> _tagFile(File audioFile, Track track) async {
898964
try {
@@ -1008,7 +1074,9 @@ class DownloadThread {
10081074
}
10091075

10101076
// Lyrics
1011-
if (settings.tags.contains('lyrics') && track.lyrics != null) {
1077+
if (settings.tags.contains('lyrics') &&
1078+
track.lyrics != null &&
1079+
track.lyrics!.unsyncedLyrics != null) {
10121080
tags.add(
10131081
MetadataTag.text(CommonTags.lyrics, track.lyrics!.unsyncedLyrics!),
10141082
);
@@ -1023,14 +1091,17 @@ class DownloadThread {
10231091
}
10241092

10251093
// Write metadata
1094+
print(audioFile.path);
1095+
print(tags);
10261096
await tagger.writeTags(audioFile.path, tags);
10271097

10281098
logger.log('Tagged file: ${audioFile.path}');
1029-
} catch (e) {
1099+
} catch (e, stackTrace) {
10301100
logger.error(
10311101
'Tagging error! $e',
10321102
DownloadInfo(trackId: download.trackId, id: download.id),
10331103
);
1104+
logger.error(stackTrace.toString());
10341105
}
10351106
}
10361107
}

0 commit comments

Comments
 (0)