| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Jellyfin.Data.Enums; |
| | 8 | | using Jellyfin.MediaEncoding.Hls.Extractors; |
| | 9 | | using MediaBrowser.Controller.Dto; |
| | 10 | | using MediaBrowser.Controller.Entities; |
| | 11 | | using MediaBrowser.Controller.Library; |
| | 12 | | using MediaBrowser.Model.Entities; |
| | 13 | | using MediaBrowser.Model.Globalization; |
| | 14 | | using MediaBrowser.Model.Tasks; |
| | 15 | |
|
| | 16 | | namespace Jellyfin.MediaEncoding.Hls.ScheduledTasks; |
| | 17 | |
|
| | 18 | | /// <inheritdoc /> |
| | 19 | | public class KeyframeExtractionScheduledTask : IScheduledTask |
| | 20 | | { |
| | 21 | | private const int Pagesize = 1000; |
| | 22 | |
|
| | 23 | | private readonly ILocalizationManager _localizationManager; |
| | 24 | | private readonly ILibraryManager _libraryManager; |
| | 25 | | private readonly IKeyframeExtractor[] _keyframeExtractors; |
| 0 | 26 | | private static readonly BaseItemKind[] _itemTypes = { BaseItemKind.Episode, BaseItemKind.Movie }; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Initializes a new instance of the <see cref="KeyframeExtractionScheduledTask"/> class. |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="localizationManager">An instance of the <see cref="ILocalizationManager"/> interface.</param> |
| | 32 | | /// <param name="libraryManager">An instance of the <see cref="ILibraryManager"/> interface.</param> |
| | 33 | | /// <param name="keyframeExtractors">The keyframe extractors.</param> |
| | 34 | | public KeyframeExtractionScheduledTask(ILocalizationManager localizationManager, ILibraryManager libraryManager, IEn |
| 21 | 35 | | { |
| 21 | 36 | | _localizationManager = localizationManager; |
| 21 | 37 | | _libraryManager = libraryManager; |
| 21 | 38 | | _keyframeExtractors = keyframeExtractors.OrderByDescending(e => e.IsMetadataBased).ToArray(); |
| 21 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <inheritdoc /> |
| 0 | 42 | | public string Name => _localizationManager.GetLocalizedString("TaskKeyframeExtractor"); |
| | 43 | |
|
| | 44 | | /// <inheritdoc /> |
| 0 | 45 | | public string Key => "KeyframeExtraction"; |
| | 46 | |
|
| | 47 | | /// <inheritdoc /> |
| 0 | 48 | | public string Description => _localizationManager.GetLocalizedString("TaskKeyframeExtractorDescription"); |
| | 49 | |
|
| | 50 | | /// <inheritdoc /> |
| 0 | 51 | | public string Category => _localizationManager.GetLocalizedString("TasksLibraryCategory"); |
| | 52 | |
|
| | 53 | | /// <inheritdoc /> |
| | 54 | | public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) |
| 0 | 55 | | { |
| 0 | 56 | | var query = new InternalItemsQuery |
| 0 | 57 | | { |
| 0 | 58 | | MediaTypes = new[] { MediaType.Video }, |
| 0 | 59 | | IsVirtualItem = false, |
| 0 | 60 | | IncludeItemTypes = _itemTypes, |
| 0 | 61 | | DtoOptions = new DtoOptions(true), |
| 0 | 62 | | SourceTypes = new[] { SourceType.Library }, |
| 0 | 63 | | Recursive = true, |
| 0 | 64 | | Limit = Pagesize |
| 0 | 65 | | }; |
| | 66 | |
|
| 0 | 67 | | var numberOfVideos = _libraryManager.GetCount(query); |
| | 68 | |
|
| 0 | 69 | | var startIndex = 0; |
| 0 | 70 | | var numComplete = 0; |
| | 71 | |
|
| 0 | 72 | | while (startIndex < numberOfVideos) |
| 0 | 73 | | { |
| 0 | 74 | | query.StartIndex = startIndex; |
| | 75 | |
|
| 0 | 76 | | var videos = _libraryManager.GetItemList(query); |
| 0 | 77 | | var currentPageCount = videos.Count; |
| | 78 | | // TODO parallelize with Parallel.ForEach? |
| 0 | 79 | | for (var i = 0; i < currentPageCount; i++) |
| 0 | 80 | | { |
| 0 | 81 | | var video = videos[i]; |
| | 82 | | // Only local files supported |
| 0 | 83 | | if (video.IsFileProtocol && File.Exists(video.Path)) |
| 0 | 84 | | { |
| 0 | 85 | | for (var j = 0; j < _keyframeExtractors.Length; j++) |
| 0 | 86 | | { |
| 0 | 87 | | var extractor = _keyframeExtractors[j]; |
| | 88 | | // The cache decorator will make sure to save them in the data dir |
| 0 | 89 | | if (extractor.TryExtractKeyframes(video.Path, out _)) |
| 0 | 90 | | { |
| 0 | 91 | | break; |
| | 92 | | } |
| 0 | 93 | | } |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | // Update progress |
| 0 | 97 | | numComplete++; |
| 0 | 98 | | double percent = (double)numComplete / numberOfVideos; |
| 0 | 99 | | progress.Report(100 * percent); |
| 0 | 100 | | } |
| | 101 | |
|
| 0 | 102 | | startIndex += Pagesize; |
| 0 | 103 | | } |
| | 104 | |
|
| 0 | 105 | | progress.Report(100); |
| 0 | 106 | | return Task.CompletedTask; |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <inheritdoc /> |
| 21 | 110 | | public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() => Enumerable.Empty<TaskTriggerInfo>(); |
| | 111 | | } |