intro-skipper/IntroSkipper.Tests/TestBlackFrames.cs

76 lines
2.0 KiB
C#
Raw Normal View History

namespace IntroSkipper.Tests;
2022-11-01 00:53:56 -05:00
using System;
using System.Collections.Generic;
using IntroSkipper.Analyzers;
using IntroSkipper.Data;
2022-11-02 17:06:21 -05:00
using Microsoft.Extensions.Logging;
2022-11-01 00:53:56 -05:00
using Xunit;
public class TestBlackFrames
{
[FactSkipFFmpegTests]
public void TestBlackFrameDetection()
{
2022-11-02 17:06:21 -05:00
var range = 1e-5;
2022-11-01 00:53:56 -05:00
var expected = new List<BlackFrame>();
2024-05-19 17:49:43 +02:00
expected.AddRange(CreateFrameSequence(2.04, 3));
2022-11-01 00:53:56 -05:00
expected.AddRange(CreateFrameSequence(5, 6));
expected.AddRange(CreateFrameSequence(8, 9.96));
2024-09-25 17:23:25 +02:00
var actual = FFmpegWrapper.DetectBlackFrames(QueueFile("rainbow.mp4"), new(0, 10), 85);
2022-11-01 00:53:56 -05:00
for (var i = 0; i < expected.Count; i++)
{
var (e, a) = (expected[i], actual[i]);
Assert.Equal(e.Percentage, a.Percentage);
2022-11-02 17:06:21 -05:00
Assert.InRange(a.Time, e.Time - range, e.Time + range);
2022-11-01 00:53:56 -05:00
}
}
2022-11-02 17:06:21 -05:00
[FactSkipFFmpegTests]
public void TestEndCreditDetection()
{
2024-05-19 17:49:43 +02:00
// new strategy new range
var range = 3;
2022-11-29 20:11:22 -06:00
2022-11-02 17:06:21 -05:00
var analyzer = CreateBlackFrameAnalyzer();
2024-09-25 17:23:25 +02:00
var episode = QueueFile("credits.mp4");
2022-11-02 17:06:21 -05:00
episode.Duration = (int)new TimeSpan(0, 5, 30).TotalSeconds;
var result = analyzer.AnalyzeMediaFile(episode, 240, 30, 85);
2022-11-02 17:06:21 -05:00
Assert.NotNull(result);
Assert.InRange(result.Start, 300 - range, 300 + range);
2022-11-02 17:06:21 -05:00
}
2024-09-25 17:23:25 +02:00
private static QueuedEpisode QueueFile(string path)
2022-11-01 00:53:56 -05:00
{
return new()
{
EpisodeId = Guid.NewGuid(),
2022-11-02 17:06:21 -05:00
Name = path,
2022-11-01 00:53:56 -05:00
Path = "../../../video/" + path
};
}
2024-09-25 17:23:25 +02:00
private static BlackFrame[] CreateFrameSequence(double start, double end)
2022-11-01 00:53:56 -05:00
{
var frames = new List<BlackFrame>();
for (var i = start; i < end; i += 0.04)
{
frames.Add(new(100, i));
}
2024-09-25 17:23:25 +02:00
return [.. frames];
2022-11-01 00:53:56 -05:00
}
2022-11-02 17:06:21 -05:00
2024-09-25 17:23:25 +02:00
private static BlackFrameAnalyzer CreateBlackFrameAnalyzer()
2022-11-02 17:06:21 -05:00
{
var logger = new LoggerFactory().CreateLogger<BlackFrameAnalyzer>();
return new(logger);
}
2022-11-01 00:53:56 -05:00
}