SubtitleScheduledTask.cs 8.2 KB

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