2
0

ChapterImagesTask.cs 5.8 KB

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