ChapterImagesTask.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.MediaEncoding;
  5. using MediaBrowser.Controller.Persistence;
  6. using MediaBrowser.Model.Logging;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using MediaBrowser.Controller.Dto;
  14. using MediaBrowser.Controller.IO;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.Tasks;
  18. using MediaBrowser.Model.Extensions;
  19. namespace Emby.Server.Implementations.ScheduledTasks
  20. {
  21. /// <summary>
  22. /// Class ChapterImagesTask
  23. /// </summary>
  24. class ChapterImagesTask : IScheduledTask
  25. {
  26. /// <summary>
  27. /// The _logger
  28. /// </summary>
  29. private readonly ILogger _logger;
  30. /// <summary>
  31. /// The _library manager
  32. /// </summary>
  33. private readonly ILibraryManager _libraryManager;
  34. private readonly IItemRepository _itemRepo;
  35. private readonly IApplicationPaths _appPaths;
  36. private readonly IEncodingManager _encodingManager;
  37. private readonly IFileSystem _fileSystem;
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="ChapterImagesTask" /> class.
  40. /// </summary>
  41. public ChapterImagesTask(ILogManager logManager, ILibraryManager libraryManager, IItemRepository itemRepo, IApplicationPaths appPaths, IEncodingManager encodingManager, IFileSystem fileSystem)
  42. {
  43. _logger = logManager.GetLogger(GetType().Name);
  44. _libraryManager = libraryManager;
  45. _itemRepo = itemRepo;
  46. _appPaths = appPaths;
  47. _encodingManager = encodingManager;
  48. _fileSystem = fileSystem;
  49. }
  50. /// <summary>
  51. /// Creates the triggers that define when the task will run
  52. /// </summary>
  53. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  54. {
  55. return new[] {
  56. new TaskTriggerInfo
  57. {
  58. Type = TaskTriggerInfo.TriggerDaily,
  59. TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
  60. MaxRuntimeMs = Convert.ToInt32(TimeSpan.FromHours(4).TotalMilliseconds)
  61. }
  62. };
  63. }
  64. public string Key
  65. {
  66. get { return "RefreshChapterImages"; }
  67. }
  68. /// <summary>
  69. /// Returns the task to be executed
  70. /// </summary>
  71. /// <param name="cancellationToken">The cancellation token.</param>
  72. /// <param name="progress">The progress.</param>
  73. /// <returns>Task.</returns>
  74. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  75. {
  76. var videos = _libraryManager.GetItemList(new InternalItemsQuery
  77. {
  78. MediaTypes = new[] { MediaType.Video },
  79. IsFolder = false,
  80. Recursive = true,
  81. DtoOptions = new DtoOptions(false)
  82. })
  83. .OfType<Video>()
  84. .ToList();
  85. var numComplete = 0;
  86. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  87. List<string> previouslyFailedImages;
  88. try
  89. {
  90. previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
  91. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  92. .ToList();
  93. }
  94. catch (FileNotFoundException)
  95. {
  96. previouslyFailedImages = new List<string>();
  97. }
  98. catch (IOException)
  99. {
  100. previouslyFailedImages = new List<string>();
  101. }
  102. foreach (var video in videos)
  103. {
  104. cancellationToken.ThrowIfCancellationRequested();
  105. var key = video.Path + video.DateModified.Ticks;
  106. var extract = !previouslyFailedImages.Contains(key, StringComparer.OrdinalIgnoreCase);
  107. try
  108. {
  109. var chapters = _itemRepo.GetChapters(video.Id);
  110. var success = await _encodingManager.RefreshChapterImages(video, chapters, extract, true, CancellationToken.None);
  111. if (!success)
  112. {
  113. previouslyFailedImages.Add(key);
  114. var parentPath = _fileSystem.GetDirectoryName(failHistoryPath);
  115. _fileSystem.CreateDirectory(parentPath);
  116. _fileSystem.WriteAllText(failHistoryPath, string.Join("|", previouslyFailedImages.ToArray(previouslyFailedImages.Count)));
  117. }
  118. numComplete++;
  119. double percent = numComplete;
  120. percent /= videos.Count;
  121. progress.Report(100 * percent);
  122. }
  123. catch (ObjectDisposedException)
  124. {
  125. break;
  126. }
  127. }
  128. }
  129. /// <summary>
  130. /// Gets the name of the task
  131. /// </summary>
  132. /// <value>The name.</value>
  133. public string Name
  134. {
  135. get
  136. {
  137. return "Chapter image extraction";
  138. }
  139. }
  140. /// <summary>
  141. /// Gets the description.
  142. /// </summary>
  143. /// <value>The description.</value>
  144. public string Description
  145. {
  146. get { return "Creates thumbnails for videos that have chapters."; }
  147. }
  148. /// <summary>
  149. /// Gets the category.
  150. /// </summary>
  151. /// <value>The category.</value>
  152. public string Category
  153. {
  154. get
  155. {
  156. return "Library";
  157. }
  158. }
  159. }
  160. }