ProbeProvider.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 LyricResolver _lyricResolver;
  45. private readonly FFProbeVideoInfo _videoProber;
  46. private readonly AudioFileProber _audioProber;
  47. private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None);
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="ProbeProvider"/> class.
  50. /// </summary>
  51. /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param>
  52. /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
  53. /// <param name="itemRepo">Instance of the <see cref="IItemRepository"/> interface.</param>
  54. /// <param name="blurayExaminer">Instance of the <see cref="IBlurayExaminer"/> interface.</param>
  55. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  56. /// <param name="encodingManager">Instance of the <see cref="IEncodingManager"/> interface.</param>
  57. /// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  58. /// <param name="subtitleManager">Instance of the <see cref="ISubtitleManager"/> interface.</param>
  59. /// <param name="chapterManager">Instance of the <see cref="IChapterManager"/> interface.</param>
  60. /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
  61. /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/>.</param>
  62. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  63. /// <param name="namingOptions">The <see cref="NamingOptions"/>.</param>
  64. public ProbeProvider(
  65. IMediaSourceManager mediaSourceManager,
  66. IMediaEncoder mediaEncoder,
  67. IItemRepository itemRepo,
  68. IBlurayExaminer blurayExaminer,
  69. ILocalizationManager localization,
  70. IEncodingManager encodingManager,
  71. IServerConfigurationManager config,
  72. ISubtitleManager subtitleManager,
  73. IChapterManager chapterManager,
  74. ILibraryManager libraryManager,
  75. IFileSystem fileSystem,
  76. ILoggerFactory loggerFactory,
  77. NamingOptions namingOptions)
  78. {
  79. _logger = loggerFactory.CreateLogger<ProbeProvider>();
  80. _audioResolver = new AudioResolver(loggerFactory.CreateLogger<AudioResolver>(), localization, mediaEncoder, fileSystem, namingOptions);
  81. _subtitleResolver = new SubtitleResolver(loggerFactory.CreateLogger<SubtitleResolver>(), localization, mediaEncoder, fileSystem, namingOptions);
  82. _lyricResolver = new LyricResolver(loggerFactory.CreateLogger<LyricResolver>(), localization, mediaEncoder, fileSystem, namingOptions);
  83. _videoProber = new FFProbeVideoInfo(
  84. loggerFactory.CreateLogger<FFProbeVideoInfo>(),
  85. mediaSourceManager,
  86. mediaEncoder,
  87. itemRepo,
  88. blurayExaminer,
  89. localization,
  90. encodingManager,
  91. config,
  92. subtitleManager,
  93. chapterManager,
  94. libraryManager,
  95. _audioResolver,
  96. _subtitleResolver);
  97. _audioProber = new AudioFileProber(
  98. loggerFactory.CreateLogger<AudioFileProber>(),
  99. mediaSourceManager,
  100. mediaEncoder,
  101. itemRepo,
  102. libraryManager,
  103. _lyricResolver);
  104. }
  105. /// <inheritdoc />
  106. public string Name => "Probe Provider";
  107. /// <inheritdoc />
  108. public int Order => 100;
  109. /// <inheritdoc />
  110. public bool HasChanged(BaseItem item, IDirectoryService directoryService)
  111. {
  112. var video = item as Video;
  113. if (video is null || video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso)
  114. {
  115. var path = item.Path;
  116. if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol)
  117. {
  118. var file = directoryService.GetFile(path);
  119. if (file is not null && file.LastWriteTimeUtc != item.DateModified)
  120. {
  121. _logger.LogDebug("Refreshing {ItemPath} due to date modified timestamp change.", path);
  122. return true;
  123. }
  124. }
  125. }
  126. if (video is not null
  127. && item.SupportsLocalMetadata
  128. && !video.IsPlaceHolder)
  129. {
  130. if (!video.SubtitleFiles.SequenceEqual(
  131. _subtitleResolver.GetExternalFiles(video, directoryService, false)
  132. .Select(info => info.Path).ToList(),
  133. StringComparer.Ordinal))
  134. {
  135. _logger.LogDebug("Refreshing {ItemPath} due to external subtitles change.", item.Path);
  136. return true;
  137. }
  138. if (!video.AudioFiles.SequenceEqual(
  139. _audioResolver.GetExternalFiles(video, directoryService, false)
  140. .Select(info => info.Path).ToList(),
  141. StringComparer.Ordinal))
  142. {
  143. _logger.LogDebug("Refreshing {ItemPath} due to external audio change.", item.Path);
  144. return true;
  145. }
  146. }
  147. if (item is Audio audio
  148. && item.SupportsLocalMetadata
  149. && !audio.LyricFiles.SequenceEqual(
  150. _lyricResolver.GetExternalFiles(audio, directoryService, false)
  151. .Select(info => info.Path).ToList(),
  152. StringComparer.Ordinal))
  153. {
  154. _logger.LogDebug("Refreshing {ItemPath} due to external lyrics change.", item.Path);
  155. return true;
  156. }
  157. return false;
  158. }
  159. /// <inheritdoc />
  160. public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  161. {
  162. return FetchVideoInfo(item, options, cancellationToken);
  163. }
  164. /// <inheritdoc />
  165. public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  166. {
  167. return FetchVideoInfo(item, options, cancellationToken);
  168. }
  169. /// <inheritdoc />
  170. public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  171. {
  172. return FetchVideoInfo(item, options, cancellationToken);
  173. }
  174. /// <inheritdoc />
  175. public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  176. {
  177. return FetchVideoInfo(item, options, cancellationToken);
  178. }
  179. /// <inheritdoc />
  180. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  181. {
  182. return FetchVideoInfo(item, options, cancellationToken);
  183. }
  184. /// <inheritdoc />
  185. public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  186. {
  187. return FetchAudioInfo(item, options, cancellationToken);
  188. }
  189. /// <inheritdoc />
  190. public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  191. {
  192. return FetchAudioInfo(item, options, cancellationToken);
  193. }
  194. /// <summary>
  195. /// Fetches video information for an item.
  196. /// </summary>
  197. /// <param name="item">The item.</param>
  198. /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param>
  199. /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
  200. /// <typeparam name="T">The type of item to resolve.</typeparam>
  201. /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns>
  202. public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  203. where T : Video
  204. {
  205. if (item.IsPlaceHolder)
  206. {
  207. return _cachedTask;
  208. }
  209. if (!item.IsCompleteMedia)
  210. {
  211. return _cachedTask;
  212. }
  213. if (item.IsVirtualItem)
  214. {
  215. return _cachedTask;
  216. }
  217. if (!options.EnableRemoteContentProbe && !item.IsFileProtocol)
  218. {
  219. return _cachedTask;
  220. }
  221. if (item.IsShortcut)
  222. {
  223. FetchShortcutInfo(item);
  224. }
  225. return _videoProber.ProbeVideo(item, options, cancellationToken);
  226. }
  227. private string NormalizeStrmLine(string line)
  228. {
  229. return line.Replace("\t", string.Empty, StringComparison.Ordinal)
  230. .Replace("\r", string.Empty, StringComparison.Ordinal)
  231. .Replace("\n", string.Empty, StringComparison.Ordinal)
  232. .Trim();
  233. }
  234. private void FetchShortcutInfo(BaseItem item)
  235. {
  236. item.ShortcutPath = File.ReadAllLines(item.Path)
  237. .Select(NormalizeStrmLine)
  238. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i) && !i.StartsWith('#'));
  239. }
  240. /// <summary>
  241. /// Fetches audio information for an item.
  242. /// </summary>
  243. /// <param name="item">The item.</param>
  244. /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param>
  245. /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
  246. /// <typeparam name="T">The type of item to resolve.</typeparam>
  247. /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns>
  248. public Task<ItemUpdateType> FetchAudioInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  249. where T : Audio
  250. {
  251. if (item.IsVirtualItem)
  252. {
  253. return _cachedTask;
  254. }
  255. if (!options.EnableRemoteContentProbe && !item.IsFileProtocol)
  256. {
  257. return _cachedTask;
  258. }
  259. if (item.IsShortcut)
  260. {
  261. FetchShortcutInfo(item);
  262. }
  263. return _audioProber.Probe(item, options, cancellationToken);
  264. }
  265. }
  266. }