ChapterImagesTask.cs 6.3 KB

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