FFProbeProvider.cs 7.5 KB

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