EmbeddedImageProvider.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #nullable disable
  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 <see cref="IMediaEncoder"/> 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. /// <summary>
  44. /// Initializes a new instance of the <see cref="EmbeddedImageProvider"/> class.
  45. /// </summary>
  46. /// <param name="mediaEncoder">The media encoder for extracting attached/embedded images.</param>
  47. public EmbeddedImageProvider(IMediaEncoder mediaEncoder)
  48. {
  49. _mediaEncoder = mediaEncoder;
  50. }
  51. /// <inheritdoc />
  52. public string Name => "Embedded Image Extractor";
  53. /// <inheritdoc />
  54. // Default to after internet image providers but before Screen Grabber
  55. public int Order => 99;
  56. /// <inheritdoc />
  57. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  58. {
  59. if (item is Video)
  60. {
  61. if (item is Episode)
  62. {
  63. return new[]
  64. {
  65. ImageType.Primary,
  66. };
  67. }
  68. return new[]
  69. {
  70. ImageType.Primary,
  71. ImageType.Backdrop,
  72. ImageType.Logo,
  73. };
  74. }
  75. return Array.Empty<ImageType>();
  76. }
  77. /// <inheritdoc />
  78. public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
  79. {
  80. var video = (Video)item;
  81. // No support for these
  82. if (video.IsPlaceHolder || video.VideoType == VideoType.Dvd)
  83. {
  84. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  85. }
  86. return GetEmbeddedImage(video, type, cancellationToken);
  87. }
  88. private async Task<DynamicImageResponse> GetEmbeddedImage(Video item, ImageType type, CancellationToken cancellationToken)
  89. {
  90. MediaSourceInfo mediaSource = new MediaSourceInfo
  91. {
  92. VideoType = item.VideoType,
  93. IsoType = item.IsoType,
  94. Protocol = item.PathProtocol ?? MediaProtocol.File,
  95. };
  96. string[] imageFileNames = type switch
  97. {
  98. ImageType.Primary => _primaryImageFileNames,
  99. ImageType.Backdrop => _backdropImageFileNames,
  100. ImageType.Logo => _logoImageFileNames,
  101. _ => _primaryImageFileNames
  102. };
  103. // Try attachments first
  104. var attachmentStream = item.GetMediaSources(false)
  105. .SelectMany(source => source.MediaAttachments)
  106. .FirstOrDefault(attachment => !string.IsNullOrEmpty(attachment.FileName)
  107. && imageFileNames.Any(name => attachment.FileName.Contains(name, StringComparison.OrdinalIgnoreCase)));
  108. if (attachmentStream != null)
  109. {
  110. return await ExtractAttachment(item, cancellationToken, attachmentStream, mediaSource);
  111. }
  112. // Fall back to EmbeddedImage streams
  113. var imageStreams = item.GetMediaStreams().FindAll(i => i.Type == MediaStreamType.EmbeddedImage);
  114. if (imageStreams.Count == 0)
  115. {
  116. // Can't extract if we don't have any EmbeddedImage streams
  117. return new DynamicImageResponse { HasImage = false };
  118. }
  119. // Extract first stream containing an element of imageFileNames
  120. var imageStream = imageStreams
  121. .FirstOrDefault(stream => !string.IsNullOrEmpty(stream.Comment)
  122. && imageFileNames.Any(name => stream.Comment.Contains(name, StringComparison.OrdinalIgnoreCase)));
  123. // Primary type only: default to first image if none found by label
  124. if (imageStream == null)
  125. {
  126. if (type == ImageType.Primary)
  127. {
  128. imageStream = imageStreams[0];
  129. }
  130. else
  131. {
  132. // No streams matched, abort
  133. return new DynamicImageResponse { HasImage = false };
  134. }
  135. }
  136. string extractedImagePath =
  137. await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, ".jpg", cancellationToken)
  138. .ConfigureAwait(false);
  139. return new DynamicImageResponse
  140. {
  141. Format = ImageFormat.Jpg,
  142. HasImage = true,
  143. Path = extractedImagePath,
  144. Protocol = MediaProtocol.File
  145. };
  146. }
  147. private async Task<DynamicImageResponse> ExtractAttachment(Video item, CancellationToken cancellationToken, MediaAttachment attachmentStream, MediaSourceInfo mediaSource)
  148. {
  149. var extension = string.IsNullOrEmpty(attachmentStream.MimeType)
  150. ? Path.GetExtension(attachmentStream.FileName)
  151. : MimeTypes.ToExtension(attachmentStream.MimeType);
  152. if (string.IsNullOrEmpty(extension))
  153. {
  154. extension = ".jpg";
  155. }
  156. string extractedAttachmentPath =
  157. await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, extension, cancellationToken)
  158. .ConfigureAwait(false);
  159. ImageFormat format = extension switch
  160. {
  161. ".bmp" => ImageFormat.Bmp,
  162. ".gif" => ImageFormat.Gif,
  163. ".jpg" => ImageFormat.Jpg,
  164. ".png" => ImageFormat.Png,
  165. ".webp" => ImageFormat.Webp,
  166. _ => ImageFormat.Jpg
  167. };
  168. return new DynamicImageResponse
  169. {
  170. Format = format,
  171. HasImage = true,
  172. Path = extractedAttachmentPath,
  173. Protocol = MediaProtocol.File
  174. };
  175. }
  176. /// <inheritdoc />
  177. public bool Supports(BaseItem item)
  178. {
  179. if (item.IsShortcut)
  180. {
  181. return false;
  182. }
  183. if (!item.IsFileProtocol)
  184. {
  185. return false;
  186. }
  187. return item is Video video && !video.IsPlaceHolder && video.IsCompleteMedia;
  188. }
  189. }
  190. }