ChapterImagesTask.cs 5.9 KB

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