private async Task <DynamicImageResponse> GetEmbeddedImage(Video item, ImageType type, CancellationToken cancellationToken) { MediaSourceInfo mediaSource = new MediaSourceInfo { VideoType = item.VideoType, IsoType = item.IsoType, Protocol = item.PathProtocol ?? MediaProtocol.File, }; string[] imageFileNames = type switch { ImageType.Primary => _primaryImageFileNames, ImageType.Backdrop => _backdropImageFileNames, ImageType.Logo => _logoImageFileNames, _ => Array.Empty <string>() }; if (imageFileNames.Length == 0) { _logger.LogWarning("Attempted to load unexpected image type: {Type}", type); return(new DynamicImageResponse { HasImage = false }); } // Try attachments first var attachmentStream = _mediaSourceManager.GetMediaAttachments(item.Id) .FirstOrDefault(attachment => !string.IsNullOrEmpty(attachment.FileName) && imageFileNames.Any(name => attachment.FileName.Contains(name, StringComparison.OrdinalIgnoreCase))); if (attachmentStream != null) { return(await ExtractAttachment(item, attachmentStream, mediaSource, cancellationToken)); } // Fall back to EmbeddedImage streams var imageStreams = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery { ItemId = item.Id, Type = MediaStreamType.EmbeddedImage }); if (imageStreams.Count == 0) { // Can't extract if we don't have any EmbeddedImage streams return(new DynamicImageResponse { HasImage = false }); } // Extract first stream containing an element of imageFileNames var imageStream = imageStreams .FirstOrDefault(stream => !string.IsNullOrEmpty(stream.Comment) && imageFileNames.Any(name => stream.Comment.Contains(name, StringComparison.OrdinalIgnoreCase))); // Primary type only: default to first image if none found by label if (imageStream == null) { if (type == ImageType.Primary) { imageStream = imageStreams[0]; } else { // No streams matched, abort return(new DynamicImageResponse { HasImage = false }); } } var format = imageStream.Codec switch { "mjpeg" => ImageFormat.Jpg, "png" => ImageFormat.Png, "gif" => ImageFormat.Gif, _ => ImageFormat.Jpg }; string extractedImagePath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, format, cancellationToken) .ConfigureAwait(false); return(new DynamicImageResponse { Format = format, HasImage = true, Path = extractedImagePath, Protocol = MediaProtocol.File }); }