Merge pull request #6436 from daullmer/splashscreen
commit
e5701c396a
@ -0,0 +1,79 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Jellyfin.Data.Enums;
|
||||||
|
using MediaBrowser.Controller.Drawing;
|
||||||
|
using MediaBrowser.Controller.Dto;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
|
using MediaBrowser.Controller.Library;
|
||||||
|
using MediaBrowser.Controller.Persistence;
|
||||||
|
using MediaBrowser.Model.Entities;
|
||||||
|
using MediaBrowser.Model.Querying;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Emby.Server.Implementations.Library;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The splashscreen post scan task.
|
||||||
|
/// </summary>
|
||||||
|
public class SplashscreenPostScanTask : ILibraryPostScanTask
|
||||||
|
{
|
||||||
|
private readonly IItemRepository _itemRepository;
|
||||||
|
private readonly IImageEncoder _imageEncoder;
|
||||||
|
private readonly ILogger<SplashscreenPostScanTask> _logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="SplashscreenPostScanTask"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="itemRepository">Instance of the <see cref="IItemRepository"/> interface.</param>
|
||||||
|
/// <param name="imageEncoder">Instance of the <see cref="IImageEncoder"/> interface.</param>
|
||||||
|
/// <param name="logger">Instance of the <see cref="ILogger{SplashscreenPostScanTask}"/> interface.</param>
|
||||||
|
public SplashscreenPostScanTask(
|
||||||
|
IItemRepository itemRepository,
|
||||||
|
IImageEncoder imageEncoder,
|
||||||
|
ILogger<SplashscreenPostScanTask> logger)
|
||||||
|
{
|
||||||
|
_itemRepository = itemRepository;
|
||||||
|
_imageEncoder = imageEncoder;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var posters = GetItemsWithImageType(ImageType.Primary).Select(x => x.GetImages(ImageType.Primary).First().Path).ToList();
|
||||||
|
var backdrops = GetItemsWithImageType(ImageType.Thumb).Select(x => x.GetImages(ImageType.Thumb).First().Path).ToList();
|
||||||
|
if (backdrops.Count == 0)
|
||||||
|
{
|
||||||
|
// Thumb images fit better because they include the title in the image but are not provided with TMDb.
|
||||||
|
// Using backdrops as a fallback to generate an image at all
|
||||||
|
_logger.LogDebug("No thumb images found. Using backdrops to generate splashscreen");
|
||||||
|
backdrops = GetItemsWithImageType(ImageType.Backdrop).Select(x => x.GetImages(ImageType.Backdrop).First().Path).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
_imageEncoder.CreateSplashscreen(posters, backdrops);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IReadOnlyList<BaseItem> GetItemsWithImageType(ImageType imageType)
|
||||||
|
{
|
||||||
|
// TODO make included libraries configurable
|
||||||
|
return _itemRepository.GetItemList(new InternalItemsQuery
|
||||||
|
{
|
||||||
|
CollapseBoxSetItems = false,
|
||||||
|
Recursive = true,
|
||||||
|
DtoOptions = new DtoOptions(false),
|
||||||
|
ImageTypes = new[] { imageType },
|
||||||
|
Limit = 30,
|
||||||
|
// TODO max parental rating configurable
|
||||||
|
MaxParentalRating = 10,
|
||||||
|
OrderBy = new[]
|
||||||
|
{
|
||||||
|
(ItemSortBy.Random, SortOrder.Ascending)
|
||||||
|
},
|
||||||
|
IncludeItemTypes = new[] { BaseItemKind.Movie, BaseItemKind.Series }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,148 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using SkiaSharp;
|
||||||
|
|
||||||
|
namespace Jellyfin.Drawing.Skia
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Used to build the splashscreen.
|
||||||
|
/// </summary>
|
||||||
|
public class SplashscreenBuilder
|
||||||
|
{
|
||||||
|
private const int FinalWidth = 1920;
|
||||||
|
private const int FinalHeight = 1080;
|
||||||
|
// generated collage resolution should be higher than the final resolution
|
||||||
|
private const int WallWidth = FinalWidth * 3;
|
||||||
|
private const int WallHeight = FinalHeight * 2;
|
||||||
|
private const int Rows = 6;
|
||||||
|
private const int Spacing = 20;
|
||||||
|
|
||||||
|
private readonly SkiaEncoder _skiaEncoder;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="SplashscreenBuilder"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="skiaEncoder">The SkiaEncoder.</param>
|
||||||
|
public SplashscreenBuilder(SkiaEncoder skiaEncoder)
|
||||||
|
{
|
||||||
|
_skiaEncoder = skiaEncoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generate a splashscreen.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="posters">The poster paths.</param>
|
||||||
|
/// <param name="backdrops">The landscape paths.</param>
|
||||||
|
/// <param name="outputPath">The output path.</param>
|
||||||
|
public void GenerateSplash(IReadOnlyList<string> posters, IReadOnlyList<string> backdrops, string outputPath)
|
||||||
|
{
|
||||||
|
using var wall = GenerateCollage(posters, backdrops);
|
||||||
|
using var transformed = Transform3D(wall);
|
||||||
|
|
||||||
|
using var outputStream = new SKFileWStream(outputPath);
|
||||||
|
using var pixmap = new SKPixmap(new SKImageInfo(FinalWidth, FinalHeight), transformed.GetPixels());
|
||||||
|
pixmap.Encode(outputStream, StripCollageBuilder.GetEncodedFormat(outputPath), 90);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generates a collage of posters and landscape pictures.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="posters">The poster paths.</param>
|
||||||
|
/// <param name="backdrops">The landscape paths.</param>
|
||||||
|
/// <returns>The created collage as a bitmap.</returns>
|
||||||
|
private SKBitmap GenerateCollage(IReadOnlyList<string> posters, IReadOnlyList<string> backdrops)
|
||||||
|
{
|
||||||
|
var posterIndex = 0;
|
||||||
|
var backdropIndex = 0;
|
||||||
|
|
||||||
|
var bitmap = new SKBitmap(WallWidth, WallHeight);
|
||||||
|
using var canvas = new SKCanvas(bitmap);
|
||||||
|
canvas.Clear(SKColors.Black);
|
||||||
|
|
||||||
|
int posterHeight = WallHeight / 6;
|
||||||
|
|
||||||
|
for (int i = 0; i < Rows; i++)
|
||||||
|
{
|
||||||
|
int imageCounter = Random.Shared.Next(0, 5);
|
||||||
|
int currentWidthPos = i * 75;
|
||||||
|
int currentHeight = i * (posterHeight + Spacing);
|
||||||
|
|
||||||
|
while (currentWidthPos < WallWidth)
|
||||||
|
{
|
||||||
|
SKBitmap? currentImage;
|
||||||
|
|
||||||
|
switch (imageCounter)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
case 2:
|
||||||
|
case 3:
|
||||||
|
currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, posters, posterIndex, out int newPosterIndex);
|
||||||
|
posterIndex = newPosterIndex;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
currentImage = SkiaHelper.GetNextValidImage(_skiaEncoder, backdrops, backdropIndex, out int newBackdropIndex);
|
||||||
|
backdropIndex = newBackdropIndex;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentImage == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Not enough valid pictures provided to create a splashscreen!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// resize to the same aspect as the original
|
||||||
|
var imageWidth = Math.Abs(posterHeight * currentImage.Width / currentImage.Height);
|
||||||
|
using var resizedBitmap = new SKBitmap(imageWidth, posterHeight);
|
||||||
|
currentImage.ScalePixels(resizedBitmap, SKFilterQuality.High);
|
||||||
|
|
||||||
|
// draw on canvas
|
||||||
|
canvas.DrawBitmap(resizedBitmap, currentWidthPos, currentHeight);
|
||||||
|
|
||||||
|
currentWidthPos += imageWidth + Spacing;
|
||||||
|
|
||||||
|
currentImage.Dispose();
|
||||||
|
|
||||||
|
if (imageCounter >= 4)
|
||||||
|
{
|
||||||
|
imageCounter = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
imageCounter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Transform the collage in 3D space.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">The bitmap to transform.</param>
|
||||||
|
/// <returns>The transformed image.</returns>
|
||||||
|
private SKBitmap Transform3D(SKBitmap input)
|
||||||
|
{
|
||||||
|
var bitmap = new SKBitmap(FinalWidth, FinalHeight);
|
||||||
|
using var canvas = new SKCanvas(bitmap);
|
||||||
|
canvas.Clear(SKColors.Black);
|
||||||
|
var matrix = new SKMatrix
|
||||||
|
{
|
||||||
|
ScaleX = 0.324108899f,
|
||||||
|
ScaleY = 0.563934922f,
|
||||||
|
SkewX = -0.244337708f,
|
||||||
|
SkewY = 0.0377609022f,
|
||||||
|
TransX = 42.0407715f,
|
||||||
|
TransY = -198.104706f,
|
||||||
|
Persp0 = -9.08959337E-05f,
|
||||||
|
Persp1 = 6.85242048E-05f,
|
||||||
|
Persp2 = 0.988209724f
|
||||||
|
};
|
||||||
|
|
||||||
|
canvas.SetMatrix(matrix);
|
||||||
|
canvas.DrawBitmap(input, 0, 0);
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue