| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Jellyfin.Data.Enums; |
| | 7 | | using MediaBrowser.Controller.Entities; |
| | 8 | | using MediaBrowser.Controller.Library; |
| | 9 | | using MediaBrowser.Controller.Trickplay; |
| | 10 | | using MediaBrowser.Model.Globalization; |
| | 11 | | using MediaBrowser.Model.Tasks; |
| | 12 | | using Microsoft.Extensions.Logging; |
| | 13 | |
|
| | 14 | | namespace MediaBrowser.Providers.Trickplay; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Class TrickplayImagesTask. |
| | 18 | | /// </summary> |
| | 19 | | public class TrickplayImagesTask : IScheduledTask |
| | 20 | | { |
| | 21 | | private const int QueryPageLimit = 100; |
| | 22 | |
|
| | 23 | | private readonly ILogger<TrickplayImagesTask> _logger; |
| | 24 | | private readonly ILibraryManager _libraryManager; |
| | 25 | | private readonly ILocalizationManager _localization; |
| | 26 | | private readonly ITrickplayManager _trickplayManager; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Initializes a new instance of the <see cref="TrickplayImagesTask"/> class. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="logger">The logger.</param> |
| | 32 | | /// <param name="libraryManager">The library manager.</param> |
| | 33 | | /// <param name="localization">The localization manager.</param> |
| | 34 | | /// <param name="trickplayManager">The trickplay manager.</param> |
| | 35 | | public TrickplayImagesTask( |
| | 36 | | ILogger<TrickplayImagesTask> logger, |
| | 37 | | ILibraryManager libraryManager, |
| | 38 | | ILocalizationManager localization, |
| | 39 | | ITrickplayManager trickplayManager) |
| | 40 | | { |
| 21 | 41 | | _libraryManager = libraryManager; |
| 21 | 42 | | _logger = logger; |
| 21 | 43 | | _localization = localization; |
| 21 | 44 | | _trickplayManager = trickplayManager; |
| 21 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <inheritdoc /> |
| 21 | 48 | | public string Name => _localization.GetLocalizedString("TaskRefreshTrickplayImages"); |
| | 49 | |
|
| | 50 | | /// <inheritdoc /> |
| 0 | 51 | | public string Description => _localization.GetLocalizedString("TaskRefreshTrickplayImagesDescription"); |
| | 52 | |
|
| | 53 | | /// <inheritdoc /> |
| 0 | 54 | | public string Key => "RefreshTrickplayImages"; |
| | 55 | |
|
| | 56 | | /// <inheritdoc /> |
| 0 | 57 | | public string Category => _localization.GetLocalizedString("TasksLibraryCategory"); |
| | 58 | |
|
| | 59 | | /// <inheritdoc /> |
| | 60 | | public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() |
| | 61 | | { |
| 21 | 62 | | return new[] |
| 21 | 63 | | { |
| 21 | 64 | | new TaskTriggerInfo |
| 21 | 65 | | { |
| 21 | 66 | | Type = TaskTriggerInfoType.DailyTrigger, |
| 21 | 67 | | TimeOfDayTicks = TimeSpan.FromHours(3).Ticks |
| 21 | 68 | | } |
| 21 | 69 | | }; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | /// <inheritdoc /> |
| | 73 | | public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) |
| | 74 | | { |
| | 75 | | var query = new InternalItemsQuery |
| | 76 | | { |
| | 77 | | MediaTypes = new[] { MediaType.Video }, |
| | 78 | | SourceTypes = new[] { SourceType.Library }, |
| | 79 | | IsVirtualItem = false, |
| | 80 | | IsFolder = false, |
| | 81 | | Recursive = true, |
| | 82 | | Limit = QueryPageLimit |
| | 83 | | }; |
| | 84 | |
|
| | 85 | | var numberOfVideos = _libraryManager.GetCount(query); |
| | 86 | |
|
| | 87 | | var startIndex = 0; |
| | 88 | | var numComplete = 0; |
| | 89 | |
|
| | 90 | | while (startIndex < numberOfVideos) |
| | 91 | | { |
| | 92 | | query.StartIndex = startIndex; |
| | 93 | | var videos = _libraryManager.GetItemList(query).OfType<Video>(); |
| | 94 | |
|
| | 95 | | foreach (var video in videos) |
| | 96 | | { |
| | 97 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 98 | |
|
| | 99 | | try |
| | 100 | | { |
| | 101 | | var libraryOptions = _libraryManager.GetLibraryOptions(video); |
| | 102 | | await _trickplayManager.RefreshTrickplayDataAsync(video, false, libraryOptions, cancellationToken).C |
| | 103 | | } |
| | 104 | | catch (Exception ex) |
| | 105 | | { |
| | 106 | | _logger.LogError(ex, "Error creating trickplay files for {ItemName}", video.Name); |
| | 107 | | } |
| | 108 | |
|
| | 109 | | numComplete++; |
| | 110 | | progress.Report(100d * numComplete / numberOfVideos); |
| | 111 | | } |
| | 112 | |
|
| | 113 | | startIndex += QueryPageLimit; |
| | 114 | | } |
| | 115 | |
|
| | 116 | | progress.Report(100); |
| | 117 | | } |
| | 118 | | } |