Example #1
0
 private void SetPropertyIfValid(TagLib.Matroska.Tag target, string value, string key)
 {
     SetPropertyIfValid(target, value, (d, e) => d.Set(key, null, e));
 }
        public void WriteSpecificTags()
        {
            if (System.IO.File.Exists(tmp_file))
            {
                System.IO.File.Delete(tmp_file);
            }

            File file;

            System.IO.File.Copy(sample_file, tmp_file);
            file = File.Create(tmp_file);
            Assert.NotNull(file);

            // Write Matroska-specific tags
            var mtag = (TagLib.Matroska.Tag)file.GetTag(TagTypes.Matroska);

            Assert.NotNull(mtag);

            mtag.PerformersRole = new[] { "TEST role 1", "TEST role 2" };
            mtag.Set("CHOREGRAPHER", null, "TEST choregrapher");

            // Retrieve Matroska 'Tags' structure
            var mtags = mtag.Tags;

            // Add a new Matroska 'Tag' structure, representing a collection, in the 'Tags' structure
            var collectag = new TagLib.Matroska.Tag(mtags, TagLib.Matroska.TargetType.COLLECTION);

            // Add a Matroska 'SimpleTag' (TagName: 'ARRANGER') in the 'Tag' structure
            collectag.Set("ARRANGER", null, "TEST arranger");

            // Add a Matroska 'SimpleTag' (TagName: 'TITLE') in the 'Tag' structure
            collectag.Set("TITLE", null, "TEST Album title");              // This should map to the standard TagLib Album tag

            // Get tracks
            var tracks = mtag.Tags.Tracks;

            // Create video tags
            var videotag = new TagLib.Matroska.Tag(mtag.Tags, TagLib.Matroska.TargetType.CHAPTER, tracks[0])
            {
                Title = "The Video test"
            };

            videotag.Set("DESCRIPTION", null, "Video track Tag test");
            videotag.SimpleTags["DESCRIPTION"][0].TagLanguage = "en";
            videotag.SimpleTags["DESCRIPTION"][0].TagDefault  = false;

            // Add another description in another language (and check encoding correctness at the same time)
            videotag.SimpleTags["DESCRIPTION"].Add(new TagLib.Matroska.SimpleTag("Test de piste vidéo"));
            videotag.SimpleTags["DESCRIPTION"][1].TagLanguage = "fr";

            // Remove Audio tags
            var audiotag = mtag.Tags.Get(tracks[1]);

            Assert.NotNull(audiotag);
            audiotag.Clear();

            // Eventually save the changes
            file.Save();

            // Read back the Matroska-specific tags
            file = File.Create(tmp_file);
            Assert.NotNull(mtag);

            mtag = (TagLib.Matroska.Tag)file.GetTag(TagTypes.Matroska);
            Assert.NotNull(mtag);

            Assert.AreEqual("TEST role 1; TEST role 2", string.Join("; ", mtag.PerformersRole));
            Assert.AreEqual("TEST choregrapher", mtag.Get("CHOREGRAPHER", null)[0]);
            Assert.AreEqual("TEST arranger", mtags.Album.Get("ARRANGER", null)[0]);
            Assert.AreEqual("TEST Album title", mtag.Album);

            // Get tracks
            tracks = mtag.Tags.Tracks;
            Assert.NotNull(tracks);

            // Test Video Track Tag
            videotag = mtag.Tags.Get(tracks[0]);
            Assert.NotNull(videotag);
            Assert.AreEqual(TagLib.Matroska.TargetType.CHAPTER, videotag.TargetType);
            Assert.AreEqual(30, videotag.TargetTypeValue);
            Assert.AreEqual("The Video test", videotag.Title);
            Assert.AreEqual("Video track Tag test", videotag.SimpleTags["DESCRIPTION"][0]);
            Assert.AreEqual("en", videotag.SimpleTags["DESCRIPTION"][0].TagLanguage);
            Assert.AreEqual(false, videotag.SimpleTags["DESCRIPTION"][0].TagDefault);

            // implicit or explicit conversion from TagLib.Matroska.SimpleTag to string is required to ensure a proper encoding
            Assert.AreEqual("Test de piste vidéo", videotag.SimpleTags["DESCRIPTION"][1].ToString());
            Assert.AreEqual("fr", videotag.SimpleTags["DESCRIPTION"][1].TagLanguage);
            Assert.AreEqual(true, videotag.SimpleTags["DESCRIPTION"][1].TagDefault);

            // Test Audio Track Tag
            audiotag = mtag.Tags.Get(tracks[1]);
            Assert.IsNull(audiotag);

            Assert.AreEqual("TEST Album title", mtag.Album);
        }