< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.ScheduledTasks.Tasks.MediaSegmentExtractionTask
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/MediaSegmentExtractionTask.cs
Line coverage
55%
Covered lines: 5
Uncovered lines: 4
Coverable lines: 9
Total lines: 118
Line coverage: 55.5%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
.ctor(...)100%11100%
get_Name()100%11100%
get_Description()100%210%
get_Category()100%210%
get_Key()100%210%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/ScheduledTasks/Tasks/MediaSegmentExtractionTask.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Jellyfin.Data.Enums;
 7using MediaBrowser.Controller;
 8using MediaBrowser.Controller.Dto;
 9using MediaBrowser.Controller.Entities;
 10using MediaBrowser.Controller.Library;
 11using MediaBrowser.Model.Globalization;
 12using MediaBrowser.Model.Tasks;
 13
 14namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
 15
 16/// <summary>
 17/// Task to obtain media segments.
 18/// </summary>
 19public 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;
 027    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    {
 2237        _libraryManager = libraryManager;
 2238        _localization = localization;
 2239        _mediaSegmentManager = mediaSegmentManager;
 2240    }
 41
 42    /// <inheritdoc/>
 2243    public string Name => _localization.GetLocalizedString("TaskExtractMediaSegments");
 44
 45    /// <inheritdoc/>
 046    public string Description => _localization.GetLocalizedString("TaskExtractMediaSegmentsDescription");
 47
 48    /// <inheritdoc/>
 049    public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
 50
 51    /// <inheritdoc/>
 052    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 = new[] { MediaType.Video, MediaType.Audio },
 66            IsVirtualItem = false,
 67            IncludeItemTypes = _itemTypes,
 68            DtoOptions = new DtoOptions(true),
 69            SourceTypes = new[] { 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                    await _mediaSegmentManager.RunSegmentPluginProviders(item, false, cancellationToken).ConfigureAwait(
 95                }
 96
 97                // Update progress
 98                numComplete++;
 99                double percent = (double)numComplete / numberOfVideos;
 100                progress.Report(100 * percent);
 101            }
 102
 103            startIndex += pagesize;
 104        }
 105
 106        progress.Report(100);
 107    }
 108
 109    /// <inheritdoc/>
 110    public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
 111    {
 112        yield return new TaskTriggerInfo
 113        {
 114            Type = TaskTriggerInfo.TriggerInterval,
 115            IntervalTicks = TimeSpan.FromHours(12).Ticks
 116        };
 117    }
 118}