/// <summary>Saves the tag to the specified path.</summary> /// <param name="path">The full path of the file.</param> private void Save(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } if (VorbisComment.VorbisComment.IsFlac(path)) { VorbisComment.VorbisComment vc = new VorbisComment.VorbisComment(path); vc.Title = Title; vc.Artist = Artist; vc.Album = Album; vc.Year = Year; vc.Genre = Genre; vc.TrackNumber = TrackNumber; vc.Comment = Comment; vc.Save(path); ID3v2Tag.RemoveTag(path); ID3v1Tag.RemoveTag(path); } else { ID3v2Tag id3v2 = new ID3v2Tag(path); id3v2.Title = Title; id3v2.Artist = Artist; id3v2.Album = Album; id3v2.Year = Year; id3v2.Genre = Genre; id3v2.TrackNumber = TrackNumber; if (id3v2.CommentsList.Count > 0) { id3v2.CommentsList[0].Description = Comment; } else { if (!string.IsNullOrEmpty(Comment)) { id3v2.CommentsList.AddNew().Description = Comment; } } ID3v1Tag id3v1 = new ID3v1Tag(path); id3v1.Title = Title; id3v1.Artist = Artist; id3v1.Album = Album; id3v1.Year = Year; id3v1.GenreIndex = GenreHelper.GetGenreIndex(Genre); id3v1.Comment = Comment; int trackNumber; if (int.TryParse(TrackNumber, out trackNumber)) { id3v1.TrackNumber = trackNumber; } else { // Handle ##/## format if (TrackNumber.Contains("/")) { if (int.TryParse(TrackNumber.Split('/')[0], out trackNumber)) { id3v1.TrackNumber = trackNumber; } else { id3v1.TrackNumber = 0; } } else { id3v1.TrackNumber = 0; } } id3v2.Save(path); id3v1.Save(path); } Read(path); }
/// <summary>Saves the tag to the specified path.</summary> /// <param name="path">The full path of the file.</param> private void Save(string path) { if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); if (VorbisComment.VorbisComment.IsFlac(path)) { VorbisComment.VorbisComment vc = new VorbisComment.VorbisComment(path); vc.Title = Title; vc.Artist = Artist; vc.Album = Album; vc.Year = Year; vc.Genre = Genre; vc.TrackNumber = TrackNumber; vc.Comment = Comment; vc.Save(path); ID3v2Tag.RemoveTag(path); ID3v1Tag.RemoveTag(path); } else { ID3v2Tag id3v2 = new ID3v2Tag(path); id3v2.Title = Title; id3v2.Artist = Artist; id3v2.Album = Album; id3v2.Year = Year; id3v2.Genre = Genre; id3v2.TrackNumber = TrackNumber; if (id3v2.CommentsList.Count > 0) { id3v2.CommentsList[0].Description = Comment; } else { if (!string.IsNullOrEmpty(Comment)) id3v2.CommentsList.AddNew().Description = Comment; } ID3v1Tag id3v1 = new ID3v1Tag(path); id3v1.Title = Title; id3v1.Artist = Artist; id3v1.Album = Album; id3v1.Year = Year; id3v1.GenreIndex = GenreHelper.GetGenreIndex(Genre); id3v1.Comment = Comment; int trackNumber; if (int.TryParse(TrackNumber, out trackNumber)) { id3v1.TrackNumber = trackNumber; } else { // Handle ##/## format if (TrackNumber.Contains("/")) { if (int.TryParse(TrackNumber.Split('/')[0], out trackNumber)) id3v1.TrackNumber = trackNumber; else id3v1.TrackNumber = 0; } else { id3v1.TrackNumber = 0; } } id3v2.Save(path); id3v1.Save(path); } Read(path); }
/// <summary>Reads the tag from the specified stream.</summary> /// <param name="stream">The stream.</param> private void Read(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } bool id3v2Found = false; string tagVersion = string.Empty; // ID3v2 if (ID3v2Tag.DoesTagExist(stream)) { ID3v2Tag id3v2 = new ID3v2Tag(stream); Title = id3v2.Title; Artist = id3v2.Artist; Album = id3v2.Album; Year = id3v2.Year; Genre = id3v2.Genre; TrackNumber = id3v2.TrackNumber; if (id3v2.CommentsList.Count > 0) { Comment = id3v2.CommentsList[0].Description; } else { Comment = null; } tagVersion = EnumUtils.GetDescription(id3v2.Header.TagVersion); id3v2Found = true; } // ID3v1 if (ID3v1Tag.DoesTagExist(stream)) { ID3v1Tag id3v1 = new ID3v1Tag(stream); // Use ID3v1 fields if ID3v2 doesn't exist or field is blank if (!id3v2Found || string.IsNullOrEmpty(Title)) { Title = id3v1.Title; } if (!id3v2Found || string.IsNullOrEmpty(Artist)) { Artist = id3v1.Artist; } if (!id3v2Found || string.IsNullOrEmpty(Album)) { Album = id3v1.Album; } if (!id3v2Found || string.IsNullOrEmpty(Year)) { Year = id3v1.Year; } if (!id3v2Found || string.IsNullOrEmpty(Genre)) { Genre = GenreHelper.GenreByIndex[id3v1.GenreIndex]; } if (!id3v2Found || string.IsNullOrEmpty(TrackNumber)) { TrackNumber = id3v1.TrackNumber.ToString(); } if (!id3v2Found || string.IsNullOrEmpty(Comment)) { Comment = id3v1.Comment; } tagVersion += (!string.IsNullOrEmpty(tagVersion) ? ", " : "") + EnumUtils.GetDescription(id3v1.TagVersion); } // Vorbis Comment if (VorbisComment.VorbisComment.IsFlac(stream)) { VorbisComment.VorbisComment vc = new VorbisComment.VorbisComment(stream); Title = vc.Title; Artist = vc.Artist; Album = vc.Album; Year = vc.Year; Genre = vc.Genre; TrackNumber = vc.TrackNumber; Comment = vc.Comment; tagVersion = (!string.IsNullOrEmpty(tagVersion) ? ", " : "") + "Vorbis Comment"; } // No tag found if (string.IsNullOrEmpty(tagVersion)) { Title = null; Artist = null; Album = null; Year = null; Genre = null; TrackNumber = null; Comment = null; tagVersion = "None"; } // Set TagVersion TagVersion = tagVersion; }
/// <summary>Reads the tag from the specified stream.</summary> /// <param name="stream">The stream.</param> private void Read(Stream stream) { if (stream == null) throw new ArgumentNullException("stream"); bool id3v2Found = false; string tagVersion = string.Empty; // ID3v2 if (ID3v2Tag.DoesTagExist(stream)) { ID3v2Tag id3v2 = new ID3v2Tag(stream); Title = id3v2.Title; Artist = id3v2.Artist; Album = id3v2.Album; Year = id3v2.Year; Genre = id3v2.Genre; TrackNumber = id3v2.TrackNumber; if (id3v2.CommentsList.Count > 0) Comment = id3v2.CommentsList[0].Description; else Comment = null; tagVersion = EnumUtils.GetDescription(id3v2.Header.TagVersion); id3v2Found = true; } // ID3v1 if (ID3v1Tag.DoesTagExist(stream)) { ID3v1Tag id3v1 = new ID3v1Tag(stream); // Use ID3v1 fields if ID3v2 doesn't exist or field is blank if (!id3v2Found || string.IsNullOrEmpty(Title)) Title = id3v1.Title; if (!id3v2Found || string.IsNullOrEmpty(Artist)) Artist = id3v1.Artist; if (!id3v2Found || string.IsNullOrEmpty(Album)) Album = id3v1.Album; if (!id3v2Found || string.IsNullOrEmpty(Year)) Year = id3v1.Year; if (!id3v2Found || string.IsNullOrEmpty(Genre)) Genre = GenreHelper.GenreByIndex[id3v1.GenreIndex]; if (!id3v2Found || string.IsNullOrEmpty(TrackNumber)) TrackNumber = id3v1.TrackNumber.ToString(); if (!id3v2Found || string.IsNullOrEmpty(Comment)) Comment = id3v1.Comment; tagVersion += (!string.IsNullOrEmpty(tagVersion) ? ", " : "") + EnumUtils.GetDescription(id3v1.TagVersion); } // Vorbis Comment if (VorbisComment.VorbisComment.IsFlac(stream)) { VorbisComment.VorbisComment vc = new VorbisComment.VorbisComment(stream); Title = vc.Title; Artist = vc.Artist; Album = vc.Album; Year = vc.Year; Genre = vc.Genre; TrackNumber = vc.TrackNumber; Comment = vc.Comment; tagVersion = (!string.IsNullOrEmpty(tagVersion) ? ", " : "") + "Vorbis Comment"; } // No tag found if (string.IsNullOrEmpty(tagVersion)) { Title = null; Artist = null; Album = null; Year = null; Genre = null; TrackNumber = null; Comment = null; tagVersion = "None"; } // Set TagVersion TagVersion = tagVersion; }