FFProbeProvider.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller.Chapters;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Entities.Audio;
  7. using MediaBrowser.Controller.Entities.Movies;
  8. using MediaBrowser.Controller.Entities.TV;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.LiveTv;
  11. using MediaBrowser.Controller.Localization;
  12. using MediaBrowser.Controller.MediaEncoding;
  13. using MediaBrowser.Controller.Persistence;
  14. using MediaBrowser.Controller.Providers;
  15. using MediaBrowser.Controller.Subtitles;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.IO;
  18. using MediaBrowser.Model.Logging;
  19. using MediaBrowser.Model.MediaInfo;
  20. using MediaBrowser.Model.Serialization;
  21. using System;
  22. using System.Linq;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. namespace MediaBrowser.Providers.MediaInfo
  26. {
  27. public class FFProbeProvider : ICustomMetadataProvider<Episode>,
  28. ICustomMetadataProvider<MusicVideo>,
  29. ICustomMetadataProvider<Movie>,
  30. ICustomMetadataProvider<AdultVideo>,
  31. ICustomMetadataProvider<LiveTvVideoRecording>,
  32. ICustomMetadataProvider<LiveTvAudioRecording>,
  33. ICustomMetadataProvider<Trailer>,
  34. ICustomMetadataProvider<Video>,
  35. ICustomMetadataProvider<Audio>,
  36. IHasChangeMonitor,
  37. IHasOrder,
  38. IForcedProvider
  39. {
  40. private readonly ILogger _logger;
  41. private readonly IIsoManager _isoManager;
  42. private readonly IMediaEncoder _mediaEncoder;
  43. private readonly IItemRepository _itemRepo;
  44. private readonly IBlurayExaminer _blurayExaminer;
  45. private readonly ILocalizationManager _localization;
  46. private readonly IApplicationPaths _appPaths;
  47. private readonly IJsonSerializer _json;
  48. private readonly IEncodingManager _encodingManager;
  49. private readonly IFileSystem _fileSystem;
  50. private readonly IServerConfigurationManager _config;
  51. private readonly ISubtitleManager _subtitleManager;
  52. private readonly IChapterManager _chapterManager;
  53. public string Name
  54. {
  55. get { return "ffprobe"; }
  56. }
  57. public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  58. {
  59. return FetchVideoInfo(item, options, cancellationToken);
  60. }
  61. public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  62. {
  63. return FetchVideoInfo(item, options, cancellationToken);
  64. }
  65. public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  66. {
  67. return FetchVideoInfo(item, options, cancellationToken);
  68. }
  69. public Task<ItemUpdateType> FetchAsync(AdultVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  70. {
  71. return FetchVideoInfo(item, options, cancellationToken);
  72. }
  73. public Task<ItemUpdateType> FetchAsync(LiveTvVideoRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  74. {
  75. return FetchVideoInfo(item, options, cancellationToken);
  76. }
  77. public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  78. {
  79. return FetchVideoInfo(item, options, cancellationToken);
  80. }
  81. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  82. {
  83. return FetchVideoInfo(item, options, cancellationToken);
  84. }
  85. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  86. {
  87. return FetchAudioInfo(item, cancellationToken);
  88. }
  89. public Task<ItemUpdateType> FetchAsync(LiveTvAudioRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  90. {
  91. return FetchAudioInfo(item, cancellationToken);
  92. }
  93. public FFProbeProvider(ILogger logger, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepo, IBlurayExaminer blurayExaminer, ILocalizationManager localization, IApplicationPaths appPaths, IJsonSerializer json, IEncodingManager encodingManager, IFileSystem fileSystem, IServerConfigurationManager config, ISubtitleManager subtitleManager, IChapterManager chapterManager)
  94. {
  95. _logger = logger;
  96. _isoManager = isoManager;
  97. _mediaEncoder = mediaEncoder;
  98. _itemRepo = itemRepo;
  99. _blurayExaminer = blurayExaminer;
  100. _localization = localization;
  101. _appPaths = appPaths;
  102. _json = json;
  103. _encodingManager = encodingManager;
  104. _fileSystem = fileSystem;
  105. _config = config;
  106. _subtitleManager = subtitleManager;
  107. _chapterManager = chapterManager;
  108. }
  109. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  110. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  111. where T : Video
  112. {
  113. if (item.LocationType != LocationType.FileSystem)
  114. {
  115. return _cachedTask;
  116. }
  117. if (item.VideoType == VideoType.Iso && !_isoManager.CanMount(item.Path))
  118. {
  119. return _cachedTask;
  120. }
  121. if (item.VideoType == VideoType.HdDvd)
  122. {
  123. return _cachedTask;
  124. }
  125. if (item.IsPlaceHolder)
  126. {
  127. return _cachedTask;
  128. }
  129. var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager);
  130. return prober.ProbeVideo(item, options, cancellationToken);
  131. }
  132. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, CancellationToken cancellationToken)
  133. where T : Audio
  134. {
  135. if (item.LocationType != LocationType.FileSystem)
  136. {
  137. return _cachedTask;
  138. }
  139. var prober = new FFProbeAudioInfo(_mediaEncoder, _itemRepo, _appPaths, _json);
  140. return prober.Probe(item, cancellationToken);
  141. }
  142. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
  143. {
  144. if (item.DateModified > date)
  145. {
  146. return true;
  147. }
  148. if (item is Audio)
  149. {
  150. // Moved to plural AlbumArtists
  151. if (date < new DateTime(2014, 8, 28))
  152. {
  153. return true;
  154. }
  155. }
  156. if (item.SupportsLocalMetadata)
  157. {
  158. var video = item as Video;
  159. if (video != null && !video.IsPlaceHolder)
  160. {
  161. return !video.SubtitleFiles
  162. .SequenceEqual(SubtitleResolver.GetSubtitleFiles(video, directoryService, _fileSystem, false)
  163. .Select(i => i.FullName)
  164. .OrderBy(i => i), StringComparer.OrdinalIgnoreCase);
  165. }
  166. }
  167. return false;
  168. }
  169. public int Order
  170. {
  171. get
  172. {
  173. // Run last
  174. return 100;
  175. }
  176. }
  177. }
  178. }