| | | 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; |
| | 35 | 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 | | { |
| | 35 | 89 | | _logger = loggerFactory.CreateLogger<ProbeProvider>(); |
| | 35 | 90 | | _audioResolver = new AudioResolver(loggerFactory.CreateLogger<AudioResolver>(), localization, mediaEncoder, |
| | 35 | 91 | | _subtitleResolver = new SubtitleResolver(loggerFactory.CreateLogger<SubtitleResolver>(), localization, media |
| | 35 | 92 | | _lyricResolver = new LyricResolver(loggerFactory.CreateLogger<LyricResolver>(), localization, mediaEncoder, |
| | | 93 | | |
| | 35 | 94 | | _videoProber = new FFProbeVideoInfo( |
| | 35 | 95 | | loggerFactory.CreateLogger<FFProbeVideoInfo>(), |
| | 35 | 96 | | mediaSourceManager, |
| | 35 | 97 | | mediaEncoder, |
| | 35 | 98 | | blurayExaminer, |
| | 35 | 99 | | localization, |
| | 35 | 100 | | chapterManager, |
| | 35 | 101 | | config, |
| | 35 | 102 | | subtitleManager, |
| | 35 | 103 | | libraryManager, |
| | 35 | 104 | | _audioResolver, |
| | 35 | 105 | | _subtitleResolver, |
| | 35 | 106 | | mediaAttachmentRepository, |
| | 35 | 107 | | mediaStreamRepository); |
| | | 108 | | |
| | 35 | 109 | | _audioProber = new AudioFileProber( |
| | 35 | 110 | | loggerFactory.CreateLogger<AudioFileProber>(), |
| | 35 | 111 | | mediaSourceManager, |
| | 35 | 112 | | mediaEncoder, |
| | 35 | 113 | | libraryManager, |
| | 35 | 114 | | _lyricResolver, |
| | 35 | 115 | | lyricManager, |
| | 35 | 116 | | mediaStreamRepository, |
| | 35 | 117 | | chapterManager); |
| | 35 | 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 | | { |
| | 8 | 129 | | var video = item as Video; |
| | 8 | 130 | | if (video is null || video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Iso) |
| | | 131 | | { |
| | 8 | 132 | | var path = item.Path; |
| | | 133 | | |
| | 8 | 134 | | if (!string.IsNullOrWhiteSpace(path) && item.IsFileProtocol) |
| | | 135 | | { |
| | 8 | 136 | | var file = directoryService.GetFile(path); |
| | 8 | 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 | | |
| | 8 | 145 | | if (IsMissingMediaInfo(item)) |
| | | 146 | | { |
| | 2 | 147 | | _logger.LogDebug("Refreshing {ItemPath} because it has no media information.", item.Path); |
| | 2 | 148 | | return true; |
| | | 149 | | } |
| | | 150 | | |
| | 6 | 151 | | if (video is not null |
| | 6 | 152 | | && item.SupportsLocalMetadata |
| | 6 | 153 | | && !video.IsPlaceHolder) |
| | | 154 | | { |
| | 5 | 155 | | var externalFiles = new HashSet<string>(_subtitleResolver.GetExternalFiles(video, directoryService, fals |
| | 5 | 156 | | if (!new HashSet<string>(video.SubtitleFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 157 | | { |
| | 0 | 158 | | _logger.LogDebug("Refreshing {ItemPath} due to external subtitles change.", item.Path); |
| | 0 | 159 | | return true; |
| | | 160 | | } |
| | | 161 | | |
| | 5 | 162 | | externalFiles = new HashSet<string>(_audioResolver.GetExternalFiles(video, directoryService, false).Sele |
| | 5 | 163 | | if (!new HashSet<string>(video.AudioFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 164 | | { |
| | 0 | 165 | | _logger.LogDebug("Refreshing {ItemPath} due to external audio change.", item.Path); |
| | 0 | 166 | | return true; |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | |
| | 6 | 170 | | if (item is Audio audio |
| | 6 | 171 | | && item.SupportsLocalMetadata) |
| | | 172 | | { |
| | 0 | 173 | | var externalFiles = new HashSet<string>(_lyricResolver.GetExternalFiles(audio, directoryService, false). |
| | 0 | 174 | | if (!new HashSet<string>(audio.LyricFiles, StringComparer.Ordinal).SetEquals(externalFiles)) |
| | | 175 | | { |
| | 0 | 176 | | _logger.LogDebug("Refreshing {ItemPath} due to external lyrics change.", item.Path); |
| | 0 | 177 | | return true; |
| | | 178 | | } |
| | | 179 | | } |
| | | 180 | | |
| | 6 | 181 | | return false; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | private static bool IsMissingMediaInfo(BaseItem item) |
| | | 185 | | { |
| | 8 | 186 | | if (item.RunTimeTicks.HasValue |
| | 8 | 187 | | || item.TotalBitrate.HasValue |
| | 8 | 188 | | || item.IsVirtualItem |
| | 8 | 189 | | || item.IsShortcut |
| | 8 | 190 | | || !item.IsFileProtocol) |
| | | 191 | | { |
| | 5 | 192 | | return false; |
| | | 193 | | } |
| | | 194 | | |
| | 3 | 195 | | return item switch |
| | 3 | 196 | | { |
| | 2 | 197 | | Video video => !video.IsPlaceHolder && video.IsCompleteMedia, |
| | 1 | 198 | | Audio => true, |
| | 0 | 199 | | _ => false |
| | 3 | 200 | | }; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <inheritdoc /> |
| | | 204 | | public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellat |
| | | 205 | | { |
| | 0 | 206 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <inheritdoc /> |
| | | 210 | | public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancel |
| | | 211 | | { |
| | 0 | 212 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <inheritdoc /> |
| | | 216 | | public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 217 | | { |
| | 0 | 218 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <inheritdoc /> |
| | | 222 | | public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellat |
| | | 223 | | { |
| | 0 | 224 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <inheritdoc /> |
| | | 228 | | public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 229 | | { |
| | 0 | 230 | | return FetchVideoInfo(item, options, cancellationToken); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | /// <inheritdoc /> |
| | | 234 | | public Task<ItemUpdateType> FetchAsync(Audio item, MetadataRefreshOptions options, CancellationToken cancellatio |
| | | 235 | | { |
| | 0 | 236 | | return FetchAudioInfo(item, options, cancellationToken); |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <inheritdoc /> |
| | | 240 | | public Task<ItemUpdateType> FetchAsync(AudioBook item, MetadataRefreshOptions options, CancellationToken cancell |
| | | 241 | | { |
| | 0 | 242 | | return FetchAudioInfo(item, options, cancellationToken); |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | /// <inheritdoc /> |
| | | 246 | | public Task<ItemUpdateType> FetchAsync(Book item, MetadataRefreshOptions options, CancellationToken cancellation |
| | | 247 | | { |
| | 0 | 248 | | if (item.IsVirtualItem || !item.IsFileProtocol) |
| | | 249 | | { |
| | 0 | 250 | | return _cachedTask; |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | long pageCount; |
| | 0 | 254 | | switch (Path.GetExtension(item.Path).ToLowerInvariant()) |
| | | 255 | | { |
| | | 256 | | case ".cb7": |
| | | 257 | | case ".cbr": |
| | | 258 | | case ".cbt": |
| | | 259 | | case ".cbz": |
| | 0 | 260 | | using (var stream = File.OpenRead(item.Path)) |
| | 0 | 261 | | using (var archive = ArchiveFactory.OpenArchive(stream)) |
| | | 262 | | { |
| | 0 | 263 | | pageCount = archive.Entries.Count(e => !e.IsDirectory); |
| | 0 | 264 | | } |
| | | 265 | | |
| | | 266 | | break; |
| | | 267 | | |
| | | 268 | | #pragma warning disable CA1416 |
| | | 269 | | case ".pdf": |
| | 0 | 270 | | using (var stream = File.OpenRead(item.Path)) |
| | | 271 | | { |
| | 0 | 272 | | pageCount = Conversion.GetPageCount(stream); |
| | 0 | 273 | | } |
| | | 274 | | |
| | | 275 | | break; |
| | | 276 | | #pragma warning restore CA1416 |
| | | 277 | | |
| | | 278 | | case ".epub": |
| | | 279 | | // TODO process CFI and store as a string when multiple progress types are supported |
| | | 280 | | // current progress value is percentage stored as a proportion of one second worth of ticks |
| | 0 | 281 | | item.RunTimeTicks = TimeSpan.TicksPerSecond; |
| | | 282 | | |
| | 0 | 283 | | return Task.FromResult(ItemUpdateType.MetadataImport); |
| | | 284 | | |
| | | 285 | | default: |
| | 0 | 286 | | return _cachedTask; |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | // TODO use page count without modification when multiple progress types are supported |
| | | 290 | | // book players report page count and the web client multiplies that value by 10000 to convert the expected |
| | 0 | 291 | | item.RunTimeTicks = pageCount * 10000; |
| | | 292 | | |
| | 0 | 293 | | return Task.FromResult(ItemUpdateType.MetadataImport); |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Fetches video information for an item. |
| | | 298 | | /// </summary> |
| | | 299 | | /// <param name="item">The item.</param> |
| | | 300 | | /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> |
| | | 301 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> |
| | | 302 | | /// <typeparam name="T">The type of item to resolve.</typeparam> |
| | | 303 | | /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns> |
| | | 304 | | public Task<ItemUpdateType> FetchVideoInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancella |
| | | 305 | | where T : Video |
| | | 306 | | { |
| | 0 | 307 | | if (item.IsPlaceHolder) |
| | | 308 | | { |
| | 0 | 309 | | return _cachedTask; |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | if (!item.IsCompleteMedia) |
| | | 313 | | { |
| | 0 | 314 | | return _cachedTask; |
| | | 315 | | } |
| | | 316 | | |
| | 0 | 317 | | if (item.IsVirtualItem) |
| | | 318 | | { |
| | 0 | 319 | | return _cachedTask; |
| | | 320 | | } |
| | | 321 | | |
| | 0 | 322 | | if (!options.EnableRemoteContentProbe && !item.IsFileProtocol) |
| | | 323 | | { |
| | 0 | 324 | | return _cachedTask; |
| | | 325 | | } |
| | | 326 | | |
| | 0 | 327 | | if (item.IsShortcut) |
| | | 328 | | { |
| | 0 | 329 | | FetchShortcutInfo(item); |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | return _videoProber.ProbeVideo(item, options, cancellationToken); |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | private string NormalizeStrmLine(string line) |
| | | 336 | | { |
| | 0 | 337 | | return line.Replace("\t", string.Empty, StringComparison.Ordinal) |
| | 0 | 338 | | .Replace("\r", string.Empty, StringComparison.Ordinal) |
| | 0 | 339 | | .Replace("\n", string.Empty, StringComparison.Ordinal) |
| | 0 | 340 | | .Trim(); |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | private void FetchShortcutInfo(BaseItem item) |
| | | 344 | | { |
| | 0 | 345 | | var shortcutPath = File.ReadAllLines(item.Path) |
| | 0 | 346 | | .Select(NormalizeStrmLine) |
| | 0 | 347 | | .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i) && !i.StartsWith('#')); |
| | | 348 | | |
| | 0 | 349 | | if (string.IsNullOrWhiteSpace(shortcutPath)) |
| | | 350 | | { |
| | 0 | 351 | | return; |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | // Only allow remote URLs in .strm files to prevent local file access |
| | 0 | 355 | | if (Uri.TryCreate(shortcutPath, UriKind.Absolute, out var uri) |
| | 0 | 356 | | && (string.Equals(uri.Scheme, "http", StringComparison.OrdinalIgnoreCase) |
| | 0 | 357 | | || string.Equals(uri.Scheme, "https", StringComparison.OrdinalIgnoreCase) |
| | 0 | 358 | | || string.Equals(uri.Scheme, "rtsp", StringComparison.OrdinalIgnoreCase) |
| | 0 | 359 | | || string.Equals(uri.Scheme, "rtp", StringComparison.OrdinalIgnoreCase))) |
| | | 360 | | { |
| | 0 | 361 | | item.ShortcutPath = shortcutPath; |
| | | 362 | | } |
| | | 363 | | else |
| | | 364 | | { |
| | 0 | 365 | | _logger.LogWarning("Ignoring invalid or non-remote .strm path in {File}: {Path}", item.Path, shortcutPat |
| | | 366 | | } |
| | 0 | 367 | | } |
| | | 368 | | |
| | | 369 | | /// <summary> |
| | | 370 | | /// Fetches audio information for an item. |
| | | 371 | | /// </summary> |
| | | 372 | | /// <param name="item">The item.</param> |
| | | 373 | | /// <param name="options">The <see cref="MetadataRefreshOptions"/>.</param> |
| | | 374 | | /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param> |
| | | 375 | | /// <typeparam name="T">The type of item to resolve.</typeparam> |
| | | 376 | | /// <returns>A <see cref="Task"/> fetching the <see cref="ItemUpdateType"/> for an item.</returns> |
| | | 377 | | public Task<ItemUpdateType> FetchAudioInfo<T>(T item, MetadataRefreshOptions options, CancellationToken cancella |
| | | 378 | | where T : Audio |
| | | 379 | | { |
| | 0 | 380 | | if (item.IsVirtualItem) |
| | | 381 | | { |
| | 0 | 382 | | return _cachedTask; |
| | | 383 | | } |
| | | 384 | | |
| | 0 | 385 | | if (!options.EnableRemoteContentProbe && !item.IsFileProtocol) |
| | | 386 | | { |
| | 0 | 387 | | return _cachedTask; |
| | | 388 | | } |
| | | 389 | | |
| | 0 | 390 | | if (item.IsShortcut) |
| | | 391 | | { |
| | 0 | 392 | | FetchShortcutInfo(item); |
| | | 393 | | } |
| | | 394 | | |
| | 0 | 395 | | return _audioProber.Probe(item, options, cancellationToken); |
| | | 396 | | } |
| | | 397 | | } |
| | | 398 | | } |