VideoImageProvider.cs 5.7 KB

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