Add pagination and fixes

This commit is contained in:
cvium 2022-01-16 22:10:22 +01:00
parent 70751722d2
commit 90736ee346
4 changed files with 60 additions and 23 deletions

View File

@ -8,6 +8,7 @@ using Jellyfin.MediaEncoding.Hls.Extractors;
using Jellyfin.MediaEncoding.Keyframes;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using Microsoft.Extensions.Logging;
namespace Jellyfin.MediaEncoding.Hls.Cache;
@ -15,6 +16,8 @@ namespace Jellyfin.MediaEncoding.Hls.Cache;
public class CacheDecorator : IKeyframeExtractor
{
private readonly IKeyframeExtractor _keyframeExtractor;
private readonly ILogger<CacheDecorator> _logger;
private readonly string _keyframeExtractorName;
private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
private readonly string _keyframeCachePath;
@ -23,11 +26,15 @@ public class CacheDecorator : IKeyframeExtractor
/// </summary>
/// <param name="applicationPaths">An instance of the <see cref="IApplicationPaths"/> interface.</param>
/// <param name="keyframeExtractor">An instance of the <see cref="IKeyframeExtractor"/> interface.</param>
public CacheDecorator(IApplicationPaths applicationPaths, IKeyframeExtractor keyframeExtractor)
/// <param name="logger">An instance of the <see cref="ILogger{CacheDecorator}"/> interface.</param>
public CacheDecorator(IApplicationPaths applicationPaths, IKeyframeExtractor keyframeExtractor, ILogger<CacheDecorator> logger)
{
_keyframeExtractor = keyframeExtractor;
ArgumentNullException.ThrowIfNull(applicationPaths);
ArgumentNullException.ThrowIfNull(keyframeExtractor);
_keyframeExtractor = keyframeExtractor;
_logger = logger;
_keyframeExtractorName = keyframeExtractor.GetType().Name;
// TODO make the dir configurable
_keyframeCachePath = Path.Combine(applicationPaths.DataPath, "keyframes");
}
@ -48,9 +55,11 @@ public class CacheDecorator : IKeyframeExtractor
if (!_keyframeExtractor.TryExtractKeyframes(filePath, out var result))
{
_logger.LogDebug("Failed to extract keyframes using {ExtractorName}", _keyframeExtractorName);
return false;
}
_logger.LogDebug("Successfully extracted keyframes using {ExtractorName}", _keyframeExtractorName);
keyframeData = result;
SaveToCache(cachePath, keyframeData);
return true;

View File

@ -18,6 +18,8 @@ namespace Jellyfin.MediaEncoding.Hls.ScheduledTasks;
/// <inheritdoc />
public class KeyframeExtractionScheduledTask : IScheduledTask
{
private const int Pagesize = 1000;
private readonly ILocalizationManager _localizationManager;
private readonly ILibraryManager _libraryManager;
private readonly IKeyframeExtractor[] _keyframeExtractors;
@ -33,7 +35,7 @@ public class KeyframeExtractionScheduledTask : IScheduledTask
{
_localizationManager = localizationManager;
_libraryManager = libraryManager;
_keyframeExtractors = keyframeExtractors.ToArray();
_keyframeExtractors = keyframeExtractors.OrderByDescending(e => e.IsMetadataBased).ToArray();
}
/// <inheritdoc />
@ -43,7 +45,7 @@ public class KeyframeExtractionScheduledTask : IScheduledTask
public string Key => "KeyframeExtraction";
/// <inheritdoc />
public string Description => "Extracts keyframes from video files to create more precise HLS playlists";
public string Description => "Extracts keyframes from video files to create more precise HLS playlists. This task may run for a long time.";
/// <inheritdoc />
public string Category => _localizationManager.GetLocalizedString("TasksLibraryCategory");
@ -58,14 +60,23 @@ public class KeyframeExtractionScheduledTask : IScheduledTask
IncludeItemTypes = _itemTypes,
DtoOptions = new DtoOptions(true),
SourceTypes = new[] { SourceType.Library },
Recursive = true
Recursive = true,
Limit = Pagesize
};
var videos = _libraryManager.GetItemList(query);
var numberOfVideos = videos.Count;
var numberOfVideos = _libraryManager.GetCount(query);
var startIndex = 0;
var numComplete = 0;
while (startIndex < numberOfVideos)
{
query.StartIndex = startIndex;
var videos = _libraryManager.GetItemList(query);
var currentPageCount = videos.Count;
// TODO parallelize with Parallel.ForEach?
for (var i = 0; i < numberOfVideos; i++)
for (var i = 0; i < currentPageCount; i++)
{
var video = videos[i];
// Only local files supported
@ -83,10 +94,15 @@ public class KeyframeExtractionScheduledTask : IScheduledTask
}
// Update progress
double percent = (double)(i + 1) / numberOfVideos;
numComplete++;
double percent = (double)numComplete / numberOfVideos;
progress.Report(100 * percent);
}
startIndex += Pagesize;
}
progress.Report(100);
return Task.CompletedTask;
}

View File

@ -106,7 +106,7 @@ internal static class EbmlReaderExtensions
if (!tracksPosition.HasValue || !cuesPosition.HasValue || !infoPosition.HasValue)
{
throw new InvalidOperationException("SeekHead is missing or does not contain Info, Tracks and Cues positions");
throw new InvalidOperationException("SeekHead is missing or does not contain Info, Tracks and Cues positions. SeekHead referencing another SeekHead is not supported");
}
return new SeekHead(infoPosition.Value, tracksPosition.Value, cuesPosition.Value);

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions;
using Jellyfin.MediaEncoding.Keyframes.Matroska.Models;
using NEbml.Core;
namespace Jellyfin.MediaEncoding.Keyframes.Matroska;
@ -22,8 +23,19 @@ public static class MatroskaKeyframeExtractor
using var reader = new EbmlReader(stream);
var seekHead = reader.ReadSeekHead();
var info = reader.ReadInfo(seekHead.InfoPosition);
var videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo);
// External lib does not support seeking backwards (yet)
Info info;
ulong videoTrackNumber;
if (seekHead.InfoPosition < seekHead.TracksPosition)
{
info = reader.ReadInfo(seekHead.InfoPosition);
videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo);
}
else
{
videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo);
info = reader.ReadInfo(seekHead.InfoPosition);
}
var keyframes = new List<long>();
reader.ReadAt(seekHead.CuesPosition);