2
0

SubtitleScheduledTask.cs 8.1 KB

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