public void CueSheetVorbisCommentShouldBeInProperty() { var cueSheetPath = Path.Combine("Data", "cuesheet.txt"); var cueSheetData = File.ReadAllText(cueSheetPath); string origFile = Path.Combine("Data", "testfile5.flac"); string newFile = Path.Combine("Data", "testfile5_temp.flac"); FileHelper.GetNewFile(origFile, newFile); using (FlacFile file = new FlacFile(Path.Combine("Data", "testfile5_temp.flac"))) { var vorbisComment = new VorbisComment(); vorbisComment["CUESHEET"] = new VorbisCommentValues(cueSheetData); file.Metadata.Add(vorbisComment); file.Save(); } using (FlacFile file = new FlacFile(Path.Combine("Data", "testfile5_temp.flac"))) { var cueSheetDataFromFile = file.VorbisComment.CueSheet; Assert.AreEqual(cueSheetData, cueSheetDataFromFile.Value); } }
public void WritingTwoArtistsShouldResultInTwoArtistsRead() { string origFile = Path.Combine("Data", "testfile5.flac"); string newFile = Path.Combine("Data", "testfile5_temp.flac"); FileHelper.GetNewFile(origFile, newFile); using (FlacFile file = new FlacFile(Path.Combine("Data", "testfile5_temp.flac"))) { var vorbisComment = new VorbisComment(); vorbisComment["ARTIST"] = new VorbisCommentValues(new string[] { "Artist A", "Artist B" }); file.Metadata.Add(vorbisComment); file.Save(); } using (FlacFile file = new FlacFile(Path.Combine("Data", "testfile5_temp.flac"))) { Assert.IsNotNull(file.VorbisComment); var artistValues = file.VorbisComment["ARTIST"]; Assert.AreEqual(2, artistValues.Count); Assert.AreEqual("Artist A", artistValues[0]); Assert.AreEqual("Artist B", artistValues[1]); } }
/// <summary> /// parsing data for Tags /// </summary> private static void P(Track t, VorbisCommentValues value, Action <Track, string> todo) { if (value != null && !string.IsNullOrEmpty(value.Value)) { todo(t, value.Value); } }
public void RemoveMethodShouldRemoveTag() { var vorbisComment = new VorbisComment(); vorbisComment["ARTIST"] = new VorbisCommentValues("Aaron"); vorbisComment.Remove("ARTIST"); Assert.IsTrue(vorbisComment.Artist.Count == 0, "Tag was not removed."); }
public static void AddTagIfNotNull(VorbisComment comments, string key, params string[] values) { if (values == null || values.Any(string.IsNullOrWhiteSpace)) { return; } comments[key] = new VorbisCommentValues(values); }
public void RemoveMethodShouldRemoveTagWithSpecificValue() { var vorbisComment = new VorbisComment(); vorbisComment["ARTIST"] = new VorbisCommentValues("Aaron"); vorbisComment.Remove("ARTIST", "Lenoir"); Assert.IsTrue(vorbisComment.Artist.Count == 1, "Tag was removed when it shouldn't have been."); vorbisComment.Remove("ARTIST", "Aaron"); Assert.IsTrue(vorbisComment.Artist.Count == 0, "Tag was not removed."); }
// just in case i find a way to get the album cover working in taglib /*private bool WriteFlacMetadata() * { * using (TagLib.File file = TagLib.File.Create(_filepath, "taglib/flac", ReadStyle.Average)) * { * var tags = (TagLib.Ogg.XiphComment) file.GetTag(TagTypes.Xiph); * * if (_trackInfo.TrackTags != null) * { * tags.Title = _trackInfo.TrackTags.Title; * tags.Performers = _trackInfo.TrackTags.Artists.Select(x => x.Name).ToArray(); * tags.Composers = _trackInfo.TrackTags.Contributors.Composers; * tags.SetField("LENGTH", _trackInfo.TrackTags.Length); * tags.SetField("ISRC", _trackInfo.TrackTags.Isrc); * tags.SetField("EXPLICIT", _trackInfo.TrackTags.ExplicitLyrics ?? "0"); * tags.SetField("REPLAYGAIN_TRACK_GAIN", _trackInfo.TrackTags.Gain); * tags.SetField("PRODUCER", _trackInfo.TrackTags.Contributors.Producers); * tags.SetField("ENGINEER", _trackInfo.TrackTags.Contributors.Engineers); * tags.SetField("MIXER", _trackInfo.TrackTags.Contributors.Mixers); * tags.SetField("WRITER", _trackInfo.TrackTags.Contributors.Writers); * tags.SetField("AUTHOR", _trackInfo.TrackTags.Contributors.Authors); * tags.SetField("PUBLISHER", _trackInfo.TrackTags.Contributors.Publishers); * * * if (_trackInfo.TrackTags.TrackNumber != null && * uint.TryParse(_trackInfo.TrackTags.TrackNumber, out uint trackNumber)) * { * tags.Track = trackNumber; * } * * if (_trackInfo.TrackTags.DiscNumber != null && * uint.TryParse(_trackInfo.TrackTags.DiscNumber, out uint discNumber)) * { * tags.Disc = discNumber; * } * * if (_trackInfo.TrackTags.Bpm != null && * uint.TryParse(_trackInfo.TrackTags.Bpm, out uint bpm)) * { * tags.BeatsPerMinute = bpm; * } * } * * if (_albumInfo.AlbumTags != null) * { * tags.Album = _albumInfo.AlbumTags.Title; * tags.AlbumArtists = _albumInfo.AlbumTags.Artists.Select(x => x.Name).ToArray(); * tags.Genres = _albumInfo.AlbumTags.Genres.GenreData.Select(x => x.Name).ToArray(); * * string year = _albumInfo.AlbumTags.ReleaseDate; * * if (!string.IsNullOrWhiteSpace(year)) * { * string[] yearSplit = year.Split("-"); * * if (yearSplit[0].Length == 4 && uint.TryParse(yearSplit[0], out uint yearParsed)) * { * tags.Year = yearParsed; * } * } * * tags.Copyright = _albumInfo.AlbumTags.Copyright; * tags.SetField("MEDIA", "Digital Media"); * tags.SetField("ORIGINALDATE", _albumInfo.AlbumTags.ReleaseDate); * tags.SetField("UPC", _albumInfo.AlbumTags.Upc); * tags.SetField("LABEL", _albumInfo.AlbumTags.Label); * * if (_albumInfo.AlbumTags.NumberOfTracks != null && * uint.TryParse(_albumInfo.AlbumTags.NumberOfTracks, out uint numberOfTracks)) * { * tags.TrackCount = numberOfTracks; * } * * if (_albumInfo.AlbumTags.NumberOfDiscs != null && * uint.TryParse(_albumInfo.AlbumTags.NumberOfDiscs, out uint numberOfDiscs)) * { * tags.DiscCount = numberOfDiscs; * } * } * * if (_trackInfo.Lyrics != null) * { * tags.Lyrics = _trackInfo.Lyrics.UnSyncedLyrics; * WriteLyricsFile(); * } * * try * { * file.Save(); * } * catch (IOException ex) * { * Console.WriteLine(ex.Message); * return false; * } * } * * // this takes 800-900ms... why can't you just work taglib * using (var file = new FlacFile(_filepath)) * { * if (_coverBytes.Length > 0) * { * var coverArt = new FlacLibSharp.Picture() * { * Description = "Cover", * ColorDepth = 8, * Data = _coverBytes, * Height = 1400, * Width = 1400, * MIMEType = MediaTypeNames.Image.Jpeg, * PictureType = FlacLibSharp.PictureType.CoverFront * }; * * file.Metadata.Add(coverArt); * } * * try * { * file.Save(); * } * catch (Exception ex) * { * Console.WriteLine(ex.Message); * return false; * } * } * * return true; * }*/ private bool WriteFlacMetadata() { using (var file = new FlacFile(_filepath)) { var comments = new VorbisComment(); if (_coverBytes.Length > 0) { var coverArt = new FlacLibSharp.Picture { Description = "Cover", ColorDepth = 8, Data = _coverBytes, Height = 1400, Width = 1400, MIMEType = MediaTypeNames.Image.Jpeg, PictureType = FlacLibSharp.PictureType.CoverFront }; file.Metadata.Add(coverArt); } comments["Media"] = new VorbisCommentValues("Digital Media"); if (_albumInfo?.AlbumTags != null) { comments.Album = new VorbisCommentValues(_albumInfo.AlbumTags.Title ?? ""); if (_albumInfo.AlbumTags?.Genres?.GenreData != null) { comments.Genre = new VorbisCommentValues(_albumInfo.AlbumTags.Genres.GenreData.Select(x => x.Name)); } string year = _albumInfo.AlbumTags.ReleaseDate ?? ""; if (!string.IsNullOrWhiteSpace(year)) { var yearSplit = year.Split("-"); if (yearSplit[0].Length == 4) { year = yearSplit[0]; } } if (_albumInfo.AlbumTags.Type == "Compilation" || _albumInfo.AlbumTags.Type == "Playlist") { comments["COMPILATION"] = new VorbisCommentValues("1"); } comments.Date = new VorbisCommentValues(year ?? ""); comments["ORIGINALDATE"] = new VorbisCommentValues(_albumInfo.AlbumTags.ReleaseDate ?? ""); comments["TRACKTOTAL"] = new VorbisCommentValues(_albumInfo.AlbumTags.NumberOfTracks ?? ""); comments["DISCTOTAL"] = new VorbisCommentValues(_albumInfo.AlbumTags.NumberOfDiscs ?? ""); comments["COPYRIGHT"] = new VorbisCommentValues(_albumInfo.AlbumTags.Copyright ?? ""); comments["UPC"] = new VorbisCommentValues(_albumInfo.AlbumTags.Upc ?? ""); comments["LABEL"] = new VorbisCommentValues(_albumInfo.AlbumTags.Label ?? ""); comments["ALBUMARTIST"] = new VorbisCommentValues(_albumInfo.AlbumTags.Artists.Select(x => x.Name)); } if (_trackInfo?.TrackTags != null) { comments.Title = new VorbisCommentValues(_trackInfo.TrackTags.Title ?? ""); comments.Artist = new VorbisCommentValues(_trackInfo.TrackTags.Artists.Select(x => x.Name)); comments["DISCNUMBER"] = new VorbisCommentValues(_trackInfo.TrackTags.DiscNumber ?? ""); comments["TRACKNUMBER"] = new VorbisCommentValues(_trackInfo.TrackTags.TrackNumber ?? ""); comments["BPM"] = new VorbisCommentValues(_trackInfo.TrackTags.Bpm ?? ""); comments["LENGTH"] = new VorbisCommentValues(_trackInfo.TrackTags.Length ?? ""); comments["ISRC"] = new VorbisCommentValues(_trackInfo.TrackTags.Isrc ?? ""); comments["EXPLICIT"] = new VorbisCommentValues(_trackInfo.TrackTags.ExplicitLyrics ?? "0"); comments["REPLAYGAIN_TRACK_GAIN"] = new VorbisCommentValues(_trackInfo.TrackTags.Gain ?? ""); MetadataHelpers.AddTagIfNotNull(comments, "COMPOSER", _trackInfo.TrackTags.Contributors.Composers); MetadataHelpers.AddTagIfNotNull(comments, "PUBLISHER", _trackInfo.TrackTags.Contributors.Publishers); MetadataHelpers.AddTagIfNotNull(comments, "PRODUCER", _trackInfo.TrackTags.Contributors.Producers); MetadataHelpers.AddTagIfNotNull(comments, "ENGINEER", _trackInfo.TrackTags.Contributors.Engineers); MetadataHelpers.AddTagIfNotNull(comments, "WRITER", _trackInfo.TrackTags.Contributors.Writers); MetadataHelpers.AddTagIfNotNull(comments, "MIXER", _trackInfo.TrackTags.Contributors.Mixers); MetadataHelpers.AddTagIfNotNull(comments, "AUTHOR", _trackInfo.TrackTags.Contributors.Authors); } if (_trackInfo?.Lyrics != null) { comments["Lyrics"] = new VorbisCommentValues(_trackInfo.Lyrics.UnSyncedLyrics ?? ""); WriteLyricsFile(); } file.Metadata.Add(comments); try { file.Save(); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } } return(true); }