VideoImageProvider.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.Dto;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.IO;
  14. using MediaBrowser.Model.MediaInfo;
  15. using Microsoft.Extensions.Logging;
  16. namespace MediaBrowser.Providers.MediaInfo
  17. {
  18. public class VideoImageProvider : IDynamicImageProvider, IHasOrder
  19. {
  20. private readonly IMediaEncoder _mediaEncoder;
  21. private readonly ILogger<VideoImageProvider> _logger;
  22. private readonly IFileSystem _fileSystem;
  23. public VideoImageProvider(IMediaEncoder mediaEncoder, ILogger<VideoImageProvider> logger, IFileSystem fileSystem)
  24. {
  25. _mediaEncoder = mediaEncoder;
  26. _logger = logger;
  27. _fileSystem = fileSystem;
  28. }
  29. public string Name => "Screen Grabber";
  30. // Make sure this comes after internet image providers
  31. public int Order => 100;
  32. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  33. {
  34. return new List<ImageType> { ImageType.Primary };
  35. }
  36. public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
  37. {
  38. var video = (Video)item;
  39. // No support for this
  40. if (video.IsPlaceHolder)
  41. {
  42. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  43. }
  44. // No support for this
  45. if (video.VideoType == VideoType.Dvd)
  46. {
  47. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  48. }
  49. // Can't extract if we didn't find a video stream in the file
  50. if (!video.DefaultVideoStreamIndex.HasValue)
  51. {
  52. _logger.LogInformation("Skipping image extraction due to missing DefaultVideoStreamIndex for {0}.", video.Path ?? string.Empty);
  53. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  54. }
  55. return GetVideoImage(video, cancellationToken);
  56. }
  57. public async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
  58. {
  59. var protocol = item.PathProtocol ?? MediaProtocol.File;
  60. var inputPath = item.Path;
  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. MediaSourceInfo mediaSource = new MediaSourceInfo
  88. {
  89. VideoType = item.VideoType,
  90. IsoType = item.IsoType,
  91. Protocol = item.PathProtocol.Value,
  92. };
  93. extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, mediaSource, imageStream, videoIndex, cancellationToken).ConfigureAwait(false);
  94. }
  95. else
  96. {
  97. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  98. // Always use 10 seconds for dvd because our duration could be out of whack
  99. var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
  100. item.RunTimeTicks.Value > 0
  101. ? TimeSpan.FromTicks(Convert.ToInt64(item.RunTimeTicks.Value * .1))
  102. : TimeSpan.FromSeconds(10);
  103. var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
  104. var mediaSource = new MediaSourceInfo
  105. {
  106. VideoType = item.VideoType,
  107. IsoType = item.IsoType,
  108. Protocol = item.PathProtocol.Value,
  109. };
  110. extractedImagePath = await _mediaEncoder.ExtractVideoImage(inputPath, item.Container, mediaSource, videoStream, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
  111. }
  112. return new DynamicImageResponse
  113. {
  114. Format = ImageFormat.Jpg,
  115. HasImage = true,
  116. Path = extractedImagePath,
  117. Protocol = MediaProtocol.File
  118. };
  119. }
  120. public bool Supports(BaseItem item)
  121. {
  122. if (item.IsShortcut)
  123. {
  124. return false;
  125. }
  126. if (!item.IsFileProtocol)
  127. {
  128. return false;
  129. }
  130. if (item is Video video && !video.IsPlaceHolder && video.IsCompleteMedia)
  131. {
  132. return true;
  133. }
  134. return false;
  135. }
  136. }
  137. }