| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using MediaBrowser.Controller.Dto; |
| | | 8 | | using MediaBrowser.Controller.Entities; |
| | | 9 | | using MediaBrowser.Controller.Library; |
| | | 10 | | using MediaBrowser.Controller.MediaSegments; |
| | | 11 | | using MediaBrowser.Model.Globalization; |
| | | 12 | | using MediaBrowser.Model.Tasks; |
| | | 13 | | |
| | | 14 | | namespace Emby.Server.Implementations.ScheduledTasks.Tasks; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Task to obtain media segments. |
| | | 18 | | /// </summary> |
| | | 19 | | public class MediaSegmentExtractionTask : IScheduledTask |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// The library manager. |
| | | 23 | | /// </summary> |
| | | 24 | | private readonly ILibraryManager _libraryManager; |
| | | 25 | | private readonly ILocalizationManager _localization; |
| | | 26 | | private readonly IMediaSegmentManager _mediaSegmentManager; |
| | 0 | 27 | | private static readonly BaseItemKind[] _itemTypes = [BaseItemKind.Episode, BaseItemKind.Movie, BaseItemKind.Audio, B |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="MediaSegmentExtractionTask" /> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="libraryManager">The library manager.</param> |
| | | 33 | | /// <param name="localization">The localization manager.</param> |
| | | 34 | | /// <param name="mediaSegmentManager">The segment manager.</param> |
| | | 35 | | public MediaSegmentExtractionTask(ILibraryManager libraryManager, ILocalizationManager localization, IMediaSegmentMa |
| | | 36 | | { |
| | 21 | 37 | | _libraryManager = libraryManager; |
| | 21 | 38 | | _localization = localization; |
| | 21 | 39 | | _mediaSegmentManager = mediaSegmentManager; |
| | 21 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc/> |
| | 21 | 43 | | public string Name => _localization.GetLocalizedString("TaskExtractMediaSegments"); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc/> |
| | 0 | 46 | | public string Description => _localization.GetLocalizedString("TaskExtractMediaSegmentsDescription"); |
| | | 47 | | |
| | | 48 | | /// <inheritdoc/> |
| | 0 | 49 | | public string Category => _localization.GetLocalizedString("TasksLibraryCategory"); |
| | | 50 | | |
| | | 51 | | /// <inheritdoc/> |
| | 0 | 52 | | public string Key => "TaskExtractMediaSegments"; |
| | | 53 | | |
| | | 54 | | /// <inheritdoc/> |
| | | 55 | | public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) |
| | | 56 | | { |
| | | 57 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 58 | | |
| | | 59 | | progress.Report(0); |
| | | 60 | | |
| | | 61 | | var pagesize = 100; |
| | | 62 | | |
| | | 63 | | var query = new InternalItemsQuery |
| | | 64 | | { |
| | | 65 | | MediaTypes = [MediaType.Video, MediaType.Audio], |
| | | 66 | | IsVirtualItem = false, |
| | | 67 | | IncludeItemTypes = _itemTypes, |
| | | 68 | | DtoOptions = new DtoOptions(true), |
| | | 69 | | SourceTypes = [SourceType.Library], |
| | | 70 | | Recursive = true, |
| | | 71 | | Limit = pagesize |
| | | 72 | | }; |
| | | 73 | | |
| | | 74 | | var numberOfVideos = _libraryManager.GetCount(query); |
| | | 75 | | |
| | | 76 | | var startIndex = 0; |
| | | 77 | | var numComplete = 0; |
| | | 78 | | |
| | | 79 | | while (startIndex < numberOfVideos) |
| | | 80 | | { |
| | | 81 | | query.StartIndex = startIndex; |
| | | 82 | | |
| | | 83 | | var baseItems = _libraryManager.GetItemList(query); |
| | | 84 | | var currentPageCount = baseItems.Count; |
| | | 85 | | // TODO parallelize with Parallel.ForEach? |
| | | 86 | | for (var i = 0; i < currentPageCount; i++) |
| | | 87 | | { |
| | | 88 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 89 | | |
| | | 90 | | var item = baseItems[i]; |
| | | 91 | | // Only local files supported |
| | | 92 | | if (item.IsFileProtocol && File.Exists(item.Path)) |
| | | 93 | | { |
| | | 94 | | var libraryOptions = _libraryManager.GetLibraryOptions(item); |
| | | 95 | | await _mediaSegmentManager.RunSegmentPluginProviders(item, libraryOptions, false, cancellationToken) |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | // Update progress |
| | | 99 | | numComplete++; |
| | | 100 | | double percent = (double)numComplete / numberOfVideos; |
| | | 101 | | progress.Report(100 * percent); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | startIndex += pagesize; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | progress.Report(100); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc/> |
| | | 111 | | public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() |
| | | 112 | | { |
| | | 113 | | yield return new TaskTriggerInfo |
| | | 114 | | { |
| | | 115 | | Type = TaskTriggerInfoType.IntervalTrigger, |
| | | 116 | | IntervalTicks = TimeSpan.FromHours(12).Ticks |
| | | 117 | | }; |
| | | 118 | | } |
| | | 119 | | } |