Skip to content

Commit

Permalink
Combine introduction and credits detection code
Browse files Browse the repository at this point in the history
  • Loading branch information
ConfusedPolarBear committed Feb 2, 2023
1 parent 1966357 commit 431aed5
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 281 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
namespace ConfusedPolarBear.Plugin.IntroSkipper;

using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
using Microsoft.Extensions.Logging;

/// <summary>
/// Common code shared by all media item analyzer tasks.
/// </summary>
public class BaseItemAnalyzerTask
{
private readonly AnalysisMode _analysisMode;

private readonly ILogger _logger;

private readonly ILoggerFactory _loggerFactory;

private readonly ILibraryManager _libraryManager;

/// <summary>
/// Initializes a new instance of the <see cref="BaseItemAnalyzerTask"/> class.
/// </summary>
/// <param name="mode">Analysis mode.</param>
/// <param name="logger">Task logger.</param>
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="libraryManager">Library manager.</param>
public BaseItemAnalyzerTask(
AnalysisMode mode,
ILogger logger,
ILoggerFactory loggerFactory,
ILibraryManager libraryManager)
{
_analysisMode = mode;
_logger = logger;
_loggerFactory = loggerFactory;
_libraryManager = libraryManager;

if (mode == AnalysisMode.Introduction)
{
EdlManager.Initialize(_logger);
}
}

/// <summary>
/// Analyze all media items on the server.
/// </summary>
/// <param name="progress">Progress.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public void AnalyzeItems(
IProgress<double> progress,
CancellationToken cancellationToken)
{
var queueManager = new QueueManager(
_loggerFactory.CreateLogger<QueueManager>(),
_libraryManager);

var queue = queueManager.GetMediaItems();

var totalQueued = 0;
foreach (var kvp in queue)
{
totalQueued += kvp.Value.Count;
}

if (totalQueued == 0)
{
throw new FingerprintException(
"No episodes to analyze. If you are limiting the list of libraries to analyze, check that all library names have been spelled correctly.");
}

if (this._analysisMode == AnalysisMode.Introduction)
{
EdlManager.LogConfiguration();
}

var totalProcessed = 0;
var options = new ParallelOptions()
{
MaxDegreeOfParallelism = Plugin.Instance!.Configuration.MaxParallelism
};

Parallel.ForEach(queue, options, (season) =>
{
var writeEdl = false;

// Since the first run of the task can run for multiple hours, ensure that none
// of the current media items were deleted from Jellyfin since the task was started.
var (episodes, unanalyzed) = queueManager.VerifyQueue(
season.Value.AsReadOnly(),
this._analysisMode);

if (episodes.Count == 0)
{
return;
}

var first = episodes[0];

if (!unanalyzed)
{
_logger.LogDebug(
"All episodes in {Name} season {Season} have already been analyzed",
first.SeriesName,
first.SeasonNumber);

return;
}

try
{
if (cancellationToken.IsCancellationRequested)
{
return;
}

var analyzed = AnalyzeItems(episodes, cancellationToken);
Interlocked.Add(ref totalProcessed, analyzed);

writeEdl = analyzed > 0 || Plugin.Instance!.Configuration.RegenerateEdlFiles;
}
catch (FingerprintException ex)
{
_logger.LogWarning(
"Unable to analyze {Series} season {Season}: unable to fingerprint: {Ex}",
first.SeriesName,
first.SeasonNumber,
ex);
}

if (
writeEdl &&
Plugin.Instance!.Configuration.EdlAction != EdlAction.None &&
_analysisMode == AnalysisMode.Introduction)
{
EdlManager.UpdateEDLFiles(episodes);
}

progress.Report((totalProcessed * 100) / totalQueued);
});

if (
_analysisMode == AnalysisMode.Introduction &&
Plugin.Instance!.Configuration.RegenerateEdlFiles)
{
_logger.LogInformation("Turning EDL file regeneration flag off");
Plugin.Instance!.Configuration.RegenerateEdlFiles = false;
Plugin.Instance!.SaveConfiguration();
}
}

/// <summary>
/// Analyze a group of media items for skippable segments.
/// </summary>
/// <param name="items">Media items to analyze.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Number of items that were successfully analyzed.</returns>
private int AnalyzeItems(
ReadOnlyCollection<QueuedEpisode> items,
CancellationToken cancellationToken)
{
var totalItems = items.Count;

// Only analyze specials (season 0) if the user has opted in.
var first = items[0];
if (first.SeasonNumber == 0 && !Plugin.Instance!.Configuration.AnalyzeSeasonZero)
{
return 0;
}

_logger.LogInformation(
"Analyzing {Count} files from {Name} season {Season}",
items.Count,
first.SeriesName,
first.SeasonNumber);

var analyzers = new Collection<IMediaFileAnalyzer>();

analyzers.Add(new ChapterAnalyzer(_loggerFactory.CreateLogger<ChapterAnalyzer>()));
analyzers.Add(new ChromaprintAnalyzer(_loggerFactory.CreateLogger<ChromaprintAnalyzer>()));

if (this._analysisMode == AnalysisMode.Credits)
{
analyzers.Add(new BlackFrameAnalyzer(_loggerFactory.CreateLogger<BlackFrameAnalyzer>()));
}

// Use each analyzer to find skippable ranges in all media files, removing successfully
// analyzed items from the queue.
foreach (var analyzer in analyzers)
{
items = analyzer.AnalyzeMediaFiles(items, this._analysisMode, cancellationToken);
}

return totalItems - items.Count;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
Expand All @@ -11,14 +10,13 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper;

/// <summary>
/// Analyze all television episodes for credits.
/// TODO: analyze all media files.
/// </summary>
public class DetectCreditsTask : IScheduledTask
{
private readonly ILogger<DetectCreditsTask> _logger;

private readonly ILoggerFactory _loggerFactory;

private readonly ILibraryManager? _libraryManager;
private readonly ILibraryManager _libraryManager;

/// <summary>
/// Initializes a new instance of the <see cref="DetectCreditsTask"/> class.
Expand All @@ -27,19 +25,10 @@ public class DetectCreditsTask : IScheduledTask
/// <param name="libraryManager">Library manager.</param>
public DetectCreditsTask(
ILoggerFactory loggerFactory,
ILibraryManager libraryManager) : this(loggerFactory)
{
_libraryManager = libraryManager;
}

/// <summary>
/// Initializes a new instance of the <see cref="DetectCreditsTask"/> class.
/// </summary>
/// <param name="loggerFactory">Logger factory.</param>
public DetectCreditsTask(ILoggerFactory loggerFactory)
ILibraryManager libraryManager)
{
_logger = loggerFactory.CreateLogger<DetectCreditsTask>();
_loggerFactory = loggerFactory;
_libraryManager = libraryManager;
}

/// <summary>
Expand Down Expand Up @@ -72,125 +61,20 @@ public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellat
{
if (_libraryManager is null)
{
throw new InvalidOperationException("Library manager must not be null");
throw new InvalidOperationException("Library manager was null");
}

// Make sure the analysis queue matches what's currently in Jellyfin.
var queueManager = new QueueManager(
_loggerFactory.CreateLogger<QueueManager>(),
var baseAnalyzer = new BaseItemAnalyzerTask(
AnalysisMode.Credits,
_loggerFactory.CreateLogger<DetectCreditsTask>(),
_loggerFactory,
_libraryManager);

var queue = queueManager.GetMediaItems();

if (queue.Count == 0)
{
throw new FingerprintException(
"No episodes to analyze. If you are limiting the list of libraries to analyze, check that all library names have been spelled correctly.");
}

var totalProcessed = 0;
var options = new ParallelOptions()
{
MaxDegreeOfParallelism = Plugin.Instance!.Configuration.MaxParallelism
};

// Analyze all episodes in the queue using the degrees of parallelism the user specified.
Parallel.ForEach(queue, options, (season) =>
{
var (episodes, unanalyzed) = queueManager.VerifyQueue(
season.Value.AsReadOnly(),
AnalysisMode.Credits);

if (episodes.Count == 0 || unanalyzed)
{
return;
}

var first = episodes[0];

try
{
if (cancellationToken.IsCancellationRequested)
{
return;
}

AnalyzeSeason(episodes, cancellationToken);
Interlocked.Add(ref totalProcessed, episodes.Count);
}
catch (FingerprintException ex)
{
_logger.LogWarning(
"Unable to analyze {Series} season {Season}: unable to fingerprint: {Ex}",
first.SeriesName,
first.SeasonNumber,
ex);
}
catch (KeyNotFoundException ex)
{
_logger.LogWarning(
"Unable to analyze {Series} season {Season}: cache miss: {Ex}",
first.SeriesName,
first.SeasonNumber,
ex);
}

var total = Plugin.Instance!.TotalQueued;
if (total > 0)
{
progress.Report((totalProcessed * 100) / total);
}
});
baseAnalyzer.AnalyzeItems(progress, cancellationToken);

return Task.CompletedTask;
}

/// <summary>
/// Analyzes all episodes in the season for end credits.
/// </summary>
/// <param name="episodes">Episodes in this season.</param>
/// <param name="cancellationToken">Cancellation token provided by the scheduled task.</param>
private void AnalyzeSeason(
ReadOnlyCollection<QueuedEpisode> episodes,
CancellationToken cancellationToken)
{
// Only analyze specials (season 0) if the user has opted in.
if (episodes[0].SeasonNumber == 0 && !Plugin.Instance!.Configuration.AnalyzeSeasonZero)
{
return;
}

// Analyze with Chromaprint first and fall back to the black frame detector
var analyzers = new IMediaFileAnalyzer[]
{
new ChromaprintAnalyzer(_loggerFactory.CreateLogger<ChromaprintAnalyzer>()),
new BlackFrameAnalyzer(_loggerFactory.CreateLogger<BlackFrameAnalyzer>())
};

// Use each analyzer to find credits in all media files, removing successfully analyzed files
// from the queue.
var remaining = new ReadOnlyCollection<QueuedEpisode>(episodes);
foreach (var analyzer in analyzers)
{
remaining = AnalyzeFiles(remaining, analyzer, cancellationToken);
}
}

private ReadOnlyCollection<QueuedEpisode> AnalyzeFiles(
ReadOnlyCollection<QueuedEpisode> episodes,
IMediaFileAnalyzer analyzer,
CancellationToken cancellationToken)
{
_logger.LogInformation(
"Analyzing {Count} episodes from {Name} season {Season} with {Analyzer}",
episodes.Count,
episodes[0].SeriesName,
episodes[0].SeasonNumber,
analyzer.GetType().Name);

return analyzer.AnalyzeMediaFiles(episodes, AnalysisMode.Credits, cancellationToken);
}

/// <summary>
/// Get task triggers.
/// </summary>
Expand Down
Loading

0 comments on commit 431aed5

Please sign in to comment.