ChapterImagesTask.cs 5.7 KB

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