VideoImageProvider.cs 5.6 KB

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