| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Emby.Naming.Common; |
| | | 10 | | using MediaBrowser.Controller.Chapters; |
| | | 11 | | using MediaBrowser.Controller.Configuration; |
| | | 12 | | using MediaBrowser.Controller.Entities; |
| | | 13 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 14 | | using MediaBrowser.Controller.Entities.Movies; |
| | | 15 | | using MediaBrowser.Controller.Entities.TV; |
| | | 16 | | using MediaBrowser.Controller.Library; |
| | | 17 | | using MediaBrowser.Controller.Lyrics; |
| | | 18 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 19 | | using MediaBrowser.Controller.Persistence; |
| | | 20 | | using MediaBrowser.Controller.Providers; |
| | | 21 | | using MediaBrowser.Controller.Subtitles; |
| | | 22 | | using MediaBrowser.Model.Entities; |
| | | 23 | | using MediaBrowser.Model.Globalization; |
| | | 24 | | using MediaBrowser.Model.IO; |
| | | 25 | | using MediaBrowser.Model.MediaInfo; |
| | | 26 | | using Microsoft.Extensions.Logging; |
| | | 27 | | using PDFtoImage; |
| | | 28 | | using SharpCompress.Archives; |
| | | 29 | | |
| | | 30 | | namespace MediaBrowser.Providers.MediaInfo |
| | | 31 | | { |
| | | 32 | | /// <summary> |
| | | 33 | | /// The probe provider. |
| | | 34 | | /// </summary> |
| | | 35 | | public class ProbeProvider : ICustomMetadataProvider<Episode>, |
| | | 36 | | ICustomMetadataProvider<MusicVideo>, |
| | | 37 | | ICustomMetadataProvider<Movie>, |
| | | 38 | | ICustomMetadataProvider<Trailer>, |
| | | 39 | | ICustomMetadataProvider<Video>, |
| | | 40 | | ICustomMetadataProvider<Audio>, |
| | | 41 | | ICustomMetadataProvider<AudioBook>, |
| | | 42 | | ICustomMetadataProvider<Book>, |
| | | 43 | | IHasOrder, |
| | | 44 | | IForcedProvider, |
| | | 45 | | IPreRefreshProvider, |
| | | 46 | | IHasItemChangeMonitor |
| | | 47 | | { |
| | | 48 | | private readonly ILogger<ProbeProvider> _logger; |
| | | 49 | | private readonly AudioResolver _audioResolver; |
| | | 50 | | private readonly SubtitleResolver _subtitleResolver; |
| | | 51 | | private readonly LyricResolver _lyricResolver; |
| | | 52 | | private readonly FFProbeVideoInfo _videoProber; |
| | | 53 | | private readonly AudioFileProber _audioProber; |
| | 22 | 54 | | private readonly Task<ItemUpdateType> _cachedTask = Task.FromResult(ItemUpdateType.None); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Initializes a new instance of the <see cref="ProbeProvider"/> class. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="mediaSourceManager">Instance of the <see cref="IMediaSourceManager"/> interface.</param> |
| | | 60 | | /// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param> |
| | | 61 | | /// <param name="blurayExaminer">Instance of the <see cref="IBlurayExaminer"/> interface.</param> |
| | | 62 | | /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param> |
| | | 63 | | /// <param name="chapterManager">Instance of the <see cref="IChapterManager"/> interface.</param> |
| | | 64 | | /// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param> |
| | | 65 | | /// <param name="subtitleManager">Instance of the <see cref="ISubtitleManager"/> interface.</param> |
| | | 66 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | | 67 | | /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/>.</param> |
| | | 68 | | /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param> |
| | | 69 | | /// <param name="namingOptions">The <see cref="NamingOptions"/>.</param> |
| | | 70 | | /// <param name="lyricManager">Instance of the <see cref="ILyricManager"/> interface.</param> |
| | | 71 | | /// <param name="mediaAttachmentRepository">Instance of the <see cref="IMediaAttachmentRepository"/> interface.< |
| | | 72 | | /// <param name="mediaStreamRepository">Instance of the <see cref="IMediaStreamRepository"/> interface.</param> |
| | | 73 | | public ProbeProvider( |
| | | 74 | | IMediaSourceManager mediaSourceManager, |
| | | 75 | | IMediaEncoder mediaEncoder, |
| | | 76 | | IBlurayExaminer blurayExaminer, |
| | | 77 | | ILocalizationManager localization, |
| | | 78 | | IChapterManager chapterManager, |
| | | 79 | | IServerConfigurationManager config, |
| | | 80 | | ISubtitleManager subtitleManager, |
| | | 81 | | ILibraryManager libraryManager, |
| | | 82 | | IFileSystem fileSystem, |
| | | 83 | | ILoggerFactory loggerFactory, |
| | | 84 | | NamingOptions namingOptions, |
| | | 85 | | ILyricManager lyricManager, |
| | | 86 | | IMediaAttachmentRepository mediaAttachmentRepository, |
| | | 87 | | IMediaStreamRepository mediaStreamRepository) |
| | | 88 | | { |
| | 22 | 89 | | _logger = loggerFactory.CreateLogger<ProbeProvider>(); |
| | 22 | 90 | | _audioResolver = new AudioResolver(loggerFactory.CreateLogger<AudioResolver>(), localization, mediaEncoder, |
| | 22 | 91 | | _subtitleResolver = new SubtitleResolver(loggerFactory.CreateLogger<SubtitleResolver>(), localization, media |
| | 22 | 92 | | _lyricResolver = new LyricResolver(loggerFactory.CreateLogger<LyricResolver>(), localization, mediaEncoder, |
| | | 93 | | |
| | 22 | 94 | | _videoProber = new FFProbeVideoInfo( |
| | 22 | 95 | | loggerFactory.CreateLogger<FFProbeVideoInfo>(), |
| | 22 | 96 | | mediaSourceManager, |
| | 22 | 97 | | mediaEncoder, |
| | 22 | 98 | | blurayExaminer, |
| | 22 | 99 | | localization, |
| | 22 | 100 | | chapterManager, |
| | 22 | 101 | | config, |
| | 22 | 102 | | subtitleManager, |
| | 22 | 103 | | libraryManager, |
| | 22 | 104 | | _audioResolver, |
| | 22 | 105 | | _subtitleResolver, |
| | 22 | 106 | | mediaAttachmentRepository, |
| | 22 | 107 | | mediaStreamRepository); |
| | | 108 | | |
| | 22 | 109 | | _audioProber = new AudioFileProber( |
| | 22 | 110 | | loggerFactory.CreateLogger<AudioFileProber>(), |
| | 22 | 111 | | mediaSourceManager, |
| | 22 | 112 | | mediaEncoder, |
| | 22 | 113 | | libraryManager, |
| | 22 | 114 | | _lyricResolver, |
| | 22 | 115 | | lyricManager, |
| | 22 | 116 | | mediaStreamRepository, |
| | 22 | 117 | | chapterManager); |
| | 22 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc /> |
| | 0 | 121 | | public string Name => "Probe Provider"; |
| | | 122 | | |
| | | 123 | | /// <inheritdoc /> |
| | 0 | 124 | | public int Order => 100; |
| | | 125 | | |
| | | 126 | | /// <inheritdoc /> |
| | | 127 | | public bool HasChanged(BaseItem item, IDirectoryService directoryService) |
| | | 128 | | { |
| | 0 | 129 | | var video = item as Video; |
| | 0 | 130 | | if (video is null || video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso) |
| | | 131 | | { |
| | 0 | 132 | | var path = item.Path; |
| | | 133 | | |
| | 0 | 134 | | if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol) |
| | | 135 | | { |
| | 0 | 136 | | var file = directoryService.GetFile(path); |
| | 0 | 137 | | if (file is not null && item.HasChanged(file.LastWriteTimeUtc)) |
| | | 138 | | { |
| | 0 | 139 | | _logger.LogDebug("Refreshing {ItemPath} due to file system modification.", path); |
| | 0 | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | if (video is not null |
| | 0 | 146 | | && item.SupportsLocalMetadata |
| | 0 | 147 | | && !video.IsPlaceHolder) |
| | | 148 | | { |
| | 0 | 149 | | var externalFiles = new HashSet<string>(_subtitleResolver.GetExternalFiles(video, directoryService, fals |
| | 0 | 150 | | if (!new HashSet<string>(video.SubtitleFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 151 | | { |
| | 0 | 152 | | _logger.LogDebug("Refreshing {ItemPath} due to external subtitles change.", item.Path); |
| | 0 | 153 | | return true; |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | externalFiles = new HashSet<string>(_audioResolver.GetExternalFiles(video, directoryService, false).Sele |
| | 0 | 157 | | if (!new HashSet<string>(video.AudioFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 158 | | { |
| | 0 | 159 | | _logger.LogDebug("Refreshing {ItemPath} due to external audio change.", item.Path); |
| | 0 | 160 | | return true; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | if (item is Audio audio |
| | 0 | 165 | | && item.SupportsLocalMetadata) |
| | | 166 | | { |
| | 0 | 167 | | var externalFiles = new HashSet<string>(_lyricResolver.GetExternalFiles(audio, directoryService, false). |
| | 0 | 168 | | if (!new HashSet<string>(audio.LyricFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 169 | | { |
| | 0 | 170 | | _logger.LogDebug("Refreshing {ItemPath} due to external lyrics change.", item.Path); |
| | 0 | 171 | | return true; |
| | | 172 | | } |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | return false; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <inheritdoc /> |
| | | 179 | | public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellat |
| | | 180 | | { |
| | 0 | 181 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <inheritdoc /> |
| | | 185 | | public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancel |
| | | 186 | | { |
| | 0 | 187 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <inheritdoc /> |
| | | 191 | | public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 192 | | { |
| | 0 | 193 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <inheritdoc /> |
| | | 197 | | public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellat |
| | | 198 | | { |
| | 0 | 199 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <inheritdoc /> |
| | | 203 | | public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 204 | | { |
| | 0 | 205 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <inheritdoc /> |
| | | 209 | | public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 210 | | { |
| | 0 | 211 | | return FetchAudioInfo(item, options, cancellationToken); |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <inheritdoc /> |
| | | 215 | | public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancell |
| | | 216 | | { |
| | 0 | 217 | | return FetchAudioInfo(item, options, cancellationToken); |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <inheritdoc /> |
| | | 221 | | public Task<ItemUpdateType> FetchAsync(Book item, MetadataRefreshOptions options, CancellationToken cancellation |
| | | 222 | | { |
| | 0 | 223 | | if (item.IsVirtualItem || !item.IsFileProtocol) |
| | | 224 | | { |
| | 0 | 225 | | return _cachedTask; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | long pageCount; |
| | 0 | 229 | | switch (Path.GetExtension(item.Path).ToLowerInvariant()) |
| | | 230 | | { |
| | | 231 | | case ".cb7": |
| | | 232 | | case ".cbr": |
| | | 233 | | case ".cbt": |
| | | 234 | | case ".cbz": |
| | 0 | 235 | | using (var stream = File.OpenRead(item.Path)) |
| | 0 | 236 | | using (var archive = ArchiveFactory.OpenArchive(stream)) |
| | | 237 | | { |
| | 0 | 238 | | pageCount = archive.Entries.Count(e => !e.IsDirectory); |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | break; |
| | | 242 | | |
| | | 243 | | #pragma warning disable CA1416 |
| | | 244 | | case ".pdf": |
| | 0 | 245 | | using (var stream = File.OpenRead(item.Path)) |
| | | 246 | | { |
| | 0 | 247 | | pageCount = Conversion.GetPageCount(stream); |
| | 0 | 248 | | } |
| | | 249 | | |
| | | 250 | | break; |
| | | 251 | | #pragma warning restore CA1416 |
| | | 252 | | |
| | | 253 | | case ".epub": |
| | | 254 | | // TODO process CFI and store as a string when multiple progress types are supported |
| | | 255 | | // current progress value is percentage stored as a proportion of one second worth of ticks |
| | 0 | 256 | | item.RunTimeTicks = TimeSpan.TicksPerSecond; |
| | | 257 | | |
| | 0 | 258 | | return Task.FromResult(ItemUpdateType.MetadataImport); |
| | | 259 | | |
| | | 260 | | default: |
| | 0 | 261 | | return _cachedTask; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | // TODO use page count without modification when multiple progress types are supported |
| | | 265 | | // book players report page count and the web client multiplies that value by 10000 to convert the expected |
| | 0 | 266 | | item.RunTimeTicks = pageCount * 10000; |
| | | 267 | | |
| | 0 | 268 | | return Task.FromResult(ItemUpdateType.MetadataImport); |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <summary> |
| | | 272 | | /// Fetches video information for an item. |
| | | 273 | | /// </summary> |
| | | 274 | | /// <param name="item">The item.</param> |
| | | 275 | | /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> |
| | | 276 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> |
| | | 277 | | /// <typeparam name="T">The type of item to resolve.</typeparam> |
| | | 278 | | /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns> |
| | | 279 | | public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancella |
| | | 280 | | where T : Video |
| | | 281 | | { |
| | 0 | 282 | | if (item.IsPlaceHolder) |
| | | 283 | | { |
| | 0 | 284 | | return _cachedTask; |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | if (!item.IsCompleteMedia) |
| | | 288 | | { |
| | 0 | 289 | | return _cachedTask; |
| | | 290 | | } |
| | | 291 | | |
| | 0 | 292 | | if (item.IsVirtualItem) |
| | | 293 | | { |
| | 0 | 294 | | return _cachedTask; |
| | | 295 | | } |
| | | 296 | | |
| | 0 | 297 | | if (!options.EnableRemoteContentProbe && !item.IsFileProtocol) |
| | | 298 | | { |
| | 0 | 299 | | return _cachedTask; |
| | | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | if (item.IsShortcut) |
| | | 303 | | { |
| | 0 | 304 | | FetchShortcutInfo(item); |
| | | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | return _videoProber.ProbeVideo(item, options, cancellationToken); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | private string NormalizeStrmLine(string line) |
| | | 311 | | { |
| | 0 | 312 | | return line.Replace("\t", string.Empty, StringComparison.Ordinal) |
| | 0 | 313 | | .Replace("\r", string.Empty, StringComparison.Ordinal) |
| | 0 | 314 | | .Replace("\n", string.Empty, StringComparison.Ordinal) |
| | 0 | 315 | | .Trim(); |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | private void FetchShortcutInfo(BaseItem item) |
| | | 319 | | { |
| | 0 | 320 | | var shortcutPath = File.ReadAllLines(item.Path) |
| | 0 | 321 | | .Select(NormalizeStrmLine) |
| | 0 | 322 | | .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i) && !i.StartsWith('#')); |
| | | 323 | | |
| | 0 | 324 | | if (string.IsNullOrWhiteSpace(shortcutPath)) |
| | | 325 | | { |
| | 0 | 326 | | return; |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | // Only allow remote URLs in .strm files to prevent local file access |
| | 0 | 330 | | if (Uri.TryCreate(shortcutPath, UriKind.Absolute, out var uri) |
| | 0 | 331 | | && (string.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) |
| | 0 | 332 | | || string.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase) |
| | 0 | 333 | | || string.Equals(uri.Scheme, "rtsp", StringComparison.OrdinalIgnoreCase) |
| | 0 | 334 | | || string.Equals(uri.Scheme, "rtp", StringComparison.OrdinalIgnoreCase))) |
| | | 335 | | { |
| | 0 | 336 | | item.ShortcutPath = shortcutPath; |
| | | 337 | | } |
| | | 338 | | else |
| | | 339 | | { |
| | 0 | 340 | | _logger.LogWarning("Ignoring invalid or non-remote .strm path in {File}: {Path}", item.Path, shortcutPat |
| | | 341 | | } |
| | 0 | 342 | | } |
| | | 343 | | |
| | | 344 | | /// <summary> |
| | | 345 | | /// Fetches audio information for an item. |
| | | 346 | | /// </summary> |
| | | 347 | | /// <param name="item">The item.</param> |
| | | 348 | | /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> |
| | | 349 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> |
| | | 350 | | /// <typeparam name="T">The type of item to resolve.</typeparam> |
| | | 351 | | /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns> |
| | | 352 | | public Task<ItemUpdateType> FetchAudioInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancella |
| | | 353 | | where T : Audio |
| | | 354 | | { |
| | 0 | 355 | | if (item.IsVirtualItem) |
| | | 356 | | { |
| | 0 | 357 | | return _cachedTask; |
| | | 358 | | } |
| | | 359 | | |
| | 0 | 360 | | if (!options.EnableRemoteContentProbe && !item.IsFileProtocol) |
| | | 361 | | { |
| | 0 | 362 | | return _cachedTask; |
| | | 363 | | } |
| | | 364 | | |
| | 0 | 365 | | if (item.IsShortcut) |
| | | 366 | | { |
| | 0 | 367 | | FetchShortcutInfo(item); |
| | | 368 | | } |
| | | 369 | | |
| | 0 | 370 | | return _audioProber.Probe(item, options, cancellationToken); |
| | | 371 | | } |
| | | 372 | | } |
| | | 373 | | } |