EmbeddedImageProvider.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Controller.Entities.TV;
  10. using MediaBrowser.Controller.MediaEncoding;
  11. using MediaBrowser.Controller.Providers;
  12. using MediaBrowser.Model.Drawing;
  13. using MediaBrowser.Model.Dto;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.MediaInfo;
  16. using MediaBrowser.Model.Net;
  17. namespace MediaBrowser.Providers.MediaInfo
  18. {
  19. /// <summary>
  20. /// Uses ffmpeg to extract embedded images.
  21. /// </summary>
  22. public class EmbeddedImageProvider : IDynamicImageProvider, IHasOrder
  23. {
  24. private static readonly string[] _primaryImageFileNames =
  25. {
  26. "poster",
  27. "folder",
  28. "cover",
  29. "default"
  30. };
  31. private static readonly string[] _backdropImageFileNames =
  32. {
  33. "backdrop",
  34. "fanart",
  35. "background",
  36. "art"
  37. };
  38. private static readonly string[] _logoImageFileNames =
  39. {
  40. "logo",
  41. };
  42. private readonly IMediaEncoder _mediaEncoder;
  43. public EmbeddedImageProvider(IMediaEncoder mediaEncoder)
  44. {
  45. _mediaEncoder = mediaEncoder;
  46. }
  47. /// <inheritdoc />
  48. public string Name => "Embedded Image Extractor";
  49. /// <inheritdoc />
  50. // Default to after internet image providers but before Screen Grabber
  51. public int Order => 99;
  52. /// <inheritdoc />
  53. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  54. {
  55. if (item is Video)
  56. {
  57. if (item is Episode)
  58. {
  59. return new List<ImageType>
  60. {
  61. ImageType.Primary,
  62. };
  63. }
  64. return new List<ImageType>
  65. {
  66. ImageType.Primary,
  67. ImageType.Backdrop,
  68. ImageType.Logo,
  69. };
  70. }
  71. return new List<ImageType>();
  72. }
  73. /// <inheritdoc />
  74. public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
  75. {
  76. var video = (Video)item;
  77. // No support for these
  78. if (video.IsPlaceHolder || video.VideoType == VideoType.Dvd)
  79. {
  80. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  81. }
  82. return GetEmbeddedImage(video, type, cancellationToken);
  83. }
  84. private async Task<DynamicImageResponse> GetEmbeddedImage(Video item, ImageType type, CancellationToken cancellationToken)
  85. {
  86. MediaSourceInfo mediaSource = new MediaSourceInfo
  87. {
  88. VideoType = item.VideoType,
  89. IsoType = item.IsoType,
  90. Protocol = item.PathProtocol ?? MediaProtocol.File,
  91. };
  92. string[] imageFileNames = type switch
  93. {
  94. ImageType.Primary => _primaryImageFileNames,
  95. ImageType.Backdrop => _backdropImageFileNames,
  96. ImageType.Logo => _logoImageFileNames,
  97. _ => _primaryImageFileNames
  98. };
  99. // Try attachments first
  100. var attachmentSources = item.GetMediaSources(false).SelectMany(source => source.MediaAttachments).ToList();
  101. var attachmentStream = attachmentSources
  102. .Where(attachment => !string.IsNullOrEmpty(attachment.FileName))
  103. .FirstOrDefault(attachment => imageFileNames.Any(name => attachment.FileName.Contains(name, StringComparison.OrdinalIgnoreCase)));
  104. if (attachmentStream != null)
  105. {
  106. var extension = string.IsNullOrEmpty(attachmentStream.MimeType) ?
  107. Path.GetExtension(attachmentStream.FileName) :
  108. MimeTypes.ToExtension(attachmentStream.MimeType);
  109. if (string.IsNullOrEmpty(extension))
  110. {
  111. extension = ".jpg";
  112. }
  113. string extractedAttachmentPath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, extension, cancellationToken).ConfigureAwait(false);
  114. ImageFormat format = extension switch
  115. {
  116. ".bmp" => ImageFormat.Bmp,
  117. ".gif" => ImageFormat.Gif,
  118. ".jpg" => ImageFormat.Jpg,
  119. ".png" => ImageFormat.Png,
  120. ".webp" => ImageFormat.Webp,
  121. _ => ImageFormat.Jpg
  122. };
  123. return new DynamicImageResponse
  124. {
  125. Format = format,
  126. HasImage = true,
  127. Path = extractedAttachmentPath,
  128. Protocol = MediaProtocol.File
  129. };
  130. }
  131. // Fall back to EmbeddedImage streams
  132. var imageStreams = item.GetMediaStreams().FindAll(i => i.Type == MediaStreamType.EmbeddedImage);
  133. if (!imageStreams.Any())
  134. {
  135. // Can't extract if we don't have any EmbeddedImage streams
  136. return new DynamicImageResponse { HasImage = false };
  137. }
  138. // Extract first stream containing an element of imageFileNames
  139. var imageStream = imageStreams
  140. .Where(stream => !string.IsNullOrEmpty(stream.Comment))
  141. .FirstOrDefault(stream => imageFileNames.Any(name => stream.Comment.Contains(name, StringComparison.OrdinalIgnoreCase)));
  142. // Primary type only: default to first image if none found by label
  143. if (imageStream == null)
  144. {
  145. if (type == ImageType.Primary)
  146. {
  147. imageStream = imageStreams[0];
  148. }
  149. else
  150. {
  151. // No streams matched, abort
  152. return new DynamicImageResponse { HasImage = false };
  153. }
  154. }
  155. string extractedImagePath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, "jpg", cancellationToken).ConfigureAwait(false);
  156. return new DynamicImageResponse
  157. {
  158. Format = ImageFormat.Jpg,
  159. HasImage = true,
  160. Path = extractedImagePath,
  161. Protocol = MediaProtocol.File
  162. };
  163. }
  164. /// <inheritdoc />
  165. public bool Supports(BaseItem item)
  166. {
  167. if (item.IsShortcut)
  168. {
  169. return false;
  170. }
  171. if (!item.IsFileProtocol)
  172. {
  173. return false;
  174. }
  175. return item is Video video && !video.IsPlaceHolder && video.IsCompleteMedia;
  176. }
  177. }
  178. }