TrickplayManager.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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.Threading;
  8. using System.Threading.Tasks;
  9. using AsyncKeyedLock;
  10. using J2N.Collections.Generic.Extensions;
  11. using Jellyfin.Database.Implementations;
  12. using Jellyfin.Database.Implementations.Entities;
  13. using MediaBrowser.Common.Configuration;
  14. using MediaBrowser.Controller.Configuration;
  15. using MediaBrowser.Controller.Drawing;
  16. using MediaBrowser.Controller.Entities;
  17. using MediaBrowser.Controller.IO;
  18. using MediaBrowser.Controller.MediaEncoding;
  19. using MediaBrowser.Controller.Trickplay;
  20. using MediaBrowser.Model.Configuration;
  21. using MediaBrowser.Model.Entities;
  22. using MediaBrowser.Model.IO;
  23. using Microsoft.EntityFrameworkCore;
  24. using Microsoft.Extensions.Logging;
  25. namespace Jellyfin.Server.Implementations.Trickplay;
  26. /// <summary>
  27. /// ITrickplayManager implementation.
  28. /// </summary>
  29. public class TrickplayManager : ITrickplayManager
  30. {
  31. private readonly ILogger<TrickplayManager> _logger;
  32. private readonly IMediaEncoder _mediaEncoder;
  33. private readonly IFileSystem _fileSystem;
  34. private readonly EncodingHelper _encodingHelper;
  35. private readonly IServerConfigurationManager _config;
  36. private readonly IImageEncoder _imageEncoder;
  37. private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
  38. private readonly IApplicationPaths _appPaths;
  39. private readonly IPathManager _pathManager;
  40. private static readonly AsyncNonKeyedLocker _resourcePool = new(1);
  41. private static readonly string[] _trickplayImgExtensions = [".jpg"];
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="TrickplayManager"/> class.
  44. /// </summary>
  45. /// <param name="logger">The logger.</param>
  46. /// <param name="mediaEncoder">The media encoder.</param>
  47. /// <param name="fileSystem">The file system.</param>
  48. /// <param name="encodingHelper">The encoding helper.</param>
  49. /// <param name="config">The server configuration manager.</param>
  50. /// <param name="imageEncoder">The image encoder.</param>
  51. /// <param name="dbProvider">The database provider.</param>
  52. /// <param name="appPaths">The application paths.</param>
  53. /// <param name="pathManager">The path manager.</param>
  54. public TrickplayManager(
  55. ILogger<TrickplayManager> logger,
  56. IMediaEncoder mediaEncoder,
  57. IFileSystem fileSystem,
  58. EncodingHelper encodingHelper,
  59. IServerConfigurationManager config,
  60. IImageEncoder imageEncoder,
  61. IDbContextFactory<JellyfinDbContext> dbProvider,
  62. IApplicationPaths appPaths,
  63. IPathManager pathManager)
  64. {
  65. _logger = logger;
  66. _mediaEncoder = mediaEncoder;
  67. _fileSystem = fileSystem;
  68. _encodingHelper = encodingHelper;
  69. _config = config;
  70. _imageEncoder = imageEncoder;
  71. _dbProvider = dbProvider;
  72. _appPaths = appPaths;
  73. _pathManager = pathManager;
  74. }
  75. /// <inheritdoc />
  76. public async Task MoveGeneratedTrickplayDataAsync(Video video, LibraryOptions libraryOptions, CancellationToken cancellationToken)
  77. {
  78. var options = _config.Configuration.TrickplayOptions;
  79. if (libraryOptions is null || !libraryOptions.EnableTrickplayImageExtraction || !CanGenerateTrickplay(video, options.Interval))
  80. {
  81. return;
  82. }
  83. var existingTrickplayResolutions = await GetTrickplayResolutions(video.Id).ConfigureAwait(false);
  84. foreach (var resolution in existingTrickplayResolutions)
  85. {
  86. cancellationToken.ThrowIfCancellationRequested();
  87. var existingResolution = resolution.Key;
  88. var tileWidth = resolution.Value.TileWidth;
  89. var tileHeight = resolution.Value.TileHeight;
  90. var shouldBeSavedWithMedia = libraryOptions is not null && libraryOptions.SaveTrickplayWithMedia;
  91. var localOutputDir = new DirectoryInfo(GetTrickplayDirectory(video, tileWidth, tileHeight, existingResolution, false));
  92. var mediaOutputDir = new DirectoryInfo(GetTrickplayDirectory(video, tileWidth, tileHeight, existingResolution, true));
  93. if (shouldBeSavedWithMedia && localOutputDir.Exists)
  94. {
  95. var localDirFiles = localOutputDir.EnumerateFiles();
  96. var mediaDirExists = mediaOutputDir.Exists;
  97. if (localDirFiles.Any() && ((mediaDirExists && mediaOutputDir.EnumerateFiles().Any()) || !mediaDirExists))
  98. {
  99. // Move images from local dir to media dir
  100. MoveContent(localOutputDir.FullName, mediaOutputDir.FullName);
  101. _logger.LogInformation("Moved trickplay images for {ItemName} to {Location}", video.Name, mediaOutputDir);
  102. }
  103. }
  104. else if (!shouldBeSavedWithMedia && mediaOutputDir.Exists)
  105. {
  106. var mediaDirFiles = mediaOutputDir.EnumerateFiles();
  107. var localDirExists = localOutputDir.Exists;
  108. if (mediaDirFiles.Any() && ((localDirExists && localOutputDir.EnumerateFiles().Any()) || !localDirExists))
  109. {
  110. // Move images from media dir to local dir
  111. MoveContent(mediaOutputDir.FullName, localOutputDir.FullName);
  112. _logger.LogInformation("Moved trickplay images for {ItemName} to {Location}", video.Name, localOutputDir);
  113. }
  114. }
  115. }
  116. }
  117. private void MoveContent(string sourceFolder, string destinationFolder)
  118. {
  119. _fileSystem.MoveDirectory(sourceFolder, destinationFolder);
  120. var parent = Directory.GetParent(sourceFolder);
  121. if (parent is not null)
  122. {
  123. var parentContent = parent.EnumerateDirectories();
  124. if (!parentContent.Any())
  125. {
  126. parent.Delete();
  127. }
  128. }
  129. }
  130. /// <inheritdoc />
  131. public async Task RefreshTrickplayDataAsync(Video video, bool replace, LibraryOptions libraryOptions, CancellationToken cancellationToken)
  132. {
  133. var options = _config.Configuration.TrickplayOptions;
  134. if (!CanGenerateTrickplay(video, options.Interval) || libraryOptions is null)
  135. {
  136. return;
  137. }
  138. var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
  139. await using (dbContext.ConfigureAwait(false))
  140. {
  141. var saveWithMedia = libraryOptions.SaveTrickplayWithMedia;
  142. var trickplayDirectory = _pathManager.GetTrickplayDirectory(video, saveWithMedia);
  143. if (!libraryOptions.EnableTrickplayImageExtraction || replace)
  144. {
  145. // Prune existing data
  146. if (Directory.Exists(trickplayDirectory))
  147. {
  148. try
  149. {
  150. Directory.Delete(trickplayDirectory, true);
  151. }
  152. catch (Exception ex)
  153. {
  154. _logger.LogWarning("Unable to clear trickplay directory: {Directory}: {Exception}", trickplayDirectory, ex);
  155. }
  156. }
  157. await dbContext.TrickplayInfos
  158. .Where(i => i.ItemId.Equals(video.Id))
  159. .ExecuteDeleteAsync(cancellationToken)
  160. .ConfigureAwait(false);
  161. if (!replace)
  162. {
  163. return;
  164. }
  165. }
  166. _logger.LogDebug("Trickplay refresh for {ItemId} (replace existing: {Replace})", video.Id, replace);
  167. if (options.Interval < 1000)
  168. {
  169. _logger.LogWarning("Trickplay image interval {Interval} is too small, reset to the minimum valid value of 1000", options.Interval);
  170. options.Interval = 1000;
  171. }
  172. foreach (var width in options.WidthResolutions)
  173. {
  174. cancellationToken.ThrowIfCancellationRequested();
  175. await RefreshTrickplayDataInternal(
  176. video,
  177. replace,
  178. width,
  179. options,
  180. saveWithMedia,
  181. cancellationToken).ConfigureAwait(false);
  182. }
  183. // Cleanup old trickplay files
  184. if (Directory.Exists(trickplayDirectory))
  185. {
  186. var existingFolders = Directory.GetDirectories(trickplayDirectory).ToList();
  187. var trickplayInfos = await dbContext.TrickplayInfos
  188. .AsNoTracking()
  189. .Where(i => i.ItemId.Equals(video.Id))
  190. .ToListAsync(cancellationToken)
  191. .ConfigureAwait(false);
  192. var expectedFolders = trickplayInfos.Select(i => GetTrickplayDirectory(video, i.TileWidth, i.TileHeight, i.Width, saveWithMedia)).ToList();
  193. var foldersToRemove = existingFolders.Except(expectedFolders);
  194. foreach (var folder in foldersToRemove)
  195. {
  196. try
  197. {
  198. _logger.LogWarning("Pruning trickplay files for {Item}", video.Path);
  199. Directory.Delete(folder, true);
  200. }
  201. catch (Exception ex)
  202. {
  203. _logger.LogWarning("Unable to remove trickplay directory: {Directory}: {Exception}", folder, ex);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. private async Task RefreshTrickplayDataInternal(
  210. Video video,
  211. bool replace,
  212. int width,
  213. TrickplayOptions options,
  214. bool saveWithMedia,
  215. CancellationToken cancellationToken)
  216. {
  217. var imgTempDir = string.Empty;
  218. using (await _resourcePool.LockAsync(cancellationToken).ConfigureAwait(false))
  219. {
  220. try
  221. {
  222. // Extract images
  223. // Note: Media sources under parent items exist as their own video/item as well. Only use this video stream for trickplay.
  224. var mediaSource = video.GetMediaSources(false).FirstOrDefault(source => Guid.Parse(source.Id).Equals(video.Id));
  225. if (mediaSource is null)
  226. {
  227. _logger.LogDebug("Found no matching media source for item {ItemId}", video.Id);
  228. return;
  229. }
  230. var mediaPath = mediaSource.Path;
  231. if (!File.Exists(mediaPath))
  232. {
  233. _logger.LogWarning("Media not found at {Path} for item {ItemID}", mediaPath, video.Id);
  234. return;
  235. }
  236. // We support video backdrops, but we should not generate trickplay images for them
  237. var parentDirectory = Directory.GetParent(mediaPath);
  238. if (parentDirectory is not null && string.Equals(parentDirectory.Name, "backdrops", StringComparison.OrdinalIgnoreCase))
  239. {
  240. _logger.LogDebug("Ignoring backdrop media found at {Path} for item {ItemID}", mediaPath, video.Id);
  241. return;
  242. }
  243. // The width has to be even, otherwise a lot of filters will not be able to sample it
  244. var actualWidth = 2 * (width / 2);
  245. // Force using the video width when the trickplay setting has a too large width
  246. if (mediaSource.VideoStream.Width is not null && mediaSource.VideoStream.Width < width)
  247. {
  248. _logger.LogWarning("Video width {VideoWidth} is smaller than trickplay setting {TrickPlayWidth}, using video width for thumbnails", mediaSource.VideoStream.Width, width);
  249. actualWidth = 2 * ((int)mediaSource.VideoStream.Width / 2);
  250. }
  251. var tileWidth = options.TileWidth;
  252. var tileHeight = options.TileHeight;
  253. var outputDir = new DirectoryInfo(GetTrickplayDirectory(video, tileWidth, tileHeight, actualWidth, saveWithMedia));
  254. // Import existing trickplay tiles
  255. if (!replace && outputDir.Exists)
  256. {
  257. var existingFiles = outputDir.GetFiles();
  258. if (existingFiles.Length > 0)
  259. {
  260. var hasTrickplayResolution = await HasTrickplayResolutionAsync(video.Id, actualWidth).ConfigureAwait(false);
  261. if (hasTrickplayResolution)
  262. {
  263. _logger.LogDebug("Found existing trickplay files for {ItemId}.", video.Id);
  264. return;
  265. }
  266. // Import tiles
  267. var localTrickplayInfo = new TrickplayInfo
  268. {
  269. ItemId = video.Id,
  270. Width = width,
  271. Interval = options.Interval,
  272. TileWidth = options.TileWidth,
  273. TileHeight = options.TileHeight,
  274. ThumbnailCount = existingFiles.Length,
  275. Height = 0,
  276. Bandwidth = 0
  277. };
  278. foreach (var tile in existingFiles)
  279. {
  280. var image = _imageEncoder.GetImageSize(tile.FullName);
  281. localTrickplayInfo.Height = Math.Max(localTrickplayInfo.Height, (int)Math.Ceiling((double)image.Height / localTrickplayInfo.TileHeight));
  282. var bitrate = (int)Math.Ceiling((decimal)tile.Length * 8 / localTrickplayInfo.TileWidth / localTrickplayInfo.TileHeight / (localTrickplayInfo.Interval / 1000));
  283. localTrickplayInfo.Bandwidth = Math.Max(localTrickplayInfo.Bandwidth, bitrate);
  284. }
  285. await SaveTrickplayInfo(localTrickplayInfo).ConfigureAwait(false);
  286. _logger.LogDebug("Imported existing trickplay files for {ItemId}.", video.Id);
  287. return;
  288. }
  289. }
  290. // Generate trickplay tiles
  291. var mediaStream = mediaSource.VideoStream;
  292. var container = mediaSource.Container;
  293. _logger.LogInformation("Creating trickplay files at {Width} width, for {Path} [ID: {ItemId}]", actualWidth, mediaPath, video.Id);
  294. imgTempDir = await _mediaEncoder.ExtractVideoImagesOnIntervalAccelerated(
  295. mediaPath,
  296. container,
  297. mediaSource,
  298. mediaStream,
  299. actualWidth,
  300. TimeSpan.FromMilliseconds(options.Interval),
  301. options.EnableHwAcceleration,
  302. options.EnableHwEncoding,
  303. options.ProcessThreads,
  304. options.Qscale,
  305. options.ProcessPriority,
  306. options.EnableKeyFrameOnlyExtraction,
  307. _encodingHelper,
  308. cancellationToken).ConfigureAwait(false);
  309. if (string.IsNullOrEmpty(imgTempDir) || !Directory.Exists(imgTempDir))
  310. {
  311. throw new InvalidOperationException("Null or invalid directory from media encoder.");
  312. }
  313. var images = _fileSystem.GetFiles(imgTempDir, _trickplayImgExtensions, false, false)
  314. .Select(i => i.FullName)
  315. .OrderBy(i => i)
  316. .ToList();
  317. // Create tiles
  318. var trickplayInfo = CreateTiles(images, actualWidth, options, outputDir.FullName);
  319. // Save tiles info
  320. try
  321. {
  322. if (trickplayInfo is not null)
  323. {
  324. trickplayInfo.ItemId = video.Id;
  325. await SaveTrickplayInfo(trickplayInfo).ConfigureAwait(false);
  326. _logger.LogInformation("Finished creation of trickplay files for {0}", mediaPath);
  327. }
  328. else
  329. {
  330. throw new InvalidOperationException("Null trickplay tiles info from CreateTiles.");
  331. }
  332. }
  333. catch (Exception ex)
  334. {
  335. _logger.LogError(ex, "Error while saving trickplay tiles info.");
  336. // Make sure no files stay in metadata folders on failure
  337. // if tiles info wasn't saved.
  338. outputDir.Delete(true);
  339. }
  340. }
  341. catch (Exception ex)
  342. {
  343. _logger.LogError(ex, "Error creating trickplay images.");
  344. }
  345. finally
  346. {
  347. if (!string.IsNullOrEmpty(imgTempDir))
  348. {
  349. Directory.Delete(imgTempDir, true);
  350. }
  351. }
  352. }
  353. }
  354. /// <inheritdoc />
  355. public TrickplayInfo CreateTiles(IReadOnlyList<string> images, int width, TrickplayOptions options, string outputDir)
  356. {
  357. if (images.Count == 0)
  358. {
  359. throw new ArgumentException("Can't create trickplay from 0 images.");
  360. }
  361. var workDir = Path.Combine(_appPaths.TempDirectory, "trickplay_" + Guid.NewGuid().ToString("N"));
  362. Directory.CreateDirectory(workDir);
  363. var trickplayInfo = new TrickplayInfo
  364. {
  365. Width = width,
  366. Interval = options.Interval,
  367. TileWidth = options.TileWidth,
  368. TileHeight = options.TileHeight,
  369. ThumbnailCount = images.Count,
  370. // Set during image generation
  371. Height = 0,
  372. Bandwidth = 0
  373. };
  374. /*
  375. * Generate trickplay tiles from sets of thumbnails
  376. */
  377. var imageOptions = new ImageCollageOptions
  378. {
  379. Width = trickplayInfo.TileWidth,
  380. Height = trickplayInfo.TileHeight
  381. };
  382. var thumbnailsPerTile = trickplayInfo.TileWidth * trickplayInfo.TileHeight;
  383. var requiredTiles = (int)Math.Ceiling((double)images.Count / thumbnailsPerTile);
  384. for (int i = 0; i < requiredTiles; i++)
  385. {
  386. // Set output/input paths
  387. var tilePath = Path.Combine(workDir, $"{i}.jpg");
  388. imageOptions.OutputPath = tilePath;
  389. imageOptions.InputPaths = images.Skip(i * thumbnailsPerTile).Take(Math.Min(thumbnailsPerTile, images.Count - (i * thumbnailsPerTile))).ToList();
  390. // Generate image and use returned height for tiles info
  391. var height = _imageEncoder.CreateTrickplayTile(imageOptions, options.JpegQuality, trickplayInfo.Width, trickplayInfo.Height != 0 ? trickplayInfo.Height : null);
  392. if (trickplayInfo.Height == 0)
  393. {
  394. trickplayInfo.Height = height;
  395. }
  396. // Update bitrate
  397. var bitrate = (int)Math.Ceiling(new FileInfo(tilePath).Length * 8m / trickplayInfo.TileWidth / trickplayInfo.TileHeight / (trickplayInfo.Interval / 1000m));
  398. trickplayInfo.Bandwidth = Math.Max(trickplayInfo.Bandwidth, bitrate);
  399. }
  400. /*
  401. * Move trickplay tiles to output directory
  402. */
  403. Directory.CreateDirectory(Directory.GetParent(outputDir)!.FullName);
  404. // Replace existing tiles if they already exist
  405. if (Directory.Exists(outputDir))
  406. {
  407. Directory.Delete(outputDir, true);
  408. }
  409. _fileSystem.MoveDirectory(workDir, outputDir);
  410. return trickplayInfo;
  411. }
  412. private bool CanGenerateTrickplay(Video video, int interval)
  413. {
  414. var videoType = video.VideoType;
  415. if (videoType == VideoType.Iso || videoType == VideoType.Dvd || videoType == VideoType.BluRay)
  416. {
  417. return false;
  418. }
  419. if (video.IsPlaceHolder)
  420. {
  421. return false;
  422. }
  423. if (video.IsShortcut)
  424. {
  425. return false;
  426. }
  427. if (!video.IsCompleteMedia)
  428. {
  429. return false;
  430. }
  431. if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value < TimeSpan.FromMilliseconds(interval).Ticks)
  432. {
  433. return false;
  434. }
  435. // Can't extract images if there are no video streams
  436. return video.GetMediaStreams().Count > 0;
  437. }
  438. /// <inheritdoc />
  439. public async Task<Dictionary<int, TrickplayInfo>> GetTrickplayResolutions(Guid itemId)
  440. {
  441. var trickplayResolutions = new Dictionary<int, TrickplayInfo>();
  442. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  443. await using (dbContext.ConfigureAwait(false))
  444. {
  445. var trickplayInfos = await dbContext.TrickplayInfos
  446. .AsNoTracking()
  447. .Where(i => i.ItemId.Equals(itemId))
  448. .ToListAsync()
  449. .ConfigureAwait(false);
  450. foreach (var info in trickplayInfos)
  451. {
  452. trickplayResolutions[info.Width] = info;
  453. }
  454. }
  455. return trickplayResolutions;
  456. }
  457. /// <inheritdoc />
  458. public async Task<IReadOnlyList<TrickplayInfo>> GetTrickplayItemsAsync(int limit, int offset)
  459. {
  460. IReadOnlyList<TrickplayInfo> trickplayItems;
  461. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  462. await using (dbContext.ConfigureAwait(false))
  463. {
  464. trickplayItems = await dbContext.TrickplayInfos
  465. .AsNoTracking()
  466. .OrderBy(i => i.ItemId)
  467. .Skip(offset)
  468. .Take(limit)
  469. .ToListAsync()
  470. .ConfigureAwait(false);
  471. }
  472. return trickplayItems;
  473. }
  474. /// <inheritdoc />
  475. public async Task SaveTrickplayInfo(TrickplayInfo info)
  476. {
  477. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  478. await using (dbContext.ConfigureAwait(false))
  479. {
  480. var oldInfo = await dbContext.TrickplayInfos.FindAsync(info.ItemId, info.Width).ConfigureAwait(false);
  481. if (oldInfo is not null)
  482. {
  483. dbContext.TrickplayInfos.Remove(oldInfo);
  484. }
  485. dbContext.Add(info);
  486. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  487. }
  488. }
  489. /// <inheritdoc />
  490. public async Task DeleteTrickplayDataAsync(Guid itemId, CancellationToken cancellationToken)
  491. {
  492. var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
  493. await dbContext.TrickplayInfos.Where(i => i.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
  494. }
  495. /// <inheritdoc />
  496. public async Task<Dictionary<string, Dictionary<int, TrickplayInfo>>> GetTrickplayManifest(BaseItem item)
  497. {
  498. var trickplayManifest = new Dictionary<string, Dictionary<int, TrickplayInfo>>();
  499. foreach (var mediaSource in item.GetMediaSources(false))
  500. {
  501. if (mediaSource.IsRemote || !Guid.TryParse(mediaSource.Id, out var mediaSourceId))
  502. {
  503. continue;
  504. }
  505. var trickplayResolutions = await GetTrickplayResolutions(mediaSourceId).ConfigureAwait(false);
  506. if (trickplayResolutions.Count > 0)
  507. {
  508. trickplayManifest[mediaSource.Id] = trickplayResolutions;
  509. }
  510. }
  511. return trickplayManifest;
  512. }
  513. /// <inheritdoc />
  514. public async Task<string> GetTrickplayTilePathAsync(BaseItem item, int width, int index, bool saveWithMedia)
  515. {
  516. var trickplayResolutions = await GetTrickplayResolutions(item.Id).ConfigureAwait(false);
  517. if (trickplayResolutions is not null && trickplayResolutions.TryGetValue(width, out var trickplayInfo))
  518. {
  519. return Path.Combine(GetTrickplayDirectory(item, trickplayInfo.TileWidth, trickplayInfo.TileHeight, width, saveWithMedia), index + ".jpg");
  520. }
  521. return string.Empty;
  522. }
  523. /// <inheritdoc />
  524. public async Task<string?> GetHlsPlaylist(Guid itemId, int width, string? apiKey)
  525. {
  526. var trickplayResolutions = await GetTrickplayResolutions(itemId).ConfigureAwait(false);
  527. if (trickplayResolutions is not null && trickplayResolutions.TryGetValue(width, out var trickplayInfo))
  528. {
  529. var builder = new StringBuilder(128);
  530. if (trickplayInfo.ThumbnailCount > 0)
  531. {
  532. const string urlFormat = "{0}.jpg?MediaSourceId={1}&ApiKey={2}";
  533. const string decimalFormat = "{0:0.###}";
  534. var resolution = $"{trickplayInfo.Width}x{trickplayInfo.Height}";
  535. var layout = $"{trickplayInfo.TileWidth}x{trickplayInfo.TileHeight}";
  536. var thumbnailsPerTile = trickplayInfo.TileWidth * trickplayInfo.TileHeight;
  537. var thumbnailDuration = trickplayInfo.Interval / 1000d;
  538. var infDuration = thumbnailDuration * thumbnailsPerTile;
  539. var tileCount = (int)Math.Ceiling((decimal)trickplayInfo.ThumbnailCount / thumbnailsPerTile);
  540. builder
  541. .AppendLine("#EXTM3U")
  542. .Append("#EXT-X-TARGETDURATION:")
  543. .AppendLine(tileCount.ToString(CultureInfo.InvariantCulture))
  544. .AppendLine("#EXT-X-VERSION:7")
  545. .AppendLine("#EXT-X-MEDIA-SEQUENCE:1")
  546. .AppendLine("#EXT-X-PLAYLIST-TYPE:VOD")
  547. .AppendLine("#EXT-X-IMAGES-ONLY");
  548. for (int i = 0; i < tileCount; i++)
  549. {
  550. // All tiles prior to the last must contain full amount of thumbnails (no black).
  551. if (i == tileCount - 1)
  552. {
  553. thumbnailsPerTile = trickplayInfo.ThumbnailCount - (i * thumbnailsPerTile);
  554. infDuration = thumbnailDuration * thumbnailsPerTile;
  555. }
  556. // EXTINF
  557. builder
  558. .Append("#EXTINF:")
  559. .AppendFormat(CultureInfo.InvariantCulture, decimalFormat, infDuration)
  560. .AppendLine(",");
  561. // EXT-X-TILES
  562. builder
  563. .Append("#EXT-X-TILES:RESOLUTION=")
  564. .Append(resolution)
  565. .Append(",LAYOUT=")
  566. .Append(layout)
  567. .Append(",DURATION=")
  568. .AppendFormat(CultureInfo.InvariantCulture, decimalFormat, thumbnailDuration)
  569. .AppendLine();
  570. // URL
  571. builder
  572. .AppendFormat(
  573. CultureInfo.InvariantCulture,
  574. urlFormat,
  575. i.ToString(CultureInfo.InvariantCulture),
  576. itemId.ToString("N"),
  577. apiKey)
  578. .AppendLine();
  579. }
  580. builder.AppendLine("#EXT-X-ENDLIST");
  581. return builder.ToString();
  582. }
  583. }
  584. return null;
  585. }
  586. /// <inheritdoc />
  587. public string GetTrickplayDirectory(BaseItem item, int tileWidth, int tileHeight, int width, bool saveWithMedia = false)
  588. {
  589. var path = _pathManager.GetTrickplayDirectory(item, saveWithMedia);
  590. var subdirectory = string.Format(
  591. CultureInfo.InvariantCulture,
  592. "{0} - {1}x{2}",
  593. width.ToString(CultureInfo.InvariantCulture),
  594. tileWidth.ToString(CultureInfo.InvariantCulture),
  595. tileHeight.ToString(CultureInfo.InvariantCulture));
  596. return Path.Combine(path, subdirectory);
  597. }
  598. private async Task<bool> HasTrickplayResolutionAsync(Guid itemId, int width)
  599. {
  600. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  601. await using (dbContext.ConfigureAwait(false))
  602. {
  603. return await dbContext.TrickplayInfos
  604. .AsNoTracking()
  605. .Where(i => i.ItemId.Equals(itemId))
  606. .AnyAsync(i => i.Width == width)
  607. .ConfigureAwait(false);
  608. }
  609. }
  610. }