jellyfin/MediaBrowser.Providers/Trickplay/TrickplayImagesTask.cs

118 lines
3.6 KiB
C#
Raw Normal View History

2023-02-22 08:08:35 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2023-11-10 15:01:39 +00:00
using Jellyfin.Data.Enums;
2023-02-22 08:08:35 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Trickplay;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.Trickplay;
/// <summary>
/// Class TrickplayImagesTask.
/// </summary>
public class TrickplayImagesTask : IScheduledTask
2023-02-22 08:08:35 +00:00
{
2023-05-18 07:45:12 +00:00
private const int QueryPageLimit = 100;
private readonly ILogger<TrickplayImagesTask> _logger;
private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localization;
private readonly ITrickplayManager _trickplayManager;
2023-02-22 08:08:35 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="TrickplayImagesTask"/> class.
2023-02-22 08:08:35 +00:00
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="libraryManager">The library manager.</param>
/// <param name="localization">The localization manager.</param>
/// <param name="trickplayManager">The trickplay manager.</param>
public TrickplayImagesTask(
ILogger<TrickplayImagesTask> logger,
ILibraryManager libraryManager,
ILocalizationManager localization,
ITrickplayManager trickplayManager)
2023-02-22 08:08:35 +00:00
{
_libraryManager = libraryManager;
_logger = logger;
_localization = localization;
_trickplayManager = trickplayManager;
}
2023-02-22 08:08:35 +00:00
/// <inheritdoc />
public string Name => _localization.GetLocalizedString("TaskRefreshTrickplayImages");
2023-02-22 08:08:35 +00:00
/// <inheritdoc />
public string Description => _localization.GetLocalizedString("TaskRefreshTrickplayImagesDescription");
2023-02-22 08:08:35 +00:00
/// <inheritdoc />
public string Key => "RefreshTrickplayImages";
2023-02-22 08:08:35 +00:00
/// <inheritdoc />
public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
2023-02-22 08:08:35 +00:00
/// <inheritdoc />
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
return new[]
2023-02-22 08:08:35 +00:00
{
new TaskTriggerInfo
2023-02-22 08:08:35 +00:00
{
Type = TaskTriggerInfo.TriggerDaily,
TimeOfDayTicks = TimeSpan.FromHours(3).Ticks
}
};
}
2023-02-22 08:08:35 +00:00
/// <inheritdoc />
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
2023-05-18 07:45:12 +00:00
var query = new InternalItemsQuery
2023-02-22 08:08:35 +00:00
{
MediaTypes = new[] { MediaType.Video },
2023-05-18 07:45:12 +00:00
SourceTypes = new[] { SourceType.Library },
IsVirtualItem = false,
IsFolder = false,
2023-05-18 07:45:12 +00:00
Recursive = true,
Limit = QueryPageLimit
};
var numberOfVideos = _libraryManager.GetCount(query);
2023-02-22 08:08:35 +00:00
2023-05-18 07:45:12 +00:00
var startIndex = 0;
var numComplete = 0;
2023-02-22 08:08:35 +00:00
2023-05-18 07:45:12 +00:00
while (startIndex < numberOfVideos)
{
2023-05-18 07:45:12 +00:00
query.StartIndex = startIndex;
2023-06-23 21:30:55 +00:00
var videos = _libraryManager.GetItemList(query).OfType<Video>();
2023-05-18 07:45:12 +00:00
foreach (var video in videos)
{
cancellationToken.ThrowIfCancellationRequested();
2023-02-22 08:08:35 +00:00
2023-05-18 07:45:12 +00:00
try
{
await _trickplayManager.RefreshTrickplayDataAsync(video, false, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error creating trickplay files for {ItemName}", video.Name);
}
2023-02-22 08:08:35 +00:00
2023-05-18 07:45:12 +00:00
numComplete++;
progress.Report(100d * numComplete / numberOfVideos);
}
startIndex += QueryPageLimit;
2023-02-22 08:08:35 +00:00
}
2023-05-18 07:45:12 +00:00
progress.Report(100);
2023-02-22 08:08:35 +00:00
}
}