ChapterImagesTask.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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).ToList();
  110. var success = await _encodingManager.RefreshChapterImages(new ChapterImageRefreshOptions
  111. {
  112. SaveChapters = true,
  113. ExtractImages = extract,
  114. Video = video,
  115. Chapters = chapters
  116. }, CancellationToken.None);
  117. if (!success)
  118. {
  119. previouslyFailedImages.Add(key);
  120. var parentPath = _fileSystem.GetDirectoryName(failHistoryPath);
  121. _fileSystem.CreateDirectory(parentPath);
  122. _fileSystem.WriteAllText(failHistoryPath, string.Join("|", previouslyFailedImages.ToArray(previouslyFailedImages.Count)));
  123. }
  124. numComplete++;
  125. double percent = numComplete;
  126. percent /= videos.Count;
  127. progress.Report(100 * percent);
  128. }
  129. catch (ObjectDisposedException)
  130. {
  131. break;
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// Gets the name of the task
  137. /// </summary>
  138. /// <value>The name.</value>
  139. public string Name
  140. {
  141. get
  142. {
  143. return "Chapter image extraction";
  144. }
  145. }
  146. /// <summary>
  147. /// Gets the description.
  148. /// </summary>
  149. /// <value>The description.</value>
  150. public string Description
  151. {
  152. get { return "Creates thumbnails for videos that have chapters."; }
  153. }
  154. /// <summary>
  155. /// Gets the category.
  156. /// </summary>
  157. /// <value>The category.</value>
  158. public string Category
  159. {
  160. get
  161. {
  162. return "Library";
  163. }
  164. }
  165. }
  166. }