2023-02-02 01:20:11 -06:00
using System ;
2024-04-20 21:12:04 +02:00
using System.Collections.Generic ;
2023-02-02 01:20:11 -06:00
using System.Collections.ObjectModel ;
2024-06-15 13:16:47 +02:00
using System.Linq ;
2023-02-02 01:20:11 -06:00
using System.Threading ;
using System.Threading.Tasks ;
2024-08-31 18:56:48 +02:00
using ConfusedPolarBear.Plugin.IntroSkipper.Analyzers ;
using ConfusedPolarBear.Plugin.IntroSkipper.Data ;
2024-10-16 16:20:21 +02:00
using ConfusedPolarBear.Plugin.IntroSkipper.Manager ;
2023-02-02 01:20:11 -06:00
using MediaBrowser.Controller.Library ;
using Microsoft.Extensions.Logging ;
2024-08-31 18:56:48 +02:00
namespace ConfusedPolarBear.Plugin.IntroSkipper.ScheduledTasks ;
2024-04-20 12:58:29 +02:00
2023-02-02 01:20:11 -06:00
/// <summary>
/// Common code shared by all media item analyzer tasks.
/// </summary>
public class BaseItemAnalyzerTask
{
2024-10-16 16:05:59 +02:00
private readonly IReadOnlyCollection < AnalysisMode > _analysisModes ;
2023-02-02 01:20:11 -06:00
private readonly ILogger _logger ;
private readonly ILoggerFactory _loggerFactory ;
private readonly ILibraryManager _libraryManager ;
2024-10-19 22:49:47 +02:00
private readonly MediaSegmentUpdateManager _mediaSegmentUpdateManager ;
2023-02-02 01:20:11 -06:00
/// <summary>
/// Initializes a new instance of the <see cref="BaseItemAnalyzerTask"/> class.
/// </summary>
2024-04-20 21:12:04 +02:00
/// <param name="modes">Analysis mode.</param>
2023-02-02 01:20:11 -06:00
/// <param name="logger">Task logger.</param>
/// <param name="loggerFactory">Logger factory.</param>
/// <param name="libraryManager">Library manager.</param>
2024-10-19 22:49:47 +02:00
/// <param name="mediaSegmentUpdateManager">MediaSegmentUpdateManager.</param>
2023-02-02 01:20:11 -06:00
public BaseItemAnalyzerTask (
2024-10-16 16:05:59 +02:00
IReadOnlyCollection < AnalysisMode > modes ,
2023-02-02 01:20:11 -06:00
ILogger logger ,
ILoggerFactory loggerFactory ,
2024-10-19 22:49:47 +02:00
ILibraryManager libraryManager ,
MediaSegmentUpdateManager mediaSegmentUpdateManager )
2023-02-02 01:20:11 -06:00
{
2024-10-16 16:05:59 +02:00
_analysisModes = modes ;
2023-02-02 01:20:11 -06:00
_logger = logger ;
_loggerFactory = loggerFactory ;
_libraryManager = libraryManager ;
2024-10-19 22:49:47 +02:00
_mediaSegmentUpdateManager = mediaSegmentUpdateManager ;
2023-02-02 01:20:11 -06:00
2024-04-20 21:12:04 +02:00
if ( Plugin . Instance ! . Configuration . EdlAction ! = EdlAction . None )
2023-02-02 01:20:11 -06:00
{
EdlManager . Initialize ( _logger ) ;
}
}
/// <summary>
/// Analyze all media items on the server.
/// </summary>
/// <param name="progress">Progress.</param>
/// <param name="cancellationToken">Cancellation token.</param>
2024-06-15 13:16:47 +02:00
/// <param name="seasonsToAnalyze">Season Ids to analyze.</param>
2024-10-19 22:49:47 +02:00
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task AnalyzeItems (
2023-02-02 01:20:11 -06:00
IProgress < double > progress ,
2024-06-15 13:16:47 +02:00
CancellationToken cancellationToken ,
2024-09-25 17:23:25 +02:00
IReadOnlyCollection < Guid > ? seasonsToAnalyze = null )
2023-02-02 01:20:11 -06:00
{
2024-03-19 20:58:48 -04:00
var ffmpegValid = FFmpegWrapper . CheckFFmpegVersion ( ) ;
2024-03-04 08:58:15 -05:00
// Assert that ffmpeg with chromaprint is installed
2024-10-10 11:38:47 -04:00
if ( Plugin . Instance ! . Configuration . WithChromaprint & & ! ffmpegValid )
2024-03-04 08:58:15 -05:00
{
throw new FingerprintException (
2024-03-05 17:27:39 -05:00
"Analysis terminated! Chromaprint is not enabled in the current ffmpeg. If Jellyfin is running natively, install jellyfin-ffmpeg5. If Jellyfin is running in a container, upgrade to version 10.8.0 or newer." ) ;
2024-03-04 08:58:15 -05:00
}
2023-02-02 01:20:11 -06:00
var queueManager = new QueueManager (
_loggerFactory . CreateLogger < QueueManager > ( ) ,
_libraryManager ) ;
var queue = queueManager . GetMediaItems ( ) ;
2024-06-15 13:16:47 +02:00
// Filter the queue based on seasonsToAnalyze
2024-09-25 17:23:25 +02:00
if ( seasonsToAnalyze is { Count : > 0 } )
2024-06-15 13:16:47 +02:00
{
2024-09-25 17:23:25 +02:00
queue = queue . Where ( kvp = > seasonsToAnalyze . Contains ( kvp . Key ) ) . ToDictionary ( kvp = > kvp . Key , kvp = > kvp . Value ) ;
2024-06-15 13:16:47 +02:00
}
2024-10-16 16:05:59 +02:00
int totalQueued = queue . Sum ( kvp = > kvp . Value . Count ) * _analysisModes . Count ;
2023-02-02 01:20:11 -06:00
if ( totalQueued = = 0 )
{
throw new FingerprintException (
2024-09-17 08:41:56 +02:00
"No libraries selected for analysis. Please visit the plugin settings to configure." ) ;
2023-02-02 01:20:11 -06:00
}
2024-04-20 21:12:04 +02:00
if ( Plugin . Instance ! . Configuration . EdlAction ! = EdlAction . None )
2023-02-02 01:20:11 -06:00
{
EdlManager . LogConfiguration ( ) ;
}
var totalProcessed = 0 ;
2024-04-20 12:58:29 +02:00
var options = new ParallelOptions
2023-02-02 01:20:11 -06:00
{
2024-10-19 22:49:47 +02:00
MaxDegreeOfParallelism = Plugin . Instance . Configuration . MaxParallelism ,
CancellationToken = cancellationToken
2023-02-02 01:20:11 -06:00
} ;
2024-10-19 22:49:47 +02:00
await Parallel . ForEachAsync ( queue , options , async ( season , ct ) = >
2023-02-02 01:20:11 -06:00
{
2024-10-19 22:49:47 +02:00
var updateManagers = false ;
2023-02-02 01:20:11 -06:00
// 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.
2024-04-20 21:12:04 +02:00
var ( episodes , requiredModes ) = queueManager . VerifyQueue (
2024-09-21 18:06:11 +02:00
season . Value ,
2024-10-16 16:05:59 +02:00
_analysisModes . Where ( m = > ! Plugin . Instance ! . IsIgnored ( season . Key , m ) ) . ToList ( ) ) ;
2023-02-02 01:20:11 -06:00
2024-09-25 17:23:25 +02:00
if ( episodes . Count = = 0 )
2023-02-02 01:20:11 -06:00
{
return ;
}
2024-10-05 19:30:30 +02:00
var first = episodes [ 0 ] ;
2024-09-25 17:23:25 +02:00
if ( requiredModes . Count = = 0 )
2023-02-02 01:20:11 -06:00
{
_logger . LogDebug (
"All episodes in {Name} season {Season} have already been analyzed" ,
first . SeriesName ,
first . SeasonNumber ) ;
2024-10-16 16:05:59 +02:00
Interlocked . Add ( ref totalProcessed , episodes . Count * _analysisModes . Count ) ; // Update total Processed directly
2024-09-10 18:08:42 +02:00
progress . Report ( totalProcessed * 100 / totalQueued ) ;
2023-02-02 01:20:11 -06:00
}
2024-10-18 14:15:09 +02:00
else if ( _analysisModes . Count ! = requiredModes . Count )
2024-04-20 21:12:04 +02:00
{
2024-09-25 17:23:25 +02:00
Interlocked . Add ( ref totalProcessed , episodes . Count ) ;
2024-09-10 18:08:42 +02:00
progress . Report ( totalProcessed * 100 / totalQueued ) ; // Partial analysis some modes have already been analyzed
2024-04-20 21:12:04 +02:00
}
2023-02-02 01:20:11 -06:00
try
{
2024-10-19 22:49:47 +02:00
if ( ct . IsCancellationRequested )
2023-02-02 01:20:11 -06:00
{
return ;
}
2024-10-16 16:05:59 +02:00
foreach ( AnalysisMode mode in requiredModes )
2024-04-20 21:12:04 +02:00
{
2024-10-19 22:49:47 +02:00
var analyzed = AnalyzeItems ( episodes , mode , ct ) ;
2024-04-20 21:12:04 +02:00
Interlocked . Add ( ref totalProcessed , analyzed ) ;
2024-10-19 22:49:47 +02:00
updateManagers = analyzed > 0 | | updateManagers ;
2023-02-02 01:20:11 -06:00
2024-09-10 18:08:42 +02:00
progress . Report ( totalProcessed * 100 / totalQueued ) ;
2024-04-20 21:12:04 +02:00
}
2023-02-02 01:20:11 -06:00
}
catch ( FingerprintException ex )
{
_logger . LogWarning (
"Unable to analyze {Series} season {Season}: unable to fingerprint: {Ex}" ,
first . SeriesName ,
first . SeasonNumber ,
ex ) ;
}
2024-10-19 22:49:47 +02:00
if ( Plugin . Instance . Configuration . RegenerateMediaSegments | | ( updateManagers & & Plugin . Instance . Configuration . UpdateMediaSegments ) )
{
await _mediaSegmentUpdateManager . UpdateMediaSegmentsAsync ( episodes , ct ) . ConfigureAwait ( false ) ;
}
if ( Plugin . Instance . Configuration . RegenerateEdlFiles | | ( updateManagers & & Plugin . Instance . Configuration . EdlAction ! = EdlAction . None ) )
2023-02-02 01:20:11 -06:00
{
EdlManager . UpdateEDLFiles ( episodes ) ;
}
2024-10-19 22:49:47 +02:00
} ) . ConfigureAwait ( false ) ;
2023-02-02 01:20:11 -06:00
2024-10-19 22:49:47 +02:00
if ( Plugin . Instance . Configuration . RegenerateMediaSegments | | Plugin . Instance . Configuration . RegenerateEdlFiles )
2023-02-02 01:20:11 -06:00
{
2024-10-19 22:49:47 +02:00
_logger . LogInformation ( "Turning Mediasegment/EDL file regeneration flag off" ) ;
Plugin . Instance . Configuration . RegenerateMediaSegments = false ;
2024-04-20 12:21:07 +02:00
Plugin . Instance . Configuration . RegenerateEdlFiles = false ;
Plugin . Instance . SaveConfiguration ( ) ;
2023-02-02 01:20:11 -06:00
}
}
/// <summary>
/// Analyze a group of media items for skippable segments.
/// </summary>
/// <param name="items">Media items to analyze.</param>
2024-04-20 21:12:04 +02:00
/// <param name="mode">Analysis mode.</param>
2023-02-02 01:20:11 -06:00
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Number of items that were successfully analyzed.</returns>
private int AnalyzeItems (
2024-10-05 19:30:30 +02:00
IReadOnlyList < QueuedEpisode > items ,
2024-10-16 16:05:59 +02:00
AnalysisMode mode ,
2023-02-02 01:20:11 -06:00
CancellationToken cancellationToken )
{
2024-10-19 22:49:47 +02:00
var totalItems = items . Count ( e = > ! e . State . IsAnalyzed ( mode ) ) ;
2023-02-02 01:20:11 -06:00
// Only analyze specials (season 0) if the user has opted in.
2024-10-05 19:30:30 +02:00
var first = items [ 0 ] ;
2024-10-18 14:15:09 +02:00
if ( ! first . IsMovie & & first . SeasonNumber = = 0 & & ! Plugin . Instance ! . Configuration . AnalyzeSeasonZero )
2023-02-02 01:20:11 -06:00
{
return 0 ;
}
2024-06-15 13:16:47 +02:00
// Remove from Blacklist
foreach ( var item in items . Where ( e = > e . State . IsBlacklisted ( mode ) ) )
{
item . State . SetBlacklisted ( mode , false ) ;
}
2023-02-02 01:20:11 -06:00
_logger . LogInformation (
2024-04-20 21:12:04 +02:00
"[Mode: {Mode}] Analyzing {Count} files from {Name} season {Season}" ,
mode ,
2023-02-02 01:20:11 -06:00
items . Count ,
first . SeriesName ,
first . SeasonNumber ) ;
2024-09-10 18:08:42 +02:00
var analyzers = new Collection < IMediaFileAnalyzer >
{
new ChapterAnalyzer ( _loggerFactory . CreateLogger < ChapterAnalyzer > ( ) )
} ;
2023-02-02 01:20:11 -06:00
2024-10-18 14:15:09 +02:00
if ( first . IsAnime & & Plugin . Instance ! . Configuration . WithChromaprint & & ! first . IsMovie )
2024-09-25 17:23:25 +02:00
{
analyzers . Add ( new ChromaprintAnalyzer ( _loggerFactory . CreateLogger < ChromaprintAnalyzer > ( ) ) ) ;
2024-06-15 13:16:47 +02:00
}
2024-09-25 17:23:25 +02:00
2024-10-16 16:05:59 +02:00
if ( mode = = AnalysisMode . Credits )
2023-02-02 01:20:11 -06:00
{
2024-09-25 17:23:25 +02:00
analyzers . Add ( new BlackFrameAnalyzer ( _loggerFactory . CreateLogger < BlackFrameAnalyzer > ( ) ) ) ;
}
2024-06-15 13:16:47 +02:00
2024-10-18 14:15:09 +02:00
if ( ! first . IsAnime & & Plugin . Instance ! . Configuration . WithChromaprint & & ! first . IsMovie )
2024-09-25 17:23:25 +02:00
{
analyzers . Add ( new ChromaprintAnalyzer ( _loggerFactory . CreateLogger < ChromaprintAnalyzer > ( ) ) ) ;
2023-02-02 01:20:11 -06:00
}
// Use each analyzer to find skippable ranges in all media files, removing successfully
// analyzed items from the queue.
foreach ( var analyzer in analyzers )
{
2024-04-20 21:12:04 +02:00
items = analyzer . AnalyzeMediaFiles ( items , mode , cancellationToken ) ;
2023-02-02 01:20:11 -06:00
}
2024-06-15 13:16:47 +02:00
// Add items without intros/credits to blacklist.
foreach ( var item in items . Where ( e = > ! e . State . IsAnalyzed ( mode ) ) )
{
item . State . SetBlacklisted ( mode , true ) ;
totalItems - = 1 ;
}
2023-02-02 03:52:31 -06:00
return totalItems ;
2023-02-02 01:20:11 -06:00
}
}