ChapterImagesTask.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.Common.IO;
  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. })
  81. .OfType<Video>()
  82. .ToList();
  83. var numComplete = 0;
  84. var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
  85. List<string> previouslyFailedImages;
  86. try
  87. {
  88. previouslyFailedImages = _fileSystem.ReadAllText(failHistoryPath)
  89. .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
  90. .ToList();
  91. }
  92. catch (FileNotFoundException)
  93. {
  94. previouslyFailedImages = new List<string>();
  95. }
  96. catch (IOException)
  97. {
  98. previouslyFailedImages = new List<string>();
  99. }
  100. foreach (var video in videos)
  101. {
  102. cancellationToken.ThrowIfCancellationRequested();
  103. var key = video.Path + video.DateModified.Ticks;
  104. var extract = !previouslyFailedImages.Contains(key, StringComparer.OrdinalIgnoreCase);
  105. try
  106. {
  107. var chapters = _itemRepo.GetChapters(video.Id).ToList();
  108. var success = await _encodingManager.RefreshChapterImages(new ChapterImageRefreshOptions
  109. {
  110. SaveChapters = true,
  111. ExtractImages = extract,
  112. Video = video,
  113. Chapters = chapters
  114. }, CancellationToken.None);
  115. if (!success)
  116. {
  117. previouslyFailedImages.Add(key);
  118. var parentPath = _fileSystem.GetDirectoryName(failHistoryPath);
  119. _fileSystem.CreateDirectory(parentPath);
  120. _fileSystem.WriteAllText(failHistoryPath, string.Join("|", previouslyFailedImages.ToArray()));
  121. }
  122. numComplete++;
  123. double percent = numComplete;
  124. percent /= videos.Count;
  125. progress.Report(100 * percent);
  126. }
  127. catch (ObjectDisposedException)
  128. {
  129. break;
  130. }
  131. }
  132. }
  133. /// <summary>
  134. /// Gets the name of the task
  135. /// </summary>
  136. /// <value>The name.</value>
  137. public string Name
  138. {
  139. get
  140. {
  141. return "Chapter image extraction";
  142. }
  143. }
  144. /// <summary>
  145. /// Gets the description.
  146. /// </summary>
  147. /// <value>The description.</value>
  148. public string Description
  149. {
  150. get { return "Creates thumbnails for videos that have chapters."; }
  151. }
  152. /// <summary>
  153. /// Gets the category.
  154. /// </summary>
  155. /// <value>The category.</value>
  156. public string Category
  157. {
  158. get
  159. {
  160. return "Library";
  161. }
  162. }
  163. }
  164. }