JeevanJames / Id3

Library to read, modify and write ID3 & Lyrics3 tags in MP3 files. Provides an extensible framework for retrieving ID3 information from online services.
Apache License 2.0
41 stars 17 forks source link

Unable to Add an ID3.v2 Tag to an MP3 #65

Open BourgeoisDirk opened 1 year ago

BourgeoisDirk commented 1 year ago

Hey, so far this is one of the most convenient ID3 Tag Editors I've come across. Only thing is, I can't actually Add an ID3 tag to a freshly created MP3.

    _private void ApplyID3Tags(string filepath)
    {
        using (var file = new Mp3(filepath, Mp3Permissions.ReadWrite))
        {
            var tag = file.GetTag(Id3TagFamily.Version2X);
            if (tag == null)
                tag = new Id3Tag();

            tag.Title = Title;

            if (!file.WriteTag(tag, WriteConflictAction.Replace))
                throw new Exception("Applying ID3 Tags Failed");
        }
    }_

This seems simple enough, but I get this error on the "file.WriteTag()" function at the end: "Sequence contains no matching element"

System.InvalidOperationException HResult=0x80131509 Message=Sequence contains no matching element Source=System.Linq StackTrace: at System.Linq.ThrowHelper.ThrowNoMatchException() at Id3.Id3Handler.GetHandler(Id3Version version) at Id3.Mp3.WriteTag(Id3Tag tag, WriteConflictAction conflictAction)

Any clue as to why I can't add an ID3 tag ? PS: GetTag() resulted in null, so i added "new Id3Tag()" Also, "file.AvailableTagVersions" count = 0

BourgeoisDirk commented 1 year ago

Full code segment:

    _private void ApplyID3Tags_Test(string filepath)
    {
        using (var file = new Id3.Mp3(filepath, Id3.Mp3Permissions.ReadWrite))
        {
            var tag = file.GetTag(Id3.Id3TagFamily.Version2X);
            if (tag == null)
                tag = new Id3.Id3Tag();

            tag.Track = Track;
            tag.AudioSourceUrl = Url;
            tag.ArtistUrls.Add(new Id3.Frames.ArtistUrlFrame(Channel?.uChannel.Url));
            tag.Artists.Value.Add(Channel?.Title);
            tag.Album = (Playlist == null ? Channel?.Title : Playlist?.Title);
            tag.Title = Title;
            tag.Subtitle = Description;
            tag.RecordingDate = UploadDate.DateTime;
            tag.Length = Duration;
            foreach (var word in Keywords)
                tag.Comments.Add(word);
            if (Thumbnail != null)
                tag.Pictures.Add(new Id3.Frames.PictureFrame()
                {
                    Description = "Thumbnail",
                    PictureData = Thumbnail.ToByteArray(),
                    PictureType = Id3.Frames.PictureType.FrontCover
                });

            if (!file.WriteTag(tag, Id3.WriteConflictAction.Replace))
                throw new Exception("Applying ID3 Tags Failed");
        }
    }_
akdoroshenko commented 1 year ago

Try this approach: file.WriteTag(tag, Id3Version.V23, WriteConflictAction.Replace);