FFProbeProvider.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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<Trailer>,
  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(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  75. {
  76. return FetchVideoInfo(item, options, cancellationToken);
  77. }
  78. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  79. {
  80. return FetchVideoInfo(item, options, cancellationToken);
  81. }
  82. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  83. {
  84. return FetchAudioInfo(item, cancellationToken);
  85. }
  86. public Task<ItemUpdateType> FetchAsync(LiveTvAudioRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  87. {
  88. return FetchAudioInfo(item, cancellationToken);
  89. }
  90. 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)
  91. {
  92. _logger = logger;
  93. _isoManager = isoManager;
  94. _mediaEncoder = mediaEncoder;
  95. _itemRepo = itemRepo;
  96. _blurayExaminer = blurayExaminer;
  97. _localization = localization;
  98. _appPaths = appPaths;
  99. _json = json;
  100. _encodingManager = encodingManager;
  101. _fileSystem = fileSystem;
  102. _config = config;
  103. _subtitleManager = subtitleManager;
  104. _chapterManager = chapterManager;
  105. _libraryManager = libraryManager;
  106. }
  107. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  108. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  109. where T : Video
  110. {
  111. if (item.LocationType != LocationType.FileSystem)
  112. {
  113. return _cachedTask;
  114. }
  115. if (item.VideoType == VideoType.Iso && !_isoManager.CanMount(item.Path))
  116. {
  117. return _cachedTask;
  118. }
  119. if (item.VideoType == VideoType.HdDvd)
  120. {
  121. return _cachedTask;
  122. }
  123. if (item.IsPlaceHolder)
  124. {
  125. return _cachedTask;
  126. }
  127. if (item.IsShortcut)
  128. {
  129. FetchShortcutInfo(item);
  130. return Task.FromResult(ItemUpdateType.MetadataImport);
  131. }
  132. var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager, _libraryManager);
  133. return prober.ProbeVideo(item, options, cancellationToken);
  134. }
  135. private void FetchShortcutInfo(Video video)
  136. {
  137. video.ShortcutPath = _fileSystem.ReadAllText(video.Path);
  138. }
  139. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, CancellationToken cancellationToken)
  140. where T : Audio
  141. {
  142. if (item.LocationType != LocationType.FileSystem)
  143. {
  144. return _cachedTask;
  145. }
  146. var prober = new FFProbeAudioInfo(_mediaEncoder, _itemRepo, _appPaths, _json, _libraryManager);
  147. return prober.Probe(item, cancellationToken);
  148. }
  149. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  150. {
  151. var file = directoryService.GetFile(item.Path);
  152. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  153. {
  154. return true;
  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. }