Add pagination and fixes
This commit is contained in:
parent
70751722d2
commit
90736ee346
|
@ -8,6 +8,7 @@ using Jellyfin.MediaEncoding.Hls.Extractors;
|
||||||
using Jellyfin.MediaEncoding.Keyframes;
|
using Jellyfin.MediaEncoding.Keyframes;
|
||||||
using MediaBrowser.Common.Configuration;
|
using MediaBrowser.Common.Configuration;
|
||||||
using MediaBrowser.Common.Extensions;
|
using MediaBrowser.Common.Extensions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Jellyfin.MediaEncoding.Hls.Cache;
|
namespace Jellyfin.MediaEncoding.Hls.Cache;
|
||||||
|
|
||||||
|
@ -15,6 +16,8 @@ namespace Jellyfin.MediaEncoding.Hls.Cache;
|
||||||
public class CacheDecorator : IKeyframeExtractor
|
public class CacheDecorator : IKeyframeExtractor
|
||||||
{
|
{
|
||||||
private readonly IKeyframeExtractor _keyframeExtractor;
|
private readonly IKeyframeExtractor _keyframeExtractor;
|
||||||
|
private readonly ILogger<CacheDecorator> _logger;
|
||||||
|
private readonly string _keyframeExtractorName;
|
||||||
private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
||||||
private readonly string _keyframeCachePath;
|
private readonly string _keyframeCachePath;
|
||||||
|
|
||||||
|
@ -23,11 +26,15 @@ public class CacheDecorator : IKeyframeExtractor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="applicationPaths">An instance of the <see cref="IApplicationPaths"/> interface.</param>
|
/// <param name="applicationPaths">An instance of the <see cref="IApplicationPaths"/> interface.</param>
|
||||||
/// <param name="keyframeExtractor">An instance of the <see cref="IKeyframeExtractor"/> 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(applicationPaths);
|
||||||
|
ArgumentNullException.ThrowIfNull(keyframeExtractor);
|
||||||
|
|
||||||
|
_keyframeExtractor = keyframeExtractor;
|
||||||
|
_logger = logger;
|
||||||
|
_keyframeExtractorName = keyframeExtractor.GetType().Name;
|
||||||
// TODO make the dir configurable
|
// TODO make the dir configurable
|
||||||
_keyframeCachePath = Path.Combine(applicationPaths.DataPath, "keyframes");
|
_keyframeCachePath = Path.Combine(applicationPaths.DataPath, "keyframes");
|
||||||
}
|
}
|
||||||
|
@ -48,9 +55,11 @@ public class CacheDecorator : IKeyframeExtractor
|
||||||
|
|
||||||
if (!_keyframeExtractor.TryExtractKeyframes(filePath, out var result))
|
if (!_keyframeExtractor.TryExtractKeyframes(filePath, out var result))
|
||||||
{
|
{
|
||||||
|
_logger.LogDebug("Failed to extract keyframes using {ExtractorName}", _keyframeExtractorName);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_logger.LogDebug("Successfully extracted keyframes using {ExtractorName}", _keyframeExtractorName);
|
||||||
keyframeData = result;
|
keyframeData = result;
|
||||||
SaveToCache(cachePath, keyframeData);
|
SaveToCache(cachePath, keyframeData);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -18,6 +18,8 @@ namespace Jellyfin.MediaEncoding.Hls.ScheduledTasks;
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public class KeyframeExtractionScheduledTask : IScheduledTask
|
public class KeyframeExtractionScheduledTask : IScheduledTask
|
||||||
{
|
{
|
||||||
|
private const int Pagesize = 1000;
|
||||||
|
|
||||||
private readonly ILocalizationManager _localizationManager;
|
private readonly ILocalizationManager _localizationManager;
|
||||||
private readonly ILibraryManager _libraryManager;
|
private readonly ILibraryManager _libraryManager;
|
||||||
private readonly IKeyframeExtractor[] _keyframeExtractors;
|
private readonly IKeyframeExtractor[] _keyframeExtractors;
|
||||||
|
@ -33,7 +35,7 @@ public class KeyframeExtractionScheduledTask : IScheduledTask
|
||||||
{
|
{
|
||||||
_localizationManager = localizationManager;
|
_localizationManager = localizationManager;
|
||||||
_libraryManager = libraryManager;
|
_libraryManager = libraryManager;
|
||||||
_keyframeExtractors = keyframeExtractors.ToArray();
|
_keyframeExtractors = keyframeExtractors.OrderByDescending(e => e.IsMetadataBased).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
@ -43,7 +45,7 @@ public class KeyframeExtractionScheduledTask : IScheduledTask
|
||||||
public string Key => "KeyframeExtraction";
|
public string Key => "KeyframeExtraction";
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <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 />
|
/// <inheritdoc />
|
||||||
public string Category => _localizationManager.GetLocalizedString("TasksLibraryCategory");
|
public string Category => _localizationManager.GetLocalizedString("TasksLibraryCategory");
|
||||||
|
@ -58,14 +60,23 @@ public class KeyframeExtractionScheduledTask : IScheduledTask
|
||||||
IncludeItemTypes = _itemTypes,
|
IncludeItemTypes = _itemTypes,
|
||||||
DtoOptions = new DtoOptions(true),
|
DtoOptions = new DtoOptions(true),
|
||||||
SourceTypes = new[] { SourceType.Library },
|
SourceTypes = new[] { SourceType.Library },
|
||||||
Recursive = true
|
Recursive = true,
|
||||||
|
Limit = Pagesize
|
||||||
};
|
};
|
||||||
|
|
||||||
var videos = _libraryManager.GetItemList(query);
|
var numberOfVideos = _libraryManager.GetCount(query);
|
||||||
var numberOfVideos = videos.Count;
|
|
||||||
|
|
||||||
|
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?
|
// TODO parallelize with Parallel.ForEach?
|
||||||
for (var i = 0; i < numberOfVideos; i++)
|
for (var i = 0; i < currentPageCount; i++)
|
||||||
{
|
{
|
||||||
var video = videos[i];
|
var video = videos[i];
|
||||||
// Only local files supported
|
// Only local files supported
|
||||||
|
@ -83,10 +94,15 @@ public class KeyframeExtractionScheduledTask : IScheduledTask
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update progress
|
// Update progress
|
||||||
double percent = (double)(i + 1) / numberOfVideos;
|
numComplete++;
|
||||||
|
double percent = (double)numComplete / numberOfVideos;
|
||||||
progress.Report(100 * percent);
|
progress.Report(100 * percent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
startIndex += Pagesize;
|
||||||
|
}
|
||||||
|
|
||||||
|
progress.Report(100);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ internal static class EbmlReaderExtensions
|
||||||
|
|
||||||
if (!tracksPosition.HasValue || !cuesPosition.HasValue || !infoPosition.HasValue)
|
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);
|
return new SeekHead(infoPosition.Value, tracksPosition.Value, cuesPosition.Value);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions;
|
using Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions;
|
||||||
|
using Jellyfin.MediaEncoding.Keyframes.Matroska.Models;
|
||||||
using NEbml.Core;
|
using NEbml.Core;
|
||||||
|
|
||||||
namespace Jellyfin.MediaEncoding.Keyframes.Matroska;
|
namespace Jellyfin.MediaEncoding.Keyframes.Matroska;
|
||||||
|
@ -22,8 +23,19 @@ public static class MatroskaKeyframeExtractor
|
||||||
using var reader = new EbmlReader(stream);
|
using var reader = new EbmlReader(stream);
|
||||||
|
|
||||||
var seekHead = reader.ReadSeekHead();
|
var seekHead = reader.ReadSeekHead();
|
||||||
var info = reader.ReadInfo(seekHead.InfoPosition);
|
// External lib does not support seeking backwards (yet)
|
||||||
var videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo);
|
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>();
|
var keyframes = new List<long>();
|
||||||
reader.ReadAt(seekHead.CuesPosition);
|
reader.ReadAt(seekHead.CuesPosition);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user