Repository navigation
feat: Read and write an arbitrary field on any tag - #139
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #139 +/- ##
==========================================
+ Coverage 93.20% 93.29% +0.08%
==========================================
Files 392 409 +17
Lines 83610 86202 +2592
Branches 9512 9886 +374
==========================================
+ Hits 77931 80422 +2491
- Misses 3356 3412 +56
- Partials 2323 2368 +45 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
decriptor
force-pushed
the
feat/custom-fields
branch
from
November 21, 2026 21:02
62b88cb to
6fee067
Compare
decriptor
force-pushed
the
feat/custom-fields
branch
2 times, most recently
from
November 22, 2026 01:46
441d1dc to
fc3f36c
Compare
decriptor
force-pushed
the
fix/id3-trailing-nul
branch
from
November 22, 2026 01:55
abe696c to
203bbbf
Compare
decriptor
force-pushed
the
feat/custom-fields
branch
2 times, most recently
from
November 22, 2026 02:16
e9f62bc to
86ff758
Compare
decriptor
force-pushed
the
fix/id3-trailing-nul
branch
from
November 22, 2026 02:17
203bbbf to
0477a14
Compare
decriptor
force-pushed
the
feat/custom-fields
branch
from
November 22, 2026 02:32
86ff758 to
71fa798
Compare
decriptor
force-pushed
the
feat/custom-fields
branch
from
November 22, 2026 21:40
71fa798 to
55a0466
Compare
Tag.GetCustomField, SetCustomField, RemoveCustomField, and SupportsCustomFields read, write, and remove a named field with one or more values in the format's own storage: a Vorbis comment field, an APE text item, an ID3v2 TXXX frame (case-insensitive, like GetUserText), an MP4 ----:com.apple.iTunes freeform field in either meta, an ASF attribute, a file-level Matroska SimpleTag, or a RIFF INFO chunk. ID3v1 reports no support. Null and empty values are skipped, U+0000 is refused, and picture storage names are refused. Notable rules: - ASF: the first value goes in the Content Description or Extended Content Description, the rest in the Metadata Library Object, as Windows writes them. A write or remove of a Content Description name also drops the same-named descriptor ffmpeg writes beside it (title, copyright, rating in lower case). Getters read the first non-empty value from every object; writing back the value read is a no-op, null clears. - ID3v2 UTF-16: one decoder (Utf16Text) skips the BOM each string carries in ID3v2.3, the layout mutagen writes, and a frame read from a file renders back with its BOMs and terminators. - List-to-single bridges store Tag.FirstNonEmpty. - CombinedTag (MP3, raw AAC), FlacTag, and the AIFF view forward the members; a test holds every non-leaf Tag type to them. WAV and AIFF write an ID3 chunk holding only a TXXX, through Id3v2Tag.IsEmpty, which now reads the one list of frame lists. - AVI INFO takes any text under the CSET rules; WAV INFO stays Latin-1. APE key rules are one function shared with the item measure. Closes #112
decriptor
force-pushed
the
feat/custom-fields
branch
from
November 22, 2026 22:22
88484f5 to
f5a53b3
Compare
Owner
Author
|
Review: 0 findings at f5a53b3. The PR is squashed to one commit, with a tree identical to the reviewed head 88484f5. What was checked:
|
8 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds one API on the base
Tagto read, write, and remove a named field, with several values where the format allows them. A caller no longer has to cast to the concrete tag:Each format stores the field where it keeps named fields:
NAME=valuefield=ID3/TAG/OggS/MP+TXXX, description = nameGetUserText/SetUserTextmatch----with meancom.apple.iTunes, name = name;datatype 1 (UTF-8) or 0 (implicit, read asGetFreeformTagreads it)dataatom per valueSimpleTagat the item level (the levelCommentandGenreuse)SimpleTagper valueGetFieldmatchesSupportsCustomFieldsis false, every call is a no-opCombinedTag(MP3, raw AAC, AVI with an ID3v2 chunk, MPEG program streams),FlacTag, and the AIFF view (AiffFile.Tag) forward all four members. A combined view writes the values into every tag that can store them and removes the name from a tag that cannot (a RIFF INFO list holds one value under a four-character name), so every tag holds the new values or not the name and every reader shows the new values or nothing; when no tag can store them it returns false and changes nothing. A test holds every concreteTagtype that is not a listed leaf of the sharedTagTypeClassification(from #136) to the four members, so a view of any shape is checked; it flags all eleven of #136's dummy view shapes.CombinedTagandFlacTagread the first tag that holds the name and write every tag that supports custom fields, as the property setters already do. ID3v1 is left as it is.Motivation and Context
TagLib# 2.3 offers
GetField/SetField/RemoveFieldon concrete tags. Here each format had its own method, and ASF and Matroska had no public way to reach an unmapped name.Closes #112
Naming
GetField/SetFieldare taken:RiffInfoTag.GetField(string fourCC)returnsstring?, and an inheritedGetFieldreturning a list would collide with it.GetValue/SetValueare taken onVorbisCommentandApeTagwith single-value semantics.*CustomFieldreads naturally besideSetUserTextFrame,SetFreeformTag, andGetValues, and says what it holds: a field the mapped properties do not name.Design decisions
SetCustomFieldreturns false and leaves the tag unchanged (tested by comparing rendered bytes) when the format cannot store the name or the values. The only exception isArgumentNullExceptionfor a null name.TITLEthere setsAlbum, andTitleprefers the segment title (documented and tested). Tests cover VorbisTITLE, APETitle, ID3TXXX:REPLAYGAIN_TRACK_GAIN, MP4CONDUCTOR, ASFWM/AlbumTitleandWM/TrackNumber(DWORD read as decimal), MatroskaCOMMENT, and RIFFINAM. On ID3v2 a name is always aTXXXdescription:"TITLE"never touchesTIT2. On MP4 it is always a freeform atom: it never touches©nam.Title,Author,Copyright,Description, andRatingare the Content Description Object's fields (§3.10). The Windows Media Format SDK exposes them under those attribute names, so they route there instead of creating a shadow attribute. The first value goes there and the others to the Metadata Library Object, as Windows stores a multi-valuedAuthor.METADATA_BLOCK_PICTURE, APECover Art*, and ASFWM/Picture. UsePicturesfor those. Without this, Vorbis rendering would silently drop the field, because it regeneratesMETADATA_BLOCK_PICTUREfrom the picture list.TXXX, andTIPL/TMCL/IPLS; it is the decoder fix: Drop a terminating NUL from ID3 text #136'sDecodeUtf16BomTextcalls too, so it keeps fix: Drop a terminating NUL from ID3 text #136's rules: a lone last byte is dropped, the 00 00 pairs after the last string are terminators rather than an empty value, and a string without a BOM uses the frame's first BOM. InTIPL/TMCL/IPLSa lone byte right after a terminator still starts an empty last string, so the role before it keeps an empty person, as fix: Drop a terminating NUL from ID3 text #136 reads it. For text frames andTXXX, one code path serves every target framework and decodes into pooled buffers, so a read allocates no more than base on netstandard2.0, netstandard2.1, net8.0 and net10.0 (measured: 64/72/136/72/2464 B for the reviewer's five frames on each). TheTIPL/TMCL/IPLSsplitter decodes each string withEncoding.GetString, which on netstandard2.0 copies the span first, as base does. A frame read from a file records where its BOMs were (one BOM, one per non-empty string, or one per string including empty ones) and how many terminators ended it, and renders back with both, so an empty last value and the terminators survive an unchanged save. A string whose text starts with U+FEFF or U+FFFE always gets its own BOM, so a reread keeps that char. A frame built in code keeps one BOM, as before; only aTXXXthatSetCustomFieldwrites in v2.3 gets a BOM per value.GetUserText/SetUserTextand the mapped properties. With two rules on one frame type,SetCustomField("foo")besideSetUserText("FOO")would write a secondTXXX. With one rule, a frame written by either API is found, replaced, and removed by the other, and no duplicate is created; a test pins this. When a frame is replaced it takes the caller's spelling. The spec allows oneTXXXper description and does not say whether case matters, so folding case also keeps one frame per name when other tools wrote it in a different case.mean:namesyntax. The name is always undercom.apple.iTunes, so a name may contain:("a:b"is a name). For other means,Mp4File.GetFreeformTag/SetFreeformTagremain.GetCustomFieldreturns their file-level records for the name (any language), and so doPerformers(Author),AlbumArtists,Composers, andGenres, which now read every value and write values 2..n back to the library.SetCustomField,RemoveCustomField, a mapped setter given a new value, andCleardrop the old library values, so a removed field is gone from the file. The ASF setters use fix: Drop a terminating NUL from ID3 text #136'sTag.IsWriteBack, with no second rule: writing back the exact value a property read is a no-op, so a read-modify-write keeps every value (a matrix over all 77AsfTagproperties on a synthesized WMA pins it, a local run over 16 real WMA files agrees, and a layout matrix over every number property andIsCompilation, with values in the Extended Content Description, the Metadata Object, and the Metadata Library, pins write-back, clear, and replace on each);nullalways clears, a stored value the getter cannot read included (the DWORD year mutagen writes, a WORD track, a DWORDWM/IsCompilation), soYear = nullremoves it as base did, andIsCompilation = falseclears a true stored in any object; andCopyToalways replaces, so a copy into ASF drops the target's Metadata Library values.""is the value read only when no value of the attribute holds text. The R128 dB setters follow the same rule. A title, artist, copyright, comment, or rating write (mapped property or custom field) also replaces the same-named Extended Content Description descriptor ffmpeg writes beside the Content Description field, so mutagen and ffprobe stop showing the old value (an ffmpeg WMA'sArtist = "V2"left mutagen readingAuthor ['V2', 'old']and ffprobeartist=old); a non-empty field hides that descriptor on read and an empty one reads it. Records about one stream are kept. The Header Extension Object is rebuilt only when its records change, so other saves keep its bytes. If either object cannot be parsed (truncated, duplicated, or behind a child that runs off the end), custom writes return false instead of leaving values behind.IsCompilation) return the first non-empty value of the attribute, from the Content Description or Extended Content Description and then the Metadata and Metadata Library Objects, the rule fix: Drop a terminating NUL from ID3 text #136 gives the ID3v2, APE, and Vorbis Comment getters. An attribute mutagen wrote as['', 'A2']readsA2, and writingA2back keeps both values; mutagen'sWM/TrackNumber = ['', '7']readsTrack = 7, andTrack = Trackkeeps both.RemoveCustomFieldremoves the name at every file level the getter can climb to, so a broader value does not show through afterwards.SetCustomFieldwrites at the item level and shadows broader levels, as the format defines. Chapter, track, edition, and attachment targets are never read or changed.FlacTagcreates a VORBIS_COMMENT block only when a field is actually stored, so a refused name does not add an empty block.Bugs fixed along the way (the same save paths)
TXXX(or any frame outside the seven base fields) is written on save. Main (fix: Return an AIFF annotation as the comment #135) madeId3v2Tag.IsEmptyfalse for any frame, which is the gate WAV and AIFF use, so this PR no longer adds its own gate:IsEmptynow readsFrameLists, the same arrayClearreads, so a frame list added later counts without a second edit. A test over every frame kind writes a WAV and an AIFF holding only that frame.mdtameta, a----atom in an iTunesilst. A remove drops every item the name was read from (in anmdtameta its keys entry stays, as for any field a set removes there),Clearremoves them all, and a copy from another tag keeps them. A custom field and a chapter list set before one save both survive, with the tag inudta/metaormoov/meta(feat: Read and write MP4 chapters #147).SetCustomField, so a fieldSetCustomFieldaccepts renders as an item the tag placement accepts.Clearremoves custom fields in every format (docs/IMPLEMENTATION-GUIDE.md rule 7), pinned for each.Id3v2Tag.ClearandIsEmptyread one array of the fifteen frame lists, pinned by a test over every frame kind.Out of scope
Tag.CopyTodoes not copy custom fields. That needs name enumeration, which this issue does not ask for.Picturesstill ignoresWM/Picturerecords in the Metadata Library.Type of Change
Checklist
dotnet test)Spec references
----/mean/name/data: the iTunes metadata layout the library already parses (ISO 14496-12 box structure)Testing
Gates (local, macOS):
dotnet build -c Release: 0 warnings, 0 errors (netstandard2.0, netstandard2.1, net8.0, net10.0)dotnet test -c Releaseon main de3ac07 plus this commit: net8.0 7066 passed, 55 skipped; net10.0 7066 passed, 55 skipped. The skips are env-gated real-file tests; with the env vars set to real samples, 7112 pass and 9 skip (samples I do not have: MPEG video, foobar2000 MP4, invalid AAC, a complete AAX, and two kit depth cases).dotnet format --verify-no-changes: cleanTagand its overrides)Unit tests:
Core/CustomFieldTests.cs: a matrix over Vorbis, APE, ID3v2.4, ID3v2.3, MP4, ASF, Matroska, and RIFF INFO. Each case checks:Per-format name rules, picture names, and collisions with mapped properties are tested separately, as are ID3v1,
CombinedTag, andFlacTag. ATXXXwritten bySetUserText("FOO")is found, replaced, and removed throughGetCustomField/SetCustomField/RemoveCustomFieldin other cases, and the tag never holds two frames; that test goes red with ordinal matching.Core/CustomFieldFileRoundTripTests.cs: a custom field set throughIMediaFile.Tagsurvives render and reopen for FLAC, MP3 (ID3v2.4 and ID3v2.3), Ogg Vorbis, Opus, Ogg FLAC, Monkey's Audio, WavPack, Musepack, MP4, ASF, WAV (ID3 and INFO), AIFF, DSF, DFF, and raw AAC (ID3v2 and APEv2). Each container also has a remove-then-reopen test. An ID3 tag holding only a custom field survives in WAV, AIFF, DSF, and DFF; this goes red for WAV and AIFF with the old gate. A FLAC file with a custom field re-saves byte-identically.Matroska/MatroskaCustomFieldTests.cs: item-level write, a broader level showing through, remove clearing both levels, chapter tags left alone, and a file withoutTags.I reverted guards (FlacTag lazy creation, Vorbis picture-name refusal, AIFF view forwarding, the ASF Content Description twin match in remove and in replace, the APE measure's character check) and each turned its tests red; the WAV/AIFF gate is now main's
IsEmpty.Real files (generated with ffmpeg from lavfi, plus TagLib#
sample.apeandTurning Lime.mkv). Each file was written throughMediaFile.Read→Tag.SetCustomField("TAGLIBSHARP2_CUSTOM", "first", "second")→SaveToFile, then read by other tools:ffmpeg -f null-c copystreamhash)Turning Lime.mkv)sample.ape)IXYZ=only)¹ That tool keeps one value per repeated key. ² exiftool prints the NUL-separated APE value without a separator. ³ Every byte before the APE tag is identical. The ffmpeg copy hash differs because its last APE packet runs into the tag, and the library writes the tag without its optional header. An unchanged save of the original does the same, so this predates this PR.
TaglibSharpSamplesmatroskatest1–test8(excepttest4, which ffmpeg itself cannot read and the library reads without tags), the five FLAC samples there, and TagLib#sample.{ogg,opus,wma,m4a,mpc,wv,flac,mp3,wav}.Core/CustomFieldIntegrationTests.csis env-gated:TAGLIBSHARP2_TEST_{FLAC,MP3,M4A,OGG,APE,WV,WMA,MKV,WAV,AIFF}. I ran it locally with all ten set (and again with MP3 v2.4, Ogg Vorbis, the lavfi MKV, and WAV+ID3). 10/10 passed on net8.0 and net10.0.Additional Notes
MediaFile.Read(path)returns aFlacFile/Mp3FilewhoseSaveToFile(path)fails with "No source path available", while the other formats re-read their source. This predates this PR and is not changed here; the integration tests save withSaveToFile(path, originalBytes).