/// <summary> /// Converts an ID3 tag to another version after resolving the differences between the two versions. The resultant tag /// will have all the frames from the source tag, but those frames not recognized in the new version will be treated as /// UnknownFrame objects. /// Similarly, frames recognized in the output tag version, but not in the source version are converted accordingly. /// </summary> /// <param name="version">Version of the tag to convert to.</param> /// <returns>The converted tag of the specified version, or null if there were any errors.</returns> public Id3Tag ConvertTo(Id3Version version) { //If the requested version is the same as this version, just return the same instance. if (Version == version) { return(this); } //Get the ID3 tag handlers for the destination and create a empty tag var destinationHandler = Id3Handler.GetHandler(version); Id3Tag destinationTag = destinationHandler.CreateTag(); foreach (Id3Frame sourceFrame in this) { if (sourceFrame is UnknownFrame unknownFrame) { string frameId = unknownFrame.Id; Id3Frame destinationFrame = destinationHandler.GetFrameFromFrameId(frameId); destinationTag.AddUntypedFrame(destinationFrame); } else { destinationTag.AddUntypedFrame(sourceFrame); } } return(destinationTag); }
public bool WriteTag(Id3Tag tag, WriteConflictAction conflictAction = WriteConflictAction.NoAction) { if (tag == null) { throw new ArgumentNullException(nameof(tag)); } EnsureWritePermissions(Mp3Messages.NoWritePermissions_CannotWriteTag); //If a tag already exists from the same family, but is a different version than the passed tag, //delete it if conflictAction is Replace. Id3Handler familyHandler = ExistingHandlers.FirstOrDefault(handler => handler.Family == tag.Family); if (familyHandler != null) { Id3Handler handler = familyHandler; if (handler.Version != tag.Version) { if (conflictAction == WriteConflictAction.NoAction) { return(false); } if (conflictAction == WriteConflictAction.Replace) { Id3Handler handlerCopy = handler; //TODO: Why did we need a copy of the handler? handlerCopy.DeleteTag(Stream); } } } //Write the tag to the file. The handler will know how to overwrite itself. Id3Handler writeHandler = Id3Handler.GetHandler(tag.Version); bool writeSuccessful = writeHandler.WriteTag(Stream, tag); if (writeSuccessful) { InvalidateExistingHandlers(); } return(writeSuccessful); }