FFProbeProvider.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.IO;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Controller.Chapters;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Entities.Audio;
  8. using MediaBrowser.Controller.Entities.Movies;
  9. using MediaBrowser.Controller.Entities.TV;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.LiveTv;
  12. using MediaBrowser.Controller.Localization;
  13. using MediaBrowser.Controller.MediaEncoding;
  14. using MediaBrowser.Controller.Persistence;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Controller.Subtitles;
  17. using MediaBrowser.Model.Entities;
  18. using MediaBrowser.Model.IO;
  19. using MediaBrowser.Model.Logging;
  20. using MediaBrowser.Model.MediaInfo;
  21. using MediaBrowser.Model.Serialization;
  22. using System;
  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. {
  39. private readonly ILogger _logger;
  40. private readonly IIsoManager _isoManager;
  41. private readonly IMediaEncoder _mediaEncoder;
  42. private readonly IItemRepository _itemRepo;
  43. private readonly IBlurayExaminer _blurayExaminer;
  44. private readonly ILocalizationManager _localization;
  45. private readonly IApplicationPaths _appPaths;
  46. private readonly IJsonSerializer _json;
  47. private readonly IEncodingManager _encodingManager;
  48. private readonly IFileSystem _fileSystem;
  49. private readonly IServerConfigurationManager _config;
  50. private readonly ISubtitleManager _subtitleManager;
  51. private readonly IChapterManager _chapterManager;
  52. public string Name
  53. {
  54. get { return "ffprobe"; }
  55. }
  56. public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  57. {
  58. return FetchVideoInfo(item, options, cancellationToken);
  59. }
  60. public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  61. {
  62. return FetchVideoInfo(item, options, cancellationToken);
  63. }
  64. public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  65. {
  66. return FetchVideoInfo(item, options, cancellationToken);
  67. }
  68. public Task<ItemUpdateType> FetchAsync(LiveTvVideoRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  69. {
  70. return FetchVideoInfo(item, options, cancellationToken);
  71. }
  72. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  73. {
  74. return FetchVideoInfo(item, options, cancellationToken);
  75. }
  76. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  77. {
  78. return FetchAudioInfo(item, cancellationToken);
  79. }
  80. public Task<ItemUpdateType> FetchAsync(LiveTvAudioRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  81. {
  82. return FetchAudioInfo(item, cancellationToken);
  83. }
  84. 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)
  85. {
  86. _logger = logger;
  87. _isoManager = isoManager;
  88. _mediaEncoder = mediaEncoder;
  89. _itemRepo = itemRepo;
  90. _blurayExaminer = blurayExaminer;
  91. _localization = localization;
  92. _appPaths = appPaths;
  93. _json = json;
  94. _encodingManager = encodingManager;
  95. _fileSystem = fileSystem;
  96. _config = config;
  97. _subtitleManager = subtitleManager;
  98. _chapterManager = chapterManager;
  99. }
  100. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  101. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  102. where T : Video
  103. {
  104. if (item.LocationType != LocationType.FileSystem)
  105. {
  106. return _cachedTask;
  107. }
  108. if (item.VideoType == VideoType.Iso && !_isoManager.CanMount(item.Path))
  109. {
  110. return _cachedTask;
  111. }
  112. if (item.VideoType == VideoType.HdDvd)
  113. {
  114. return _cachedTask;
  115. }
  116. if (item.IsPlaceHolder)
  117. {
  118. return _cachedTask;
  119. }
  120. if (item.IsShortcut)
  121. {
  122. FetchShortcutInfo(item);
  123. return Task.FromResult(ItemUpdateType.MetadataEdit);
  124. }
  125. var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager);
  126. return prober.ProbeVideo(item, options, cancellationToken);
  127. }
  128. private void FetchShortcutInfo(Video video)
  129. {
  130. video.ShortcutPath = File.ReadAllText(video.Path);
  131. }
  132. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, CancellationToken cancellationToken)
  133. where T : Audio
  134. {
  135. if (item.LocationType != LocationType.FileSystem)
  136. {
  137. return _cachedTask;
  138. }
  139. var prober = new FFProbeAudioInfo(_mediaEncoder, _itemRepo, _appPaths, _json);
  140. return prober.Probe(item, cancellationToken);
  141. }
  142. public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
  143. {
  144. if (status.ItemDateModified.HasValue)
  145. {
  146. if (status.ItemDateModified.Value != item.DateModified)
  147. {
  148. return true;
  149. }
  150. }
  151. if (item.SupportsLocalMetadata)
  152. {
  153. var video = item as Video;
  154. if (video != null && !video.IsPlaceHolder)
  155. {
  156. return !video.SubtitleFiles
  157. .SequenceEqual(SubtitleResolver.GetSubtitleFiles(video, directoryService, _fileSystem, false)
  158. .Select(i => i.FullName)
  159. .OrderBy(i => i), StringComparer.OrdinalIgnoreCase);
  160. }
  161. }
  162. return false;
  163. }
  164. public int Order
  165. {
  166. get
  167. {
  168. // Run last
  169. return 100;
  170. }
  171. }
  172. }
  173. }