ProbeProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #nullable disable
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Emby.Naming.Common;
  8. using MediaBrowser.Controller.Chapters;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.Entities.Audio;
  12. using MediaBrowser.Controller.Entities.Movies;
  13. using MediaBrowser.Controller.Entities.TV;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Controller.MediaEncoding;
  16. using MediaBrowser.Controller.Persistence;
  17. using MediaBrowser.Controller.Providers;
  18. using MediaBrowser.Controller.Subtitles;
  19. using MediaBrowser.Model.Entities;
  20. using MediaBrowser.Model.Globalization;
  21. using MediaBrowser.Model.IO;
  22. using MediaBrowser.Model.MediaInfo;
  23. using Microsoft.Extensions.Logging;
  24. namespace MediaBrowser.Providers.MediaInfo
  25. {
  26. /// <summary>
  27. /// The probe provider.
  28. /// </summary>
  29. public class ProbeProvider : ICustomMetadataProvider<Episode>,
  30. ICustomMetadataProvider<MusicVideo>,
  31. ICustomMetadataProvider<Movie>,
  32. ICustomMetadataProvider<Trailer>,
  33. ICustomMetadataProvider<Video>,
  34. ICustomMetadataProvider<Audio>,
  35. ICustomMetadataProvider<AudioBook>,
  36. IHasOrder,
  37. IForcedProvider,
  38. IPreRefreshProvider,
  39. IHasItemChangeMonitor
  40. {
  41. private readonly ILogger<ProbeProvider> _logger;
  42. private readonly AudioResolver _audioResolver;
  43. private readonly SubtitleResolver _subtitleResolver;
  44. private readonly FFProbeVideoInfo _videoProber;
  45. private readonly AudioFileProber _audioProber;
  46. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="ProbeProvider"/> class.
  49. /// </summary>
  50. /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param>
  51. /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
  52. /// <param name="itemRepo">Instance of the <see cref="IItemRepository"/> interface.</param>
  53. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  54. /// <param name="encodingManager">Instance of the <see cref="IEncodingManager"/> interface.</param>
  55. /// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  56. /// <param name="subtitleManager">Instance of the <see cref="ISubtitleManager"/> interface.</param>
  57. /// <param name="chapterManager">Instance of the <see cref="IChapterManager"/> interface.</param>
  58. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  59. /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/>.</param>
  60. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  61. /// <param name="namingOptions">The <see cref="NamingOptions"/>.</param>
  62. public ProbeProvider(
  63. IMediaSourceManager mediaSourceManager,
  64. IMediaEncoder mediaEncoder,
  65. IItemRepository itemRepo,
  66. ILocalizationManager localization,
  67. IEncodingManager encodingManager,
  68. IServerConfigurationManager config,
  69. ISubtitleManager subtitleManager,
  70. IChapterManager chapterManager,
  71. ILibraryManager libraryManager,
  72. IFileSystem fileSystem,
  73. ILoggerFactory loggerFactory,
  74. NamingOptions namingOptions)
  75. {
  76. _logger = loggerFactory.CreateLogger<ProbeProvider>();
  77. _audioProber = new AudioFileProber(mediaSourceManager, mediaEncoder, itemRepo, libraryManager);
  78. _audioResolver = new AudioResolver(loggerFactory.CreateLogger<AudioResolver>(), localization, mediaEncoder, fileSystem, namingOptions);
  79. _subtitleResolver = new SubtitleResolver(loggerFactory.CreateLogger<SubtitleResolver>(), localization, mediaEncoder, fileSystem, namingOptions);
  80. _videoProber = new FFProbeVideoInfo(
  81. loggerFactory.CreateLogger<FFProbeVideoInfo>(),
  82. mediaSourceManager,
  83. mediaEncoder,
  84. itemRepo,
  85. localization,
  86. encodingManager,
  87. config,
  88. subtitleManager,
  89. chapterManager,
  90. libraryManager,
  91. _audioResolver,
  92. _subtitleResolver);
  93. }
  94. /// <inheritdoc />
  95. public string Name => "Probe Provider";
  96. /// <inheritdoc />
  97. public int Order => 100;
  98. /// <inheritdoc />
  99. public bool HasChanged(BaseItem item, IDirectoryService directoryService)
  100. {
  101. var video = item as Video;
  102. if (video is null || video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso)
  103. {
  104. var path = item.Path;
  105. if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol)
  106. {
  107. var file = directoryService.GetFile(path);
  108. if (file is not null && file.LastWriteTimeUtc != item.DateModified)
  109. {
  110. _logger.LogDebug("Refreshing {ItemPath} due to date modified timestamp change.", path);
  111. return true;
  112. }
  113. }
  114. }
  115. if (item.SupportsLocalMetadata && video is not null && !video.IsPlaceHolder
  116. && !video.SubtitleFiles.SequenceEqual(
  117. _subtitleResolver.GetExternalFiles(video, directoryService, false)
  118. .Select(info => info.Path).ToList(),
  119. StringComparer.Ordinal))
  120. {
  121. _logger.LogDebug("Refreshing {ItemPath} due to external subtitles change.", item.Path);
  122. return true;
  123. }
  124. if (item.SupportsLocalMetadata && video is not null && !video.IsPlaceHolder
  125. && !video.AudioFiles.SequenceEqual(
  126. _audioResolver.GetExternalFiles(video, directoryService, false)
  127. .Select(info => info.Path).ToList(),
  128. StringComparer.Ordinal))
  129. {
  130. _logger.LogDebug("Refreshing {ItemPath} due to external audio change.", item.Path);
  131. return true;
  132. }
  133. return false;
  134. }
  135. /// <inheritdoc />
  136. public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  137. {
  138. return FetchVideoInfo(item, options, cancellationToken);
  139. }
  140. /// <inheritdoc />
  141. public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  142. {
  143. return FetchVideoInfo(item, options, cancellationToken);
  144. }
  145. /// <inheritdoc />
  146. public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  147. {
  148. return FetchVideoInfo(item, options, cancellationToken);
  149. }
  150. /// <inheritdoc />
  151. public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  152. {
  153. return FetchVideoInfo(item, options, cancellationToken);
  154. }
  155. /// <inheritdoc />
  156. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  157. {
  158. return FetchVideoInfo(item, options, cancellationToken);
  159. }
  160. /// <inheritdoc />
  161. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  162. {
  163. return FetchAudioInfo(item, options, cancellationToken);
  164. }
  165. /// <inheritdoc />
  166. public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  167. {
  168. return FetchAudioInfo(item, options, cancellationToken);
  169. }
  170. /// <summary>
  171. /// Fetches video information for an item.
  172. /// </summary>
  173. /// <param name="item">The item.</param>
  174. /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param>
  175. /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
  176. /// <typeparam name="T">The type of item to resolve.</typeparam>
  177. /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns>
  178. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  179. where T : Video
  180. {
  181. if (item.IsPlaceHolder)
  182. {
  183. return _cachedTask;
  184. }
  185. if (!item.IsCompleteMedia)
  186. {
  187. return _cachedTask;
  188. }
  189. if (item.IsVirtualItem)
  190. {
  191. return _cachedTask;
  192. }
  193. if (!options.EnableRemoteContentProbe && !item.IsFileProtocol)
  194. {
  195. return _cachedTask;
  196. }
  197. if (item.IsShortcut)
  198. {
  199. FetchShortcutInfo(item);
  200. }
  201. return _videoProber.ProbeVideo(item, options, cancellationToken);
  202. }
  203. private string NormalizeStrmLine(string line)
  204. {
  205. return line.Replace("\t", string.Empty, StringComparison.Ordinal)
  206. .Replace("\r", string.Empty, StringComparison.Ordinal)
  207. .Replace("\n", string.Empty, StringComparison.Ordinal)
  208. .Trim();
  209. }
  210. private void FetchShortcutInfo(BaseItem item)
  211. {
  212. item.ShortcutPath = File.ReadAllLines(item.Path)
  213. .Select(NormalizeStrmLine)
  214. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i) && !i.StartsWith('#'));
  215. }
  216. /// <summary>
  217. /// Fetches audio information for an item.
  218. /// </summary>
  219. /// <param name="item">The item.</param>
  220. /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param>
  221. /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
  222. /// <typeparam name="T">The type of item to resolve.</typeparam>
  223. /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns>
  224. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  225. where T : Audio
  226. {
  227. if (item.IsVirtualItem)
  228. {
  229. return _cachedTask;
  230. }
  231. if (!options.EnableRemoteContentProbe && !item.IsFileProtocol)
  232. {
  233. return _cachedTask;
  234. }
  235. if (item.IsShortcut)
  236. {
  237. FetchShortcutInfo(item);
  238. }
  239. return _audioProber.Probe(item, options, cancellationToken);
  240. }
  241. }
  242. }