| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Text.RegularExpressions; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using AsyncKeyedLock; |
| | | 11 | | using J2N.Collections.Generic.Extensions; |
| | | 12 | | using Jellyfin.Database.Implementations; |
| | | 13 | | using Jellyfin.Database.Implementations.Entities; |
| | | 14 | | using MediaBrowser.Common.Configuration; |
| | | 15 | | using MediaBrowser.Controller.Configuration; |
| | | 16 | | using MediaBrowser.Controller.Drawing; |
| | | 17 | | using MediaBrowser.Controller.Entities; |
| | | 18 | | using MediaBrowser.Controller.IO; |
| | | 19 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 20 | | using MediaBrowser.Controller.Trickplay; |
| | | 21 | | using MediaBrowser.Model.Configuration; |
| | | 22 | | using MediaBrowser.Model.Entities; |
| | | 23 | | using MediaBrowser.Model.IO; |
| | | 24 | | using Microsoft.EntityFrameworkCore; |
| | | 25 | | using Microsoft.Extensions.Logging; |
| | | 26 | | |
| | | 27 | | namespace Jellyfin.Server.Implementations.Trickplay; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// ITrickplayManager implementation. |
| | | 31 | | /// </summary> |
| | | 32 | | public partial class TrickplayManager : ITrickplayManager |
| | | 33 | | { |
| | | 34 | | private readonly ILogger<TrickplayManager> _logger; |
| | | 35 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 36 | | private readonly IFileSystem _fileSystem; |
| | | 37 | | private readonly EncodingHelper _encodingHelper; |
| | | 38 | | private readonly IServerConfigurationManager _config; |
| | | 39 | | private readonly IImageEncoder _imageEncoder; |
| | | 40 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 41 | | private readonly IApplicationPaths _appPaths; |
| | | 42 | | private readonly IPathManager _pathManager; |
| | | 43 | | |
| | 0 | 44 | | private static readonly AsyncNonKeyedLocker _resourcePool = new(1); |
| | 0 | 45 | | private static readonly string[] _trickplayImgExtensions = [".jpg"]; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes a new instance of the <see cref="TrickplayManager"/> class. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="logger">The logger.</param> |
| | | 51 | | /// <param name="mediaEncoder">The media encoder.</param> |
| | | 52 | | /// <param name="fileSystem">The file system.</param> |
| | | 53 | | /// <param name="encodingHelper">The encoding helper.</param> |
| | | 54 | | /// <param name="config">The server configuration manager.</param> |
| | | 55 | | /// <param name="imageEncoder">The image encoder.</param> |
| | | 56 | | /// <param name="dbProvider">The database provider.</param> |
| | | 57 | | /// <param name="appPaths">The application paths.</param> |
| | | 58 | | /// <param name="pathManager">The path manager.</param> |
| | | 59 | | public TrickplayManager( |
| | | 60 | | ILogger<TrickplayManager> logger, |
| | | 61 | | IMediaEncoder mediaEncoder, |
| | | 62 | | IFileSystem fileSystem, |
| | | 63 | | EncodingHelper encodingHelper, |
| | | 64 | | IServerConfigurationManager config, |
| | | 65 | | IImageEncoder imageEncoder, |
| | | 66 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 67 | | IApplicationPaths appPaths, |
| | | 68 | | IPathManager pathManager) |
| | | 69 | | { |
| | 21 | 70 | | _logger = logger; |
| | 21 | 71 | | _mediaEncoder = mediaEncoder; |
| | 21 | 72 | | _fileSystem = fileSystem; |
| | 21 | 73 | | _encodingHelper = encodingHelper; |
| | 21 | 74 | | _config = config; |
| | 21 | 75 | | _imageEncoder = imageEncoder; |
| | 21 | 76 | | _dbProvider = dbProvider; |
| | 21 | 77 | | _appPaths = appPaths; |
| | 21 | 78 | | _pathManager = pathManager; |
| | 21 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public async Task MoveGeneratedTrickplayDataAsync(Video video, LibraryOptions libraryOptions, CancellationToken canc |
| | | 83 | | { |
| | 0 | 84 | | var options = _config.Configuration.TrickplayOptions; |
| | 0 | 85 | | if (libraryOptions is null || !libraryOptions.EnableTrickplayImageExtraction || !CanGenerateTrickplay(video, opt |
| | | 86 | | { |
| | 0 | 87 | | return; |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | var existingTrickplayResolutions = await GetTrickplayResolutions(video.Id).ConfigureAwait(false); |
| | 0 | 91 | | foreach (var resolution in existingTrickplayResolutions) |
| | | 92 | | { |
| | 0 | 93 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 94 | | var existingResolution = resolution.Key; |
| | 0 | 95 | | var tileWidth = resolution.Value.TileWidth; |
| | 0 | 96 | | var tileHeight = resolution.Value.TileHeight; |
| | 0 | 97 | | var shouldBeSavedWithMedia = libraryOptions is not null && libraryOptions.SaveTrickplayWithMedia; |
| | 0 | 98 | | var localOutputDir = new DirectoryInfo(GetTrickplayDirectory(video, tileWidth, tileHeight, existingResolutio |
| | 0 | 99 | | var mediaOutputDir = new DirectoryInfo(GetTrickplayDirectory(video, tileWidth, tileHeight, existingResolutio |
| | 0 | 100 | | if (shouldBeSavedWithMedia && localOutputDir.Exists) |
| | | 101 | | { |
| | 0 | 102 | | var localDirFiles = localOutputDir.EnumerateFiles(); |
| | 0 | 103 | | var mediaDirExists = mediaOutputDir.Exists; |
| | 0 | 104 | | if (localDirFiles.Any() && ((mediaDirExists && mediaOutputDir.EnumerateFiles().Any()) || !mediaDirExists |
| | | 105 | | { |
| | | 106 | | // Move images from local dir to media dir |
| | 0 | 107 | | MoveContent(localOutputDir.FullName, mediaOutputDir.FullName); |
| | 0 | 108 | | _logger.LogInformation("Moved trickplay images for {ItemName} to {Location}", video.Name, mediaOutpu |
| | | 109 | | } |
| | | 110 | | } |
| | 0 | 111 | | else if (!shouldBeSavedWithMedia && mediaOutputDir.Exists) |
| | | 112 | | { |
| | 0 | 113 | | var mediaDirFiles = mediaOutputDir.EnumerateFiles(); |
| | 0 | 114 | | var localDirExists = localOutputDir.Exists; |
| | 0 | 115 | | if (mediaDirFiles.Any() && ((localDirExists && localOutputDir.EnumerateFiles().Any()) || !localDirExists |
| | | 116 | | { |
| | | 117 | | // Move images from media dir to local dir |
| | 0 | 118 | | MoveContent(mediaOutputDir.FullName, localOutputDir.FullName); |
| | 0 | 119 | | _logger.LogInformation("Moved trickplay images for {ItemName} to {Location}", video.Name, localOutpu |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | private void MoveContent(string sourceFolder, string destinationFolder) |
| | | 126 | | { |
| | 0 | 127 | | _fileSystem.MoveDirectory(sourceFolder, destinationFolder); |
| | 0 | 128 | | var parent = Directory.GetParent(sourceFolder); |
| | 0 | 129 | | if (parent is not null) |
| | | 130 | | { |
| | 0 | 131 | | var parentContent = parent.EnumerateDirectories(); |
| | 0 | 132 | | if (!parentContent.Any()) |
| | | 133 | | { |
| | 0 | 134 | | parent.Delete(); |
| | | 135 | | } |
| | | 136 | | } |
| | 0 | 137 | | } |
| | | 138 | | |
| | | 139 | | private async Task DiscoverExistingTrickplayAsync(Video video, bool saveWithMedia, CancellationToken cancellationTok |
| | | 140 | | { |
| | 0 | 141 | | var options = _config.Configuration.TrickplayOptions; |
| | 0 | 142 | | var existing = await GetTrickplayResolutions(video.Id).ConfigureAwait(false); |
| | | 143 | | |
| | | 144 | | // Remove DB rows whose on-disk folder no longer exists in either possible location. |
| | | 145 | | // Checking both locations avoids dropping rows mid-`SaveTrickplayWithMedia` migration. |
| | 0 | 146 | | var orphanedWidths = new List<int>(); |
| | 0 | 147 | | foreach (var (width, info) in existing) |
| | | 148 | | { |
| | 0 | 149 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 150 | | var localDir = GetTrickplayDirectory(video, info.TileWidth, info.TileHeight, info.Width, false); |
| | 0 | 151 | | var mediaDir = GetTrickplayDirectory(video, info.TileWidth, info.TileHeight, info.Width, true); |
| | 0 | 152 | | if (!HasTrickplayTiles(localDir) && !HasTrickplayTiles(mediaDir)) |
| | | 153 | | { |
| | 0 | 154 | | orphanedWidths.Add(width); |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | if (orphanedWidths.Count > 0) |
| | | 159 | | { |
| | 0 | 160 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 161 | | await using (dbContext.ConfigureAwait(false)) |
| | | 162 | | { |
| | 0 | 163 | | await dbContext.TrickplayInfos |
| | 0 | 164 | | .Where(i => i.ItemId.Equals(video.Id) && orphanedWidths.Contains(i.Width)) |
| | 0 | 165 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 166 | | .ConfigureAwait(false); |
| | | 167 | | } |
| | | 168 | | |
| | 0 | 169 | | foreach (var width in orphanedWidths) |
| | | 170 | | { |
| | 0 | 171 | | _logger.LogInformation("Removed orphaned trickplay DB entry width={Width} for {Path}", width, video.Path |
| | 0 | 172 | | existing.Remove(width); |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | var trickplayDirectory = _pathManager.GetTrickplayDirectory(video, saveWithMedia); |
| | 0 | 177 | | if (!Directory.Exists(trickplayDirectory)) |
| | | 178 | | { |
| | 0 | 179 | | return; |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | foreach (var subdir in new DirectoryInfo(trickplayDirectory).EnumerateDirectories()) |
| | | 183 | | { |
| | 0 | 184 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 185 | | |
| | 0 | 186 | | var match = TrickplaySubdirRegex().Match(subdir.Name); |
| | 0 | 187 | | if (!match.Success) |
| | | 188 | | { |
| | | 189 | | continue; |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | var width = int.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture); |
| | 0 | 193 | | var tileWidth = int.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture); |
| | 0 | 194 | | var tileHeight = int.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture); |
| | | 195 | | |
| | 0 | 196 | | if (existing.ContainsKey(width)) |
| | | 197 | | { |
| | | 198 | | continue; |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | var tiles = subdir.GetFiles("*.jpg") |
| | 0 | 202 | | .OrderBy(t => t.Name, StringComparer.Ordinal) |
| | 0 | 203 | | .ToArray(); |
| | 0 | 204 | | if (tiles.Length == 0) |
| | | 205 | | { |
| | | 206 | | continue; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | // The encoder pads the last tile to a full TileWidth*TileHeight grid, so the real |
| | | 210 | | // thumbnail count cannot be read from tile dimensions. Instead, bound the count from |
| | | 211 | | // the tile count and per-tile capacity, then pick an interval consistent with the |
| | | 212 | | // video runtime - snapping to the server's configured interval when it fits. |
| | 0 | 213 | | var thumbsPerTile = tileWidth * tileHeight; |
| | 0 | 214 | | var maxThumbs = tiles.Length * thumbsPerTile; |
| | 0 | 215 | | var minThumbs = tiles.Length > 1 ? ((tiles.Length - 1) * thumbsPerTile) + 1 : 1; |
| | | 216 | | |
| | | 217 | | int interval; |
| | | 218 | | int thumbnailCount; |
| | 0 | 219 | | if (video.RunTimeTicks is long ticks) |
| | | 220 | | { |
| | 0 | 221 | | var runtimeMs = ticks / TimeSpan.TicksPerMillisecond; |
| | 0 | 222 | | var minInterval = Math.Max(1000L, (long)Math.Ceiling(runtimeMs / (double)maxThumbs)); |
| | 0 | 223 | | var maxInterval = Math.Max(minInterval, (long)Math.Floor(runtimeMs / (double)minThumbs)); |
| | | 224 | | |
| | 0 | 225 | | if (options.Interval >= minInterval && options.Interval <= maxInterval) |
| | | 226 | | { |
| | 0 | 227 | | interval = options.Interval; |
| | | 228 | | } |
| | | 229 | | else |
| | | 230 | | { |
| | 0 | 231 | | var midpoint = (minInterval + maxInterval) / 2.0; |
| | 0 | 232 | | var snapped = (long)Math.Round(midpoint / 1000d) * 1000L; |
| | 0 | 233 | | interval = (int)Math.Clamp(snapped, minInterval, maxInterval); |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | thumbnailCount = Math.Clamp( |
| | 0 | 237 | | (int)Math.Round(runtimeMs / (double)interval), |
| | 0 | 238 | | minThumbs, |
| | 0 | 239 | | maxThumbs); |
| | | 240 | | } |
| | | 241 | | else |
| | | 242 | | { |
| | 0 | 243 | | interval = Math.Max(1000, options.Interval); |
| | 0 | 244 | | thumbnailCount = maxThumbs; |
| | | 245 | | } |
| | | 246 | | |
| | 0 | 247 | | var firstSize = _imageEncoder.GetImageSize(tiles[0].FullName); |
| | 0 | 248 | | var thumbPxH = Math.Max(1, (int)Math.Ceiling((double)firstSize.Height / tileHeight)); |
| | | 249 | | |
| | 0 | 250 | | var info = new TrickplayInfo |
| | 0 | 251 | | { |
| | 0 | 252 | | ItemId = video.Id, |
| | 0 | 253 | | Width = width, |
| | 0 | 254 | | Interval = interval, |
| | 0 | 255 | | TileWidth = tileWidth, |
| | 0 | 256 | | TileHeight = tileHeight, |
| | 0 | 257 | | ThumbnailCount = thumbnailCount, |
| | 0 | 258 | | Height = thumbPxH, |
| | 0 | 259 | | Bandwidth = 0, |
| | 0 | 260 | | }; |
| | | 261 | | |
| | 0 | 262 | | foreach (var tile in tiles) |
| | | 263 | | { |
| | 0 | 264 | | var bitrate = (int)Math.Ceiling((decimal)tile.Length * 8 / tileWidth / tileHeight / (interval / 1000m)); |
| | 0 | 265 | | info.Bandwidth = Math.Max(info.Bandwidth, bitrate); |
| | | 266 | | } |
| | | 267 | | |
| | 0 | 268 | | await SaveTrickplayInfo(info).ConfigureAwait(false); |
| | 0 | 269 | | _logger.LogInformation( |
| | 0 | 270 | | "Discovered existing trickplay {Width} - {TileWidth}x{TileHeight} ({ThumbnailCount} thumbnails, {Interva |
| | 0 | 271 | | width, |
| | 0 | 272 | | tileWidth, |
| | 0 | 273 | | tileHeight, |
| | 0 | 274 | | thumbnailCount, |
| | 0 | 275 | | interval, |
| | 0 | 276 | | video.Path); |
| | | 277 | | } |
| | 0 | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <inheritdoc /> |
| | | 281 | | public async Task RefreshTrickplayDataAsync(Video video, bool replace, LibraryOptions libraryOptions, CancellationTo |
| | | 282 | | { |
| | 0 | 283 | | var options = _config.Configuration.TrickplayOptions; |
| | 0 | 284 | | if (!CanGenerateTrickplay(video, options.Interval) || libraryOptions is null) |
| | | 285 | | { |
| | 0 | 286 | | return; |
| | | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | var saveWithMedia = libraryOptions.SaveTrickplayWithMedia; |
| | | 290 | | |
| | | 291 | | // Catalog any existing trickplay folders on disk before any prune/generate. This picks up |
| | | 292 | | // user-placed files even when their (width, tile dims) don't match the server's configured values. |
| | 0 | 293 | | if (!replace) |
| | | 294 | | { |
| | 0 | 295 | | await DiscoverExistingTrickplayAsync(video, saveWithMedia, cancellationToken).ConfigureAwait(false); |
| | | 296 | | } |
| | | 297 | | |
| | 0 | 298 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 299 | | await using (dbContext.ConfigureAwait(false)) |
| | | 300 | | { |
| | 0 | 301 | | var trickplayDirectory = _pathManager.GetTrickplayDirectory(video, saveWithMedia); |
| | | 302 | | |
| | | 303 | | // When extraction is disabled and files live next to media, treat them as user-managed: |
| | | 304 | | // discovery above already catalogued whatever is on disk, leave it alone. |
| | 0 | 305 | | if (!libraryOptions.EnableTrickplayImageExtraction && !replace && saveWithMedia) |
| | | 306 | | { |
| | | 307 | | return; |
| | | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | if (!libraryOptions.EnableTrickplayImageExtraction || replace) |
| | | 311 | | { |
| | | 312 | | // Prune existing data |
| | 0 | 313 | | if (Directory.Exists(trickplayDirectory)) |
| | | 314 | | { |
| | | 315 | | try |
| | | 316 | | { |
| | 0 | 317 | | Directory.Delete(trickplayDirectory, true); |
| | 0 | 318 | | } |
| | 0 | 319 | | catch (Exception ex) |
| | | 320 | | { |
| | 0 | 321 | | _logger.LogWarning("Unable to clear trickplay directory: {Directory}: {Exception}", trickplayDir |
| | 0 | 322 | | } |
| | | 323 | | } |
| | | 324 | | |
| | 0 | 325 | | await dbContext.TrickplayInfos |
| | 0 | 326 | | .Where(i => i.ItemId.Equals(video.Id)) |
| | 0 | 327 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 328 | | .ConfigureAwait(false); |
| | | 329 | | |
| | 0 | 330 | | if (!replace) |
| | | 331 | | { |
| | | 332 | | return; |
| | | 333 | | } |
| | | 334 | | } |
| | | 335 | | |
| | 0 | 336 | | _logger.LogDebug("Trickplay refresh for {ItemId} (replace existing: {Replace})", video.Id, replace); |
| | | 337 | | |
| | 0 | 338 | | if (options.Interval < 1000) |
| | | 339 | | { |
| | 0 | 340 | | _logger.LogWarning("Trickplay image interval {Interval} is too small, reset to the minimum valid value o |
| | 0 | 341 | | options.Interval = 1000; |
| | | 342 | | } |
| | | 343 | | |
| | 0 | 344 | | foreach (var width in options.WidthResolutions) |
| | | 345 | | { |
| | 0 | 346 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 0 | 347 | | await RefreshTrickplayDataInternal( |
| | 0 | 348 | | video, |
| | 0 | 349 | | replace, |
| | 0 | 350 | | width, |
| | 0 | 351 | | options, |
| | 0 | 352 | | saveWithMedia, |
| | 0 | 353 | | cancellationToken).ConfigureAwait(false); |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | // Cleanup old trickplay files |
| | 0 | 357 | | if (Directory.Exists(trickplayDirectory)) |
| | | 358 | | { |
| | 0 | 359 | | var existingFolders = Directory.GetDirectories(trickplayDirectory); |
| | 0 | 360 | | var trickplayInfos = await dbContext.TrickplayInfos |
| | 0 | 361 | | .AsNoTracking() |
| | 0 | 362 | | .Where(i => i.ItemId.Equals(video.Id)) |
| | 0 | 363 | | .ToListAsync(cancellationToken) |
| | 0 | 364 | | .ConfigureAwait(false); |
| | 0 | 365 | | var expectedFolders = trickplayInfos.Select(i => GetTrickplayDirectory(video, i.TileWidth, i.TileHeight, |
| | 0 | 366 | | var foldersToRemove = existingFolders.Except(expectedFolders); |
| | 0 | 367 | | foreach (var folder in foldersToRemove) |
| | | 368 | | { |
| | | 369 | | try |
| | | 370 | | { |
| | 0 | 371 | | _logger.LogWarning("Pruning trickplay files for {Item}", video.Path); |
| | 0 | 372 | | Directory.Delete(folder, true); |
| | 0 | 373 | | } |
| | 0 | 374 | | catch (Exception ex) |
| | | 375 | | { |
| | 0 | 376 | | _logger.LogWarning("Unable to remove trickplay directory: {Directory}: {Exception}", folder, ex) |
| | 0 | 377 | | } |
| | | 378 | | } |
| | 0 | 379 | | } |
| | 0 | 380 | | } |
| | 0 | 381 | | } |
| | | 382 | | |
| | | 383 | | private async Task RefreshTrickplayDataInternal( |
| | | 384 | | Video video, |
| | | 385 | | bool replace, |
| | | 386 | | int width, |
| | | 387 | | TrickplayOptions options, |
| | | 388 | | bool saveWithMedia, |
| | | 389 | | CancellationToken cancellationToken) |
| | | 390 | | { |
| | 0 | 391 | | var imgTempDir = string.Empty; |
| | | 392 | | |
| | 0 | 393 | | using (await _resourcePool.LockAsync(cancellationToken).ConfigureAwait(false)) |
| | | 394 | | { |
| | | 395 | | try |
| | | 396 | | { |
| | | 397 | | // Extract images |
| | | 398 | | // Note: Media sources under parent items exist as their own video/item as well. Only use this video str |
| | 0 | 399 | | var mediaSource = video.GetMediaSources(false).FirstOrDefault(source => Guid.Parse(source.Id).Equals(vid |
| | | 400 | | |
| | 0 | 401 | | if (mediaSource is null) |
| | | 402 | | { |
| | 0 | 403 | | _logger.LogDebug("Found no matching media source for item {ItemId}", video.Id); |
| | 0 | 404 | | return; |
| | | 405 | | } |
| | | 406 | | |
| | 0 | 407 | | var mediaPath = mediaSource.Path; |
| | 0 | 408 | | if (!File.Exists(mediaPath)) |
| | | 409 | | { |
| | 0 | 410 | | _logger.LogWarning("Media not found at {Path} for item {ItemID}", mediaPath, video.Id); |
| | 0 | 411 | | return; |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | // We support video backdrops, but we should not generate trickplay images for them |
| | 0 | 415 | | var parentDirectory = Directory.GetParent(video.Path); |
| | 0 | 416 | | if (parentDirectory is not null && string.Equals(parentDirectory.Name, "backdrops", StringComparison.Ord |
| | | 417 | | { |
| | 0 | 418 | | _logger.LogDebug("Ignoring backdrop media found at {Path} for item {ItemID}", video.Path, video.Id); |
| | 0 | 419 | | return; |
| | | 420 | | } |
| | | 421 | | |
| | | 422 | | // The width has to be even, otherwise a lot of filters will not be able to sample it |
| | 0 | 423 | | var actualWidth = 2 * (width / 2); |
| | | 424 | | |
| | | 425 | | // Force using the video width when the trickplay setting has a too large width |
| | 0 | 426 | | if (mediaSource.VideoStream.Width is not null && mediaSource.VideoStream.Width < width) |
| | | 427 | | { |
| | 0 | 428 | | _logger.LogWarning("Video width {VideoWidth} is smaller than trickplay setting {TrickPlayWidth}, usi |
| | 0 | 429 | | actualWidth = 2 * ((int)mediaSource.VideoStream.Width / 2); |
| | | 430 | | } |
| | | 431 | | |
| | 0 | 432 | | var tileWidth = options.TileWidth; |
| | 0 | 433 | | var tileHeight = options.TileHeight; |
| | 0 | 434 | | var outputDir = new DirectoryInfo(GetTrickplayDirectory(video, tileWidth, tileHeight, actualWidth, saveW |
| | | 435 | | |
| | | 436 | | // Import existing trickplay tiles |
| | 0 | 437 | | if (!replace && outputDir.Exists) |
| | | 438 | | { |
| | 0 | 439 | | var existingFiles = outputDir.GetFiles(); |
| | 0 | 440 | | if (existingFiles.Length > 0) |
| | | 441 | | { |
| | 0 | 442 | | var hasTrickplayResolution = await HasTrickplayResolutionAsync(video.Id, actualWidth).ConfigureA |
| | 0 | 443 | | if (hasTrickplayResolution) |
| | | 444 | | { |
| | 0 | 445 | | _logger.LogDebug("Found existing trickplay files for {ItemId}.", video.Id); |
| | 0 | 446 | | return; |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | // Import tiles |
| | 0 | 450 | | var localTrickplayInfo = new TrickplayInfo |
| | 0 | 451 | | { |
| | 0 | 452 | | ItemId = video.Id, |
| | 0 | 453 | | Width = width, |
| | 0 | 454 | | Interval = options.Interval, |
| | 0 | 455 | | TileWidth = options.TileWidth, |
| | 0 | 456 | | TileHeight = options.TileHeight, |
| | 0 | 457 | | ThumbnailCount = existingFiles.Length, |
| | 0 | 458 | | Height = 0, |
| | 0 | 459 | | Bandwidth = 0 |
| | 0 | 460 | | }; |
| | | 461 | | |
| | 0 | 462 | | foreach (var tile in existingFiles) |
| | | 463 | | { |
| | 0 | 464 | | var image = _imageEncoder.GetImageSize(tile.FullName); |
| | 0 | 465 | | localTrickplayInfo.Height = Math.Max(localTrickplayInfo.Height, (int)Math.Ceiling((double)im |
| | 0 | 466 | | var bitrate = (int)Math.Ceiling((decimal)tile.Length * 8 / localTrickplayInfo.TileWidth / lo |
| | 0 | 467 | | localTrickplayInfo.Bandwidth = Math.Max(localTrickplayInfo.Bandwidth, bitrate); |
| | | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | await SaveTrickplayInfo(localTrickplayInfo).ConfigureAwait(false); |
| | | 471 | | |
| | 0 | 472 | | _logger.LogDebug("Imported existing trickplay files for {ItemId}.", video.Id); |
| | 0 | 473 | | return; |
| | | 474 | | } |
| | 0 | 475 | | } |
| | | 476 | | |
| | | 477 | | // Generate trickplay tiles |
| | 0 | 478 | | var mediaStream = mediaSource.VideoStream; |
| | 0 | 479 | | var container = mediaSource.Container; |
| | | 480 | | |
| | 0 | 481 | | _logger.LogInformation("Creating trickplay files at {Width} width, for {Path} [ID: {ItemId}]", actualWid |
| | 0 | 482 | | imgTempDir = await _mediaEncoder.ExtractVideoImagesOnIntervalAccelerated( |
| | 0 | 483 | | mediaPath, |
| | 0 | 484 | | container, |
| | 0 | 485 | | mediaSource, |
| | 0 | 486 | | mediaStream, |
| | 0 | 487 | | actualWidth, |
| | 0 | 488 | | TimeSpan.FromMilliseconds(options.Interval), |
| | 0 | 489 | | options.EnableHwAcceleration, |
| | 0 | 490 | | options.EnableHwEncoding, |
| | 0 | 491 | | options.ProcessThreads, |
| | 0 | 492 | | options.Qscale, |
| | 0 | 493 | | options.ProcessPriority, |
| | 0 | 494 | | options.EnableKeyFrameOnlyExtraction, |
| | 0 | 495 | | _encodingHelper, |
| | 0 | 496 | | cancellationToken).ConfigureAwait(false); |
| | | 497 | | |
| | 0 | 498 | | if (string.IsNullOrEmpty(imgTempDir) || !Directory.Exists(imgTempDir)) |
| | | 499 | | { |
| | 0 | 500 | | throw new InvalidOperationException("Null or invalid directory from media encoder."); |
| | | 501 | | } |
| | | 502 | | |
| | 0 | 503 | | var images = _fileSystem.GetFiles(imgTempDir, _trickplayImgExtensions, false, false) |
| | 0 | 504 | | .Select(i => i.FullName) |
| | 0 | 505 | | .OrderBy(i => i) |
| | 0 | 506 | | .ToList(); |
| | | 507 | | |
| | | 508 | | // Create tiles |
| | 0 | 509 | | var trickplayInfo = CreateTiles(images, actualWidth, options, outputDir.FullName); |
| | | 510 | | |
| | | 511 | | // Save tiles info |
| | | 512 | | try |
| | | 513 | | { |
| | 0 | 514 | | if (trickplayInfo is not null) |
| | | 515 | | { |
| | 0 | 516 | | trickplayInfo.ItemId = video.Id; |
| | 0 | 517 | | await SaveTrickplayInfo(trickplayInfo).ConfigureAwait(false); |
| | | 518 | | |
| | 0 | 519 | | _logger.LogInformation("Finished creation of trickplay files for {0}", mediaPath); |
| | | 520 | | } |
| | | 521 | | else |
| | | 522 | | { |
| | 0 | 523 | | throw new InvalidOperationException("Null trickplay tiles info from CreateTiles."); |
| | | 524 | | } |
| | 0 | 525 | | } |
| | 0 | 526 | | catch (Exception ex) |
| | | 527 | | { |
| | 0 | 528 | | _logger.LogError(ex, "Error while saving trickplay tiles info."); |
| | | 529 | | |
| | | 530 | | // Make sure no files stay in metadata folders on failure |
| | | 531 | | // if tiles info wasn't saved. |
| | 0 | 532 | | outputDir.Delete(true); |
| | 0 | 533 | | } |
| | 0 | 534 | | } |
| | 0 | 535 | | catch (Exception ex) |
| | | 536 | | { |
| | 0 | 537 | | _logger.LogError(ex, "Error creating trickplay images."); |
| | 0 | 538 | | } |
| | | 539 | | finally |
| | | 540 | | { |
| | 0 | 541 | | if (!string.IsNullOrEmpty(imgTempDir)) |
| | | 542 | | { |
| | 0 | 543 | | Directory.Delete(imgTempDir, true); |
| | | 544 | | } |
| | | 545 | | } |
| | 0 | 546 | | } |
| | 0 | 547 | | } |
| | | 548 | | |
| | | 549 | | /// <inheritdoc /> |
| | | 550 | | public TrickplayInfo CreateTiles(IReadOnlyList<string> images, int width, TrickplayOptions options, string outputDir |
| | | 551 | | { |
| | 0 | 552 | | if (images.Count == 0) |
| | | 553 | | { |
| | 0 | 554 | | throw new ArgumentException("Can't create trickplay from 0 images."); |
| | | 555 | | } |
| | | 556 | | |
| | 0 | 557 | | var workDir = Path.Combine(_appPaths.TempDirectory, "trickplay_" + Guid.NewGuid().ToString("N")); |
| | 0 | 558 | | Directory.CreateDirectory(workDir); |
| | | 559 | | |
| | | 560 | | try |
| | | 561 | | { |
| | 0 | 562 | | var trickplayInfo = new TrickplayInfo |
| | 0 | 563 | | { |
| | 0 | 564 | | Width = width, |
| | 0 | 565 | | Interval = options.Interval, |
| | 0 | 566 | | TileWidth = options.TileWidth, |
| | 0 | 567 | | TileHeight = options.TileHeight, |
| | 0 | 568 | | ThumbnailCount = images.Count, |
| | 0 | 569 | | // Set during image generation |
| | 0 | 570 | | Height = 0, |
| | 0 | 571 | | Bandwidth = 0 |
| | 0 | 572 | | }; |
| | | 573 | | |
| | | 574 | | /* |
| | | 575 | | * Generate trickplay tiles from sets of thumbnails |
| | | 576 | | */ |
| | 0 | 577 | | var imageOptions = new ImageCollageOptions |
| | 0 | 578 | | { |
| | 0 | 579 | | Width = trickplayInfo.TileWidth, |
| | 0 | 580 | | Height = trickplayInfo.TileHeight |
| | 0 | 581 | | }; |
| | | 582 | | |
| | 0 | 583 | | var thumbnailsPerTile = trickplayInfo.TileWidth * trickplayInfo.TileHeight; |
| | 0 | 584 | | var requiredTiles = (int)Math.Ceiling((double)images.Count / thumbnailsPerTile); |
| | | 585 | | |
| | 0 | 586 | | for (int i = 0; i < requiredTiles; i++) |
| | | 587 | | { |
| | | 588 | | // Set output/input paths |
| | 0 | 589 | | var tilePath = Path.Combine(workDir, $"{i}.jpg"); |
| | | 590 | | |
| | 0 | 591 | | imageOptions.OutputPath = tilePath; |
| | 0 | 592 | | imageOptions.InputPaths = images.Skip(i * thumbnailsPerTile).Take(Math.Min(thumbnailsPerTile, images.Cou |
| | | 593 | | |
| | | 594 | | // Generate image and use returned height for tiles info |
| | 0 | 595 | | var height = _imageEncoder.CreateTrickplayTile(imageOptions, options.JpegQuality, trickplayInfo.Width, t |
| | 0 | 596 | | if (trickplayInfo.Height == 0) |
| | | 597 | | { |
| | 0 | 598 | | trickplayInfo.Height = height; |
| | | 599 | | } |
| | | 600 | | |
| | | 601 | | // Update bitrate |
| | 0 | 602 | | var bitrate = (int)Math.Ceiling(new FileInfo(tilePath).Length * 8m / trickplayInfo.TileWidth / trickplay |
| | 0 | 603 | | trickplayInfo.Bandwidth = Math.Max(trickplayInfo.Bandwidth, bitrate); |
| | | 604 | | } |
| | | 605 | | |
| | | 606 | | /* |
| | | 607 | | * Move trickplay tiles to output directory |
| | | 608 | | */ |
| | 0 | 609 | | Directory.CreateDirectory(Directory.GetParent(outputDir)!.FullName); |
| | | 610 | | |
| | | 611 | | // Replace existing tiles if they already exist |
| | 0 | 612 | | if (Directory.Exists(outputDir)) |
| | | 613 | | { |
| | 0 | 614 | | Directory.Delete(outputDir, true); |
| | | 615 | | } |
| | | 616 | | |
| | 0 | 617 | | _fileSystem.MoveDirectory(workDir, outputDir); |
| | | 618 | | |
| | 0 | 619 | | return trickplayInfo; |
| | | 620 | | } |
| | 0 | 621 | | catch |
| | | 622 | | { |
| | 0 | 623 | | Directory.Delete(workDir, true); |
| | 0 | 624 | | throw; |
| | | 625 | | } |
| | 0 | 626 | | } |
| | | 627 | | |
| | | 628 | | private bool CanGenerateTrickplay(Video video, int interval) |
| | | 629 | | { |
| | 0 | 630 | | var videoType = video.VideoType; |
| | 0 | 631 | | if (videoType == VideoType.Iso || videoType == VideoType.Dvd || videoType == VideoType.BluRay) |
| | | 632 | | { |
| | 0 | 633 | | return false; |
| | | 634 | | } |
| | | 635 | | |
| | 0 | 636 | | if (video.IsPlaceHolder) |
| | | 637 | | { |
| | 0 | 638 | | return false; |
| | | 639 | | } |
| | | 640 | | |
| | 0 | 641 | | if (video.IsShortcut) |
| | | 642 | | { |
| | 0 | 643 | | return false; |
| | | 644 | | } |
| | | 645 | | |
| | 0 | 646 | | if (!video.IsCompleteMedia) |
| | | 647 | | { |
| | 0 | 648 | | return false; |
| | | 649 | | } |
| | | 650 | | |
| | 0 | 651 | | if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value < TimeSpan.FromMilliseconds(interval).Ticks) |
| | | 652 | | { |
| | 0 | 653 | | return false; |
| | | 654 | | } |
| | | 655 | | |
| | | 656 | | // Can't extract images if there are no video streams |
| | 0 | 657 | | return video.GetMediaStreams().Count > 0; |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | /// <inheritdoc /> |
| | | 661 | | public async Task<Dictionary<int, TrickplayInfo>> GetTrickplayResolutions(Guid itemId) |
| | | 662 | | { |
| | 0 | 663 | | var trickplayResolutions = new Dictionary<int, TrickplayInfo>(); |
| | | 664 | | |
| | 0 | 665 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 0 | 666 | | await using (dbContext.ConfigureAwait(false)) |
| | | 667 | | { |
| | 0 | 668 | | var trickplayInfos = await dbContext.TrickplayInfos |
| | 0 | 669 | | .AsNoTracking() |
| | 0 | 670 | | .Where(i => i.ItemId.Equals(itemId)) |
| | 0 | 671 | | .ToListAsync() |
| | 0 | 672 | | .ConfigureAwait(false); |
| | | 673 | | |
| | 0 | 674 | | foreach (var info in trickplayInfos) |
| | | 675 | | { |
| | 0 | 676 | | trickplayResolutions[info.Width] = info; |
| | | 677 | | } |
| | | 678 | | } |
| | | 679 | | |
| | 0 | 680 | | return trickplayResolutions; |
| | 0 | 681 | | } |
| | | 682 | | |
| | | 683 | | /// <inheritdoc /> |
| | | 684 | | public async Task<IReadOnlyList<TrickplayInfo>> GetTrickplayItemsAsync(int limit, int offset) |
| | | 685 | | { |
| | | 686 | | IReadOnlyList<TrickplayInfo> trickplayItems; |
| | | 687 | | |
| | 21 | 688 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 21 | 689 | | await using (dbContext.ConfigureAwait(false)) |
| | | 690 | | { |
| | 21 | 691 | | trickplayItems = await dbContext.TrickplayInfos |
| | 21 | 692 | | .AsNoTracking() |
| | 21 | 693 | | .OrderBy(i => i.ItemId) |
| | 21 | 694 | | .Skip(offset) |
| | 21 | 695 | | .Take(limit) |
| | 21 | 696 | | .ToListAsync() |
| | 21 | 697 | | .ConfigureAwait(false); |
| | | 698 | | } |
| | | 699 | | |
| | 21 | 700 | | return trickplayItems; |
| | 21 | 701 | | } |
| | | 702 | | |
| | | 703 | | /// <inheritdoc /> |
| | | 704 | | public async Task SaveTrickplayInfo(TrickplayInfo info) |
| | | 705 | | { |
| | 0 | 706 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 0 | 707 | | await using (dbContext.ConfigureAwait(false)) |
| | | 708 | | { |
| | 0 | 709 | | var oldInfo = await dbContext.TrickplayInfos.FindAsync(info.ItemId, info.Width).ConfigureAwait(false); |
| | 0 | 710 | | if (oldInfo is not null) |
| | | 711 | | { |
| | 0 | 712 | | dbContext.TrickplayInfos.Remove(oldInfo); |
| | | 713 | | } |
| | | 714 | | |
| | 0 | 715 | | dbContext.Add(info); |
| | | 716 | | |
| | 0 | 717 | | await dbContext.SaveChangesAsync().ConfigureAwait(false); |
| | | 718 | | } |
| | 0 | 719 | | } |
| | | 720 | | |
| | | 721 | | /// <inheritdoc /> |
| | | 722 | | public async Task DeleteTrickplayDataAsync(Guid itemId, CancellationToken cancellationToken) |
| | | 723 | | { |
| | 0 | 724 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 725 | | await dbContext.TrickplayInfos.Where(i => i.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).Configu |
| | 0 | 726 | | } |
| | | 727 | | |
| | | 728 | | /// <inheritdoc /> |
| | | 729 | | public async Task<Dictionary<string, Dictionary<int, TrickplayInfo>>> GetTrickplayManifest(BaseItem item) |
| | | 730 | | { |
| | 0 | 731 | | var trickplayManifest = new Dictionary<string, Dictionary<int, TrickplayInfo>>(); |
| | 0 | 732 | | foreach (var mediaSource in item.GetMediaSources(false)) |
| | | 733 | | { |
| | 0 | 734 | | if (mediaSource.IsRemote || !Guid.TryParse(mediaSource.Id, out var mediaSourceId)) |
| | | 735 | | { |
| | | 736 | | continue; |
| | | 737 | | } |
| | | 738 | | |
| | 0 | 739 | | var trickplayResolutions = await GetTrickplayResolutions(mediaSourceId).ConfigureAwait(false); |
| | | 740 | | |
| | 0 | 741 | | if (trickplayResolutions.Count > 0) |
| | | 742 | | { |
| | 0 | 743 | | trickplayManifest[mediaSource.Id] = trickplayResolutions; |
| | | 744 | | } |
| | 0 | 745 | | } |
| | | 746 | | |
| | 0 | 747 | | return trickplayManifest; |
| | 0 | 748 | | } |
| | | 749 | | |
| | | 750 | | /// <inheritdoc /> |
| | | 751 | | public async Task<string> GetTrickplayTilePathAsync(BaseItem item, int width, int index, bool saveWithMedia) |
| | | 752 | | { |
| | 0 | 753 | | var trickplayResolutions = await GetTrickplayResolutions(item.Id).ConfigureAwait(false); |
| | 0 | 754 | | if (trickplayResolutions is not null && trickplayResolutions.TryGetValue(width, out var trickplayInfo)) |
| | | 755 | | { |
| | 0 | 756 | | return Path.Combine(GetTrickplayDirectory(item, trickplayInfo.TileWidth, trickplayInfo.TileHeight, width, sa |
| | | 757 | | } |
| | | 758 | | |
| | 0 | 759 | | return string.Empty; |
| | 0 | 760 | | } |
| | | 761 | | |
| | | 762 | | /// <inheritdoc /> |
| | | 763 | | public async Task<string?> GetHlsPlaylist(Guid itemId, int width, string? apiKey) |
| | | 764 | | { |
| | 0 | 765 | | var trickplayResolutions = await GetTrickplayResolutions(itemId).ConfigureAwait(false); |
| | 0 | 766 | | if (trickplayResolutions is not null && trickplayResolutions.TryGetValue(width, out var trickplayInfo)) |
| | | 767 | | { |
| | 0 | 768 | | var builder = new StringBuilder(128); |
| | | 769 | | |
| | 0 | 770 | | if (trickplayInfo.ThumbnailCount > 0) |
| | | 771 | | { |
| | | 772 | | const string urlFormat = "{0}.jpg?MediaSourceId={1}&ApiKey={2}"; |
| | | 773 | | const string decimalFormat = "{0:0.###}"; |
| | | 774 | | |
| | 0 | 775 | | var resolution = $"{trickplayInfo.Width}x{trickplayInfo.Height}"; |
| | 0 | 776 | | var layout = $"{trickplayInfo.TileWidth}x{trickplayInfo.TileHeight}"; |
| | 0 | 777 | | var thumbnailsPerTile = trickplayInfo.TileWidth * trickplayInfo.TileHeight; |
| | 0 | 778 | | var thumbnailDuration = trickplayInfo.Interval / 1000d; |
| | 0 | 779 | | var infDuration = thumbnailDuration * thumbnailsPerTile; |
| | 0 | 780 | | var tileCount = (int)Math.Ceiling((decimal)trickplayInfo.ThumbnailCount / thumbnailsPerTile); |
| | | 781 | | |
| | 0 | 782 | | builder |
| | 0 | 783 | | .AppendLine("#EXTM3U") |
| | 0 | 784 | | .Append("#EXT-X-TARGETDURATION:") |
| | 0 | 785 | | .AppendLine(tileCount.ToString(CultureInfo.InvariantCulture)) |
| | 0 | 786 | | .AppendLine("#EXT-X-VERSION:7") |
| | 0 | 787 | | .AppendLine("#EXT-X-MEDIA-SEQUENCE:1") |
| | 0 | 788 | | .AppendLine("#EXT-X-PLAYLIST-TYPE:VOD") |
| | 0 | 789 | | .AppendLine("#EXT-X-IMAGES-ONLY"); |
| | | 790 | | |
| | 0 | 791 | | for (int i = 0; i < tileCount; i++) |
| | | 792 | | { |
| | | 793 | | // All tiles prior to the last must contain full amount of thumbnails (no black). |
| | 0 | 794 | | if (i == tileCount - 1) |
| | | 795 | | { |
| | 0 | 796 | | thumbnailsPerTile = trickplayInfo.ThumbnailCount - (i * thumbnailsPerTile); |
| | 0 | 797 | | infDuration = thumbnailDuration * thumbnailsPerTile; |
| | | 798 | | } |
| | | 799 | | |
| | | 800 | | // EXTINF |
| | 0 | 801 | | builder |
| | 0 | 802 | | .Append("#EXTINF:") |
| | 0 | 803 | | .AppendFormat(CultureInfo.InvariantCulture, decimalFormat, infDuration) |
| | 0 | 804 | | .AppendLine(","); |
| | | 805 | | |
| | | 806 | | // EXT-X-TILES |
| | 0 | 807 | | builder |
| | 0 | 808 | | .Append("#EXT-X-TILES:RESOLUTION=") |
| | 0 | 809 | | .Append(resolution) |
| | 0 | 810 | | .Append(",LAYOUT=") |
| | 0 | 811 | | .Append(layout) |
| | 0 | 812 | | .Append(",DURATION=") |
| | 0 | 813 | | .AppendFormat(CultureInfo.InvariantCulture, decimalFormat, thumbnailDuration) |
| | 0 | 814 | | .AppendLine(); |
| | | 815 | | |
| | | 816 | | // URL |
| | 0 | 817 | | builder |
| | 0 | 818 | | .AppendFormat( |
| | 0 | 819 | | CultureInfo.InvariantCulture, |
| | 0 | 820 | | urlFormat, |
| | 0 | 821 | | i.ToString(CultureInfo.InvariantCulture), |
| | 0 | 822 | | itemId.ToString("N"), |
| | 0 | 823 | | apiKey) |
| | 0 | 824 | | .AppendLine(); |
| | | 825 | | } |
| | | 826 | | |
| | 0 | 827 | | builder.AppendLine("#EXT-X-ENDLIST"); |
| | 0 | 828 | | return builder.ToString(); |
| | | 829 | | } |
| | | 830 | | } |
| | | 831 | | |
| | 0 | 832 | | return null; |
| | 0 | 833 | | } |
| | | 834 | | |
| | | 835 | | /// <inheritdoc /> |
| | | 836 | | public string GetTrickplayDirectory(BaseItem item, int tileWidth, int tileHeight, int width, bool saveWithMedia = fa |
| | | 837 | | { |
| | 0 | 838 | | var path = _pathManager.GetTrickplayDirectory(item, saveWithMedia); |
| | 0 | 839 | | var subdirectory = string.Format( |
| | 0 | 840 | | CultureInfo.InvariantCulture, |
| | 0 | 841 | | "{0} - {1}x{2}", |
| | 0 | 842 | | width.ToString(CultureInfo.InvariantCulture), |
| | 0 | 843 | | tileWidth.ToString(CultureInfo.InvariantCulture), |
| | 0 | 844 | | tileHeight.ToString(CultureInfo.InvariantCulture)); |
| | | 845 | | |
| | 0 | 846 | | return Path.Combine(path, subdirectory); |
| | | 847 | | } |
| | | 848 | | |
| | | 849 | | [GeneratedRegex(@"^(\d+) - (\d+)x(\d+)$")] |
| | | 850 | | private static partial Regex TrickplaySubdirRegex(); |
| | | 851 | | |
| | | 852 | | private static bool HasTrickplayTiles(string directory) |
| | | 853 | | { |
| | 0 | 854 | | if (!Directory.Exists(directory)) |
| | | 855 | | { |
| | 0 | 856 | | return false; |
| | | 857 | | } |
| | | 858 | | |
| | 0 | 859 | | return new DirectoryInfo(directory).EnumerateFiles("*.jpg").Any(); |
| | | 860 | | } |
| | | 861 | | |
| | | 862 | | private async Task<bool> HasTrickplayResolutionAsync(Guid itemId, int width) |
| | | 863 | | { |
| | 0 | 864 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 0 | 865 | | await using (dbContext.ConfigureAwait(false)) |
| | | 866 | | { |
| | 0 | 867 | | return await dbContext.TrickplayInfos |
| | 0 | 868 | | .AsNoTracking() |
| | 0 | 869 | | .Where(i => i.ItemId.Equals(itemId)) |
| | 0 | 870 | | .AnyAsync(i => i.Width == width) |
| | 0 | 871 | | .ConfigureAwait(false); |
| | | 872 | | } |
| | 0 | 873 | | } |
| | | 874 | | } |