diff --git a/FFMpegCore.Test/MetaDataBuilderTests.cs b/FFMpegCore.Test/MetaDataBuilderTests.cs new file mode 100644 index 0000000..5f0a144 --- /dev/null +++ b/FFMpegCore.Test/MetaDataBuilderTests.cs @@ -0,0 +1,54 @@ +using FFMpegCore.Builders.MetaData; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FFMpegCore.Test +{ + [TestClass] + public class MetaDataBuilderTests + { + [TestMethod] + public void TestMetaDataBuilderIntegrity() + { + var source = new + { + Album = "Kanon und Gigue", + Artist = "Pachelbel", + Title = "Kanon und Gigue in D-Dur", + Copyright = "Copyright Lol", + Composer = "Pachelbel", + Genres = new[] { "Synthwave", "Classics" }, + Tracks = new[] + { + new { Duration = TimeSpan.FromSeconds(10), Title = "Chapter 01" }, + new { Duration = TimeSpan.FromSeconds(10), Title = "Chapter 02" }, + new { Duration = TimeSpan.FromSeconds(10), Title = "Chapter 03" }, + new { Duration = TimeSpan.FromSeconds(10), Title = "Chapter 04" }, + } + }; + + var builder = new MetaDataBuilder() + .WithTitle(source.Title) + .WithArtists(source.Artist) + .WithComposers(source.Composer) + .WithAlbumArtists(source.Artist) + .WithGenres(source.Genres) + .WithCopyright(source.Copyright) + .AddChapters(source.Tracks, x => (x.Duration, x.Title)); + + var metadata = builder.Build(); + var serialized = MetaDataSerializer.Instance.Serialize(metadata); + + Assert.IsTrue(serialized.StartsWith(";FFMETADATA1", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(serialized.Contains("genre=Synthwave; Classics", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(serialized.Contains("title=Chapter 01", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(serialized.Contains("album_artist=Pachelbel", StringComparison.OrdinalIgnoreCase)); + } + } +} diff --git a/FFMpegCore/FFMpeg/Builders/MetaData/MetaDataBuilder.cs b/FFMpegCore/FFMpeg/Builders/MetaData/MetaDataBuilder.cs index 8c7360b..29c13c2 100644 --- a/FFMpegCore/FFMpeg/Builders/MetaData/MetaDataBuilder.cs +++ b/FFMpegCore/FFMpeg/Builders/MetaData/MetaDataBuilder.cs @@ -8,12 +8,23 @@ public class MetaDataBuilder { private MetaData _metaData = new MetaData(); - public MetaDataBuilder WithEntry(string key, string value) + public MetaDataBuilder WithEntry(string key, string entry) { - _metaData.Entries[key] = value; + if (_metaData.Entries.TryGetValue(key, out var value) && !string.IsNullOrWhiteSpace(value)) + { + entry = String.Concat(value, "; ", entry); + } + + _metaData.Entries[key] = entry; return this; } + public MetaDataBuilder WithEntry(string key, params string[] values) + => this.WithEntry(key, String.Join("; ", values)); + + public MetaDataBuilder WithEntry(string key, IEnumerable values) + => this.WithEntry(key, String.Join("; ", values)); + public MetaDataBuilder AddChapter(ChapterData chapterData) { _metaData.Chapters.Add(chapterData); @@ -41,7 +52,7 @@ public MetaDataBuilder AddChapter(TimeSpan duration, string? title = null) ( start: start, end: end, - title: title + title: title ?? String.Empty )); return this; @@ -63,13 +74,16 @@ public MetaDataBuilder AddChapter(TimeSpan duration, string? title = null) public MetaDataBuilder WithTitle(string value) => WithEntry("title", value); //artist=Dennis E. Taylor - public MetaDataBuilder WithArtist(string value) => WithEntry("artist", value); + public MetaDataBuilder WithArtists(params string[] value) => WithEntry("artist", value); + public MetaDataBuilder WithArtists(IEnumerable value) => WithEntry("artist", value); //composer=J. K. Rowling - public MetaDataBuilder WithComposer(string value) => WithEntry("composer", value); + public MetaDataBuilder WithComposers(params string[] value) => WithEntry("composer", value); + public MetaDataBuilder WithComposers(IEnumerable value) => WithEntry("composer", value); //album_artist=Dennis E. Taylor - public MetaDataBuilder WithAlbumArtist(string value) => WithEntry("album_artist", value); + public MetaDataBuilder WithAlbumArtists(params string[] value) => WithEntry("album_artist", value); + public MetaDataBuilder WithAlbumArtists(IEnumerable value) => WithEntry("album_artist", value); //album=Alle diese Welten: Bobiverse 3 public MetaDataBuilder WithAlbum(string value) => WithEntry("album", value); @@ -78,17 +92,18 @@ public MetaDataBuilder AddChapter(TimeSpan duration, string? title = null) public MetaDataBuilder WithDate(string value) => WithEntry("date", value); //genre=Hörbuch - public MetaDataBuilder WithGenre(string value) => WithEntry("genre", value); + public MetaDataBuilder WithGenres(params string[] value) => WithEntry("genre", value); + public MetaDataBuilder WithGenres(IEnumerable value) => WithEntry("genre", value); //comment=Chapter 200 - public MetaDataBuilder WithComment(string value) => WithEntry("comment", value); + public MetaDataBuilder WithComments(params string[] value) => WithEntry("comment", value); + public MetaDataBuilder WithComments(IEnumerable value) => WithEntry("comment", value); //encoder=Lavf58.47.100 public MetaDataBuilder WithEncoder(string value) => WithEntry("encoder", value); - public ReadOnlyMetaData Build() - { - return new ReadOnlyMetaData(_metaData); - } + + + public ReadOnlyMetaData Build() => new ReadOnlyMetaData(_metaData); } } \ No newline at end of file