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