TrickplayManager.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. var existingFolders = Directory.GetDirectories(trickplayDirectory).ToList();
  185. var trickplayInfos = await dbContext.TrickplayInfos
  186. .AsNoTracking()
  187. .Where(i => i.ItemId.Equals(video.Id))
  188. .ToListAsync(cancellationToken)
  189. .ConfigureAwait(false);
  190. var expectedFolders = trickplayInfos.Select(i => GetTrickplayDirectory(video, i.TileWidth, i.TileHeight, i.Width, saveWithMedia)).ToList();
  191. var foldersToRemove = existingFolders.Except(expectedFolders);
  192. foreach (var folder in foldersToRemove)
  193. {
  194. try
  195. {
  196. _logger.LogWarning("Pruning trickplay files for {Item}", video.Path);
  197. Directory.Delete(folder, true);
  198. }
  199. catch (Exception ex)
  200. {
  201. _logger.LogWarning("Unable to remove trickplay directory: {Directory}: {Exception}", folder, ex);
  202. }
  203. }
  204. }
  205. }
  206. private async Task RefreshTrickplayDataInternal(
  207. Video video,
  208. bool replace,
  209. int width,
  210. TrickplayOptions options,
  211. bool saveWithMedia,
  212. CancellationToken cancellationToken)
  213. {
  214. var imgTempDir = string.Empty;
  215. using (await _resourcePool.LockAsync(cancellationToken).ConfigureAwait(false))
  216. {
  217. try
  218. {
  219. // Extract images
  220. // Note: Media sources under parent items exist as their own video/item as well. Only use this video stream for trickplay.
  221. var mediaSource = video.GetMediaSources(false).FirstOrDefault(source => Guid.Parse(source.Id).Equals(video.Id));
  222. if (mediaSource is null)
  223. {
  224. _logger.LogDebug("Found no matching media source for item {ItemId}", video.Id);
  225. return;
  226. }
  227. var mediaPath = mediaSource.Path;
  228. if (!File.Exists(mediaPath))
  229. {
  230. _logger.LogWarning("Media not found at {Path} for item {ItemID}", mediaPath, video.Id);
  231. return;
  232. }
  233. // We support video backdrops, but we should not generate trickplay images for them
  234. var parentDirectory = Directory.GetParent(mediaPath);
  235. if (parentDirectory is not null && string.Equals(parentDirectory.Name, "backdrops", StringComparison.OrdinalIgnoreCase))
  236. {
  237. _logger.LogDebug("Ignoring backdrop media found at {Path} for item {ItemID}", mediaPath, video.Id);
  238. return;
  239. }
  240. // The width has to be even, otherwise a lot of filters will not be able to sample it
  241. var actualWidth = 2 * (width / 2);
  242. // Force using the video width when the trickplay setting has a too large width
  243. if (mediaSource.VideoStream.Width is not null && mediaSource.VideoStream.Width < width)
  244. {
  245. _logger.LogWarning("Video width {VideoWidth} is smaller than trickplay setting {TrickPlayWidth}, using video width for thumbnails", mediaSource.VideoStream.Width, width);
  246. actualWidth = 2 * ((int)mediaSource.VideoStream.Width / 2);
  247. }
  248. var tileWidth = options.TileWidth;
  249. var tileHeight = options.TileHeight;
  250. var outputDir = new DirectoryInfo(GetTrickplayDirectory(video, tileWidth, tileHeight, actualWidth, saveWithMedia));
  251. // Import existing trickplay tiles
  252. if (!replace && outputDir.Exists)
  253. {
  254. var existingFiles = outputDir.GetFiles();
  255. if (existingFiles.Length > 0)
  256. {
  257. var hasTrickplayResolution = await HasTrickplayResolutionAsync(video.Id, actualWidth).ConfigureAwait(false);
  258. if (hasTrickplayResolution)
  259. {
  260. _logger.LogDebug("Found existing trickplay files for {ItemId}.", video.Id);
  261. return;
  262. }
  263. // Import tiles
  264. var localTrickplayInfo = new TrickplayInfo
  265. {
  266. ItemId = video.Id,
  267. Width = width,
  268. Interval = options.Interval,
  269. TileWidth = options.TileWidth,
  270. TileHeight = options.TileHeight,
  271. ThumbnailCount = existingFiles.Length,
  272. Height = 0,
  273. Bandwidth = 0
  274. };
  275. foreach (var tile in existingFiles)
  276. {
  277. var image = _imageEncoder.GetImageSize(tile.FullName);
  278. localTrickplayInfo.Height = Math.Max(localTrickplayInfo.Height, (int)Math.Ceiling((double)image.Height / localTrickplayInfo.TileHeight));
  279. var bitrate = (int)Math.Ceiling((decimal)tile.Length * 8 / localTrickplayInfo.TileWidth / localTrickplayInfo.TileHeight / (localTrickplayInfo.Interval / 1000));
  280. localTrickplayInfo.Bandwidth = Math.Max(localTrickplayInfo.Bandwidth, bitrate);
  281. }
  282. await SaveTrickplayInfo(localTrickplayInfo).ConfigureAwait(false);
  283. _logger.LogDebug("Imported existing trickplay files for {ItemId}.", video.Id);
  284. return;
  285. }
  286. }
  287. // Generate trickplay tiles
  288. var mediaStream = mediaSource.VideoStream;
  289. var container = mediaSource.Container;
  290. _logger.LogInformation("Creating trickplay files at {Width} width, for {Path} [ID: {ItemId}]", actualWidth, mediaPath, video.Id);
  291. imgTempDir = await _mediaEncoder.ExtractVideoImagesOnIntervalAccelerated(
  292. mediaPath,
  293. container,
  294. mediaSource,
  295. mediaStream,
  296. actualWidth,
  297. TimeSpan.FromMilliseconds(options.Interval),
  298. options.EnableHwAcceleration,
  299. options.EnableHwEncoding,
  300. options.ProcessThreads,
  301. options.Qscale,
  302. options.ProcessPriority,
  303. options.EnableKeyFrameOnlyExtraction,
  304. _encodingHelper,
  305. cancellationToken).ConfigureAwait(false);
  306. if (string.IsNullOrEmpty(imgTempDir) || !Directory.Exists(imgTempDir))
  307. {
  308. throw new InvalidOperationException("Null or invalid directory from media encoder.");
  309. }
  310. var images = _fileSystem.GetFiles(imgTempDir, _trickplayImgExtensions, false, false)
  311. .Select(i => i.FullName)
  312. .OrderBy(i => i)
  313. .ToList();
  314. // Create tiles
  315. var trickplayInfo = CreateTiles(images, actualWidth, options, outputDir.FullName);
  316. // Save tiles info
  317. try
  318. {
  319. if (trickplayInfo is not null)
  320. {
  321. trickplayInfo.ItemId = video.Id;
  322. await SaveTrickplayInfo(trickplayInfo).ConfigureAwait(false);
  323. _logger.LogInformation("Finished creation of trickplay files for {0}", mediaPath);
  324. }
  325. else
  326. {
  327. throw new InvalidOperationException("Null trickplay tiles info from CreateTiles.");
  328. }
  329. }
  330. catch (Exception ex)
  331. {
  332. _logger.LogError(ex, "Error while saving trickplay tiles info.");
  333. // Make sure no files stay in metadata folders on failure
  334. // if tiles info wasn't saved.
  335. outputDir.Delete(true);
  336. }
  337. }
  338. catch (Exception ex)
  339. {
  340. _logger.LogError(ex, "Error creating trickplay images.");
  341. }
  342. finally
  343. {
  344. if (!string.IsNullOrEmpty(imgTempDir))
  345. {
  346. Directory.Delete(imgTempDir, true);
  347. }
  348. }
  349. }
  350. }
  351. /// <inheritdoc />
  352. public TrickplayInfo CreateTiles(IReadOnlyList<string> images, int width, TrickplayOptions options, string outputDir)
  353. {
  354. if (images.Count == 0)
  355. {
  356. throw new ArgumentException("Can't create trickplay from 0 images.");
  357. }
  358. var workDir = Path.Combine(_appPaths.TempDirectory, "trickplay_" + Guid.NewGuid().ToString("N"));
  359. Directory.CreateDirectory(workDir);
  360. var trickplayInfo = new TrickplayInfo
  361. {
  362. Width = width,
  363. Interval = options.Interval,
  364. TileWidth = options.TileWidth,
  365. TileHeight = options.TileHeight,
  366. ThumbnailCount = images.Count,
  367. // Set during image generation
  368. Height = 0,
  369. Bandwidth = 0
  370. };
  371. /*
  372. * Generate trickplay tiles from sets of thumbnails
  373. */
  374. var imageOptions = new ImageCollageOptions
  375. {
  376. Width = trickplayInfo.TileWidth,
  377. Height = trickplayInfo.TileHeight
  378. };
  379. var thumbnailsPerTile = trickplayInfo.TileWidth * trickplayInfo.TileHeight;
  380. var requiredTiles = (int)Math.Ceiling((double)images.Count / thumbnailsPerTile);
  381. for (int i = 0; i < requiredTiles; i++)
  382. {
  383. // Set output/input paths
  384. var tilePath = Path.Combine(workDir, $"{i}.jpg");
  385. imageOptions.OutputPath = tilePath;
  386. imageOptions.InputPaths = images.Skip(i * thumbnailsPerTile).Take(Math.Min(thumbnailsPerTile, images.Count - (i * thumbnailsPerTile))).ToList();
  387. // Generate image and use returned height for tiles info
  388. var height = _imageEncoder.CreateTrickplayTile(imageOptions, options.JpegQuality, trickplayInfo.Width, trickplayInfo.Height != 0 ? trickplayInfo.Height : null);
  389. if (trickplayInfo.Height == 0)
  390. {
  391. trickplayInfo.Height = height;
  392. }
  393. // Update bitrate
  394. var bitrate = (int)Math.Ceiling(new FileInfo(tilePath).Length * 8m / trickplayInfo.TileWidth / trickplayInfo.TileHeight / (trickplayInfo.Interval / 1000m));
  395. trickplayInfo.Bandwidth = Math.Max(trickplayInfo.Bandwidth, bitrate);
  396. }
  397. /*
  398. * Move trickplay tiles to output directory
  399. */
  400. Directory.CreateDirectory(Directory.GetParent(outputDir)!.FullName);
  401. // Replace existing tiles if they already exist
  402. if (Directory.Exists(outputDir))
  403. {
  404. Directory.Delete(outputDir, true);
  405. }
  406. _fileSystem.MoveDirectory(workDir, outputDir);
  407. return trickplayInfo;
  408. }
  409. private bool CanGenerateTrickplay(Video video, int interval)
  410. {
  411. var videoType = video.VideoType;
  412. if (videoType == VideoType.Iso || videoType == VideoType.Dvd || videoType == VideoType.BluRay)
  413. {
  414. return false;
  415. }
  416. if (video.IsPlaceHolder)
  417. {
  418. return false;
  419. }
  420. if (video.IsShortcut)
  421. {
  422. return false;
  423. }
  424. if (!video.IsCompleteMedia)
  425. {
  426. return false;
  427. }
  428. if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value < TimeSpan.FromMilliseconds(interval).Ticks)
  429. {
  430. return false;
  431. }
  432. // Can't extract images if there are no video streams
  433. return video.GetMediaStreams().Count > 0;
  434. }
  435. /// <inheritdoc />
  436. public async Task<Dictionary<int, TrickplayInfo>> GetTrickplayResolutions(Guid itemId)
  437. {
  438. var trickplayResolutions = new Dictionary<int, TrickplayInfo>();
  439. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  440. await using (dbContext.ConfigureAwait(false))
  441. {
  442. var trickplayInfos = await dbContext.TrickplayInfos
  443. .AsNoTracking()
  444. .Where(i => i.ItemId.Equals(itemId))
  445. .ToListAsync()
  446. .ConfigureAwait(false);
  447. foreach (var info in trickplayInfos)
  448. {
  449. trickplayResolutions[info.Width] = info;
  450. }
  451. }
  452. return trickplayResolutions;
  453. }
  454. /// <inheritdoc />
  455. public async Task<IReadOnlyList<TrickplayInfo>> GetTrickplayItemsAsync(int limit, int offset)
  456. {
  457. IReadOnlyList<TrickplayInfo> trickplayItems;
  458. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  459. await using (dbContext.ConfigureAwait(false))
  460. {
  461. trickplayItems = await dbContext.TrickplayInfos
  462. .AsNoTracking()
  463. .OrderBy(i => i.ItemId)
  464. .Skip(offset)
  465. .Take(limit)
  466. .ToListAsync()
  467. .ConfigureAwait(false);
  468. }
  469. return trickplayItems;
  470. }
  471. /// <inheritdoc />
  472. public async Task SaveTrickplayInfo(TrickplayInfo info)
  473. {
  474. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  475. await using (dbContext.ConfigureAwait(false))
  476. {
  477. var oldInfo = await dbContext.TrickplayInfos.FindAsync(info.ItemId, info.Width).ConfigureAwait(false);
  478. if (oldInfo is not null)
  479. {
  480. dbContext.TrickplayInfos.Remove(oldInfo);
  481. }
  482. dbContext.Add(info);
  483. await dbContext.SaveChangesAsync().ConfigureAwait(false);
  484. }
  485. }
  486. /// <inheritdoc />
  487. public async Task DeleteTrickplayDataAsync(Guid itemId, CancellationToken cancellationToken)
  488. {
  489. var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
  490. await dbContext.TrickplayInfos.Where(i => i.ItemId.Equals(itemId)).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false);
  491. }
  492. /// <inheritdoc />
  493. public async Task<Dictionary<string, Dictionary<int, TrickplayInfo>>> GetTrickplayManifest(BaseItem item)
  494. {
  495. var trickplayManifest = new Dictionary<string, Dictionary<int, TrickplayInfo>>();
  496. foreach (var mediaSource in item.GetMediaSources(false))
  497. {
  498. if (mediaSource.IsRemote || !Guid.TryParse(mediaSource.Id, out var mediaSourceId))
  499. {
  500. continue;
  501. }
  502. var trickplayResolutions = await GetTrickplayResolutions(mediaSourceId).ConfigureAwait(false);
  503. if (trickplayResolutions.Count > 0)
  504. {
  505. trickplayManifest[mediaSource.Id] = trickplayResolutions;
  506. }
  507. }
  508. return trickplayManifest;
  509. }
  510. /// <inheritdoc />
  511. public async Task<string> GetTrickplayTilePathAsync(BaseItem item, int width, int index, bool saveWithMedia)
  512. {
  513. var trickplayResolutions = await GetTrickplayResolutions(item.Id).ConfigureAwait(false);
  514. if (trickplayResolutions is not null && trickplayResolutions.TryGetValue(width, out var trickplayInfo))
  515. {
  516. return Path.Combine(GetTrickplayDirectory(item, trickplayInfo.TileWidth, trickplayInfo.TileHeight, width, saveWithMedia), index + ".jpg");
  517. }
  518. return string.Empty;
  519. }
  520. /// <inheritdoc />
  521. public async Task<string?> GetHlsPlaylist(Guid itemId, int width, string? apiKey)
  522. {
  523. var trickplayResolutions = await GetTrickplayResolutions(itemId).ConfigureAwait(false);
  524. if (trickplayResolutions is not null && trickplayResolutions.TryGetValue(width, out var trickplayInfo))
  525. {
  526. var builder = new StringBuilder(128);
  527. if (trickplayInfo.ThumbnailCount > 0)
  528. {
  529. const string urlFormat = "{0}.jpg?MediaSourceId={1}&ApiKey={2}";
  530. const string decimalFormat = "{0:0.###}";
  531. var resolution = $"{trickplayInfo.Width}x{trickplayInfo.Height}";
  532. var layout = $"{trickplayInfo.TileWidth}x{trickplayInfo.TileHeight}";
  533. var thumbnailsPerTile = trickplayInfo.TileWidth * trickplayInfo.TileHeight;
  534. var thumbnailDuration = trickplayInfo.Interval / 1000d;
  535. var infDuration = thumbnailDuration * thumbnailsPerTile;
  536. var tileCount = (int)Math.Ceiling((decimal)trickplayInfo.ThumbnailCount / thumbnailsPerTile);
  537. builder
  538. .AppendLine("#EXTM3U")
  539. .Append("#EXT-X-TARGETDURATION:")
  540. .AppendLine(tileCount.ToString(CultureInfo.InvariantCulture))
  541. .AppendLine("#EXT-X-VERSION:7")
  542. .AppendLine("#EXT-X-MEDIA-SEQUENCE:1")
  543. .AppendLine("#EXT-X-PLAYLIST-TYPE:VOD")
  544. .AppendLine("#EXT-X-IMAGES-ONLY");
  545. for (int i = 0; i < tileCount; i++)
  546. {
  547. // All tiles prior to the last must contain full amount of thumbnails (no black).
  548. if (i == tileCount - 1)
  549. {
  550. thumbnailsPerTile = trickplayInfo.ThumbnailCount - (i * thumbnailsPerTile);
  551. infDuration = thumbnailDuration * thumbnailsPerTile;
  552. }
  553. // EXTINF
  554. builder
  555. .Append("#EXTINF:")
  556. .AppendFormat(CultureInfo.InvariantCulture, decimalFormat, infDuration)
  557. .AppendLine(",");
  558. // EXT-X-TILES
  559. builder
  560. .Append("#EXT-X-TILES:RESOLUTION=")
  561. .Append(resolution)
  562. .Append(",LAYOUT=")
  563. .Append(layout)
  564. .Append(",DURATION=")
  565. .AppendFormat(CultureInfo.InvariantCulture, decimalFormat, thumbnailDuration)
  566. .AppendLine();
  567. // URL
  568. builder
  569. .AppendFormat(
  570. CultureInfo.InvariantCulture,
  571. urlFormat,
  572. i.ToString(CultureInfo.InvariantCulture),
  573. itemId.ToString("N"),
  574. apiKey)
  575. .AppendLine();
  576. }
  577. builder.AppendLine("#EXT-X-ENDLIST");
  578. return builder.ToString();
  579. }
  580. }
  581. return null;
  582. }
  583. /// <inheritdoc />
  584. public string GetTrickplayDirectory(BaseItem item, int tileWidth, int tileHeight, int width, bool saveWithMedia = false)
  585. {
  586. var path = _pathManager.GetTrickplayDirectory(item, saveWithMedia);
  587. var subdirectory = string.Format(
  588. CultureInfo.InvariantCulture,
  589. "{0} - {1}x{2}",
  590. width.ToString(CultureInfo.InvariantCulture),
  591. tileWidth.ToString(CultureInfo.InvariantCulture),
  592. tileHeight.ToString(CultureInfo.InvariantCulture));
  593. return Path.Combine(path, subdirectory);
  594. }
  595. private async Task<bool> HasTrickplayResolutionAsync(Guid itemId, int width)
  596. {
  597. var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
  598. await using (dbContext.ConfigureAwait(false))
  599. {
  600. return await dbContext.TrickplayInfos
  601. .AsNoTracking()
  602. .Where(i => i.ItemId.Equals(itemId))
  603. .AnyAsync(i => i.Width == width)
  604. .ConfigureAwait(false);
  605. }
  606. }
  607. }