95 lines
3.5 KiB
C#
95 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
using MediaBrowser.Controller.Library;
|
|
using MediaBrowser.Model.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace JellyfinPlugin.Tasks
|
|
{
|
|
public class LibraryRetroactiveScanTask : IScheduledTask
|
|
{
|
|
private readonly ILogger<LibraryRetroactiveScanTask> _logger;
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
public LibraryRetroactiveScanTask(ILogger<LibraryRetroactiveScanTask> logger, ILibraryManager libraryManager)
|
|
{
|
|
_logger = logger;
|
|
_libraryManager = libraryManager;
|
|
}
|
|
|
|
public string Name => "Scan Missing Black Bars (Black Bar Remover)";
|
|
public string Key => "BlackBarRetroactiveScanTask";
|
|
public string Description => "Scans the existing library, skipping already analyzed files. Run this after installing the plugin.";
|
|
public string Category => "Library";
|
|
|
|
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
|
{
|
|
var config = Plugin.Instance!.Configuration;
|
|
if (!config.IsEnabled)
|
|
{
|
|
_logger.LogInformation("Plugin is disabled. Skipping.");
|
|
return;
|
|
}
|
|
|
|
var items = _libraryManager.RootFolder.GetRecursiveChildren()
|
|
.Where(x => x is Movie || x is Episode)
|
|
.ToList();
|
|
|
|
int total = items.Count;
|
|
int processed = 0;
|
|
|
|
foreach (var item in items)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
if (item.ProviderIds != null &&
|
|
item.ProviderIds.TryGetValue("BlackBarCrop", out string? existingCrop) &&
|
|
!string.IsNullOrEmpty(existingCrop))
|
|
{
|
|
processed++;
|
|
progress.Report((processed / (double)total) * 100);
|
|
continue;
|
|
}
|
|
|
|
var path = item.Path;
|
|
if (!string.IsNullOrEmpty(path) && File.Exists(path))
|
|
{
|
|
try
|
|
{
|
|
string cropData = await CropHelper.DetectCropAsync(path, config.CropDetectLimit);
|
|
|
|
if (!string.IsNullOrEmpty(cropData))
|
|
{
|
|
if (item.ProviderIds == null)
|
|
{
|
|
item.ProviderIds = new Dictionary<string, string>();
|
|
}
|
|
item.ProviderIds["BlackBarCrop"] = cropData;
|
|
await _libraryManager.UpdateItemAsync(item, item, ItemUpdateType.MetadataEdit, cancellationToken);
|
|
_logger.LogInformation("Found black bars {Crop} in {Path}", cropData, path);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error scanning file {Path}", path);
|
|
}
|
|
}
|
|
|
|
processed++;
|
|
progress.Report((processed / (double)total) * 100);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
|
{
|
|
return Array.Empty<TaskTriggerInfo>();
|
|
}
|
|
}
|
|
}
|