VideoImageProvider.cs 5.8 KB

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