public bool WriteTag(Id3Tag tag, int majorVersion, int minorVersion, WriteConflictAction conflictAction = WriteConflictAction.NoAction) { tag.MajorVersion = majorVersion; tag.MinorVersion = minorVersion; return(WriteTag(tag, conflictAction)); }
/// <summary> /// Gets the rating from an Unknown frame. POPM /// </summary> /// <remarks> /// http://id3.org/id3v2.4.0-frames POPM /// </remarks> /// <param name="tag">The tag.</param> /// <returns></returns> private static void GetRating(SongTagFile song, Id3.Id3Tag tag) { var unknownFrames = (tag.Frames.Where(x => x.GetType() == typeof(UnknownFrame))); foreach (var item in unknownFrames) { var s = item as UnknownFrame; if (s.Id == "POPM") { //Byte Length 35 - if it includes a playcount //Byte Length 31 - No Playcount - Rating should be at the end of the array var f = s.Encode(); //Get rating byte depending on length byte[] ratings = new byte[1]; if (f.Length == 31) { ratings[0] = f[30]; } else if (f.Length == 35) { ratings[0] = f[f.Length - 5]; } song.Rating = ConvertRating(ratings[0]); return; } } }
/// <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); }
/// <summary> /// Creates a basic tag corresponding to the version of the handler. /// </summary> /// <returns>The basic ID3 tag.</returns> internal Id3Tag CreateTag() { var tag = new Id3Tag { Version = Version, Family = Family }; return(tag); }
internal Id3Tag CreateTag() { var tag = new Id3Tag { MajorVersion = MajorVersion, MinorVersion = MinorVersion, Family = Family }; return(tag); }
/// <summary> /// Returns a collection of all ID3 tags present in the MP3 data. /// </summary> /// <returns>A collection of all ID3 tags present in the MP3 data.</returns> public IEnumerable <Id3Tag> GetAllTags() { var tags = new Id3Tag[ExistingHandlers.Count]; for (int i = 0; i < tags.Length; i++) { Id3Handler handler = ExistingHandlers[i].Handler; tags[i] = handler.ReadTag(_stream); } return(tags); }
private string FireResolveMissingDataEvent(Id3Tag tag, Id3Frame frame, string sourceName) { EventHandler <ResolveMissingDataEventArgs> resolveMissingData = ResolveMissingData; if (resolveMissingData != null) { var args = new ResolveMissingDataEventArgs(tag, frame, sourceName); resolveMissingData(this, args); return(args.Value); } return(null); }
private RenamingEventArgs FireRenamingEvent(Id3Tag tag, string oldName, string newName) { EventHandler <RenamingEventArgs> renaming = Renaming; var args = new RenamingEventArgs(tag, oldName) { NewName = newName }; if (renaming != null) { renaming(this, args); } return(args); }
/// <summary> /// Gets (parses) the discog identifier from an <see cref="Id3.Id3Tag"/> /// </summary> /// <param name="tag">The tag.</param> /// <returns></returns> private Discog GetDiscogId(Id3.Id3Tag tag) { string _discogsId = "0"; Id3Frame discogsFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("DISCOGS_RELEASE_ID\0")).FirstOrDefault(); Discog discog = new Discog(); if (discogsFrame != null) { _discogsId = discogsFrame.ToString().ToUpper().Replace("DISCOGS_RELEASE_ID\0", ""); } else { //Try get release Id other search discogsFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("DISCOGSID\0")).FirstOrDefault(); if (discogsFrame != null) { _discogsId = discogsFrame.ToString().ToUpper().Replace("DISCOGSID\0", ""); } else { discogsFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("DISCOGS-ID\0")).FirstOrDefault(); if (discogsFrame == null) { return(discog); } _discogsId = discogsFrame.ToString().ToUpper().Replace("DISCOGS-ID\0", ""); } } if (!string.IsNullOrEmpty(_discogsId)) { int i; int.TryParse(_discogsId, out i); if (i != 0) { discog.ReleaseId = i; } else { discog.ReleaseId = 0; } } return(discog); }
/// <summary> /// Compares two tags based on their version details. /// </summary> /// <param name="other">The tag instance to compare against.</param> /// <returns>TODO:</returns> public int CompareTo(Id3Tag other) { if (other == null) { return(1); } int majorComparison = MajorVersion.CompareTo(other.MajorVersion); int minorComparison = MinorVersion.CompareTo(other.MinorVersion); if (majorComparison == 0 && minorComparison == 0) { return(0); } return(majorComparison != 0 ? majorComparison : minorComparison); }
/// <summary> /// Retrieves an ID3 tag of the specified tag family type - version 2.x or version 1.x. /// </summary> /// <param name="family">The ID3 tag family type required.</param> /// <returns>The ID3 tag of the specified tag family type, or null if it doesn't exist.</returns> public Id3Tag GetTag(Id3TagFamily family) { IEnumerable <RegisteredId3Handler> familyHandlers = ExistingHandlers.GetHandlers(family); RegisteredId3Handler familyHandler = familyHandlers.FirstOrDefault(); if (familyHandler == null) { return(null); } Id3Handler handler = familyHandler.Handler; Id3Tag tag = handler.ReadTag(_stream); return(tag); }
public bool WriteTag(Id3Tag tag, WriteConflictAction conflictAction = WriteConflictAction.NoAction) { EnsureWritePermissions(Id3Messages.NoWritePermissions_CannotWriteTag); if (tag == null) { throw new ArgumentNullException("tag"); } //The tag should specify major version number if (tag.MajorVersion == 0) { throw new ArgumentException(Id3Messages.MajorTagVersionMissing, "tag"); } //Get any existing handlers from the same family as the tag IEnumerable <RegisteredId3Handler> familyHandlers = ExistingHandlers.GetHandlers(tag.Family); //If a tag already exists from the same family, but is a different version than the passed tag, //delete it if conflictAction is Replace. RegisteredId3Handler familyHandler = familyHandlers.FirstOrDefault(); if (familyHandler != null) { Id3Handler handler = familyHandler.Handler; if (handler.MajorVersion != tag.MajorVersion || handler.MinorVersion != tag.MinorVersion) { if (conflictAction == WriteConflictAction.NoAction) { return(false); } if (conflictAction == WriteConflictAction.Replace) { Id3Handler handlerCopy = handler; handlerCopy.DeleteTag(_stream); } } } //Write the tag to the file. The handler will know how to overwrite itself. RegisteredId3Handler registeredHandler = RegisteredHandlers.GetHandler(tag.MajorVersion, tag.MinorVersion); bool writeSuccessful = registeredHandler.Handler.WriteTag(_stream, tag); if (writeSuccessful) { InvalidateExistingHandlers(); } return(writeSuccessful); }
public static Id3Tag Merge(params Id3Tag[] tags) { if (tags.Length == 0) { throw new ArgumentNullException("tags", "Specify 2 or more tags to merge"); } if (tags.Length == 1) { return(tags[0]); } var tag = new Id3Tag(); tag.MergeWith(tags); return(tag); }
/// <summary> /// Gets the country name from the file tag /// </summary> /// <param name="tag">The tag.</param> /// <returns></returns> private string GetCountry(Id3.Id3Tag tag) { Id3Frame countryFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("DISCOGS_COUNTRY\0")).FirstOrDefault(); if (countryFrame != null) { return(countryFrame.ToString().ToUpper().Replace("DISCOGS_COUNTRY\0", "")); } else { countryFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("COUNTRY\0")).FirstOrDefault(); if (countryFrame != null) { return(countryFrame.ToString().ToUpper().Replace("COUNTRY\0", "")); } } return(null); }
protected override Id3Tag[] GetTagInfo(Id3Tag tag) { string filename = Path.GetFileNameWithoutExtension(Inputs.FileName); if (filename == null) { return(Id3Tag.Empty); } string[] breakup = filename.Split(new[] { " - " }, StringSplitOptions.None); if (breakup.Length <= 1) { return(Id3Tag.Empty); } var result = new Id3Tag(); result.Artists.Value.Add(breakup[0].Trim()); result.Title.Value = breakup[1].Trim(); return(new[] { result }); }
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); }
/// <summary> /// Retrieves either ID3V2 or Id3V1 tag /// </summary> /// <param name="mp3Stream">The MP3 stream.</param> /// <returns></returns> /// <exception cref="NullReferenceException"></exception> private Id3Tag GetTagFromStream(Mp3Stream mp3Stream) { Id3.Id3Tag tag = null; //Exit, we have no tags if (!mp3Stream.HasTags) { return(tag); } //Get the file start tag which is ID3v2 tag = mp3Stream.GetTag(Id3TagFamily.FileStartTag); if (tag == null) { tag = mp3Stream.GetTag(Id3TagFamily.FileEndTag); if (tag == null) { throw new NullReferenceException($"Couldn't parse tag for file"); } } //Get the file start tag which is ID3v2 else if (!tag.Year.IsAssigned) { var tagForYear = mp3Stream.GetTag(Id3TagFamily.FileEndTag); if (tagForYear != null) { if (tagForYear.Year.IsAssigned) { var year = tagForYear.Year.AsDateTime.Value; tag.Year.Value = Convert.ToString(year.Year); } } } return(tag); }
//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. public Id3Tag ConvertTo(int majorVersion, int minorVersion) { if (MajorVersion == majorVersion && MinorVersion == minorVersion) { return(this); } RegisteredId3Handler sourceHandler = Mp3Stream.RegisteredHandlers.GetHandler(MajorVersion, MinorVersion); if (sourceHandler == null) { return(null); } RegisteredId3Handler destinationHandler = Mp3Stream.RegisteredHandlers.GetHandler(majorVersion, minorVersion); if (destinationHandler == null) { return(null); } Id3Tag destinationTag = destinationHandler.Handler.CreateTag(); foreach (Id3Frame sourceFrame in Frames) { var unknownFrame = sourceFrame as UnknownFrame; if (unknownFrame != null) { string frameId = unknownFrame.Id; Id3Frame destinationFrame = destinationHandler.Handler.GetFrameFromFrameId(frameId); destinationTag.Frames.Add(destinationFrame); } else { destinationTag.Frames.Add(sourceFrame); } } return(destinationTag); }
private IEnumerable <T> RenameOrSuggest <T>(IEnumerable <string> filePaths) where T : RenameAction, new() { foreach (string filePath in filePaths) { if (string.IsNullOrWhiteSpace(filePath)) { yield return(new T { Directory = filePath, OriginalName = filePath, Status = RenameStatus.Error, ErrorMessage = Id3FileMessages.InvalidFilePath }); continue; } if (!File.Exists(filePath)) { yield return(new T { Directory = filePath, OriginalName = filePath, Status = RenameStatus.Error, ErrorMessage = Id3FileMessages.MissingFile }); continue; } var result = new T { Directory = Path.GetDirectoryName(filePath), OriginalName = Path.GetFileName(filePath) }; using (var mp3 = new Mp3File(filePath)) { Id3Tag tag = mp3.GetTag(2, 3); if (tag == null) { result.Status = RenameStatus.Error; result.ErrorMessage = Id3FileMessages.MissingId3v23TagInFile; yield return(result); continue; } string missingFrameName = null; string newName = FramePlaceholderPattern.Replace(_pattern, match => { string frameName = match.Groups[1].Value; string frameNameKey = frameName.ToLowerInvariant(); PropertyInfo frameProperty = _mapping[frameNameKey]; var frame = (Id3Frame)frameProperty.GetValue(tag, null); string frameValue = frame.IsAssigned ? frame.ToString() : FireResolveMissingDataEvent(tag, frame, result.OriginalName); if (string.IsNullOrEmpty(frameValue)) { missingFrameName = frameName; } return(frameValue); }); if (missingFrameName != null) { result.Status = RenameStatus.Error; result.ErrorMessage = string.Format(Id3FileMessages.MissingDataForFrame, missingFrameName); yield return(result); continue; } result.NewName = newName + ".mp3"; RenamingEventArgs renamingEventResult = FireRenamingEvent(tag, result.OriginalName, result.NewName); if (renamingEventResult.Cancel) { result.Status = RenameStatus.Cancelled; } else { result.NewName = renamingEventResult.NewName; } if (result.OriginalName.Equals(result.NewName, StringComparison.Ordinal)) { result.Status = RenameStatus.CorrectlyNamed; } yield return(result); } } }
public bool Equals(Id3Tag other) { return(MajorVersion == other.MajorVersion && MinorVersion == other.MinorVersion); }
internal RenamingEventArgs(Id3Tag tag, string oldName) { _tag = tag; _oldName = oldName; }
public bool WriteTag(Id3Tag tag, Id3Version version, WriteConflictAction conflictAction = WriteConflictAction.NoAction) { tag.Version = version; return(WriteTag(tag, conflictAction)); }
internal ResolveMissingDataEventArgs(Id3Tag tag, Id3Frame frame, string sourceName) { _tag = tag; _frame = frame; _sourceName = sourceName; }
internal abstract bool WriteTag(Stream stream, Id3Tag tag);
public bool UpdateTag(Id3Tag tag) { return(WriteTag(tag, WriteConflictAction.Replace)); }