public static TagLib.File ConvertMusicFileTagToTag(MusicFileTag musicTag) { TagLib.File tagInfo = TagLib.File.Create(musicTag.FilePath); // V1 if (!musicTag.EnabledV1) { tagInfo.RemoveTags(TagTypes.Id3v1); } else { tagInfo.GetTag(TagTypes.Id3v1).Performers = new string[] { musicTag.ArtistV1 ?? "" }; tagInfo.GetTag(TagTypes.Id3v1).Genres = new string[] { musicTag.GenreV1 ?? "" }; tagInfo.GetTag(TagTypes.Id3v1).Album = musicTag.AlbumV1 ?? ""; tagInfo.GetTag(TagTypes.Id3v1).Title = musicTag.TitleV1 ?? ""; tagInfo.GetTag(TagTypes.Id3v1).Comment = musicTag.CommentV1 ?? ""; int.TryParse(musicTag.YearV1, out int tmpYearV1); if (tmpYearV1 > 0) { tagInfo.GetTag(TagTypes.Id3v1).Year = (uint)tmpYearV1; } int.TryParse(musicTag.TrackV1, out int tmpTrackV1); if (tmpTrackV1 > 0) { tagInfo.GetTag(TagTypes.Id3v1).Track = (uint)tmpTrackV1; } } // V2 if (!musicTag.EnabledV2) { tagInfo.RemoveTags(TagTypes.Id3v2); } else { TagLib.Id3v2.Tag id3v2Tag = tagInfo.GetTag(TagTypes.Id3v2) as TagLib.Id3v2.Tag; id3v2Tag.Version = Convert.ToByte(musicTag.VersionV2); id3v2Tag.SetTextFrame("TDLY", musicTag.PlayListDelayV2); id3v2Tag.SetTextFrame("TRCK", musicTag.TrackNumberV2); id3v2Tag.SetTextFrame("TPOS", musicTag.PartOfSetV2); id3v2Tag.SetTextFrame("TBPM", musicTag.BPMV2); id3v2Tag.SetTextFrame("TPE1", musicTag.ArtistV2); id3v2Tag.SetTextFrame("TCON", musicTag.GenreV2); id3v2Tag.SetTextFrame("TLAN", musicTag.LanguageV2); id3v2Tag.SetTextFrame("TKEY", musicTag.KeyV2); id3v2Tag.SetTextFrame("TSST", musicTag.SetSubtitleV2); id3v2Tag.SetTextFrame("TIT1", musicTag.ContentDescriptionV2); id3v2Tag.SetTextFrame("TMOO", musicTag.MoodV2); id3v2Tag.SetTextFrame("TPE4", musicTag.InterpretedV2); id3v2Tag.SetTextFrame("TSOA", musicTag.AlbumSortV2); id3v2Tag.SetTextFrame("TALB", musicTag.AlbumV2); id3v2Tag.SetTextFrame("TIT3", musicTag.SubtitleV2); id3v2Tag.SetTextFrame("TSOT", musicTag.TitleSortV2); id3v2Tag.SetTextFrame("TIT2", musicTag.TitleV2); /// Frames from /// http://id3.org/id3v2.3.0#Declared_ID3v2_frames } return(tagInfo); }
public static MusicFileTag ConvertTagToMusicFileTag(TagLib.Id3v1.Tag tagV1, TagLib.Id3v2.Tag tagV2, string filePath) { var tmp = new MusicFileTag { // File Details FilePath = filePath, FileName = Path.GetFileName(filePath), FileSize = Logic.BasicFunctions.FormatFileSize(new FileInfo(filePath).Length), }; // TagTypes TagTypes myTagTypes = TagLib.File.Create(filePath).TagTypesOnDisk; tmp.EnabledV1 = myTagTypes.ToString().ToLower().Contains("id3v1") ? true : false; tmp.EnabledV2 = myTagTypes.ToString().ToLower().Contains("id3v2") ? true : false; // V1 if (tmp.EnabledV1) { tmp.ArtistV1 = tagV1.FirstPerformer == null ? "" : tagV1.FirstPerformer.Replace("�", string.Empty); tmp.AlbumV1 = tagV1.Album == null ? "" : tagV1.Album.Replace("�", string.Empty); tmp.GenreV1 = tagV1.FirstGenre == null ? "" : tagV1.FirstGenre.Replace("�", string.Empty); tmp.YearV1 = tagV1.Year.ToString() == null ? "" : tagV1.Year.ToString().Replace("�", string.Empty); tmp.TitleV1 = tagV1.Title == null ? "" : tagV1.Title.Replace("�", string.Empty); tmp.CommentV1 = tagV1.Comment == null ? "" : tagV1.Comment.Replace("�", string.Empty); tmp.TrackV1 = tagV1.Track.ToString() == null ? "" : tagV1.Track.ToString().Replace("�", string.Empty); } // V2 if (tmp.EnabledV2) { tmp.VersionV2 = tagV2.Version.ToString(); // Version 3 tmp.PlayListDelayV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TDLY").Replace("�", string.Empty); tmp.TrackNumberV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TRCK").Replace("�", string.Empty); tmp.PartOfSetV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TPOS").Replace("�", string.Empty); tmp.BPMV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TBPM").Replace("�", string.Empty); tmp.ArtistV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TPE1").Replace("�", string.Empty); tmp.GenreV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TCON").Replace("�", string.Empty); tmp.LanguageV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TLAN").Replace("�", string.Empty); tmp.KeyV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TKEY").Replace("�", string.Empty); tmp.SetSubtitleV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TSST").Replace("�", string.Empty); tmp.ContentDescriptionV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TIT1").Replace("�", string.Empty); tmp.InterpretedV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TPE4").Replace("�", string.Empty); tmp.AlbumV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TALB").Replace("�", string.Empty); tmp.TitleV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TIT2").Replace("�", string.Empty); /// Frames from /// http://id3.org/id3v2.3.0#Declared_ID3v2_frames // + Version 4 if (tagV2.Version == 4) { tmp.TitleSortV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TSOT").Replace("�", string.Empty); tmp.AlbumSortV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TSOA").Replace("�", string.Empty); tmp.MoodV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TMOO").Replace("�", string.Empty); tmp.SubtitleV2 = Logic.TaggingLogic.GetTagContent(tagV2, "TIT3").Replace("�", string.Empty); } } else { tmp.VersionV2 = "4"; } return(tmp); }