public void TestPicturesAreCopied() { string tempFile = TestPath.Samples + "tmpwrite_sample_v2_only.mp3"; System.IO.File.Copy(sample_file, tempFile, true); // Put a picture on the starting file File file = TagLib.File.Create(tempFile); var picture = new Picture(TestPath.Samples + "sample_gimp.gif") { Type = PictureType.BackCover, Description = "TEST description 1" }; file.Tag.Pictures = new[] { picture }; file.Save(); Assert.IsTrue(file.Tag.Pictures.Count() == 1, "File should start with 1 picture"); // Now construct a new tag with a title, APIC and GEOB frame var tag = new TagLib.Id3v2.Tag(); tag.Title = "FOOBAR"; // single red dot (1x1 px red image) APIC frame found in wild var redDot = new byte[] { 65, 80, 73, 67, 0, 0, 0, 155, 0, 0, 0, 105, 109, 97, 103, 101, 47, 112, 110, 103, 0, 3, 0, 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 2, 0, 0, 0, 144, 119, 83, 222, 0, 0, 0, 4, 103, 65, 77, 65, 0, 0, 177, 143, 11, 252, 97, 5, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 18, 0, 0, 11, 18, 1, 210, 221, 126, 252, 0, 0, 0, 24, 116, 69, 88, 116, 83, 111, 102, 116, 119, 97, 114, 101, 0, 112, 97, 105, 110, 116, 46, 110, 101, 116, 32, 52, 46, 49, 46, 53, 100, 71, 88, 82, 0, 0, 0, 12, 73, 68, 65, 84, 24, 87, 99, 248, 47, 162, 0, 0, 3, 73, 1, 52, 163, 224, 5, 179, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130 }; var pictureFrame = new TagLib.Id3v2.AttachmentFrame(redDot, 3); var geobFrame = new TagLib.Id3v2.AttachmentFrame(); geobFrame.MimeType = "plain/text"; geobFrame.Description = "random"; geobFrame.Type = PictureType.NotAPicture; geobFrame.Data = "random text in geob"; tag.AddFrame(pictureFrame); tag.AddFrame(geobFrame); tag.CopyTo(file.Tag, false); Assert.AreEqual("MP3 title", file.Tag.Title, "Title shouldn't be copied if overwrite=false"); Assert.AreEqual(1, file.Tag.Pictures.Count(), "GEOB/APIC frames shouldn't be copied if overwrite=false"); tag.CopyTo(file.Tag, true); Assert.AreEqual(tag.Title, file.Tag.Title, "Title wasn't copied"); Assert.AreEqual(tag.Pictures.Count(), file.Tag.Pictures.Count(), "GEOB/APIC frames weren't copied"); }
public Mp3Tagger(string path, bool write = true) { f = Utils.OpenFile(path, write); t = (TagLib.Id3v2.Tag)f.TagLibFile.GetTag(TagLib.TagTypes.Id3v2, true); if (t == null) { SafeClose(); throw new HaException("MP3 file has no Id3v2 tag"); } // ID3 is lame as hell, doesnt actually have textual description for tags (unlike beast-ass Xiph) valueTags.Add("album", FixString(t.Album)); valueTags.Add("albumartist", FixString(Aggregate(t.AlbumArtists))); valueTags.Add("artist", FixString(Aggregate(t.Performers))); valueTags.Add("year", t.Year.ToString()); IEnumerable <TagLib.Id3v2.UserTextInformationFrame> frames = t.GetFrames <TagLib.Id3v2.UserTextInformationFrame>().Where(x => x.Description.ToLower() == "hatag"); if (frames.Count() > 0) { utif = frames.ElementAt(0); string data = utif.Text.Length == 0 ? "" : utif.Text[0]; data.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(x => tags.Add(x)); } else { utif = new TagLib.Id3v2.UserTextInformationFrame("hatag"); utif.TextEncoding = TagLib.StringType.Latin1; t.AddFrame(utif); } }
/// <summary> /// Schreibt die Metadata des übergebenen Songs /// </summary> /// <param name="song"></param> /// <returns></returns> public static Boolean WriteMetaData(MP3File song) { String orgrating = String.Empty; try { //Taglib Objekt erstellen TagLib.File taglibobjekt = TagLib.File.Create(song.Pfad); //taglibobjekt.Tag.Genres[0] = song.Genre; switch (taglibobjekt.MimeType) { case "taglib/flac": case "taglib/m4a": if (song.Bewertung != "No") { //MM Behandelt Bomben bei FLAc anders als bei MP3 //Beim setzten wird hier nun -1 auf 0 gesetzt und 0 als nicht vorhanden. orgrating = song.Bewertung; //Für ein Catch den alten wert merken. if (song.Bewertung == "0") { song.Bewertung = ""; } if (song.Bewertung == "-1") { song.Bewertung = "0"; } taglibobjekt.Tag.Rating = song.Bewertung; } taglibobjekt.Tag.Mood = song.Stimmung.ToString(); taglibobjekt.Tag.Occasion = song.Gelegenheit.ToString(); taglibobjekt.Tag.Tempo = song.Geschwindigkeit.ToString().Replace("_", " "); taglibobjekt.Tag.MMCustom1 = song.BewertungMine; taglibobjekt.Tag.MMCustom2 = (song.Aufwecken) ? "Aufwecken" : String.Empty; taglibobjekt.Tag.MMCustom3 = (song.ArtistPlaylist) ? "true" : String.Empty; break; case "taglib/mp3": TagLib.Id3v2.Tag id3v2tag = taglibobjekt.GetTag(TagLib.TagTypes.Id3v2) as TagLib.Id3v2.Tag; #region Rating if (song.Bewertung != "No") { TagLib.Id3v2.PopularimeterFrame popm = TagLib.Id3v2.PopularimeterFrame.Get(id3v2tag, "no@email", true); //Das Rating wurde entfernt oder gesetzt if (song.Bewertung == "0") { if (id3v2tag != null) { id3v2tag.RemoveFrame(popm); } } else { int ratingval = 0; //Bombe if (song.Bewertung == "10") //0,5 { ratingval = 30; } if (song.Bewertung == "20") //1 { ratingval = 45; } if (song.Bewertung == "30") //1,5 { ratingval = 55; } if (song.Bewertung == "40") //2 { ratingval = 100; } if (song.Bewertung == "50") //2,5 { ratingval = 120; } if (song.Bewertung == "60") //3 { ratingval = 153; } if (song.Bewertung == "70") //3,5 { ratingval = 180; } if (song.Bewertung == "80") //4 { ratingval = 202; } if (song.Bewertung == "90") //4,5 { ratingval = 245; } if (song.Bewertung == "100") //5 { ratingval = 253; } popm.Rating = Convert.ToByte(ratingval); } } #endregion Rating #region Gelegenenheiten /*Ermitteln und ändern falls vorhanden. Andernfalls neu generien*/ if (id3v2tag != null) { IEnumerable <TagLib.Id3v2.Frame> comm = id3v2tag.GetFrames("COMM"); Boolean setgelegenheit = false; Boolean setgeschwindigkeit = false; Boolean setstimmung = false; Boolean aufwecken = false; Boolean artisplaylist = false; Boolean setratingMine = false; // Boolean ratingmine = false; foreach (var b in comm) { string des = ((TagLib.Id3v2.CommentsFrame)b).Description; switch (des) { case "MusicMatch_Situation": case "Songs-DB_Occasion": ((TagLib.Id3v2.CommentsFrame)b).Text = song.Gelegenheit.ToString(); setgelegenheit = true; break; case "MusicMatch_Tempo": case "Songs-DB_Tempo": ((TagLib.Id3v2.CommentsFrame)b).Text = song.Geschwindigkeit.ToString().Replace("_", " "); setgeschwindigkeit = true; break; case "MusicMatch_Mood": case "Songs-DB_Mood": ((TagLib.Id3v2.CommentsFrame)b).Text = song.Stimmung.ToString(); setstimmung = true; break; case "Songs-DB_Custom2": ((TagLib.Id3v2.CommentsFrame)b).Text = song.Aufwecken ? "Aufwecken" : ""; aufwecken = true; break; case "Songs-DB_Custom3": ((TagLib.Id3v2.CommentsFrame)b).Text = song.ArtistPlaylist ? "true" : ""; artisplaylist = true; break; case "Songs-DB_Custom1": ((TagLib.Id3v2.CommentsFrame)b).Text = song.BewertungMine; setratingMine = true; break; } } //Ende foreach if (!aufwecken && song.Aufwecken) { TagLib.Id3v2.CommentsFrame mms = new TagLib.Id3v2.CommentsFrame("Songs-DB_Custom2", "xxx") { Text = "Aufwecken" }; id3v2tag.AddFrame(mms); } if (!artisplaylist && song.ArtistPlaylist) { TagLib.Id3v2.CommentsFrame mms = new TagLib.Id3v2.CommentsFrame("Songs-DB_Custom3", "xxx") { Text = "true" }; id3v2tag.AddFrame(mms); } if (!setratingMine) { TagLib.Id3v2.CommentsFrame mms = new TagLib.Id3v2.CommentsFrame("Songs-DB_Custom1", "xxx") { Text = song.BewertungMine }; id3v2tag.AddFrame(mms); } if (!setgelegenheit) { TagLib.Id3v2.CommentsFrame mms = new TagLib.Id3v2.CommentsFrame("MusicMatch_Situation", "xxx"); TagLib.Id3v2.CommentsFrame sdo = new TagLib.Id3v2.CommentsFrame("Songs-DB_Occasion", "xxx"); mms.Text = song.Gelegenheit.ToString(); sdo.Text = song.Gelegenheit.ToString(); id3v2tag.AddFrame(mms); id3v2tag.AddFrame(sdo); } if (!setgeschwindigkeit) { TagLib.Id3v2.CommentsFrame mms = new TagLib.Id3v2.CommentsFrame("MusicMatch_Tempo", "xxx"); TagLib.Id3v2.CommentsFrame sdo = new TagLib.Id3v2.CommentsFrame("Songs-DB_Tempo", "xxx"); mms.Text = song.Geschwindigkeit.ToString().Replace("_", " "); sdo.Text = song.Geschwindigkeit.ToString().Replace("_", " "); id3v2tag.AddFrame(mms); id3v2tag.AddFrame(sdo); } if (!setstimmung) { TagLib.Id3v2.CommentsFrame mms = new TagLib.Id3v2.CommentsFrame("MusicMatch_Mood", "xxx"); TagLib.Id3v2.CommentsFrame sdo = new TagLib.Id3v2.CommentsFrame("Songs-DB_Mood", "xxx"); mms.Text = song.Stimmung.ToString(); sdo.Text = song.Stimmung.ToString(); id3v2tag.AddFrame(mms); id3v2tag.AddFrame(sdo); } } #endregion Gelegenheiten break; } taglibobjekt.Save(); taglibobjekt.Dispose(); //For Debuging /* * StringBuilder sb = new StringBuilder(); * sb.AppendLine("Update Done"); * sb.AppendLine(lied.Pfad); * sb.AppendLine(lied.Bewertung); * using (StreamWriter outfile = new StreamWriter(@"C:\done.txt")) * { * outfile.Write(sb.ToString()); * * } */ return(true); } catch { if (!String.IsNullOrEmpty(orgrating)) { song.Bewertung = orgrating; //OriginaleBewertung wieder herstellen. } return(false); } }