VideoImageProvider.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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<VideoImageProvider> _logger;
  20. private readonly IFileSystem _fileSystem;
  21. public VideoImageProvider(IMediaEncoder mediaEncoder, ILogger<VideoImageProvider> 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(
  56. _fileSystem,
  57. item.Path,
  58. null,
  59. item.GetPlayableStreamFileNames());
  60. var mediaStreams =
  61. item.GetMediaStreams();
  62. var imageStreams =
  63. mediaStreams
  64. .Where(i => i.Type == MediaStreamType.EmbeddedImage)
  65. .ToList();
  66. var imageStream = imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("front", StringComparison.OrdinalIgnoreCase) != -1) ??
  67. imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1) ??
  68. imageStreams.FirstOrDefault();
  69. string extractedImagePath;
  70. if (imageStream != null)
  71. {
  72. // Instead of using the raw stream index, we need to use nth video/embedded image stream
  73. var videoIndex = -1;
  74. foreach (var mediaStream in mediaStreams)
  75. {
  76. if (mediaStream.Type == MediaStreamType.Video ||
  77. mediaStream.Type == MediaStreamType.EmbeddedImage)
  78. {
  79. videoIndex++;
  80. }
  81. if (mediaStream == imageStream)
  82. {
  83. break;
  84. }
  85. }
  86. extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, imageStream, videoIndex, cancellationToken).ConfigureAwait(false);
  87. }
  88. else
  89. {
  90. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  91. // Always use 10 seconds for dvd because our duration could be out of whack
  92. var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
  93. item.RunTimeTicks.Value > 0
  94. ? TimeSpan.FromTicks(Convert.ToInt64(item.RunTimeTicks.Value * .1))
  95. : TimeSpan.FromSeconds(10);
  96. var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
  97. extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, protocol, videoStream, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
  98. }
  99. return new DynamicImageResponse
  100. {
  101. Format = ImageFormat.Jpg,
  102. HasImage = true,
  103. Path = extractedImagePath,
  104. Protocol = MediaProtocol.File
  105. };
  106. }
  107. public string Name => "Screen Grabber";
  108. public bool Supports(BaseItem item)
  109. {
  110. if (item.IsShortcut)
  111. {
  112. return false;
  113. }
  114. if (!item.IsFileProtocol)
  115. {
  116. return false;
  117. }
  118. var video = item as Video;
  119. if (video != null && !video.IsPlaceHolder && video.IsCompleteMedia)
  120. {
  121. return true;
  122. }
  123. return false;
  124. }
  125. // Make sure this comes after internet image providers
  126. public int Order => 100;
  127. }
  128. }