FFProbeProvider.cs 7.5 KB

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