2024-10-25 14:31:50 -04:00
|
|
|
// Copyright (C) 2024 Intro-Skipper contributors <intro-skipper.org>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only.
|
2024-10-25 14:15:12 -04:00
|
|
|
|
2022-11-02 17:06:21 -05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
2024-11-02 18:17:22 +01:00
|
|
|
using System.Threading.Tasks;
|
2024-10-19 23:50:41 +02:00
|
|
|
using IntroSkipper.Configuration;
|
|
|
|
using IntroSkipper.Data;
|
2022-11-02 17:06:21 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2024-10-19 23:50:41 +02:00
|
|
|
namespace IntroSkipper.Analyzers;
|
2024-04-20 12:58:29 +02:00
|
|
|
|
2022-11-02 17:06:21 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Media file analyzer used to detect end credits that consist of text overlaid on a black background.
|
|
|
|
/// Bisects the end of the video file to perform an efficient search.
|
|
|
|
/// </summary>
|
2024-10-31 11:58:56 +01:00
|
|
|
public class BlackFrameAnalyzer(ILogger<BlackFrameAnalyzer> logger) : IMediaFileAnalyzer
|
2022-11-02 17:06:21 -05:00
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
private readonly PluginConfiguration _config = Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
2022-11-02 17:06:21 -05:00
|
|
|
private readonly TimeSpan _maximumError = new(0, 0, 4);
|
2024-10-31 11:58:56 +01:00
|
|
|
private readonly ILogger<BlackFrameAnalyzer> _logger = logger;
|
2022-11-02 17:06:21 -05:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-11-02 18:17:22 +01:00
|
|
|
public async Task<IReadOnlyList<QueuedEpisode>> AnalyzeMediaFiles(
|
2024-10-05 19:30:30 +02:00
|
|
|
IReadOnlyList<QueuedEpisode> analysisQueue,
|
2024-10-16 16:05:59 +02:00
|
|
|
AnalysisMode mode,
|
2022-11-02 17:06:21 -05:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2024-10-16 16:05:59 +02:00
|
|
|
if (mode != AnalysisMode.Credits)
|
2022-11-02 17:06:21 -05:00
|
|
|
{
|
|
|
|
throw new NotImplementedException("mode must equal Credits");
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
var episodesWithoutIntros = analysisQueue.Where(e => !e.IsAnalyzed).ToList();
|
2024-06-15 13:16:47 +02:00
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
var searchStart = 0.0;
|
2024-05-01 11:13:13 +02:00
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
foreach (var episode in episodesWithoutIntros)
|
2022-11-02 17:06:21 -05:00
|
|
|
{
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
if (!AnalyzeChapters(episode, out var credit))
|
2024-10-18 14:15:09 +02:00
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
if (searchStart < _config.MinimumCreditsDuration)
|
2024-05-01 11:13:13 +02:00
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
searchStart = FindSearchStart(episode);
|
2024-05-01 11:13:13 +02:00
|
|
|
}
|
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
credit = AnalyzeMediaFile(
|
|
|
|
episode,
|
|
|
|
searchStart,
|
|
|
|
_config.BlackFrameMinimumPercentage);
|
2024-05-01 11:13:13 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 13:35:33 +02:00
|
|
|
if (credit is null || !credit.Valid)
|
2022-11-02 17:06:21 -05:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
episode.IsAnalyzed = true;
|
|
|
|
await Plugin.Instance!.UpdateTimestampAsync(credit, mode).ConfigureAwait(false);
|
|
|
|
searchStart = episode.Duration - credit.Start + _config.MinimumCreditsDuration;
|
2022-11-02 17:06:21 -05:00
|
|
|
}
|
|
|
|
|
2024-11-06 10:30:00 +01:00
|
|
|
return analysisQueue;
|
2022-11-02 17:06:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Analyzes an individual media file. Only public because of unit tests.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="episode">Media file to analyze.</param>
|
2024-05-01 11:13:13 +02:00
|
|
|
/// <param name="searchStart">Search Start Piont.</param>
|
2022-11-02 17:06:21 -05:00
|
|
|
/// <param name="minimum">Percentage of the frame that must be black.</param>
|
|
|
|
/// <returns>Credits timestamp.</returns>
|
2024-11-21 15:42:55 +01:00
|
|
|
public Segment? AnalyzeMediaFile(QueuedEpisode episode, double searchStart, int minimum)
|
2022-11-02 17:06:21 -05:00
|
|
|
{
|
2022-11-26 02:28:40 -06:00
|
|
|
// Start by analyzing the last N minutes of the file.
|
2024-11-21 15:42:55 +01:00
|
|
|
var searchDistance = 2 * _config.MinimumCreditsDuration;
|
2024-05-01 11:13:13 +02:00
|
|
|
var upperLimit = searchStart;
|
2024-11-21 15:42:55 +01:00
|
|
|
var lowerLimit = Math.Max(searchStart - searchDistance, _config.MinimumCreditsDuration);
|
2024-05-01 11:13:13 +02:00
|
|
|
var start = TimeSpan.FromSeconds(upperLimit);
|
|
|
|
var end = TimeSpan.FromSeconds(lowerLimit);
|
2022-11-02 17:06:21 -05:00
|
|
|
var firstFrameTime = 0.0;
|
|
|
|
|
|
|
|
// Continue bisecting the end of the file until the range that contains the first black
|
|
|
|
// frame is smaller than the maximum permitted error.
|
|
|
|
while (start - end > _maximumError)
|
|
|
|
{
|
|
|
|
// Analyze the middle two seconds from the current bisected range
|
|
|
|
var midpoint = (start + end) / 2;
|
|
|
|
var scanTime = episode.Duration - midpoint.TotalSeconds;
|
|
|
|
var tr = new TimeRange(scanTime, scanTime + 2);
|
|
|
|
|
|
|
|
_logger.LogTrace(
|
|
|
|
"{Episode}, dur {Duration}, bisect [{BStart}, {BEnd}], time [{Start}, {End}]",
|
|
|
|
episode.Name,
|
|
|
|
episode.Duration,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
tr.Start,
|
|
|
|
tr.End);
|
|
|
|
|
|
|
|
var frames = FFmpegWrapper.DetectBlackFrames(episode, tr, minimum);
|
2022-11-26 02:28:40 -06:00
|
|
|
_logger.LogTrace(
|
|
|
|
"{Episode} at {Start} has {Count} black frames",
|
|
|
|
episode.Name,
|
|
|
|
tr.Start,
|
|
|
|
frames.Length);
|
2022-11-02 17:06:21 -05:00
|
|
|
|
|
|
|
if (frames.Length == 0)
|
|
|
|
{
|
|
|
|
// Since no black frames were found, slide the range closer to the end
|
2024-05-10 14:05:59 +02:00
|
|
|
start = midpoint - TimeSpan.FromSeconds(2);
|
2024-05-01 11:13:13 +02:00
|
|
|
|
|
|
|
if (midpoint - TimeSpan.FromSeconds(lowerLimit) < _maximumError)
|
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
lowerLimit = Math.Max(lowerLimit - (0.5 * searchDistance), _config.MinimumCreditsDuration);
|
2024-05-01 11:13:13 +02:00
|
|
|
|
|
|
|
// Reset end for a new search with the increased duration
|
|
|
|
end = TimeSpan.FromSeconds(lowerLimit);
|
|
|
|
}
|
2022-11-02 17:06:21 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Some black frames were found, slide the range closer to the start
|
|
|
|
end = midpoint;
|
|
|
|
firstFrameTime = frames[0].Time + scanTime;
|
2024-05-01 11:13:13 +02:00
|
|
|
|
|
|
|
if (TimeSpan.FromSeconds(upperLimit) - midpoint < _maximumError)
|
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
upperLimit = Math.Min(upperLimit + (0.5 * searchDistance), episode.Duration - episode.CreditsFingerprintStart);
|
2024-05-01 11:13:13 +02:00
|
|
|
|
|
|
|
// Reset start for a new search with the increased duration
|
|
|
|
start = TimeSpan.FromSeconds(upperLimit);
|
|
|
|
}
|
2022-11-02 17:06:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstFrameTime > 0)
|
|
|
|
{
|
|
|
|
return new(episode.EpisodeId, new TimeRange(firstFrameTime, episode.Duration));
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2024-11-21 15:42:55 +01:00
|
|
|
|
|
|
|
private bool AnalyzeChapters(QueuedEpisode episode, out Segment? segment)
|
|
|
|
{
|
|
|
|
// Get last chapter that falls within the valid credits duration range
|
|
|
|
var suitableChapters = Plugin.Instance!.GetChapters(episode.EpisodeId)
|
|
|
|
.Select(c => TimeSpan.FromTicks(c.StartPositionTicks).TotalSeconds)
|
|
|
|
.Where(s => s >= episode.CreditsFingerprintStart &&
|
|
|
|
s <= episode.Duration - _config.MinimumCreditsDuration)
|
|
|
|
.OrderByDescending(s => s).ToList();
|
|
|
|
|
|
|
|
// If suitable chapters found, use them to find the search start point
|
|
|
|
foreach (var chapterStart in suitableChapters)
|
|
|
|
{
|
|
|
|
// Check for black frames at chapter start
|
|
|
|
var startRange = new TimeRange(chapterStart, chapterStart + 1);
|
|
|
|
var hasBlackFramesAtStart = FFmpegWrapper.DetectBlackFrames(
|
|
|
|
episode,
|
|
|
|
startRange,
|
|
|
|
_config.BlackFrameMinimumPercentage).Length > 0;
|
|
|
|
|
|
|
|
if (!hasBlackFramesAtStart)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify no black frames before chapter start
|
|
|
|
var beforeRange = new TimeRange(chapterStart - 5, chapterStart - 4);
|
|
|
|
var hasBlackFramesBefore = FFmpegWrapper.DetectBlackFrames(
|
|
|
|
episode,
|
|
|
|
beforeRange,
|
|
|
|
_config.BlackFrameMinimumPercentage).Length > 0;
|
|
|
|
|
|
|
|
if (!hasBlackFramesBefore)
|
|
|
|
{
|
|
|
|
segment = new(episode.EpisodeId, new TimeRange(chapterStart, episode.Duration));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
segment = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private double FindSearchStart(QueuedEpisode episode)
|
|
|
|
{
|
|
|
|
var searchStart = 3 * _config.MinimumCreditsDuration;
|
|
|
|
var scanTime = episode.Duration - searchStart;
|
|
|
|
var tr = new TimeRange(scanTime - 0.5, scanTime); // Short search range since accuracy isn't important here.
|
|
|
|
|
|
|
|
// Keep increasing search start time while black frames are found, to avoid false positives
|
|
|
|
while (FFmpegWrapper.DetectBlackFrames(episode, tr, _config.BlackFrameMinimumPercentage).Length > 0)
|
|
|
|
{
|
|
|
|
// Increase by 2x minimum credits duration each iteration
|
|
|
|
searchStart += 2 * _config.MinimumCreditsDuration;
|
|
|
|
scanTime = episode.Duration - searchStart;
|
|
|
|
tr = new TimeRange(scanTime - 0.5, scanTime);
|
|
|
|
|
|
|
|
// Don't search past the required credits duration from the end
|
|
|
|
if (searchStart > episode.Duration - episode.CreditsFingerprintStart)
|
|
|
|
{
|
|
|
|
searchStart = episode.Duration - episode.CreditsFingerprintStart;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchStart;
|
|
|
|
}
|
2022-11-02 17:06:21 -05:00
|
|
|
}
|