MigrateKeyframeData.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text.Json;
  8. using Jellyfin.Data.Enums;
  9. using Jellyfin.Database.Implementations;
  10. using Jellyfin.Database.Implementations.Entities;
  11. using Jellyfin.Extensions.Json;
  12. using MediaBrowser.Common.Configuration;
  13. using MediaBrowser.Common.Extensions;
  14. using Microsoft.EntityFrameworkCore;
  15. using Microsoft.Extensions.Logging;
  16. namespace Jellyfin.Server.Migrations.Routines;
  17. /// <summary>
  18. /// Migration to move extracted files to the new directories.
  19. /// </summary>
  20. [JellyfinMigration("2025-04-21T00:00:00", nameof(MigrateKeyframeData))]
  21. public class MigrateKeyframeData : IDatabaseMigrationRoutine
  22. {
  23. private readonly ILogger<MigrateKeyframeData> _logger;
  24. private readonly IApplicationPaths _appPaths;
  25. private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
  26. private static readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="MigrateKeyframeData"/> class.
  29. /// </summary>
  30. /// <param name="logger">The logger.</param>
  31. /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
  32. /// <param name="dbProvider">The EFCore db factory.</param>
  33. public MigrateKeyframeData(
  34. ILogger<MigrateKeyframeData> logger,
  35. IApplicationPaths appPaths,
  36. IDbContextFactory<JellyfinDbContext> dbProvider)
  37. {
  38. _logger = logger;
  39. _appPaths = appPaths;
  40. _dbProvider = dbProvider;
  41. }
  42. private string KeyframeCachePath => Path.Combine(_appPaths.DataPath, "keyframes");
  43. /// <inheritdoc />
  44. public void Perform()
  45. {
  46. const int Limit = 5000;
  47. int itemCount = 0, offset = 0;
  48. var sw = Stopwatch.StartNew();
  49. using var context = _dbProvider.CreateDbContext();
  50. var baseQuery = context.BaseItems.Where(b => b.MediaType == MediaType.Video.ToString() && !b.IsVirtualItem && !b.IsFolder).OrderBy(e => e.Id);
  51. var records = baseQuery.Count();
  52. _logger.LogInformation("Checking {Count} items for importable keyframe data.", records);
  53. context.KeyframeData.ExecuteDelete();
  54. using var transaction = context.Database.BeginTransaction();
  55. do
  56. {
  57. var results = baseQuery.Skip(offset).Take(Limit).Select(b => new Tuple<Guid, string?>(b.Id, b.Path)).ToList();
  58. foreach (var result in results)
  59. {
  60. if (TryGetKeyframeData(result.Item1, result.Item2, out var data))
  61. {
  62. itemCount++;
  63. context.KeyframeData.Add(data);
  64. }
  65. }
  66. offset += Limit;
  67. if (offset > records)
  68. {
  69. offset = records;
  70. }
  71. _logger.LogInformation("Checked: {Count} - Imported: {Items} - Time: {Time}", offset, itemCount, sw.Elapsed);
  72. } while (offset < records);
  73. context.SaveChanges();
  74. transaction.Commit();
  75. _logger.LogInformation("Imported keyframes for {Count} items in {Time}", itemCount, sw.Elapsed);
  76. if (Directory.Exists(KeyframeCachePath))
  77. {
  78. Directory.Delete(KeyframeCachePath, true);
  79. }
  80. }
  81. private bool TryGetKeyframeData(Guid id, string? path, [NotNullWhen(true)] out KeyframeData? data)
  82. {
  83. data = null;
  84. if (!string.IsNullOrEmpty(path))
  85. {
  86. var cachePath = GetCachePath(KeyframeCachePath, path);
  87. if (TryReadFromCache(cachePath, out var keyframeData))
  88. {
  89. data = new()
  90. {
  91. ItemId = id,
  92. KeyframeTicks = keyframeData.KeyframeTicks.ToList(),
  93. TotalDuration = keyframeData.TotalDuration
  94. };
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. private string? GetCachePath(string keyframeCachePath, string filePath)
  101. {
  102. DateTime? lastWriteTimeUtc;
  103. try
  104. {
  105. lastWriteTimeUtc = File.GetLastWriteTimeUtc(filePath);
  106. }
  107. catch (IOException e)
  108. {
  109. _logger.LogDebug("Skipping {Path}: {Exception}", filePath, e.Message);
  110. return null;
  111. }
  112. ReadOnlySpan<char> filename = (filePath + "_" + lastWriteTimeUtc.Value.Ticks.ToString(CultureInfo.InvariantCulture)).GetMD5() + ".json";
  113. var prefix = filename[..1];
  114. return Path.Join(keyframeCachePath, prefix, filename);
  115. }
  116. private static bool TryReadFromCache(string? cachePath, [NotNullWhen(true)] out MediaEncoding.Keyframes.KeyframeData? cachedResult)
  117. {
  118. if (File.Exists(cachePath))
  119. {
  120. var bytes = File.ReadAllBytes(cachePath);
  121. cachedResult = JsonSerializer.Deserialize<MediaEncoding.Keyframes.KeyframeData>(bytes, _jsonOptions);
  122. return cachedResult is not null;
  123. }
  124. cachedResult = null;
  125. return false;
  126. }
  127. }