VideoImageProvider.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma warning disable CS1591
  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.MediaEncoding;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Model.Drawing;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Model.MediaInfo;
  14. using Microsoft.Extensions.Logging;
  15. namespace MediaBrowser.Providers.MediaInfo
  16. {
  17. public class VideoImageProvider : IDynamicImageProvider, IHasOrder
  18. {
  19. private readonly IMediaEncoder _mediaEncoder;
  20. private readonly ILogger<VideoImageProvider> _logger;
  21. private readonly IFileSystem _fileSystem;
  22. public VideoImageProvider(IMediaEncoder mediaEncoder, ILogger<VideoImageProvider> logger, IFileSystem fileSystem)
  23. {
  24. _mediaEncoder = mediaEncoder;
  25. _logger = logger;
  26. _fileSystem = fileSystem;
  27. }
  28. public string Name => "Screen Grabber";
  29. // Make sure this comes after internet image providers
  30. public int Order => 100;
  31. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  32. {
  33. return new List<ImageType> { ImageType.Primary };
  34. }
  35. public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
  36. {
  37. var video = (Video)item;
  38. // No support for this
  39. if (video.IsPlaceHolder)
  40. {
  41. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  42. }
  43. // No support for this
  44. if (video.VideoType == VideoType.Iso || video.VideoType == VideoType.Dvd || video.VideoType == VideoType.BluRay)
  45. {
  46. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  47. }
  48. // Can't extract if we didn't find a video stream in the file
  49. if (!video.DefaultVideoStreamIndex.HasValue)
  50. {
  51. _logger.LogInformation("Skipping image extraction due to missing DefaultVideoStreamIndex for {0}.", video.Path ?? string.Empty);
  52. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  53. }
  54. return GetVideoImage(video, cancellationToken);
  55. }
  56. public async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
  57. {
  58. var protocol = item.PathProtocol ?? MediaProtocol.File;
  59. var inputPath = MediaEncoderHelpers.GetInputArgument(
  60. _fileSystem,
  61. item.Path,
  62. null,
  63. item.GetPlayableStreamFileNames());
  64. var mediaStreams =
  65. item.GetMediaStreams();
  66. var imageStreams =
  67. mediaStreams
  68. .Where(i => i.Type == MediaStreamType.EmbeddedImage)
  69. .ToList();
  70. var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("front", StringComparison.OrdinalIgnoreCase) != -1) ??
  71. imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1) ??
  72. imageStreams.FirstOrDefault();
  73. string extractedImagePath;
  74. if (imageStream != null)
  75. {
  76. // Instead of using the raw stream index, we need to use nth video/embedded image stream
  77. var videoIndex = -1;
  78. foreach (var mediaStream in mediaStreams)
  79. {
  80. if (mediaStream.Type == MediaStreamType.Video ||
  81. mediaStream.Type == MediaStreamType.EmbeddedImage)
  82. {
  83. videoIndex++;
  84. }
  85. if (mediaStream == imageStream)
  86. {
  87. break;
  88. }
  89. }
  90. extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, imageStream, videoIndex, cancellationToken).ConfigureAwait(false);
  91. }
  92. else
  93. {
  94. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  95. // Always use 10 seconds for dvd because our duration could be out of whack
  96. var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
  97. item.RunTimeTicks.Value > 0
  98. ? TimeSpan.FromTicks(Convert.ToInt64(item.RunTimeTicks.Value * .1))
  99. : TimeSpan.FromSeconds(10);
  100. var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
  101. extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, videoStream, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
  102. }
  103. return new DynamicImageResponse
  104. {
  105. Format = ImageFormat.Jpg,
  106. HasImage = true,
  107. Path = extractedImagePath,
  108. Protocol = MediaProtocol.File
  109. };
  110. }
  111. public bool Supports(BaseItem item)
  112. {
  113. if (item.IsShortcut)
  114. {
  115. return false;
  116. }
  117. if (!item.IsFileProtocol)
  118. {
  119. return false;
  120. }
  121. var video = item as Video;
  122. if (video != null && !video.IsPlaceHolder && video.IsCompleteMedia)
  123. {
  124. return true;
  125. }
  126. return false;
  127. }
  128. }
  129. }