2021-10-03 19:28:21 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-10-07 20:37:59 +00:00
|
|
|
using System.IO;
|
2021-10-03 19:28:21 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2021-10-05 23:13:08 +00:00
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2021-10-03 19:28:21 +00:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Drawing;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2021-10-07 20:37:59 +00:00
|
|
|
using MediaBrowser.Model.Net;
|
2021-10-03 19:28:21 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Uses ffmpeg to extract embedded images.
|
|
|
|
/// </summary>
|
|
|
|
public class EmbeddedImageProvider : IDynamicImageProvider, IHasOrder
|
|
|
|
{
|
2021-10-05 23:13:08 +00:00
|
|
|
private static readonly string[] _primaryImageFileNames =
|
|
|
|
{
|
|
|
|
"poster",
|
|
|
|
"folder",
|
|
|
|
"cover",
|
|
|
|
"default"
|
|
|
|
};
|
|
|
|
|
|
|
|
private static readonly string[] _backdropImageFileNames =
|
|
|
|
{
|
|
|
|
"backdrop",
|
|
|
|
"fanart",
|
|
|
|
"background",
|
|
|
|
"art"
|
|
|
|
};
|
|
|
|
|
|
|
|
private static readonly string[] _logoImageFileNames =
|
|
|
|
{
|
|
|
|
"logo",
|
|
|
|
};
|
|
|
|
|
2021-10-03 19:28:21 +00:00
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
2021-10-11 10:34:18 +00:00
|
|
|
public EmbeddedImageProvider(IMediaEncoder mediaEncoder)
|
2021-10-03 19:28:21 +00:00
|
|
|
{
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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 23:13:08 +00:00
|
|
|
if (item is Video)
|
|
|
|
{
|
|
|
|
if (item is Episode)
|
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
|
|
|
ImageType.Primary,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
|
|
|
ImageType.Primary,
|
|
|
|
ImageType.Backdrop,
|
|
|
|
ImageType.Logo,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-11 10:34:18 +00:00
|
|
|
return new List<ImageType>();
|
2021-10-03 19:28:21 +00: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 23:13:08 +00:00
|
|
|
return GetEmbeddedImage(video, type, cancellationToken);
|
2021-10-03 19:28:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 23:13:08 +00:00
|
|
|
private async Task<DynamicImageResponse> GetEmbeddedImage(Video item, ImageType type, CancellationToken cancellationToken)
|
2021-10-03 19:28:21 +00:00
|
|
|
{
|
|
|
|
MediaSourceInfo mediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
VideoType = item.VideoType,
|
|
|
|
IsoType = item.IsoType,
|
|
|
|
Protocol = item.PathProtocol ?? MediaProtocol.File,
|
|
|
|
};
|
|
|
|
|
2021-10-07 20:37:59 +00:00
|
|
|
string[] imageFileNames = type switch
|
2021-10-05 23:13:08 +00:00
|
|
|
{
|
2021-10-07 20:37:59 +00:00
|
|
|
ImageType.Primary => _primaryImageFileNames,
|
|
|
|
ImageType.Backdrop => _backdropImageFileNames,
|
|
|
|
ImageType.Logo => _logoImageFileNames,
|
|
|
|
_ => _primaryImageFileNames
|
|
|
|
};
|
|
|
|
|
|
|
|
// Try attachments first
|
|
|
|
var attachmentSources = item.GetMediaSources(false).SelectMany(source => source.MediaAttachments).ToList();
|
|
|
|
var attachmentStream = attachmentSources
|
2021-10-11 10:34:18 +00:00
|
|
|
.Where(attachment => !string.IsNullOrEmpty(attachment.FileName))
|
|
|
|
.FirstOrDefault(attachment => imageFileNames.Any(name => attachment.FileName.Contains(name, StringComparison.OrdinalIgnoreCase)));
|
2021-10-07 20:37:59 +00:00
|
|
|
|
|
|
|
if (attachmentStream != null)
|
|
|
|
{
|
2021-10-11 10:34:18 +00:00
|
|
|
var extension = string.IsNullOrEmpty(attachmentStream.MimeType) ?
|
2021-10-07 20:37:59 +00:00
|
|
|
Path.GetExtension(attachmentStream.FileName) :
|
2021-10-11 10:34:18 +00:00
|
|
|
MimeTypes.ToExtension(attachmentStream.MimeType);
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(extension))
|
|
|
|
{
|
|
|
|
extension = ".jpg";
|
|
|
|
}
|
2021-10-07 20:37:59 +00:00
|
|
|
|
|
|
|
string extractedAttachmentPath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, extension, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
ImageFormat format = extension switch
|
|
|
|
{
|
2021-10-11 10:34:18 +00:00
|
|
|
".bmp" => ImageFormat.Bmp,
|
|
|
|
".gif" => ImageFormat.Gif,
|
|
|
|
".jpg" => ImageFormat.Jpg,
|
|
|
|
".png" => ImageFormat.Png,
|
|
|
|
".webp" => ImageFormat.Webp,
|
2021-10-07 20:37:59 +00:00
|
|
|
_ => ImageFormat.Jpg
|
|
|
|
};
|
|
|
|
|
|
|
|
return new DynamicImageResponse
|
|
|
|
{
|
|
|
|
Format = format,
|
|
|
|
HasImage = true,
|
|
|
|
Path = extractedAttachmentPath,
|
|
|
|
Protocol = MediaProtocol.File
|
|
|
|
};
|
2021-10-05 23:13:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 20:37:59 +00:00
|
|
|
// Fall back to EmbeddedImage streams
|
|
|
|
var imageStreams = item.GetMediaStreams().FindAll(i => i.Type == MediaStreamType.EmbeddedImage);
|
2021-10-03 19:28:21 +00:00
|
|
|
|
2021-10-05 23:13:08 +00:00
|
|
|
if (!imageStreams.Any())
|
2021-10-03 19:28:21 +00:00
|
|
|
{
|
|
|
|
// Can't extract if we don't have any EmbeddedImage streams
|
|
|
|
return new DynamicImageResponse { HasImage = false };
|
|
|
|
}
|
|
|
|
|
2021-10-05 23:13:08 +00:00
|
|
|
// Extract first stream containing an element of imageFileNames
|
|
|
|
var imageStream = imageStreams
|
|
|
|
.Where(stream => !string.IsNullOrEmpty(stream.Comment))
|
2021-10-11 10:34:18 +00:00
|
|
|
.FirstOrDefault(stream => imageFileNames.Any(name => stream.Comment.Contains(name, StringComparison.OrdinalIgnoreCase)));
|
2021-10-05 23:13:08 +00: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 19:28:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 20:37:59 +00:00
|
|
|
string extractedImagePath = await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, "jpg", cancellationToken).ConfigureAwait(false);
|
2021-10-05 23:13:08 +00:00
|
|
|
|
2021-10-03 19:28:21 +00:00
|
|
|
return new DynamicImageResponse
|
|
|
|
{
|
|
|
|
Format = ImageFormat.Jpg,
|
|
|
|
HasImage = true,
|
|
|
|
Path = extractedImagePath,
|
|
|
|
Protocol = MediaProtocol.File
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|