FFProbeProvider.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. 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 Task<ItemUpdateType> FetchAsync(AudioPodcast item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  91. {
  92. return FetchAudioInfo(item, cancellationToken);
  93. }
  94. public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  95. {
  96. return FetchAudioInfo(item, cancellationToken);
  97. }
  98. 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)
  99. {
  100. _logger = logger;
  101. _isoManager = isoManager;
  102. _mediaEncoder = mediaEncoder;
  103. _itemRepo = itemRepo;
  104. _blurayExaminer = blurayExaminer;
  105. _localization = localization;
  106. _appPaths = appPaths;
  107. _json = json;
  108. _encodingManager = encodingManager;
  109. _fileSystem = fileSystem;
  110. _config = config;
  111. _subtitleManager = subtitleManager;
  112. _chapterManager = chapterManager;
  113. _libraryManager = libraryManager;
  114. }
  115. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  116. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  117. where T : Video
  118. {
  119. if (item.LocationType != LocationType.FileSystem)
  120. {
  121. return _cachedTask;
  122. }
  123. if (item.VideoType == VideoType.Iso && !_isoManager.CanMount(item.Path))
  124. {
  125. return _cachedTask;
  126. }
  127. if (item.IsPlaceHolder)
  128. {
  129. return _cachedTask;
  130. }
  131. if (!item.IsCompleteMedia)
  132. {
  133. return _cachedTask;
  134. }
  135. if (item.IsShortcut)
  136. {
  137. FetchShortcutInfo(item);
  138. return Task.FromResult(ItemUpdateType.MetadataImport);
  139. }
  140. var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager, _libraryManager);
  141. return prober.ProbeVideo(item, options, cancellationToken);
  142. }
  143. private void FetchShortcutInfo(Video video)
  144. {
  145. video.ShortcutPath = _fileSystem.ReadAllText(video.Path)
  146. .Replace("\t", string.Empty)
  147. .Replace("\r", string.Empty)
  148. .Replace("\n", string.Empty)
  149. .Trim();
  150. }
  151. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, CancellationToken cancellationToken)
  152. where T : Audio
  153. {
  154. if (item.LocationType != LocationType.FileSystem)
  155. {
  156. return _cachedTask;
  157. }
  158. var prober = new FFProbeAudioInfo(_mediaEncoder, _itemRepo, _appPaths, _json, _libraryManager);
  159. return prober.Probe(item, cancellationToken);
  160. }
  161. public int Order
  162. {
  163. get
  164. {
  165. // Run last
  166. return 100;
  167. }
  168. }
  169. }
  170. }