diff --git a/src/file_tags.rs b/src/file_tags.rs index c57bc36..55381a1 100644 --- a/src/file_tags.rs +++ b/src/file_tags.rs @@ -22,6 +22,7 @@ fn tag_type_for_file(ft: FileType) -> TagType { } /// Write updated metadata back to the music file's embedded tags. +/// Skips the write if all tags already match (avoids expensive FLAC rewrites). pub fn write_tags( file_path: &str, details: &RecordingDetails, @@ -46,6 +47,39 @@ pub fn write_tags( .cloned() .unwrap_or_else(|| lofty::tag::Tag::new(tag_type)); + // Check if all tags already match — skip the expensive write if so + let existing_title = tag.title().map(|s| s.to_string()); + let existing_artist = tag.artist().map(|s| s.to_string()); + let existing_album = tag.album().map(|s| s.to_string()); + let existing_year = tag.year(); + let existing_genre = tag.genre().map(|s| s.to_string()); + + let want_album = release.map(|r| r.title.clone()); + let want_year = year.map(|y| y as u32); + let want_genre = genre.map(|g| g.to_string()); + + let title_ok = existing_title.as_deref() == Some(&details.title); + let artist_ok = existing_artist.as_deref() == Some(&details.artist); + let album_ok = match (&existing_album, &want_album) { + (Some(e), Some(w)) => e == w, + (None, None) => true, + _ => false, + }; + let year_ok = existing_year == want_year; + let genre_ok = match (&existing_genre, &want_genre) { + (Some(e), Some(w)) => e == w, + (None, None) => true, + _ => false, + }; + + if title_ok && artist_ok && album_ok && year_ok && genre_ok { + tracing::debug!( + path = file_path, + "file tags already correct, skipping write" + ); + return Ok(()); + } + // Set metadata tag.set_title(details.title.clone()); tag.set_artist(details.artist.clone());