SubtitleScheduledTask.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Controller.Configuration;
  8. using MediaBrowser.Controller.Dto;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Subtitles;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Providers;
  14. using MediaBrowser.Model.Serialization;
  15. using MediaBrowser.Model.Tasks;
  16. using Microsoft.Extensions.Logging;
  17. namespace MediaBrowser.Providers.MediaInfo
  18. {
  19. public class SubtitleScheduledTask : IScheduledTask
  20. {
  21. private readonly ILibraryManager _libraryManager;
  22. private readonly IServerConfigurationManager _config;
  23. private readonly ISubtitleManager _subtitleManager;
  24. private readonly IMediaSourceManager _mediaSourceManager;
  25. private readonly ILogger _logger;
  26. private readonly IJsonSerializer _json;
  27. public SubtitleScheduledTask(
  28. ILibraryManager libraryManager,
  29. IJsonSerializer json,
  30. IServerConfigurationManager config,
  31. ISubtitleManager subtitleManager,
  32. ILogger<SubtitleScheduledTask> logger,
  33. IMediaSourceManager mediaSourceManager)
  34. {
  35. _libraryManager = libraryManager;
  36. _config = config;
  37. _subtitleManager = subtitleManager;
  38. _logger = logger;
  39. _mediaSourceManager = mediaSourceManager;
  40. _json = json;
  41. }
  42. private SubtitleOptions GetOptions()
  43. {
  44. return _config.GetConfiguration<SubtitleOptions>("subtitles");
  45. }
  46. public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  47. {
  48. var options = GetOptions();
  49. var types = new[] { "Episode", "Movie" };
  50. var dict = new Dictionary<Guid, BaseItem>();
  51. foreach (var library in _libraryManager.RootFolder.Children.ToList())
  52. {
  53. var libraryOptions = _libraryManager.GetLibraryOptions(library);
  54. string[] subtitleDownloadLanguages;
  55. bool SkipIfEmbeddedSubtitlesPresent;
  56. bool SkipIfAudioTrackMatches;
  57. bool RequirePerfectMatch;
  58. if (libraryOptions.SubtitleDownloadLanguages == null)
  59. {
  60. subtitleDownloadLanguages = options.DownloadLanguages;
  61. SkipIfEmbeddedSubtitlesPresent = options.SkipIfEmbeddedSubtitlesPresent;
  62. SkipIfAudioTrackMatches = options.SkipIfAudioTrackMatches;
  63. RequirePerfectMatch = options.RequirePerfectMatch;
  64. }
  65. else
  66. {
  67. subtitleDownloadLanguages = libraryOptions.SubtitleDownloadLanguages;
  68. SkipIfEmbeddedSubtitlesPresent = libraryOptions.SkipSubtitlesIfEmbeddedSubtitlesPresent;
  69. SkipIfAudioTrackMatches = libraryOptions.SkipSubtitlesIfAudioTrackMatches;
  70. RequirePerfectMatch = libraryOptions.RequirePerfectSubtitleMatch;
  71. }
  72. foreach (var lang in subtitleDownloadLanguages)
  73. {
  74. var query = new InternalItemsQuery
  75. {
  76. MediaTypes = new string[] { MediaType.Video },
  77. IsVirtualItem = false,
  78. IncludeItemTypes = types,
  79. DtoOptions = new DtoOptions(true),
  80. SourceTypes = new[] { SourceType.Library },
  81. Parent = library,
  82. Recursive = true
  83. };
  84. if (SkipIfAudioTrackMatches)
  85. {
  86. query.HasNoAudioTrackWithLanguage = lang;
  87. }
  88. if (SkipIfEmbeddedSubtitlesPresent)
  89. {
  90. // Exclude if it already has any subtitles of the same language
  91. query.HasNoSubtitleTrackWithLanguage = lang;
  92. }
  93. else
  94. {
  95. // Exclude if it already has external subtitles of the same language
  96. query.HasNoExternalSubtitleTrackWithLanguage = lang;
  97. }
  98. var videosByLanguage = _libraryManager.GetItemList(query);
  99. foreach (var video in videosByLanguage)
  100. {
  101. dict[video.Id] = video;
  102. }
  103. }
  104. }
  105. var videos = dict.Values.ToList();
  106. if (videos.Count == 0)
  107. {
  108. return;
  109. }
  110. var numComplete = 0;
  111. foreach (var video in videos)
  112. {
  113. cancellationToken.ThrowIfCancellationRequested();
  114. try
  115. {
  116. await DownloadSubtitles(video as Video, options, cancellationToken).ConfigureAwait(false);
  117. }
  118. catch (Exception ex)
  119. {
  120. _logger.LogError(ex, "Error downloading subtitles for {Path}", video.Path);
  121. }
  122. // Update progress
  123. numComplete++;
  124. double percent = numComplete;
  125. percent /= videos.Count;
  126. progress.Report(100 * percent);
  127. }
  128. }
  129. private async Task<bool> DownloadSubtitles(Video video, SubtitleOptions options, CancellationToken cancellationToken)
  130. {
  131. var mediaStreams = video.GetMediaStreams();
  132. var libraryOptions = _libraryManager.GetLibraryOptions(video);
  133. string[] subtitleDownloadLanguages;
  134. bool SkipIfEmbeddedSubtitlesPresent;
  135. bool SkipIfAudioTrackMatches;
  136. bool RequirePerfectMatch;
  137. if (libraryOptions.SubtitleDownloadLanguages == null)
  138. {
  139. subtitleDownloadLanguages = options.DownloadLanguages;
  140. SkipIfEmbeddedSubtitlesPresent = options.SkipIfEmbeddedSubtitlesPresent;
  141. SkipIfAudioTrackMatches = options.SkipIfAudioTrackMatches;
  142. RequirePerfectMatch = options.RequirePerfectMatch;
  143. }
  144. else
  145. {
  146. subtitleDownloadLanguages = libraryOptions.SubtitleDownloadLanguages;
  147. SkipIfEmbeddedSubtitlesPresent = libraryOptions.SkipSubtitlesIfEmbeddedSubtitlesPresent;
  148. SkipIfAudioTrackMatches = libraryOptions.SkipSubtitlesIfAudioTrackMatches;
  149. RequirePerfectMatch = libraryOptions.RequirePerfectSubtitleMatch;
  150. }
  151. var downloadedLanguages = await new SubtitleDownloader(_logger,
  152. _subtitleManager)
  153. .DownloadSubtitles(video,
  154. mediaStreams,
  155. SkipIfEmbeddedSubtitlesPresent,
  156. SkipIfAudioTrackMatches,
  157. RequirePerfectMatch,
  158. subtitleDownloadLanguages,
  159. libraryOptions.DisabledSubtitleFetchers,
  160. libraryOptions.SubtitleFetcherOrder,
  161. cancellationToken).ConfigureAwait(false);
  162. // Rescan
  163. if (downloadedLanguages.Count > 0)
  164. {
  165. await video.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  166. return false;
  167. }
  168. return true;
  169. }
  170. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  171. {
  172. return new[] {
  173. // Every so often
  174. new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
  175. };
  176. }
  177. public string Name => "Download missing subtitles";
  178. public string Description => "Searches the internet for missing subtitles based on metadata configuration.";
  179. public string Category => "Library";
  180. public string Key => "DownloadSubtitles";
  181. public bool IsHidden => false;
  182. public bool IsEnabled => true;
  183. public bool IsLogged => true;
  184. }
  185. }