You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ItemKey::Unknown has served its purpose, and has had better alternatives for a couple years now. It's been a source of confusion numerous times, and its existence also requires extra verification logic on conversions. I'd like to see it gone.
I envision people taking their Tags, converting them to a concrete format (e.g. VorbisComments), and then adding their custom fields to that. I'd rather not have a way to generically add custom fields, since the mappings for the concrete ItemKey variants are known to be valid, but with Unknown, there's extra verification that needs to be done on conversions that could be skipped if I just get rid of it entirely.
ItemKey supports the keys that the vast majority of users will need, anything else would be niche and require a little extra effort working with the concrete formats.
Hi! I understand the motivation behind simplifying the ItemKey enum and removing ItemKey::Unknown, but I'd like to offer a user perspective that may have been overlooked.
I'm currently building a music library and metadata API where users can scan, view, and edit tags generically across different formats (MP3, FLAC, M4A, etc.). For this use case, ItemKey::Unknown has been extremely valuable.
It allows me to support custom or non-standard tags like RATING, MOOD, BPM_LABEL, and others without needing to write backend-specific logic for each format. While these tags may seem niche from a spec point of view, they are actually very common in real-world scenarios — especially for advanced playback systems, smart playlisting, DJ software, and even consumer apps that want to store user preferences.
By removing ItemKey::Unknown, I would be forced to detect the tag backend (e.g., Vorbis, ID3v2, MP4) and write logic per format just to set or read a single custom field — which breaks the abstraction that Tag provides.
For example, I’ve encountered many high-quality FLAC files in the wild where fields like TRACKTOTAL or DISCTOTAL are not parsed via tag.track_total() or tag.disk_total() but are present as raw strings under those names. In my implementation, I handle this fallback like this:
metadata.set_total_tracks(tag.track_total().map(|n| n asu16).or_else(|| {
tag.get_string(&ItemKey::Unknown("TRACKTOTAL".to_string())).and_then(|s| s.trim().parse::<u16>().ok())}));
metadata.set_total_discs(tag.disk_total().map(|n| n asu16).or_else(|| {
tag.get_string(&ItemKey::Unknown("DISCTOTAL".to_string())).and_then(|s| s.trim().parse::<u16>().ok())}));
I completely understand if ItemKey::Unknown needs to be deprecated or discouraged for performance or consistency reasons. But completely removing it would break a lot of real, practical use cases that rely on flexible metadata editing.
Thanks for your work on Lofty — it’s a great library and I really appreciate the attention to correctness and long-term design!
Reacted by UnknownSuperficialNight and Floyd Haremsa
It allows me to support custom or non-standard tags like RATING, MOOD, BPM_LABEL [...] While these tags may seem niche from a spec point of view, they are actually very common in real-world scenarios
I'm always open to adding more ItemKey variants, feel free to suggest the ones that you have custom logic for (like BPM_LABEL, which I've haven't seen before).
And for the record, RATING and MOOD map to ItemKey::Popularimeter and ItemKey::Mood for Vorbis Comments. You can see the full mapping here:
For example, I’ve encountered many high-quality FLAC files in the wild where fields like TRACKTOTAL or DISCTOTAL are not parsed via tag.track_total() or tag.disk_total()
You should be able to do:
metadata.set_total_tracks(tag.track_total().map(|n| n asu16).or_else(|| {
tag.get_string(&ItemKey::TrackTotal).and_then(|s| s.trim().parse::<u16>().ok())}));
metadata.set_total_discs(tag.disk_total().map(|n| n asu16).or_else(|| {
tag.get_string(&ItemKey::DiscTotal).and_then(|s| s.trim().parse::<u16>().ok())}));
ItemKey::TrackTotal and ItemKey::DiscTotal should map to the expected keys:
Thanks for the clarification and for maintaining such a great library! 🙏 I just tested using ItemKey::TrackTotal and ItemKey::DiscTotal as you suggested and it works perfectly—my FLAC files now parse both fields without needing Unknown. I hadn’t tried it that way originally, so apologies for the extra noise!
When I have a free moment, I’d be happy to contribute a small docs PR with these examples to help others avoid the same confusion. Thanks again! 😊
Hi, this change seemed to have made it worse for me.
Before I could simply put my own tags into the file using the Unknown thing in a completely generic way.
Now the complexity exploded, I have to adjust my code to deal with the file type, cast to the concrete tag type, and then I can't use the generic keys anymore.
I hope that I'm doing something wrong.
@abique You can still use the generic types and move to concrete tag types only when necessary
In the simple case, you can just split your custom items into a separate handler that does a concrete conversion and insertion:
use lofty::tag::Tag;fnprocess_tag(tag:Tag) -> Tag{// Do all your generic stuff
tag.set_artist(String::from("Foo"));// Then the custom stuffadd_custom_tags(tag)}fnadd_custom_tags(tag:Tag) -> Tag{match tag.tag_type(){TagType::Id3v2 => {letmut id3v2:Id3v2Tag = tag.into();
id3v2.insert_user_text(String::from("MY_FIELD"),String::from("SOME_VALUE"));return id3v2.into();}// And so on for the other formats...}}
I tried to use TaggedFile to keep the opening and writing the same across formats.
However when I tried to step down to the format level when setting my custom tags, the into() -> Tag calls discard custom information.
Is there a way to still do this using TaggedFile?
fnadd_custom_tag(tag:Tag,yt_id:&str) -> Result<Tag>{match tag.tag_type(){TagType::Id3v2 => {letmut id3v2:Id3v2Tag = tag.into();
id3v2.remove_user_text(YT_ID_KEY);
id3v2.insert_user_text(YT_ID_KEY.to_string(), yt_id.to_string());Ok(id3v2.into())// uses split_tag and DISCARDS custom tags},TagType::Mp4Ilst => {letmut mp4:Ilst = tag.into();let _ = mp4.remove(&MP4_ATOM_YT_ID_KEY);
mp4.insert(Atom::new(MP4_ATOM_YT_ID_KEY,AtomData::UTF8(yt_id.to_string()),));Ok(mp4.into())// uses split_tag and DISCARDS custom tags},TagType::VorbisComments => {letmut vorbis:VorbisComments = tag.into();let _ = vorbis.remove(YT_ID_KEY);
vorbis.push(YT_ID_KEY.to_string(), yt_id.to_string());Ok(vorbis.into())// uses split_tag and DISCARDS custom tags},
other => bail!("unsupported tag type for custom fields: {other:?}"),}}
What I would want is a way to apply format specific changes to a TaggedFile. If a way exists I didn't find it.
With the current Tag api it is very easy to loose information and custom tags.
Activity
Undead34 commentedon May 13, 2025
Hi! I understand the motivation behind simplifying the ItemKey enum and removing ItemKey::Unknown, but I'd like to offer a user perspective that may have been overlooked.
I'm currently building a music library and metadata API where users can scan, view, and edit tags generically across different formats (MP3, FLAC, M4A, etc.). For this use case, ItemKey::Unknown has been extremely valuable.
It allows me to support custom or non-standard tags like RATING, MOOD, BPM_LABEL, and others without needing to write backend-specific logic for each format. While these tags may seem niche from a spec point of view, they are actually very common in real-world scenarios — especially for advanced playback systems, smart playlisting, DJ software, and even consumer apps that want to store user preferences.
By removing ItemKey::Unknown, I would be forced to detect the tag backend (e.g., Vorbis, ID3v2, MP4) and write logic per format just to set or read a single custom field — which breaks the abstraction that Tag provides.
For example, I’ve encountered many high-quality FLAC files in the wild where fields like
TRACKTOTALorDISCTOTALare not parsed viatag.track_total()ortag.disk_total()but are present as raw strings under those names. In my implementation, I handle this fallback like this:I completely understand if ItemKey::Unknown needs to be deprecated or discouraged for performance or consistency reasons. But completely removing it would break a lot of real, practical use cases that rely on flexible metadata editing.
Thanks for your work on Lofty — it’s a great library and I really appreciate the attention to correctness and long-term design!
Serial-ATA commentedon May 13, 2025
@Undead34 Thanks for sharing your thoughts!
I'm always open to adding more
ItemKeyvariants, feel free to suggest the ones that you have custom logic for (likeBPM_LABEL, which I've haven't seen before).And for the record,
RATINGandMOODmap toItemKey::PopularimeterandItemKey::Moodfor Vorbis Comments. You can see the full mapping here:lofty-rs/lofty/src/tag/item.rs
Lines 337 to 413 in d9eb83b
You should be able to do:
ItemKey::TrackTotalandItemKey::DiscTotalshould map to the expected keys:lofty-rs/lofty/src/tag/item.rs
Lines 371 to 373 in d9eb83b
Does that not work?
Undead34 commentedon May 13, 2025
Thanks for the clarification and for maintaining such a great library! 🙏 I just tested using
ItemKey::TrackTotalandItemKey::DiscTotalas you suggested and it works perfectly—my FLAC files now parse both fields without needingUnknown. I hadn’t tried it that way originally, so apologies for the extra noise!When I have a free moment, I’d be happy to contribute a small docs PR with these examples to help others avoid the same confusion. Thanks again! 😊
abique commentedon Apr 9, 2026
Hi, this change seemed to have made it worse for me.
Before I could simply put my own tags into the file using the Unknown thing in a completely generic way.
Now the complexity exploded, I have to adjust my code to deal with the file type, cast to the concrete tag type, and then I can't use the generic keys anymore.
I hope that I'm doing something wrong.
Serial-ATA commentedon Apr 26, 2026
@abique You can still use the generic types and move to concrete tag types only when necessary
In the simple case, you can just split your custom items into a separate handler that does a concrete conversion and insertion:
Or, you could do the inverse and read concrete file types up front and use
SplitTag/MergeTag. You can check out an example of that here: https://gitlab.com/uklotzde/aoide-rs/-/blob/main/crates/media-file/src/fmt/mod.rs#L1174It's definitely some extra effort, but I'd rather do it this way than have users pass in arbitrary keys that may:
raldone01 commentedon Oct 8, 2026
I tried to use
TaggedFileto keep the opening and writing the same across formats.However when I tried to step down to the format level when setting my custom tags, the
into() -> Tagcalls discard custom information.Is there a way to still do this using
TaggedFile?What I would want is a way to apply format specific changes to a
TaggedFile. If a way exists I didn't find it.With the current
Tagapi it is very easy to loose information and custom tags.Serial-ATA commentedon Oct 8, 2026
@raldone01
Tags should preserve any items it cant convert:But if you:
TagTypeGlobalOptions::preserve_format_specific_items()Then anything format-specific is discarded
raldone01 commentedon Oct 8, 2026
Mhh okay then I am very confused.
This fails for me. I did not change the global options but I am not 100% sure about the default value from the docs.
Serial-ATA commentedon Oct 8, 2026
Yeah, looks like I forgot to add it for
VorbisCommentsafter #526. I'll fix that in 0.25.5. Thanks!