Enhance Trickplay (#11883)
parent
675a8a9ec9
commit
c56dbc1c44
@ -0,0 +1,73 @@
|
|||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using DiscUtils;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
|
using MediaBrowser.Controller.Library;
|
||||||
|
using MediaBrowser.Controller.Trickplay;
|
||||||
|
|
||||||
|
namespace Jellyfin.Server.Migrations.Routines;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Migration to move trickplay files to the new directory.
|
||||||
|
/// </summary>
|
||||||
|
public class MoveTrickplayFiles : IMigrationRoutine
|
||||||
|
{
|
||||||
|
private readonly ITrickplayManager _trickplayManager;
|
||||||
|
private readonly IFileSystem _fileSystem;
|
||||||
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="MoveTrickplayFiles"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="trickplayManager">Instance of the <see cref="ITrickplayManager"/> interface.</param>
|
||||||
|
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
|
||||||
|
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
|
||||||
|
public MoveTrickplayFiles(ITrickplayManager trickplayManager, IFileSystem fileSystem, ILibraryManager libraryManager)
|
||||||
|
{
|
||||||
|
_trickplayManager = trickplayManager;
|
||||||
|
_fileSystem = fileSystem;
|
||||||
|
_libraryManager = libraryManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Guid Id => new("4EF123D5-8EFF-4B0B-869D-3AED07A60E1B");
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string Name => "MoveTrickplayFiles";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool PerformOnNewInstall => true;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public void Perform()
|
||||||
|
{
|
||||||
|
var trickplayItems = _trickplayManager.GetTrickplayItemsAsync().GetAwaiter().GetResult();
|
||||||
|
foreach (var itemId in trickplayItems)
|
||||||
|
{
|
||||||
|
var resolutions = _trickplayManager.GetTrickplayResolutions(itemId).GetAwaiter().GetResult();
|
||||||
|
var item = _libraryManager.GetItemById(itemId);
|
||||||
|
if (item is null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var resolution in resolutions)
|
||||||
|
{
|
||||||
|
var oldPath = GetOldTrickplayDirectory(item, resolution.Key);
|
||||||
|
var newPath = _trickplayManager.GetTrickplayDirectory(item, resolution.Value.TileWidth, resolution.Value.TileHeight, resolution.Value.Width, false);
|
||||||
|
if (_fileSystem.DirectoryExists(oldPath))
|
||||||
|
{
|
||||||
|
_fileSystem.MoveDirectory(oldPath, newPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetOldTrickplayDirectory(BaseItem item, int? width = null)
|
||||||
|
{
|
||||||
|
var path = Path.Combine(item.GetInternalMetadataPath(), "trickplay");
|
||||||
|
|
||||||
|
return width.HasValue ? Path.Combine(path, width.Value.ToString(CultureInfo.InvariantCulture)) : path;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Jellyfin.Data.Enums;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
|
using MediaBrowser.Controller.Library;
|
||||||
|
using MediaBrowser.Controller.Trickplay;
|
||||||
|
using MediaBrowser.Model.Globalization;
|
||||||
|
using MediaBrowser.Model.Tasks;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Providers.Trickplay;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Class TrickplayMoveImagesTask.
|
||||||
|
/// </summary>
|
||||||
|
public class TrickplayMoveImagesTask : IScheduledTask
|
||||||
|
{
|
||||||
|
private const int QueryPageLimit = 100;
|
||||||
|
|
||||||
|
private readonly ILogger<TrickplayMoveImagesTask> _logger;
|
||||||
|
private readonly ILibraryManager _libraryManager;
|
||||||
|
private readonly ILocalizationManager _localization;
|
||||||
|
private readonly ITrickplayManager _trickplayManager;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="TrickplayMoveImagesTask"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger.</param>
|
||||||
|
/// <param name="libraryManager">The library manager.</param>
|
||||||
|
/// <param name="localization">The localization manager.</param>
|
||||||
|
/// <param name="trickplayManager">The trickplay manager.</param>
|
||||||
|
public TrickplayMoveImagesTask(
|
||||||
|
ILogger<TrickplayMoveImagesTask> logger,
|
||||||
|
ILibraryManager libraryManager,
|
||||||
|
ILocalizationManager localization,
|
||||||
|
ITrickplayManager trickplayManager)
|
||||||
|
{
|
||||||
|
_libraryManager = libraryManager;
|
||||||
|
_logger = logger;
|
||||||
|
_localization = localization;
|
||||||
|
_trickplayManager = trickplayManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string Name => _localization.GetLocalizedString("TaskMoveTrickplayImages");
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string Description => _localization.GetLocalizedString("TaskMoveTrickplayImagesDescription");
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string Key => "MoveTrickplayImages";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() => [];
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var trickplayItems = await _trickplayManager.GetTrickplayItemsAsync().ConfigureAwait(false);
|
||||||
|
var query = new InternalItemsQuery
|
||||||
|
{
|
||||||
|
MediaTypes = [MediaType.Video],
|
||||||
|
SourceTypes = [SourceType.Library],
|
||||||
|
IsVirtualItem = false,
|
||||||
|
IsFolder = false,
|
||||||
|
Recursive = true,
|
||||||
|
Limit = QueryPageLimit
|
||||||
|
};
|
||||||
|
|
||||||
|
var numberOfVideos = _libraryManager.GetCount(query);
|
||||||
|
|
||||||
|
var startIndex = 0;
|
||||||
|
var numComplete = 0;
|
||||||
|
|
||||||
|
while (startIndex < numberOfVideos)
|
||||||
|
{
|
||||||
|
query.StartIndex = startIndex;
|
||||||
|
var videos = _libraryManager.GetItemList(query).OfType<Video>().ToList();
|
||||||
|
videos.RemoveAll(i => !trickplayItems.Contains(i.Id));
|
||||||
|
|
||||||
|
foreach (var video in videos)
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var libraryOptions = _libraryManager.GetLibraryOptions(video);
|
||||||
|
await _trickplayManager.MoveGeneratedTrickplayDataAsync(video, libraryOptions, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error moving trickplay files for {ItemName}", video.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
numComplete++;
|
||||||
|
progress.Report(100d * numComplete / numberOfVideos);
|
||||||
|
}
|
||||||
|
|
||||||
|
startIndex += QueryPageLimit;
|
||||||
|
}
|
||||||
|
|
||||||
|
progress.Report(100);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue