ChapterImagesTask.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using MediaBrowser.Common.ScheduledTasks;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Model.Tasks;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel.Composition;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.ScheduledTasks
  11. {
  12. [Export(typeof(IScheduledTask))]
  13. class ChapterImagesTask : BaseScheduledTask<Kernel>
  14. {
  15. /// <summary>
  16. /// Creates the triggers that define when the task will run
  17. /// </summary>
  18. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  19. protected override IEnumerable<BaseTaskTrigger> GetDefaultTriggers()
  20. {
  21. return new BaseTaskTrigger[]
  22. {
  23. new DailyTrigger { TimeOfDay = TimeSpan.FromHours(4) }
  24. };
  25. }
  26. /// <summary>
  27. /// Returns the task to be executed
  28. /// </summary>
  29. /// <param name="cancellationToken">The cancellation token.</param>
  30. /// <param name="progress">The progress.</param>
  31. /// <returns>Task.</returns>
  32. protected override Task ExecuteInternal(CancellationToken cancellationToken, IProgress<TaskProgress> progress)
  33. {
  34. var videos = Kernel.RootFolder.RecursiveChildren.OfType<Video>().Where(v => v.Chapters != null).ToList();
  35. var numComplete = 0;
  36. var tasks = videos.Select(v => Task.Run(async () =>
  37. {
  38. try
  39. {
  40. await Kernel.FFMpegManager.PopulateChapterImages(v, cancellationToken, true, true);
  41. }
  42. catch (OperationCanceledException)
  43. {
  44. throw;
  45. }
  46. catch (Exception ex)
  47. {
  48. Logger.ErrorException("Error creating chapter images for {0}", ex, v.Name);
  49. }
  50. finally
  51. {
  52. lock (progress)
  53. {
  54. numComplete++;
  55. double percent = numComplete;
  56. percent /= videos.Count;
  57. progress.Report(new TaskProgress { PercentComplete = 100 * percent });
  58. }
  59. }
  60. }));
  61. return Task.WhenAll(tasks);
  62. }
  63. /// <summary>
  64. /// Gets the name of the task
  65. /// </summary>
  66. /// <value>The name.</value>
  67. public override string Name
  68. {
  69. get { return "Create video chapter thumbnails"; }
  70. }
  71. /// <summary>
  72. /// Gets the description.
  73. /// </summary>
  74. /// <value>The description.</value>
  75. public override string Description
  76. {
  77. get { return "Creates thumbnails for videos that have chapters."; }
  78. }
  79. /// <summary>
  80. /// Gets the category.
  81. /// </summary>
  82. /// <value>The category.</value>
  83. public override string Category
  84. {
  85. get
  86. {
  87. return "Library";
  88. }
  89. }
  90. }
  91. }