Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `Tag.GetCustomField`, `Tag.SetCustomField`, `Tag.RemoveCustomField`, and `Tag.SupportsCustomFields` read,
write, and remove a named field with one or more values on any tag (#112). The field lives in the format's
own storage: a Vorbis comment field, an APE text item, an ID3v2 `TXXX` frame, an MP4 `----:com.apple.iTunes`
freeform atom, an ASF attribute, a file-level Matroska `SimpleTag`, or a RIFF INFO chunk (Latin-1 in WAV;
any text in AVI, saved under the file's CSET rules). ID3v1 reports no support. `CombinedTag` (MP3, raw AAC,
AVI with an ID3v2 chunk, MPEG program streams), `FlacTag`, and the AIFF view (`AiffFile.Tag`) forward the
members. A combined view writes the values into every tag that can store them and removes the name from a
tag that cannot (RIFF INFO holds one value), so no tag keeps an older value. On ASF the first value goes to
the Content Description or Extended Content Description and the others to the Metadata Library Object, as
Windows writes them. `GetCustomField`, `Performers`, `AlbumArtists`, `Composers`, and `Genres` read every
value; `SetCustomField` and those four properties write them back the same way; a remove, a mapped setter
given a new value, and `Clear` drop the Metadata Library values too. ASF follows the write-back rule of
ID3v2, APE, and Vorbis Comments (`Tag.IsWriteBack`): writing back the exact value a mapped property read
changes nothing, `null` always clears, a stored value the getter cannot read included (the DWORD year
mutagen writes, say), and `CopyTo` always replaces. A single-value ASF getter returns the first non-empty
value of its attribute (a string, a number, or a BOOL), from the Content Description or Extended Content
Description and then the Metadata and Metadata Library Objects, as the ID3v2, APE, and Vorbis Comment
getters do, and `IsCompilation = false` clears a true stored in any of them.
- `MatroskaFile.Tracks` lists the first 4096 known-size `TrackEntry` elements as `MatroskaTrack` objects, and `MatroskaFile.IsTrackListTruncated` tells when a file had more. Each track has `TrackNumber`, `TrackUid`, `TrackType`, `CodecId`, `IsDefault`, and `IsEnabled` (RFC 9559 §5.1.4.1).
- `MatroskaTrack.IsEnabled` is written back on save. A stored `FlagEnabled` is rewritten in place. A missing one is added to the `TrackEntry`, taking the room from the `Void` right after `Tracks` when it has enough so no element moves; otherwise everything after `Tracks` moves once, with every SeekHead, Cues and Cluster `Position` value and CRC-32 updated, each widened when it no longer fits. Adding a FlagEnabled (minver 2) also makes the EBML header's `DocTypeVersion` at least 2: a stored lower value is raised, and an empty or missing one is written (RFC 9559 section 7); no Segment Position moves. A track the caller did not set keeps its bytes.
- `AiffFile.Id3v2Tag`, and `AiffFile.Title`, `Artist`, `Comment` and `Copyright`
Expand Down Expand Up @@ -94,10 +112,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Matroska: a save keeps one copy of the Segment body at a time; a move no longer holds a second one.
- **MP4**: A version 1 `mvhd` or `mdhd` whose 64-bit duration is past `TimeSpan.MaxValue` no longer throws
out of `Mp4File.Read` or `Mp4AudioPropertiesParser.Parse`; the duration saturates.
- ID3v2 UTF-16 text with a byte order mark on each string (ID3v2.3 §3.3, mutagen's default) reads without a
U+FEFF at the start of every value after the first, in text frames, `TXXX`, and `TIPL`/`TMCL`/`IPLS`. One
decoder reads all of them on every target framework. A text or `TXXX` frame read from a file renders with the
BOMs and the trailing terminators it was read with, so an empty last value survives an unchanged save. A
`TXXX` that `SetCustomField` writes in an ID3v2.3 tag has a BOM before each value; a frame built in code keeps
one BOM, as before.
- Writing back the gain `R128TrackGainDb` or `R128AlbumGainDb` read keeps the stored text, so `-0512` is not
rewritten as `-512`. `null` still clears it, text the getter cannot parse included.
- An ASF title, artist, copyright, comment, or rating write replaces the same-named Extended Content Description
descriptor that ffmpeg writes beside the Content Description field, so other tools no longer show the old value.
An empty Content Description field reads that descriptor.

### Changed

- An APE artist, album artist, composer, or genre is the first name. It is not a comma-joined string. A Basic copy from APE into MP4, ASF, RIFF INFO, or ID3v1 keeps that first name. A copy into ID3v2 keeps the full list.
- An APE artist, album artist, composer, or genre is the first name. It is not a comma-joined string. A Basic copy from APE into MP4, ASF, RIFF INFO, or ID3v1 keeps that first name. A copy with the Extended options into ASF keeps every name, the first in the Content Description and the rest in the Metadata Library. A copy into ID3v2 keeps the full list.
- A single-value ID3v2 or Vorbis Comment property, and an APE artist, album artist, composer, or genre, returns the first non-empty value, and an empty string only when every value is empty. Other APE text items still read every non-empty value joined with ", " (`A\0B` reads "A, B"). So do the public `Id3v2Tag.GetTextFrame`, `Id3v2Tag.GetUserText`, and `VorbisComment.GetValue`, and the ID3v2 comment and lyrics, which read the first non-empty frame without a description, then the first other one. ID3v2.4 §4.2 stores several strings as a NUL-separated list and Vorbis field names may repeat, so `"\0V2"` and `ARTIST=` + `ARTIST=V2` read `V2`. A value is kept as written: only an empty value is skipped, so `" \0B"` reads `" "`, as other readers show it. `Id3v2Tag.GetTextFrameValues` and the ID3v2 list properties no longer trim or drop blank NUL-separated values, so for NUL-separated values the single value and the first list entry agree. A value with `/` and no NUL is still split on `/` and trimmed, in any ID3v2 version, so `AC/DC` reads Artist `AC/DC` and Performers [`AC`, `DC`], as before. This replaces the earlier reading of a leading NUL as an empty first value, and it differs from mutagen and TagLib#, which return the empty first ID3v2 value; for a comment it matches what ffprobe and TagLib# show. Writing back the value read changes nothing, so a multi-value frame, item, or repeated field keeps every value. `= ""` clears: it removes the ID3v2 frame (for a comment or lyrics, every frame without a description) or the APE item, and leaves one empty Vorbis field. The comparison is ordinal, so a case-only edit still writes, and `null` clears like `= ""`. For an ID3v2 comment or lyrics, both remove only the frames without a description; a described comment or lyrics frame (such as `ID3v1 Comment`) is then still read, as before. This covers every ID3v2 string property (every text and TXXX frame, the year, the podcast feed URL, the MusicBrainz recording ID, the comment and the lyrics, and the movement number and total), every APE text property, and every Vorbis text field.
- `CopyTo` reads every source value before it writes any. It replaces the target's list with the source value, even when that equals the target's first value. A list (performers, album artists, composers, genres, and the sort and role lists) that the target already holds is left as it is, and with `Extended` the artist and genre travel in those lists, so a `Basic` copy alone is what writes a single name over a list. When the source and target read from a common tag (a tag copied into itself, or a file's combined `Tag` copied into one of its own tags or into another view over them, as two reads of `Mp3File.Tag` are), the copy only adds values the target does not show. An empty source value still clears the field, as `= ""` does, so through a view it also clears what the view showed from another tag (an ID3v1 title, an AIFF `NAME` chunk). So `mp3.Id3v2Tag.CopyTo (mp3.Tag)` no longer rewrites the ID3v1 tag with values the combined view already shows from ID3v2. A fallback field of its own beside a value equal to the copied one stays: Vorbis `ORIGINALYEAR`, APE `Date`, and the ID3v2.3 `TDOR` beside `TORY`. Pictures are outside these rules: a non-empty source picture list always replaces the target's, as before.
- A list copied into a tag that holds one name (MP4, ASF, RIFF INFO, ID3v1, or the base `Tag` list setters) stores its first non-empty entry, not an empty first entry. A copy of `ARTIST=` + `ARTIST=V2` into an MP4, WAV, or WMA file stores `V2`, as a `Basic` copy already did, instead of clearing the artist and genre.
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ See the [Migration Guide](docs/MIGRATION-FROM-TAGLIB.md) for detailed comparison
| **MusicBrainz IDs** | ✅ | ✅ | ✅ | ✅ | ✅² | ✅¹ | ✅¹ | ✅ | ✅⁹ | ✅¹ | ✅¹ | ✅⁸ | ✅⁸ | ✅⁸ | ✅¹ |
| **Lyrics** | ✅ | ✅² | ✅² | ✅² | ✅² | ✅¹ | ✅¹ | ✅ | ✅⁹ | ✅¹ | ✅¹ | ✅⁸ | ✅⁸ | ✅⁸ | ✅¹ |
| **Chapters** | ✅ | — | — | — | — | — | — | ✅¹¹ | — | — | — | — | — | — | — |
| **Custom fields** | ✅ | ✅ | ✅ | ✅ | ✅² | ✅¹² | ✅¹ | ✅¹³ | ✅⁹ | ✅¹ | ✅¹ | ✅⁸ | ✅⁸ | ✅⁸ | ✅¹ |
| **Performer roles** | ✅ | ✅ | ✅ | ✅ | ✅² | ✅¹ | ✅¹ | — | ✅⁹ | ✅¹ | ✅¹ | ✅⁸ | ✅⁸ | ✅⁸ | ✅¹ |
| **BWF broadcast metadata** | — | — | — | — | — | ✅ | — | — | — | — | — | — | — | — | — |
| **Surround sound info** | — | — | — | — | — | ✅³ | — | ✅⁶ | — | ✅⁷ | ✅⁷ | — | — | — | — |
Expand All @@ -59,6 +60,8 @@ See the [Migration Guide](docs/MIGRATION-FROM-TAGLIB.md) for detailed comparison
⁹ Via ASF Extended Content Description
¹⁰ From the ADTS frame headers or the ADIF header. HE-AAC reports the AAC core sample rate.
¹¹ Nero `chpl` box and QuickTime chapter track, read and write (`Mp4File.Chapters`)
¹² Via ID3v2 `TXXX`, or a four-character RIFF INFO chunk when the file has no ID3v2 tag
¹³ Via ----:com.apple.iTunes freeform atoms

### Tag Format by Container

Expand Down Expand Up @@ -157,6 +160,11 @@ Console.WriteLine($"AIFF: {aiff.AudioProperties?.SampleRate}Hz");
aiff.Id3v2Tag = new Id3v2Tag { Title = "Updated Title" };
aiff.SaveToFile("song.aiff");

// Custom fields on any tag (Vorbis field, APE item, TXXX, MP4 freeform, ASF attribute, Matroska SimpleTag)
var tagged = MediaFile.Read("song.flac").Tag!;
tagged.SetCustomField("MY_APP_DATA", "first", "second");
IReadOnlyList<string> custom = tagged.GetCustomField("MY_APP_DATA");

// Async support for high-throughput scenarios
var asyncResult = await Mp4File.ReadFromFileAsync("song.m4a");
```
Expand Down
25 changes: 25 additions & 0 deletions docs/Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ if (result.IsSuccess)

### Read Custom Fields

Any tag, whatever the format, through the base `Tag`:

```csharp
var tag = MediaFile.Read ("song.flac").Tag!;
if (tag.SupportsCustomFields) {
IReadOnlyList<string> values = tag.GetCustomField ("MY_APP_DATA");
}
```

Or through the format's own API:

```csharp
// ID3v2 - User-defined text (TXXX frames)
var id3v2 = mp3.Id3v2Tag!;
Expand Down Expand Up @@ -192,6 +203,20 @@ tag.R128AlbumGain = "256"; // +1 dB

### Write Custom Fields

Any tag, whatever the format. The field is stored where the format keeps named fields: a Vorbis comment, an
APE item, an ID3v2 `TXXX` frame, an MP4 `----:com.apple.iTunes:` atom, an ASF attribute, a Matroska
`SimpleTag`, or a RIFF INFO chunk (four-character names only). `SetCustomField` returns false and changes
nothing when the format cannot store the name or the values; ID3v1 stores none.

```csharp
var tag = file.Tag!;
tag.SetCustomField ("MY_APP_DATA", "first", "second"); // several values
tag.SetCustomField ("CATALOG_ID", "ABC-12345");
tag.RemoveCustomField ("OLD_FIELD");
```

Or through the format's own API:

```csharp
// ID3v2 - User-defined text (TXXX frames)
var id3v2 = mp3.Id3v2Tag!;
Expand Down
13 changes: 13 additions & 0 deletions docs/MIGRATION-FROM-TAGLIB.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,19 @@ string? gain = id3v2?.GetUserText("REPLAYGAIN_TRACK_GAIN");
id3v2?.SetUserText("REPLAYGAIN_TRACK_GAIN", "-6.5 dB");
```

### Custom Fields on Any Tag

TagLib# 2.3 reads and writes a named field on a concrete tag with `GetField`, `SetField`, and `RemoveField`.
TagLibSharp2 has the same operations on the base `Tag`, so the caller does not need the concrete type.
`RiffInfoTag.GetField` already means "read one FOURCC", so the new members are named for what they hold:

| TagLib# | TagLibSharp2 |
|---------|--------------|
| `GetField(name)` | `tag.GetCustomField(name)` (all values) |
| `SetField(name, values)` | `tag.SetCustomField(name, values)` (returns false if the format cannot store it) |
| `RemoveField(name)` | `tag.RemoveCustomField(name)` |
| — | `tag.SupportsCustomFields` (false for ID3v1) |

## Binary Data Handling

**TagLib#** uses `ByteVector`:
Expand Down
37 changes: 37 additions & 0 deletions src/TagLibSharp2/Aiff/AiffTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,43 @@ public override BinaryData Render () =>
/// <inheritdoc/>
public override void Clear () => _file.ClearMappedFields ();

/// <inheritdoc/>
/// <remarks>Custom fields live in the file's ID3 tag, as <c>TXXX</c> frames; the native text chunks have fixed names.</remarks>
public override bool SupportsCustomFields => true;

/// <inheritdoc/>
public override IReadOnlyList<string> GetCustomField (string name)
{
CustomFieldValues.ThrowIfNull (name);
return Id3?.GetCustomField (name) ?? CustomFieldValues.None;
}

/// <inheritdoc/>
/// <remarks>
/// Writes a <c>TXXX</c> frame in the file's ID3 tag. The tag is created only when the field is stored, so a
/// refused name or a write of no values does not add an ID3 chunk.
/// </remarks>
public override bool SetCustomField (string name, params string[] values)
{
CustomFieldValues.ThrowIfNull (name);
if (Id3 is { } existing)
return existing.SetCustomField (name, values);

var created = new Id3v2Tag ();
if (!created.SetCustomField (name, values))
return false;
if (!created.IsEmpty)
_file.Id3v2Tag = created;
return true;
}

/// <inheritdoc/>
public override bool RemoveCustomField (string name)
{
CustomFieldValues.ThrowIfNull (name);
return Id3?.RemoveCustomField (name) ?? false;
}

Id3v2Tag? Id3 => _file.Id3v2Tag;

/// <inheritdoc/>
Expand Down
56 changes: 56 additions & 0 deletions src/TagLibSharp2/Ape/ApeTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,62 @@ void SetOrRemoveProperty (string key, string? value, string? current, string? al
/// </summary>
public bool Remove (string key) => _items.Remove (key);

/// <inheritdoc/>
public override bool SupportsCustomFields => true;

/// <inheritdoc/>
/// <remarks>
/// Reads a text or external locator item. Keys compare case-insensitively, and a zero byte separates
/// values (https://wiki.hydrogenaud.io/index.php?title=APE_key and
/// https://wiki.hydrogenaud.io/index.php?title=APE_Item_Value). A binary item has no text and returns
/// no values.
/// </remarks>
public override IReadOnlyList<string> GetCustomField (string name)
{
CustomFieldValues.ThrowIfNull (name);
if (!IsCustomFieldKey (name) || !_items.TryGetValue (name, out var item))
return CustomFieldValues.None;
if (item.ItemType is not (ApeItemType.Text or ApeItemType.ExternalLocator))
return CustomFieldValues.None;

var values = new List<string> ();
CustomFieldValues.AddSplit (System.Text.Encoding.UTF8.GetString (item.RawValue), values);
return values;
}

/// <inheritdoc/>
/// <remarks>
/// Writes one UTF-8 text item with the values separated by a zero byte
/// (https://wiki.hydrogenaud.io/index.php?title=APE_Item_Value). The key replaces any item whose key
/// differs only in case, because APEv2 does not allow two such keys
/// (https://wiki.hydrogenaud.io/index.php?title=APE_key).
/// </remarks>
public override bool SetCustomField (string name, params string[] values)
{
CustomFieldValues.ThrowIfNull (name);
if (!IsCustomFieldKey (name) || !CustomFieldValues.TryNormalize (values, out var kept))
return false;

_items.Remove (name);
if (kept.Count > 0)
_items[name] = ApeTagItem.CreateText (name, string.Join ("\0", kept));
return true;
}

/// <inheritdoc/>
public override bool RemoveCustomField (string name)
{
CustomFieldValues.ThrowIfNull (name);
return IsCustomFieldKey (name) && _items.Remove (name);
}

/// <summary>
/// A valid APEv2 key that is not one of the <c>Cover Art</c> keys <see cref="Pictures"/> owns.
/// </summary>
static bool IsCustomFieldKey (string name) =>
ApeTagItem.IsValidKey (name) &&
!name.StartsWith ("Cover Art", StringComparison.OrdinalIgnoreCase);

private void SetOrRemove (string key, string? value, string? alias = null)
{
if (alias is not null)
Expand Down
Loading
Loading