FFProbeProvider.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 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)
  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. }
  101. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  102. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  103. where T : Video
  104. {
  105. if (item.LocationType != LocationType.FileSystem)
  106. {
  107. return _cachedTask;
  108. }
  109. if (item.VideoType == VideoType.Iso && !_isoManager.CanMount(item.Path))
  110. {
  111. return _cachedTask;
  112. }
  113. if (item.VideoType == VideoType.HdDvd)
  114. {
  115. return _cachedTask;
  116. }
  117. if (item.IsPlaceHolder)
  118. {
  119. return _cachedTask;
  120. }
  121. if (item.IsShortcut)
  122. {
  123. FetchShortcutInfo(item);
  124. return Task.FromResult(ItemUpdateType.MetadataEdit);
  125. }
  126. var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager);
  127. return prober.ProbeVideo(item, options, cancellationToken);
  128. }
  129. private void FetchShortcutInfo(Video video)
  130. {
  131. video.ShortcutPath = File.ReadAllText(video.Path);
  132. }
  133. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, CancellationToken cancellationToken)
  134. where T : Audio
  135. {
  136. if (item.LocationType != LocationType.FileSystem)
  137. {
  138. return _cachedTask;
  139. }
  140. var prober = new FFProbeAudioInfo(_mediaEncoder, _itemRepo, _appPaths, _json);
  141. return prober.Probe(item, cancellationToken);
  142. }
  143. public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
  144. {
  145. if (status.ItemDateModified.HasValue)
  146. {
  147. if (status.ItemDateModified.Value != item.DateModified)
  148. {
  149. return true;
  150. }
  151. }
  152. if (item.SupportsLocalMetadata)
  153. {
  154. var video = item as Video;
  155. if (video != null && !video.IsPlaceHolder)
  156. {
  157. return !video.SubtitleFiles
  158. .SequenceEqual(SubtitleResolver.GetSubtitleFiles(video, directoryService, _fileSystem, false)
  159. .Select(i => i.FullName)
  160. .OrderBy(i => i), StringComparer.OrdinalIgnoreCase);
  161. }
  162. }
  163. return false;
  164. }
  165. public int Order
  166. {
  167. get
  168. {
  169. // Run last
  170. return 100;
  171. }
  172. }
  173. }
  174. }