public static ID3v1Tag FromV2Tag(TagBase tag, IGenreManager genreManager) { if (tag == null) { throw new ArgumentNullException("tag", "Argument 'tag' can not be null."); } ID3v1Tag information = new ID3v1Tag(); Frames.AlbumTextFrame album = tag.SearchForFrame(typeof(Frames.AlbumTextFrame)) as Frames.AlbumTextFrame; Frames.ArtistTextFrame artist = tag.SearchForFrame(typeof(Frames.ArtistTextFrame)) as Frames.ArtistTextFrame; Frames.CommentExtendedTextFrame comment = tag.SearchForFrame(typeof(Frames.CommentExtendedTextFrame)) as Frames.CommentExtendedTextFrame; Frames.GenreTextFrame genre = tag.SearchForFrame(typeof(Frames.GenreTextFrame)) as Frames.GenreTextFrame; Frames.ReleaseTimeTextFrame releaseTime = tag.SearchForFrame(typeof(Frames.ReleaseTimeTextFrame)) as Frames.ReleaseTimeTextFrame; Frames.TitleTextFrame title = tag.SearchForFrame(typeof(Frames.TitleTextFrame)) as Frames.TitleTextFrame; Frames.TrackTextFrame track = tag.SearchForFrame(typeof(Frames.TrackTextFrame)) as Frames.TrackTextFrame; Frames.YearTextFrame year = tag.SearchForFrame(typeof(Frames.YearTextFrame)) as Frames.YearTextFrame; if (album != null) { information.Album = album.Text; } if (artist != null) { information.Artist = album.Text; } if (comment != null) { information.Comment = album.Text; } if (genre != null) { information.GenreCode = genreManager.GetGenreCode(genre.Text); } if (releaseTime != null) { information.Year = releaseTime.Text.Substring(0, 4); } else if (year != null) { information.Year = year.Text; } if (track != null) { information.TrackNumber = (byte)track.TrackNumber; } if (title != null) { information.Title = title.Text; } return(information); }
/// <summary> /// Writes the information provided in the TagInformation object /// to the given FileStream in ID3v1.1 format. /// </summary> /// <param name="stream">The FileStream that can read and write to the file. Must also support seeking.</param> /// <param name="tag">The TagInformation object containing the tag information. </param> private void WriteToFile(FileStream stream) { if (!(stream.CanRead && stream.CanSeek && stream.CanSeek)) { throw (new ArgumentException("The passed Stream object must support seeking, reading and writing.")); } ID3v1Tag tagInformation = new ID3v1Tag(); if (!tagInformation.ReadFromFile(stream)) { stream.Seek(stream.Length, SeekOrigin.Begin); } else { stream.Seek(stream.Length - 128, SeekOrigin.Begin); } stream.Write(Encoding.GetEncoding("ISO-8859-1").GetBytes("TAG"), 0, 3); byte[] b = new byte[0]; if (Title != "") { b = Encoding.GetEncoding("ISO-8859-1").GetBytes(Title); } for (int i = 0; i < 30; i++) { if (i < b.Length) { stream.WriteByte(b[i]); } else { stream.WriteByte(0); } } b = new byte[0]; if (Artist != "") { b = Encoding.GetEncoding("ISO-8859-1").GetBytes(Artist); } for (int i = 0; i < 30; i++) { if (i < b.Length) { stream.WriteByte(b[i]); } else { stream.WriteByte(0); } } b = new byte[0]; if (Album != "") { b = Encoding.GetEncoding("ISO-8859-1").GetBytes(Album); } for (int i = 0; i < 30; i++) { if (i < b.Length) { stream.WriteByte(b[i]); } else { stream.WriteByte(0); } } b = new byte[0]; if (Year != "") { b = Encoding.GetEncoding("ISO-8859-1").GetBytes(Year); } for (int i = 0; i < 4; i++) { if (i < b.Length) { stream.WriteByte(b[i]); } else { stream.WriteByte(0); } } b = new byte[0]; if (Comment != "") { b = Encoding.GetEncoding("ISO-8859-1").GetBytes(Comment); } for (int i = 0; i < 28; i++) { if (i < b.Length) { stream.WriteByte(b[i]); } else { stream.WriteByte(0); } } stream.WriteByte(0); stream.WriteByte(TrackNumber); stream.WriteByte(GenreCode); }