SubtitleScheduledTask.cs 7.8 KB

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