SubtitleScheduledTask.cs 8.0 KB

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