1

Forgive me if this question is already answered somewhere on this site, but I didn't find anything when I searched for it. I've written a ID3v1/2 tag editor for .mp3 files in C# using taglib-sharp, and taglib-sharp treats the track numbers as uint numbers. According to id3.org:

The 'Track number/Position in set' frame is a numeric string containing the order number of the audio-file on its original recording. This may be extended with a "/" character and a numeric string containing the total numer of tracks/elements on the original recording. E.g. "4/9".

Personally I don't use "/", but I tend to write "03" instead of "3". Is there a simple way to write the track number to the tag as a string directly, instead of via a uint?

Also, side question: taglib doesn't seem to support some tags, specifically URL, Orig. Artist, Publisher and Encoded. Any idea on what to do with those?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Eric
  • 11
  • 3
  • I believe it is common to write leading zeros to the TRCK field. It works in players such as VLC and WinAmp, but there is a bug in Windows Explorer that interprets some 3-digit numbers as octal numbers. I too prefer leading zeros, but perhaps it is best to avoid? – rlv-dan May 13 '20 at 16:30

2 Answers2

3

UPDATE: Since this answer was originally written, GetTextAsString was made public. This answer has been updated to reflect that.

The Track field in TagLib# is a universal approximation and simplification of various tagging specifications intent for the tag field. For ID3v2 tags, this is assuming the TRCK field consists of one or two numbers strings separated by a slash and converting them into numbers, per the specification.

That said, it is a text field and you can do whatever you want with it. You just need to access the text frame to read or write it.

Writing is easy through Id3v2.Tag.SetTextFrame:

var tag = (Id3v2.Tag)file.GetTag(TagTypes.Id3v2, true); // Get or create ID3v2 tag.
tag.SetTextFrame("TRCK", "03"); // Add or update TRCK frame.

Since TRCK is a single-string text frame, it can similarly be read using Id3v2.Tag.GetTextAsString:

var tag = (Id3v2.Tag)file.GetTag(TagTypes.Id3v2, false);
var trackNumber = tag?.GetTextAsString("TRCK");
Brian Nickel
  • 26,890
  • 5
  • 80
  • 110
  • GetTextAsString() method is private. – Snakebyte Aug 11 '14 at 08:08
  • Sorry, I literally just downloaded tagLib#. What type of object is `file` in your sample code? – sab669 Jul 19 '15 at 15:43
  • 1
    @sab669 this example works for any tag file that support Id3v2 tags (e.g., an MP3). You could try it on any file type if you do proper null checking. – Brian Nickel Jul 19 '15 at 15:51
  • what is FrameType? – Heisenberg Mar 15 '19 at 18:40
  • @Heisenberg, I had to look at the source to find `FrameType`. It's in FrameTypes.cs, and for some reason the author decided it should be private. His words from the code: _It is, however, not necessary for external users to use this class._ TRCK is defined as `ReadOnlyByteVector TRCK = "TRCK"` – Mmm Oct 04 '19 at 21:13
  • @Mmm As the original author, I too wonder what my motivations were for keeping such things private. :/ – Brian Nickel Oct 07 '19 at 16:20
-1

No you can't save a string into an int or uint without casting it first. Why don't just you save your value 3 as a uint as per the library you are using and use something like:

uint track = 3;
string strTrack = track.ToString("00");

to display it?

If it's allowed by its license, you can still modify the library to suit your needs.

I've taken a quick look at taglib sharp in the past and it's far from supporting all existing frames in a tag. It supports the most common frames only. For the other ones, I think there is some kind of default class you can use but I don't recall the name. Otherwise you can still go and extend the library by yourself unless there are some other such libraries available, which I am not aware of.

Guillaume
  • 1,782
  • 1
  • 25
  • 42
  • I know you can't implicitly convert between string and int/uint. The problem is that the conversion between uint and string is done by taglib, if you only use taglib you have to use uint, which destroys any information regarding extra zeroes, dashes, etc. before it's converted back to string by the library. I have no idea, however, how to modify the library or how to work directly with the tags. – Eric Jan 15 '13 at 05:01
  • If you want to use '03' for display only, you can use my solution. If it's for storing indeed, you will lose information. So if it's really a problem, you just can't use taglib-sharp unless you manage to extend it. The fact this library does not follow ID3 specifications is indeed an issue. Check their Website to see if you can download their sources and if it's allowed to modify it. – Guillaume Jan 15 '13 at 05:21