VideoImageProvider.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma warning disable CA1826 // CA1826 Do not use Enumerable methods on Indexable collections.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.MediaEncoding;
  10. using MediaBrowser.Controller.Persistence;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Model.Drawing;
  13. using MediaBrowser.Model.Dto;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.MediaInfo;
  16. using Microsoft.Extensions.Logging;
  17. namespace MediaBrowser.Providers.MediaInfo
  18. {
  19. /// <summary>
  20. /// Uses <see cref="IMediaEncoder"/> to create still images from the main video.
  21. /// </summary>
  22. public class VideoImageProvider : IDynamicImageProvider, IHasOrder
  23. {
  24. private readonly IMediaSourceManager _mediaSourceManager;
  25. private readonly IMediaEncoder _mediaEncoder;
  26. private readonly ILogger<VideoImageProvider> _logger;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="VideoImageProvider"/> class.
  29. /// </summary>
  30. /// <param name="mediaSourceManager">The media source manager for fetching item streams.</param>
  31. /// <param name="mediaEncoder">The media encoder for capturing images.</param>
  32. /// <param name="logger">The logger.</param>
  33. public VideoImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, ILogger<VideoImageProvider> logger)
  34. {
  35. _mediaSourceManager = mediaSourceManager;
  36. _mediaEncoder = mediaEncoder;
  37. _logger = logger;
  38. }
  39. /// <inheritdoc />
  40. public string Name => "Screen Grabber";
  41. /// <inheritdoc />
  42. // Make sure this comes after internet image providers
  43. public int Order => 100;
  44. /// <inheritdoc />
  45. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  46. {
  47. return new[] { ImageType.Primary };
  48. }
  49. /// <inheritdoc />
  50. public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
  51. {
  52. var video = (Video)item;
  53. // No support for these
  54. if (video.IsPlaceHolder || video.VideoType == VideoType.Dvd)
  55. {
  56. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  57. }
  58. // Can't extract if we didn't find a video stream in the file
  59. if (!video.DefaultVideoStreamIndex.HasValue)
  60. {
  61. _logger.LogInformation("Skipping image extraction due to missing DefaultVideoStreamIndex for {Path}.", video.Path ?? string.Empty);
  62. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  63. }
  64. return GetVideoImage(video, cancellationToken);
  65. }
  66. private async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
  67. {
  68. MediaSourceInfo mediaSource = new MediaSourceInfo
  69. {
  70. VideoType = item.VideoType,
  71. IsoType = item.IsoType,
  72. Protocol = item.PathProtocol ?? MediaProtocol.File,
  73. };
  74. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  75. // Always use 10 seconds for dvd because our duration could be out of whack
  76. var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks > 0
  77. ? TimeSpan.FromTicks(item.RunTimeTicks.Value / 10)
  78. : TimeSpan.FromSeconds(10);
  79. var query = new MediaStreamQuery { ItemId = item.Id, Index = item.DefaultVideoStreamIndex };
  80. var videoStream = _mediaSourceManager.GetMediaStreams(query).FirstOrDefault();
  81. if (videoStream is null)
  82. {
  83. query.Type = MediaStreamType.Video;
  84. query.Index = null;
  85. videoStream = _mediaSourceManager.GetMediaStreams(query).FirstOrDefault();
  86. }
  87. if (videoStream is null)
  88. {
  89. _logger.LogInformation("Skipping image extraction: no video stream found for {Path}.", item.Path ?? string.Empty);
  90. return new DynamicImageResponse { HasImage = false };
  91. }
  92. string extractedImagePath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, videoStream, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
  93. return new DynamicImageResponse
  94. {
  95. Format = ImageFormat.Jpg,
  96. HasImage = true,
  97. Path = extractedImagePath,
  98. Protocol = MediaProtocol.File
  99. };
  100. }
  101. /// <inheritdoc />
  102. public bool Supports(BaseItem item)
  103. {
  104. if (item.IsShortcut)
  105. {
  106. return false;
  107. }
  108. if (!item.IsFileProtocol)
  109. {
  110. return false;
  111. }
  112. return item is Video video && !video.IsPlaceHolder && video.IsCompleteMedia;
  113. }
  114. }
  115. }