2
0

FFProbeProvider.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.Model.Globalization;
  24. namespace MediaBrowser.Providers.MediaInfo
  25. {
  26. public class FFProbeProvider : ICustomMetadataProvider<Episode>,
  27. ICustomMetadataProvider<MusicVideo>,
  28. ICustomMetadataProvider<Movie>,
  29. ICustomMetadataProvider<LiveTvVideoRecording>,
  30. ICustomMetadataProvider<LiveTvAudioRecording>,
  31. ICustomMetadataProvider<Trailer>,
  32. ICustomMetadataProvider<Video>,
  33. ICustomMetadataProvider<Audio>,
  34. ICustomMetadataProvider<AudioPodcast>,
  35. ICustomMetadataProvider<AudioBook>,
  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(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  76. {
  77. return FetchVideoInfo(item, options, cancellationToken);
  78. }
  79. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  80. {
  81. return FetchVideoInfo(item, options, cancellationToken);
  82. }
  83. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  84. {
  85. return FetchAudioInfo(item, cancellationToken);
  86. }
  87. public Task<ItemUpdateType> FetchAsync(LiveTvAudioRecording item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  88. {
  89. return FetchAudioInfo(item, cancellationToken);
  90. }
  91. public Task<ItemUpdateType> FetchAsync(AudioPodcast item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  92. {
  93. return FetchAudioInfo(item, cancellationToken);
  94. }
  95. public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  96. {
  97. return FetchAudioInfo(item, cancellationToken);
  98. }
  99. 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)
  100. {
  101. _logger = logger;
  102. _isoManager = isoManager;
  103. _mediaEncoder = mediaEncoder;
  104. _itemRepo = itemRepo;
  105. _blurayExaminer = blurayExaminer;
  106. _localization = localization;
  107. _appPaths = appPaths;
  108. _json = json;
  109. _encodingManager = encodingManager;
  110. _fileSystem = fileSystem;
  111. _config = config;
  112. _subtitleManager = subtitleManager;
  113. _chapterManager = chapterManager;
  114. _libraryManager = libraryManager;
  115. }
  116. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  117. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  118. where T : Video
  119. {
  120. if (item.LocationType != LocationType.FileSystem)
  121. {
  122. return _cachedTask;
  123. }
  124. if (item.VideoType == VideoType.Iso && !_isoManager.CanMount(item.Path))
  125. {
  126. return _cachedTask;
  127. }
  128. if (item.VideoType == VideoType.HdDvd)
  129. {
  130. return _cachedTask;
  131. }
  132. if (item.IsPlaceHolder)
  133. {
  134. return _cachedTask;
  135. }
  136. if (item.IsShortcut)
  137. {
  138. FetchShortcutInfo(item);
  139. return Task.FromResult(ItemUpdateType.MetadataImport);
  140. }
  141. var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager, _libraryManager);
  142. return prober.ProbeVideo(item, options, cancellationToken);
  143. }
  144. private void FetchShortcutInfo(Video video)
  145. {
  146. video.ShortcutPath = _fileSystem.ReadAllText(video.Path);
  147. }
  148. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, CancellationToken cancellationToken)
  149. where T : Audio
  150. {
  151. if (item.LocationType != LocationType.FileSystem)
  152. {
  153. return _cachedTask;
  154. }
  155. var prober = new FFProbeAudioInfo(_mediaEncoder, _itemRepo, _appPaths, _json, _libraryManager);
  156. return prober.Probe(item, cancellationToken);
  157. }
  158. public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
  159. {
  160. if (item.EnableRefreshOnDateModifiedChange && !string.IsNullOrWhiteSpace(item.Path) && item.LocationType == LocationType.FileSystem)
  161. {
  162. var file = directoryService.GetFile(item.Path);
  163. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  164. {
  165. return true;
  166. }
  167. }
  168. if (item.SupportsLocalMetadata)
  169. {
  170. var video = item as Video;
  171. if (video != null && !video.IsPlaceHolder)
  172. {
  173. return !video.SubtitleFiles
  174. .SequenceEqual(SubtitleResolver.GetSubtitleFiles(video, directoryService, _fileSystem, false)
  175. .OrderBy(i => i), StringComparer.OrdinalIgnoreCase);
  176. }
  177. }
  178. return false;
  179. }
  180. public int Order
  181. {
  182. get
  183. {
  184. // Run last
  185. return 100;
  186. }
  187. }
  188. }
  189. }