FFProbeProvider.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.IO;
  23. using System.Linq;
  24. using System.Threading;
  25. using System.Threading.Tasks;
  26. namespace MediaBrowser.Providers.MediaInfo
  27. {
  28. public class FFProbeProvider : ICustomMetadataProvider<Episode>,
  29. ICustomMetadataProvider<MusicVideo>,
  30. ICustomMetadataProvider<Movie>,
  31. ICustomMetadataProvider<LiveTvVideoRecording>,
  32. ICustomMetadataProvider<LiveTvAudioRecording>,
  33. ICustomMetadataProvider<Video>,
  34. ICustomMetadataProvider<Audio>,
  35. IHasItemChangeMonitor,
  36. IHasOrder,
  37. IForcedProvider,
  38. IPreRefreshProvider
  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. private readonly ILibraryManager _libraryManager;
  54. public string Name
  55. {
  56. get { return "ffprobe"; }
  57. }
  58. public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  59. {
  60. return FetchVideoInfo(item, options, cancellationToken);
  61. }
  62. public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  63. {
  64. return FetchVideoInfo(item, options, cancellationToken);
  65. }
  66. public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  67. {
  68. return FetchVideoInfo(item, options, cancellationToken);
  69. }
  70. public Task<ItemUpdateType> FetchAsync(LiveTvVideoRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  71. {
  72. return FetchVideoInfo(item, options, cancellationToken);
  73. }
  74. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  75. {
  76. return FetchVideoInfo(item, options, cancellationToken);
  77. }
  78. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  79. {
  80. return FetchAudioInfo(item, cancellationToken);
  81. }
  82. public Task<ItemUpdateType> FetchAsync(LiveTvAudioRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  83. {
  84. return FetchAudioInfo(item, cancellationToken);
  85. }
  86. 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)
  87. {
  88. _logger = logger;
  89. _isoManager = isoManager;
  90. _mediaEncoder = mediaEncoder;
  91. _itemRepo = itemRepo;
  92. _blurayExaminer = blurayExaminer;
  93. _localization = localization;
  94. _appPaths = appPaths;
  95. _json = json;
  96. _encodingManager = encodingManager;
  97. _fileSystem = fileSystem;
  98. _config = config;
  99. _subtitleManager = subtitleManager;
  100. _chapterManager = chapterManager;
  101. _libraryManager = libraryManager;
  102. }
  103. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  104. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  105. where T : Video
  106. {
  107. if (item.LocationType != LocationType.FileSystem)
  108. {
  109. return _cachedTask;
  110. }
  111. if (item.VideoType == VideoType.Iso && !_isoManager.CanMount(item.Path))
  112. {
  113. return _cachedTask;
  114. }
  115. if (item.VideoType == VideoType.HdDvd)
  116. {
  117. return _cachedTask;
  118. }
  119. if (item.IsPlaceHolder)
  120. {
  121. return _cachedTask;
  122. }
  123. if (item.IsShortcut)
  124. {
  125. FetchShortcutInfo(item);
  126. return Task.FromResult(ItemUpdateType.MetadataImport);
  127. }
  128. var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager, _libraryManager);
  129. return prober.ProbeVideo(item, options, cancellationToken);
  130. }
  131. private void FetchShortcutInfo(Video video)
  132. {
  133. video.ShortcutPath = File.ReadAllText(video.Path);
  134. }
  135. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, CancellationToken cancellationToken)
  136. where T : Audio
  137. {
  138. if (item.LocationType != LocationType.FileSystem)
  139. {
  140. return _cachedTask;
  141. }
  142. var prober = new FFProbeAudioInfo(_mediaEncoder, _itemRepo, _appPaths, _json, _libraryManager);
  143. return prober.Probe(item, cancellationToken);
  144. }
  145. public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
  146. {
  147. if (status.ItemDateModified.HasValue)
  148. {
  149. if (status.ItemDateModified.Value != item.DateModified)
  150. {
  151. return true;
  152. }
  153. }
  154. if (item.SupportsLocalMetadata)
  155. {
  156. var video = item as Video;
  157. if (video != null && !video.IsPlaceHolder)
  158. {
  159. return !video.SubtitleFiles
  160. .SequenceEqual(SubtitleResolver.GetSubtitleFiles(video, directoryService, _fileSystem, false)
  161. .Select(i => i.FullName)
  162. .OrderBy(i => i), StringComparer.OrdinalIgnoreCase);
  163. }
  164. }
  165. return false;
  166. }
  167. public int Order
  168. {
  169. get
  170. {
  171. // Run last
  172. return 100;
  173. }
  174. }
  175. }
  176. }