mono / taglib-sharp

Library for reading and writing metadata in media files
GNU Lesser General Public License v2.1
1.26k stars 308 forks source link

Adding New Tags #205

Open jamsoft opened 4 years ago

jamsoft commented 4 years ago

I'm in the process of adding additional tag to Riff files.

Reading the new tag is all working fine. However when it comes to writing the new tag data back to the file I'm getting a file that has this new tag data in, but its also writing the original version of the tag into the the new file.

Meaning this new file now has two versions of the same tag data. I must be missing something in the write code that tells the lib that it's already written that data to the file stream.

Any pointers?

MrCSharp22 commented 4 years ago

Can you share the code? Both the reading bit and writing.

jamsoft commented 4 years ago

So the write code is:

// Embed the DivX tag in "IDVX and embed it as
// the fourth tag.
if (divx_tag != null && !divx_tag.IsEmpty) {
    ByteVector tag_data = divx_tag.Render ();
    data.Add ("IDVX");
    data.Add (ByteVector.FromUInt ((uint)tag_data.Count, false));
    data.Add (tag_data);
}
-- NEW CODE
if (bext != null) {
    ByteVector tag_data = bext.Render ();
    if (tag_data.Count > 10) {
        if (tag_data.Count % 2 == 1)
            tag_data.Add (0);
    }
    data.Add ("bext");
    data.Add (ByteVector.FromUInt ((uint) tag_data.Count, false));
    data.Add (tag_data);
}
-- NEW CODE

Read (false, ReadStyle.None, out var riff_size, out var tag_start, out var tag_end);

This is the read code:

// the bext data
case "BEXT":
case "bext":
    if (read_tags && bext == null) {
        bext = new BextTag (this, position + 8, size);
    }
    tag_found = true;
    break;
}

I think whats happening is that the tag_found = true updated the start and end positions and treats this byte range as UNKNOWN STUFF and just writes it as a block even though the tag data was previously written. Not sure though.

jamsoft commented 4 years ago

I also think that due to the audio files I'm dealing with are from advanced audio applications like Digital Audio Workstations (think Cubase and Logic) that the files I'm dealing with are not your usual ripped audio CD files and I think taglib sharp makes a LOT of unsafe assumptions about the file structures.

It doesn't break files per se but it does seem to do things it shouldn't imho.

For instance this same file once written ends up with two JUNK chunks. Also taglib ALWAYS writes an id3 tag. Even if the original file didn't have one.

Hmm ...

jamsoft commented 4 years ago

I also think that I'm spotting things stuff as I'm checking files at the binary level and with RiffPad.

Like I said, it's not breaking files as such but I'm a little uneasy with it making unrequired changes to file contents (id3 & JUNK) mentioned above.

MrCSharp22 commented 4 years ago

I am wondering if you have tried to remove all tags from the file first before writing anything to it?