Refactored, added unit test

Former-commit-id: f560244628
This commit is contained in:
Jonas Kamsker 2022-01-06 19:38:37 +01:00
parent e1a5c8aea2
commit 1f0a4f2a0e
2 changed files with 81 additions and 12 deletions

View file

@ -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));
}
}
}

View file

@ -8,12 +8,23 @@ public class MetaDataBuilder
{ {
private MetaData _metaData = new MetaData(); 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; return this;
} }
public MetaDataBuilder WithEntry(string key, params string[] values)
=> this.WithEntry(key, String.Join("; ", values));
public MetaDataBuilder WithEntry(string key, IEnumerable<string> values)
=> this.WithEntry(key, String.Join("; ", values));
public MetaDataBuilder AddChapter(ChapterData chapterData) public MetaDataBuilder AddChapter(ChapterData chapterData)
{ {
_metaData.Chapters.Add(chapterData); _metaData.Chapters.Add(chapterData);
@ -41,7 +52,7 @@ public MetaDataBuilder AddChapter(TimeSpan duration, string? title = null)
( (
start: start, start: start,
end: end, end: end,
title: title title: title ?? String.Empty
)); ));
return this; return this;
@ -63,13 +74,16 @@ public MetaDataBuilder AddChapter(TimeSpan duration, string? title = null)
public MetaDataBuilder WithTitle(string value) => WithEntry("title", value); public MetaDataBuilder WithTitle(string value) => WithEntry("title", value);
//artist=Dennis E. Taylor //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<string> value) => WithEntry("artist", value);
//composer=J. K. Rowling //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<string> value) => WithEntry("composer", value);
//album_artist=Dennis E. Taylor //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<string> value) => WithEntry("album_artist", value);
//album=Alle diese Welten: Bobiverse 3 //album=Alle diese Welten: Bobiverse 3
public MetaDataBuilder WithAlbum(string value) => WithEntry("album", value); 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); public MetaDataBuilder WithDate(string value) => WithEntry("date", value);
//genre=Hörbuch //genre=Hörbuch
public MetaDataBuilder WithGenre(string value) => WithEntry("genre", value); public MetaDataBuilder WithGenres(params string[] value) => WithEntry("genre", value);
public MetaDataBuilder WithGenres(IEnumerable<string> value) => WithEntry("genre", value);
//comment=Chapter 200 //comment=Chapter 200
public MetaDataBuilder WithComment(string value) => WithEntry("comment", value); public MetaDataBuilder WithComments(params string[] value) => WithEntry("comment", value);
public MetaDataBuilder WithComments(IEnumerable<string> value) => WithEntry("comment", value);
//encoder=Lavf58.47.100 //encoder=Lavf58.47.100
public MetaDataBuilder WithEncoder(string value) => WithEntry("encoder", value); public MetaDataBuilder WithEncoder(string value) => WithEntry("encoder", value);
public ReadOnlyMetaData Build()
{
return new ReadOnlyMetaData(_metaData); public ReadOnlyMetaData Build() => new ReadOnlyMetaData(_metaData);
}
} }
} }