FFProbeProvider.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using MediaBrowser.Common.Configuration;
  6. using MediaBrowser.Controller.Channels;
  7. using MediaBrowser.Controller.Chapters;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Entities.Audio;
  11. using MediaBrowser.Controller.Entities.Movies;
  12. using MediaBrowser.Controller.Entities.TV;
  13. using MediaBrowser.Controller.Library;
  14. using MediaBrowser.Controller.MediaEncoding;
  15. using MediaBrowser.Controller.Persistence;
  16. using MediaBrowser.Controller.Providers;
  17. using MediaBrowser.Controller.Subtitles;
  18. using MediaBrowser.Model.Entities;
  19. using MediaBrowser.Model.Globalization;
  20. using MediaBrowser.Model.IO;
  21. using MediaBrowser.Model.MediaInfo;
  22. using MediaBrowser.Model.Serialization;
  23. using Microsoft.Extensions.Logging;
  24. namespace MediaBrowser.Providers.MediaInfo
  25. {
  26. public class FFProbeProvider : ICustomMetadataProvider<Episode>,
  27. ICustomMetadataProvider<MusicVideo>,
  28. ICustomMetadataProvider<Movie>,
  29. ICustomMetadataProvider<Trailer>,
  30. ICustomMetadataProvider<Video>,
  31. ICustomMetadataProvider<Audio>,
  32. ICustomMetadataProvider<AudioBook>,
  33. IHasOrder,
  34. IForcedProvider,
  35. IPreRefreshProvider,
  36. IHasItemChangeMonitor
  37. {
  38. private readonly ILogger _logger;
  39. private readonly IIsoManager _isoManager;
  40. private readonly IMediaEncoder _mediaEncoder;
  41. private readonly IItemRepository _itemRepo;
  42. private readonly IBlurayExaminer _blurayExaminer;
  43. private readonly ILocalizationManager _localization;
  44. private readonly IApplicationPaths _appPaths;
  45. private readonly IJsonSerializer _json;
  46. private readonly IEncodingManager _encodingManager;
  47. private readonly IFileSystem _fileSystem;
  48. private readonly IServerConfigurationManager _config;
  49. private readonly ISubtitleManager _subtitleManager;
  50. private readonly IChapterManager _chapterManager;
  51. private readonly ILibraryManager _libraryManager;
  52. private readonly IChannelManager _channelManager;
  53. private readonly IMediaSourceManager _mediaSourceManager;
  54. public string Name => "ffprobe";
  55. public bool HasChanged(BaseItem item, IDirectoryService directoryService)
  56. {
  57. var video = item as Video;
  58. if (video == null || video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso)
  59. {
  60. var path = item.Path;
  61. if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol)
  62. {
  63. var file = directoryService.GetFile(path);
  64. if (file != null && file.LastWriteTimeUtc != item.DateModified)
  65. {
  66. _logger.LogDebug("Refreshing {0} due to date modified timestamp change.", path);
  67. return true;
  68. }
  69. }
  70. }
  71. if (item.SupportsLocalMetadata)
  72. {
  73. if (video != null && !video.IsPlaceHolder)
  74. {
  75. if (!video.SubtitleFiles
  76. .SequenceEqual(_subtitleResolver.GetExternalSubtitleFiles(video, directoryService, false), StringComparer.Ordinal))
  77. {
  78. _logger.LogDebug("Refreshing {0} due to external subtitles change.", item.Path);
  79. return true;
  80. }
  81. }
  82. }
  83. return false;
  84. }
  85. public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  86. {
  87. return FetchVideoInfo(item, options, cancellationToken);
  88. }
  89. public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  90. {
  91. return FetchVideoInfo(item, options, cancellationToken);
  92. }
  93. public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  94. {
  95. return FetchVideoInfo(item, options, cancellationToken);
  96. }
  97. public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  98. {
  99. return FetchVideoInfo(item, options, cancellationToken);
  100. }
  101. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  102. {
  103. return FetchVideoInfo(item, options, cancellationToken);
  104. }
  105. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  106. {
  107. return FetchAudioInfo(item, options, cancellationToken);
  108. }
  109. public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  110. {
  111. return FetchAudioInfo(item, options, cancellationToken);
  112. }
  113. private SubtitleResolver _subtitleResolver;
  114. public FFProbeProvider(ILogger logger, IMediaSourceManager mediaSourceManager, IChannelManager channelManager, 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)
  115. {
  116. _logger = logger;
  117. _isoManager = isoManager;
  118. _mediaEncoder = mediaEncoder;
  119. _itemRepo = itemRepo;
  120. _blurayExaminer = blurayExaminer;
  121. _localization = localization;
  122. _appPaths = appPaths;
  123. _json = json;
  124. _encodingManager = encodingManager;
  125. _fileSystem = fileSystem;
  126. _config = config;
  127. _subtitleManager = subtitleManager;
  128. _chapterManager = chapterManager;
  129. _libraryManager = libraryManager;
  130. _channelManager = channelManager;
  131. _mediaSourceManager = mediaSourceManager;
  132. _subtitleResolver = new SubtitleResolver(BaseItem.LocalizationManager, fileSystem);
  133. }
  134. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  135. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  136. where T : Video
  137. {
  138. if (item.VideoType == VideoType.Iso)
  139. {
  140. return _cachedTask;
  141. }
  142. if (item.IsPlaceHolder)
  143. {
  144. return _cachedTask;
  145. }
  146. if (!item.IsCompleteMedia)
  147. {
  148. return _cachedTask;
  149. }
  150. if (item.IsVirtualItem)
  151. {
  152. return _cachedTask;
  153. }
  154. if (!options.EnableRemoteContentProbe && !item.IsFileProtocol)
  155. {
  156. return _cachedTask;
  157. }
  158. if (item.IsShortcut)
  159. {
  160. FetchShortcutInfo(item);
  161. }
  162. var prober = new FFProbeVideoInfo(_logger, _mediaSourceManager, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager, _chapterManager, _libraryManager);
  163. return prober.ProbeVideo(item, options, cancellationToken);
  164. }
  165. private string NormalizeStrmLine(string line)
  166. {
  167. return line.Replace("\t", string.Empty)
  168. .Replace("\r", string.Empty)
  169. .Replace("\n", string.Empty)
  170. .Trim();
  171. }
  172. private void FetchShortcutInfo(BaseItem item)
  173. {
  174. item.ShortcutPath = _fileSystem.ReadAllLines(item.Path)
  175. .Select(NormalizeStrmLine)
  176. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i) && !i.StartsWith("#", StringComparison.OrdinalIgnoreCase));
  177. }
  178. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  179. where T : Audio
  180. {
  181. if (item.IsVirtualItem)
  182. {
  183. return _cachedTask;
  184. }
  185. if (!options.EnableRemoteContentProbe && !item.IsFileProtocol)
  186. {
  187. return _cachedTask;
  188. }
  189. if (item.IsShortcut)
  190. {
  191. FetchShortcutInfo(item);
  192. }
  193. var prober = new FFProbeAudioInfo(_mediaSourceManager, _mediaEncoder, _itemRepo, _appPaths, _json, _libraryManager);
  194. return prober.Probe(item, options, cancellationToken);
  195. }
  196. // Run last
  197. public int Order => 100;
  198. }
  199. }