FFProbeProvider.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.MediaEncoding;
  11. using MediaBrowser.Controller.Persistence;
  12. using MediaBrowser.Controller.Providers;
  13. using MediaBrowser.Controller.Subtitles;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Logging;
  17. using MediaBrowser.Model.MediaInfo;
  18. using MediaBrowser.Model.Serialization;
  19. using System;
  20. using System.Linq;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using MediaBrowser.Common.IO;
  24. using MediaBrowser.Controller.IO;
  25. using MediaBrowser.Model.IO;
  26. using MediaBrowser.Model.Globalization;
  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<Trailer>,
  35. ICustomMetadataProvider<Video>,
  36. ICustomMetadataProvider<Audio>,
  37. IHasItemChangeMonitor,
  38. IHasOrder,
  39. IForcedProvider,
  40. IPreRefreshProvider
  41. {
  42. private readonly ILogger _logger;
  43. private readonly IIsoManager _isoManager;
  44. private readonly IMediaEncoder _mediaEncoder;
  45. private readonly IItemRepository _itemRepo;
  46. private readonly IBlurayExaminer _blurayExaminer;
  47. private readonly ILocalizationManager _localization;
  48. private readonly IApplicationPaths _appPaths;
  49. private readonly IJsonSerializer _json;
  50. private readonly IEncodingManager _encodingManager;
  51. private readonly IFileSystem _fileSystem;
  52. private readonly IServerConfigurationManager _config;
  53. private readonly ISubtitleManager _subtitleManager;
  54. private readonly IChapterManager _chapterManager;
  55. private readonly ILibraryManager _libraryManager;
  56. public string Name
  57. {
  58. get { return "ffprobe"; }
  59. }
  60. public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  61. {
  62. return FetchVideoInfo(item, options, cancellationToken);
  63. }
  64. public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  65. {
  66. return FetchVideoInfo(item, options, cancellationToken);
  67. }
  68. public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  69. {
  70. return FetchVideoInfo(item, options, cancellationToken);
  71. }
  72. public Task<ItemUpdateType> FetchAsync(LiveTvVideoRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  73. {
  74. return FetchVideoInfo(item, options, cancellationToken);
  75. }
  76. public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  77. {
  78. return FetchVideoInfo(item, options, cancellationToken);
  79. }
  80. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  81. {
  82. return FetchVideoInfo(item, options, cancellationToken);
  83. }
  84. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  85. {
  86. return FetchAudioInfo(item, cancellationToken);
  87. }
  88. public Task<ItemUpdateType> FetchAsync(LiveTvAudioRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  89. {
  90. return FetchAudioInfo(item, cancellationToken);
  91. }
  92. 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)
  93. {
  94. _logger = logger;
  95. _isoManager = isoManager;
  96. _mediaEncoder = mediaEncoder;
  97. _itemRepo = itemRepo;
  98. _blurayExaminer = blurayExaminer;
  99. _localization = localization;
  100. _appPaths = appPaths;
  101. _json = json;
  102. _encodingManager = encodingManager;
  103. _fileSystem = fileSystem;
  104. _config = config;
  105. _subtitleManager = subtitleManager;
  106. _chapterManager = chapterManager;
  107. _libraryManager = libraryManager;
  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. if (item.IsShortcut)
  130. {
  131. FetchShortcutInfo(item);
  132. return Task.FromResult(ItemUpdateType.MetadataImport);
  133. }
  134. var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager, _libraryManager);
  135. return prober.ProbeVideo(item, options, cancellationToken);
  136. }
  137. private void FetchShortcutInfo(Video video)
  138. {
  139. video.ShortcutPath = _fileSystem.ReadAllText(video.Path);
  140. }
  141. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, CancellationToken cancellationToken)
  142. where T : Audio
  143. {
  144. if (item.LocationType != LocationType.FileSystem)
  145. {
  146. return _cachedTask;
  147. }
  148. var prober = new FFProbeAudioInfo(_mediaEncoder, _itemRepo, _appPaths, _json, _libraryManager);
  149. return prober.Probe(item, cancellationToken);
  150. }
  151. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  152. {
  153. if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
  154. {
  155. var file = directoryService.GetFile(item.Path);
  156. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  157. {
  158. return true;
  159. }
  160. }
  161. if (item.SupportsLocalMetadata)
  162. {
  163. var video = item as Video;
  164. if (video != null && !video.IsPlaceHolder)
  165. {
  166. return !video.SubtitleFiles
  167. .SequenceEqual(SubtitleResolver.GetSubtitleFiles(video, directoryService, _fileSystem, false)
  168. .Select(i => i.FullName)
  169. .OrderBy(i => i), StringComparer.OrdinalIgnoreCase);
  170. }
  171. }
  172. return false;
  173. }
  174. public int Order
  175. {
  176. get
  177. {
  178. // Run last
  179. return 100;
  180. }
  181. }
  182. }
  183. }