2021-10-26 09:49:01 -04:00
|
|
|
#nullable disable
|
|
|
|
|
2021-10-03 15:28:21 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-10-07 16:37:59 -04:00
|
|
|
using System.IO;
|
2021-10-03 15:28:21 -04:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2021-10-05 19:13:08 -04:00
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2021-11-18 09:04:30 -05:00
|
|
|
using MediaBrowser.Controller.Library;
|
2021-10-03 15:28:21 -04:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2021-11-18 09:04:30 -05:00
|
|
|
using MediaBrowser.Controller.Persistence;
|
2021-10-03 15:28:21 -04:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Drawing;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2021-10-07 16:37:59 -04:00
|
|
|
using MediaBrowser.Model.Net;
|
2021-11-15 09:30:43 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-10-03 15:28:21 -04:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
{
|
|
|
|
/// <summary>
|
2021-10-11 15:25:12 -04:00
|
|
|
/// Uses <see cref="IMediaEncoder"/> to extract embedded images.
|
2021-10-03 15:28:21 -04:00
|
|
|
/// </summary>
|
|
|
|
public class EmbeddedImageProvider : IDynamicImageProvider, IHasOrder
|
|
|
|
{
|
2021-10-05 19:13:08 -04:00
|
|
|
private static readonly string[] _primaryImageFileNames =
|
|
|
|
{
|
|
|
|
"poster",
|
|
|
|
"folder",
|
|
|
|
"cover",
|
2022-10-08 23:20:10 -04:00
|
|
|
"default",
|
|
|
|
"movie",
|
|
|
|
"show"
|
2021-10-05 19:13:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
private static readonly string[] _backdropImageFileNames =
|
|
|
|
{
|
|
|
|
"backdrop",
|
|
|
|
"background",
|
|
|
|
"art"
|
|
|
|
};
|
|
|
|
|
|
|
|
private static readonly string[] _logoImageFileNames =
|
|
|
|
{
|
|
|
|
"logo",
|
|
|
|
};
|
|
|
|
|
2021-11-18 09:04:30 -05:00
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2021-10-03 15:28:21 -04:00
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2021-11-15 09:30:43 -05:00
|
|
|
private readonly ILogger<EmbeddedImageProvider> _logger;
|
2021-10-03 15:28:21 -04:00
|
|
|
|
2021-10-11 15:25:12 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="EmbeddedImageProvider"/> class.
|
|
|
|
/// </summary>
|
2021-11-18 09:04:30 -05:00
|
|
|
/// <param name="mediaSourceManager">The media source manager for fetching item streams and attachments.</param>
|
2021-10-11 15:25:12 -04:00
|
|
|
/// <param name="mediaEncoder">The media encoder for extracting attached/embedded images.</param>
|
2021-11-15 09:30:43 -05:00
|
|
|
/// <param name="logger">The logger.</param>
|
2021-11-18 09:04:30 -05:00
|
|
|
public EmbeddedImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, ILogger<EmbeddedImageProvider> logger)
|
2021-10-03 15:28:21 -04:00
|
|
|
{
|
2021-11-18 09:04:30 -05:00
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2021-10-03 15:28:21 -04:00
|
|
|
_mediaEncoder = mediaEncoder;
|
2021-11-15 09:30:43 -05:00
|
|
|
_logger = logger;
|
2021-10-03 15:28:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public string Name => "Embedded Image Extractor";
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
// Default to after internet image providers but before Screen Grabber
|
|
|
|
public int Order => 99;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
|
|
|
{
|
2021-10-05 19:13:08 -04:00
|
|
|
if (item is Video)
|
|
|
|
{
|
|
|
|
if (item is Episode)
|
|
|
|
{
|
2021-10-11 15:25:12 -04:00
|
|
|
return new[]
|
2021-10-05 19:13:08 -04:00
|
|
|
{
|
|
|
|
ImageType.Primary,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:25:12 -04:00
|
|
|
return new[]
|
2021-10-05 19:13:08 -04:00
|
|
|
{
|
|
|
|
ImageType.Primary,
|
|
|
|
ImageType.Backdrop,
|
|
|
|
ImageType.Logo,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:25:12 -04:00
|
|
|
return Array.Empty<ImageType>();
|
2021-10-03 15:28:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var video = (Video)item;
|
|
|
|
|
|
|
|
// No support for these
|
|
|
|
if (video.IsPlaceHolder || video.VideoType == VideoType.Dvd)
|
|
|
|
{
|
|
|
|
return Task.FromResult(new DynamicImageResponse { HasImage = false });
|
|
|
|
}
|
|
|
|
|
2021-10-05 19:13:08 -04:00
|
|
|
return GetEmbeddedImage(video, type, cancellationToken);
|
2021-10-03 15:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-10-05 19:13:08 -04:00
|
|
|
private async Task<DynamicImageResponse> GetEmbeddedImage(Video item, ImageType type, CancellationToken cancellationToken)
|
2021-10-03 15:28:21 -04:00
|
|
|
{
|
|
|
|
MediaSourceInfo mediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
VideoType = item.VideoType,
|
|
|
|
IsoType = item.IsoType,
|
|
|
|
Protocol = item.PathProtocol ?? MediaProtocol.File,
|
|
|
|
};
|
|
|
|
|
2021-10-07 16:37:59 -04:00
|
|
|
string[] imageFileNames = type switch
|
2021-10-05 19:13:08 -04:00
|
|
|
{
|
2021-10-07 16:37:59 -04:00
|
|
|
ImageType.Primary => _primaryImageFileNames,
|
|
|
|
ImageType.Backdrop => _backdropImageFileNames,
|
|
|
|
ImageType.Logo => _logoImageFileNames,
|
2021-11-14 17:13:45 -05:00
|
|
|
_ => Array.Empty<string>()
|
2021-10-07 16:37:59 -04:00
|
|
|
};
|
|
|
|
|
2021-11-15 09:30:43 -05:00
|
|
|
if (imageFileNames.Length == 0)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Attempted to load unexpected image type: {Type}", type);
|
|
|
|
return new DynamicImageResponse { HasImage = false };
|
|
|
|
}
|
|
|
|
|
2021-10-07 16:37:59 -04:00
|
|
|
// Try attachments first
|
2021-11-18 09:04:30 -05:00
|
|
|
var attachmentStream = _mediaSourceManager.GetMediaAttachments(item.Id)
|
2021-10-11 15:25:12 -04:00
|
|
|
.FirstOrDefault(attachment => !string.IsNullOrEmpty(attachment.FileName)
|
|
|
|
&& imageFileNames.Any(name => attachment.FileName.Contains(name, StringComparison.OrdinalIgnoreCase)));
|
2021-10-07 16:37:59 -04:00
|
|
|
|
|
|
|
if (attachmentStream != null)
|
|
|
|
{
|
2021-11-08 14:39:02 -05:00
|
|
|
return await ExtractAttachment(item, attachmentStream, mediaSource, cancellationToken);
|
2021-10-05 19:13:08 -04:00
|
|
|
}
|
|
|
|
|
2021-10-07 16:37:59 -04:00
|
|
|
// Fall back to EmbeddedImage streams
|
2021-11-18 09:04:30 -05:00
|
|
|
var imageStreams = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery
|
|
|
|
{
|
|
|
|
ItemId = item.Id,
|
|
|
|
Type = MediaStreamType.EmbeddedImage
|
|
|
|
});
|
2021-10-03 15:28:21 -04:00
|
|
|
|
2021-10-11 15:25:12 -04:00
|
|
|
if (imageStreams.Count == 0)
|
2021-10-03 15:28:21 -04:00
|
|
|
{
|
|
|
|
// Can't extract if we don't have any EmbeddedImage streams
|
|
|
|
return new DynamicImageResponse { HasImage = false };
|
|
|
|
}
|
|
|
|
|
2021-10-05 19:13:08 -04:00
|
|
|
// Extract first stream containing an element of imageFileNames
|
|
|
|
var imageStream = imageStreams
|
2021-10-11 15:25:12 -04:00
|
|
|
.FirstOrDefault(stream => !string.IsNullOrEmpty(stream.Comment)
|
|
|
|
&& imageFileNames.Any(name => stream.Comment.Contains(name, StringComparison.OrdinalIgnoreCase)));
|
2021-10-05 19:13:08 -04:00
|
|
|
|
|
|
|
// 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 };
|
|
|
|
}
|
2021-10-03 15:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-11-12 10:30:30 -05:00
|
|
|
var format = imageStream.Codec switch
|
|
|
|
{
|
|
|
|
"mjpeg" => ImageFormat.Jpg,
|
|
|
|
"png" => ImageFormat.Png,
|
|
|
|
"gif" => ImageFormat.Gif,
|
|
|
|
_ => ImageFormat.Jpg
|
|
|
|
};
|
|
|
|
|
2021-10-11 15:25:12 -04:00
|
|
|
string extractedImagePath =
|
2021-11-12 10:11:15 -05:00
|
|
|
await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, format, cancellationToken)
|
2021-10-11 15:25:12 -04:00
|
|
|
.ConfigureAwait(false);
|
2021-10-05 19:13:08 -04:00
|
|
|
|
2021-10-03 15:28:21 -04:00
|
|
|
return new DynamicImageResponse
|
|
|
|
{
|
2021-11-12 10:11:15 -05:00
|
|
|
Format = format,
|
2021-10-03 15:28:21 -04:00
|
|
|
HasImage = true,
|
|
|
|
Path = extractedImagePath,
|
|
|
|
Protocol = MediaProtocol.File
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:39:02 -05:00
|
|
|
private async Task<DynamicImageResponse> ExtractAttachment(Video item, MediaAttachment attachmentStream, MediaSourceInfo mediaSource, CancellationToken cancellationToken)
|
2021-10-11 15:25:12 -04:00
|
|
|
{
|
|
|
|
var extension = string.IsNullOrEmpty(attachmentStream.MimeType)
|
|
|
|
? Path.GetExtension(attachmentStream.FileName)
|
|
|
|
: MimeTypes.ToExtension(attachmentStream.MimeType);
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(extension))
|
|
|
|
{
|
|
|
|
extension = ".jpg";
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageFormat format = extension switch
|
|
|
|
{
|
|
|
|
".bmp" => ImageFormat.Bmp,
|
|
|
|
".gif" => ImageFormat.Gif,
|
|
|
|
".jpg" => ImageFormat.Jpg,
|
|
|
|
".png" => ImageFormat.Png,
|
|
|
|
".webp" => ImageFormat.Webp,
|
|
|
|
_ => ImageFormat.Jpg
|
|
|
|
};
|
|
|
|
|
2021-11-12 10:11:15 -05:00
|
|
|
string extractedAttachmentPath =
|
|
|
|
await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, format, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
2021-10-11 15:25:12 -04:00
|
|
|
return new DynamicImageResponse
|
|
|
|
{
|
|
|
|
Format = format,
|
|
|
|
HasImage = true,
|
|
|
|
Path = extractedAttachmentPath,
|
|
|
|
Protocol = MediaProtocol.File
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-03 15:28:21 -04:00
|
|
|
/// <inheritdoc />
|
|
|
|
public bool Supports(BaseItem item)
|
|
|
|
{
|
|
|
|
if (item.IsShortcut)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!item.IsFileProtocol)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item is Video video && !video.IsPlaceHolder && video.IsCompleteMedia;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|