Merge 4168ac1efc
into 086fbd49cf
commit
a9462df25a
@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Database.Implementations;
|
||||
using Jellyfin.Database.Implementations.Entities;
|
||||
using MediaBrowser.Controller.Persistence;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Item;
|
||||
|
||||
/// <summary>
|
||||
/// Repository for obtaining Keyframe data.
|
||||
/// </summary>
|
||||
public class KeyframeRepository : IKeyframeRepository
|
||||
{
|
||||
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="KeyframeRepository"/> class.
|
||||
/// </summary>
|
||||
/// <param name="dbProvider">The EFCore db factory.</param>
|
||||
public KeyframeRepository(IDbContextFactory<JellyfinDbContext> dbProvider)
|
||||
{
|
||||
_dbProvider = dbProvider;
|
||||
}
|
||||
|
||||
private static MediaEncoding.Keyframes.KeyframeData Map(KeyframeData entity)
|
||||
{
|
||||
return new MediaEncoding.Keyframes.KeyframeData(
|
||||
entity.TotalDuration,
|
||||
(entity.KeyframeTicks ?? []).ToList());
|
||||
}
|
||||
|
||||
private KeyframeData Map(MediaEncoding.Keyframes.KeyframeData dto, Guid itemId)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
ItemId = itemId,
|
||||
TotalDuration = dto.TotalDuration,
|
||||
KeyframeTicks = dto.KeyframeTicks.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<MediaEncoding.Keyframes.KeyframeData> GetKeyframeData(Guid itemId)
|
||||
{
|
||||
using var context = _dbProvider.CreateDbContext();
|
||||
|
||||
return context.KeyframeData.AsNoTracking().Where(e => e.ItemId.Equals(itemId)).Select(e => Map(e)).ToList();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task SaveKeyframeDataAsync(Guid itemId, MediaEncoding.Keyframes.KeyframeData data, CancellationToken cancellationToken)
|
||||
{
|
||||
using var context = _dbProvider.CreateDbContext();
|
||||
using var transaction = await context.Database.BeginTransactionAsync(cancellationToken).ConfigureAwait(false);
|
||||
await context.KeyframeData.Where(e => e.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
|
||||
await context.KeyframeData.AddAsync(Map(data, itemId), cancellationToken).ConfigureAwait(false);
|
||||
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Jellyfin.Data.Enums;
|
||||
using Jellyfin.Database.Implementations;
|
||||
using Jellyfin.Database.Implementations.Entities;
|
||||
using Jellyfin.Extensions.Json;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Jellyfin.Server.Migrations.Routines;
|
||||
|
||||
/// <summary>
|
||||
/// Migration to move extracted files to the new directories.
|
||||
/// </summary>
|
||||
public class MigrateKeyframeData : IDatabaseMigrationRoutine
|
||||
{
|
||||
private readonly ILibraryManager _libraryManager;
|
||||
private readonly ILogger<MoveTrickplayFiles> _logger;
|
||||
private readonly IApplicationPaths _appPaths;
|
||||
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
|
||||
private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MigrateKeyframeData"/> class.
|
||||
/// </summary>
|
||||
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
|
||||
/// <param name="dbProvider">The EFCore db factory.</param>
|
||||
public MigrateKeyframeData(
|
||||
ILibraryManager libraryManager,
|
||||
ILogger<MoveTrickplayFiles> logger,
|
||||
IApplicationPaths appPaths,
|
||||
IDbContextFactory<JellyfinDbContext> dbProvider)
|
||||
{
|
||||
_libraryManager = libraryManager;
|
||||
_logger = logger;
|
||||
_appPaths = appPaths;
|
||||
_dbProvider = dbProvider;
|
||||
}
|
||||
|
||||
private string KeyframeCachePath => Path.Combine(_appPaths.DataPath, "keyframes");
|
||||
|
||||
/// <inheritdoc />
|
||||
public Guid Id => new("EA4bCAE1-09A4-428E-9B90-4B4FD2EA1B24");
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Name => "MigrateKeyframeData";
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool PerformOnNewInstall => false;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Perform()
|
||||
{
|
||||
const int Limit = 100;
|
||||
int itemCount = 0, offset = 0, previousCount;
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
var itemsQuery = new InternalItemsQuery
|
||||
{
|
||||
MediaTypes = [MediaType.Video],
|
||||
SourceTypes = [SourceType.Library],
|
||||
IsVirtualItem = false,
|
||||
IsFolder = false
|
||||
};
|
||||
|
||||
using var context = _dbProvider.CreateDbContext();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
List<KeyframeData> keyframes = [];
|
||||
|
||||
do
|
||||
{
|
||||
var result = _libraryManager.GetItemsResult(itemsQuery);
|
||||
_logger.LogInformation("Importing keyframes for {Count} items", result.TotalRecordCount);
|
||||
|
||||
var items = result.Items;
|
||||
previousCount = items.Count;
|
||||
offset += Limit;
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (TryGetKeyframeData(item, out var data))
|
||||
{
|
||||
keyframes.Add(data);
|
||||
}
|
||||
|
||||
if (++itemCount % 10_000 == 0)
|
||||
{
|
||||
context.KeyframeData.AddRange(keyframes);
|
||||
keyframes.Clear();
|
||||
_logger.LogInformation("Imported keyframes for {Count} items in {Time}", itemCount, sw.Elapsed);
|
||||
}
|
||||
}
|
||||
} while (previousCount == Limit);
|
||||
|
||||
context.KeyframeData.AddRange(keyframes);
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
|
||||
_logger.LogInformation("Imported keyframes for {Count} items in {Time}", itemCount, sw.Elapsed);
|
||||
|
||||
Directory.Delete(KeyframeCachePath, true);
|
||||
}
|
||||
|
||||
private bool TryGetKeyframeData(BaseItem item, [NotNullWhen(true)] out KeyframeData? data)
|
||||
{
|
||||
data = null;
|
||||
var path = item.Path;
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
var cachePath = GetCachePath(KeyframeCachePath, path);
|
||||
if (TryReadFromCache(cachePath, out var keyframeData))
|
||||
{
|
||||
data = new()
|
||||
{
|
||||
ItemId = item.Id,
|
||||
KeyframeTicks = keyframeData.KeyframeTicks.ToList(),
|
||||
TotalDuration = keyframeData.TotalDuration
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private string? GetCachePath(string keyframeCachePath, string filePath)
|
||||
{
|
||||
DateTime? lastWriteTimeUtc;
|
||||
try
|
||||
{
|
||||
lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_logger.LogDebug("Skipping {Path}: {Exception}", filePath, e.Message);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
ReadOnlySpan<char> filename = (filePath + "_" + lastWriteTimeUtc.Value.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5() + ".json";
|
||||
var prefix = filename[..1];
|
||||
|
||||
return Path.Join(keyframeCachePath, prefix, filename);
|
||||
}
|
||||
|
||||
private static bool TryReadFromCache(string? cachePath, [NotNullWhen(true)] out MediaEncoding.Keyframes.KeyframeData? cachedResult)
|
||||
{
|
||||
if (File.Exists(cachePath))
|
||||
{
|
||||
var bytes = File.ReadAllBytes(cachePath);
|
||||
cachedResult = JsonSerializer.Deserialize<MediaEncoding.Keyframes.KeyframeData>(bytes, _jsonOptions);
|
||||
|
||||
return cachedResult is not null;
|
||||
}
|
||||
|
||||
cachedResult = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.MediaEncoding.Keyframes;
|
||||
|
||||
namespace MediaBrowser.Controller.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// Provides methods for accessing keyframe data.
|
||||
/// </summary>
|
||||
public interface IKeyframeRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the keyframe data.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item id.</param>
|
||||
/// <returns>The keyframe data.</returns>
|
||||
IReadOnlyList<KeyframeData> GetKeyframeData(Guid itemId);
|
||||
|
||||
/// <summary>
|
||||
/// Saves the keyframe data.
|
||||
/// </summary>
|
||||
/// <param name="itemId">The item id.</param>
|
||||
/// <param name="data">The keyframe data.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The task object representing the asynchronous operation.</returns>
|
||||
Task SaveKeyframeDataAsync(Guid itemId, KeyframeData data, CancellationToken cancellationToken);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
#pragma warning disable CA2227 // Collection properties should be read only
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Jellyfin.Database.Implementations.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Keyframe information for a specific file.
|
||||
/// </summary>
|
||||
public class KeyframeData
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or Sets the ItemId.
|
||||
/// </summary>
|
||||
public required Guid ItemId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the total duration of the stream in ticks.
|
||||
/// </summary>
|
||||
public long TotalDuration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the keyframes in ticks.
|
||||
/// </summary>
|
||||
public ICollection<long>? KeyframeTicks { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the item reference.
|
||||
/// </summary>
|
||||
public BaseItemEntity? Item { get; set; }
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using Jellyfin.Database.Implementations.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Jellyfin.Database.Implementations.ModelConfiguration;
|
||||
|
||||
/// <summary>
|
||||
/// KeyframeData Configuration.
|
||||
/// </summary>
|
||||
public class KeyframeDataConfiguration : IEntityTypeConfiguration<KeyframeData>
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Configure(EntityTypeBuilder<KeyframeData> builder)
|
||||
{
|
||||
builder.HasKey(e => e.ItemId);
|
||||
builder.HasOne(e => e.Item).WithMany().HasForeignKey(e => e.ItemId);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue