ChapterImagesTask.cs 6.0 KB

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