VideoImageProvider.cs 4.2 KB

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