diff --git a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs
index 7d62fb6e13..1418e583e7 100644
--- a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs
+++ b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs
@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Dlna;
+using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
@@ -95,10 +96,10 @@ namespace MediaBrowser.Controller.MediaEncoding
/// Media source information.
/// Media stream information.
/// Index of the stream to extract from.
- /// The extension of the file to write, including the '.'.
+ /// The format of the file to write.
/// CancellationToken to use for operation.
/// Location of video image.
- Task ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, string outputExtension, CancellationToken cancellationToken);
+ Task ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, ImageFormat? targetFormat, CancellationToken cancellationToken);
///
/// Gets the media info.
diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
index a2bac7b49a..1c97a19828 100644
--- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
@@ -19,6 +19,7 @@ using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.MediaEncoding.Probing;
using MediaBrowser.Model.Dlna;
+using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
@@ -478,17 +479,17 @@ namespace MediaBrowser.MediaEncoding.Encoder
Protocol = MediaProtocol.File
};
- return ExtractImage(path, null, null, imageStreamIndex, mediaSource, true, null, null, ".jpg", cancellationToken);
+ return ExtractImage(path, null, null, imageStreamIndex, mediaSource, true, null, null, ImageFormat.Jpg, cancellationToken);
}
public Task ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream videoStream, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken)
{
- return ExtractImage(inputFile, container, videoStream, null, mediaSource, false, threedFormat, offset, ".jpg", cancellationToken);
+ return ExtractImage(inputFile, container, videoStream, null, mediaSource, false, threedFormat, offset, ImageFormat.Jpg, cancellationToken);
}
- public Task ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, string outputExtension, CancellationToken cancellationToken)
+ public Task ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, ImageFormat? targetFormat, CancellationToken cancellationToken)
{
- return ExtractImage(inputFile, container, imageStream, imageStreamIndex, mediaSource, false, null, null, outputExtension, cancellationToken);
+ return ExtractImage(inputFile, container, imageStream, imageStreamIndex, mediaSource, false, null, null, targetFormat, cancellationToken);
}
private async Task ExtractImage(
@@ -500,7 +501,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
bool isAudio,
Video3DFormat? threedFormat,
TimeSpan? offset,
- string outputExtension,
+ ImageFormat? targetFormat,
CancellationToken cancellationToken)
{
var inputArgument = GetInputArgument(inputFile, mediaSource);
@@ -510,7 +511,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
// The failure of HDR extraction usually occurs when using custom ffmpeg that does not contain the zscale filter.
try
{
- return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, true, outputExtension, cancellationToken).ConfigureAwait(false);
+ return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, true, targetFormat, cancellationToken).ConfigureAwait(false);
}
catch (ArgumentException)
{
@@ -523,7 +524,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
try
{
- return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, true, outputExtension, cancellationToken).ConfigureAwait(false);
+ return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, true, targetFormat, cancellationToken).ConfigureAwait(false);
}
catch (ArgumentException)
{
@@ -536,7 +537,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
try
{
- return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, false, outputExtension, cancellationToken).ConfigureAwait(false);
+ return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, false, targetFormat, cancellationToken).ConfigureAwait(false);
}
catch (ArgumentException)
{
@@ -548,24 +549,25 @@ namespace MediaBrowser.MediaEncoding.Encoder
}
}
- return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, false, outputExtension, cancellationToken).ConfigureAwait(false);
+ return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, false, targetFormat, cancellationToken).ConfigureAwait(false);
}
- private async Task ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int? imageStreamIndex, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, bool allowTonemap, string outputExtension, CancellationToken cancellationToken)
+ private async Task ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int? imageStreamIndex, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, bool allowTonemap, ImageFormat? targetFormat, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(inputPath))
{
throw new ArgumentNullException(nameof(inputPath));
}
- if (string.IsNullOrEmpty(outputExtension))
+ var outputExtension = targetFormat switch
{
- outputExtension = ".jpg";
- }
- else if (outputExtension[0] != '.')
- {
- outputExtension = "." + outputExtension;
- }
+ ImageFormat.Bmp => ".bmp",
+ ImageFormat.Gif => ".gif",
+ ImageFormat.Jpg => ".jpg",
+ ImageFormat.Png => ".png",
+ ImageFormat.Webp => ".webp",
+ _ => ".jpg"
+ };
var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + outputExtension);
Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));
diff --git a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
index 9279cb220a..32ff1dee6b 100644
--- a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
+++ b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
@@ -721,15 +721,13 @@ namespace MediaBrowser.MediaEncoding.Probing
}
else if (string.Equals(streamInfo.CodecType, "video", StringComparison.OrdinalIgnoreCase))
{
- stream.Type = isAudio || string.Equals(stream.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase) || string.Equals(stream.Codec, "gif", StringComparison.OrdinalIgnoreCase) || string.Equals(stream.Codec, "png", StringComparison.OrdinalIgnoreCase)
- ? MediaStreamType.EmbeddedImage
- : MediaStreamType.Video;
-
stream.AverageFrameRate = GetFrameRate(streamInfo.AverageFrameRate);
stream.RealFrameRate = GetFrameRate(streamInfo.RFrameRate);
- if (isAudio || string.Equals(stream.Codec, "gif", StringComparison.OrdinalIgnoreCase) ||
- string.Equals(stream.Codec, "png", StringComparison.OrdinalIgnoreCase))
+ if (isAudio
+ || string.Equals(stream.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)
+ || string.Equals(stream.Codec, "gif", StringComparison.OrdinalIgnoreCase)
+ || string.Equals(stream.Codec, "png", StringComparison.OrdinalIgnoreCase))
{
stream.Type = MediaStreamType.EmbeddedImage;
}
diff --git a/MediaBrowser.Providers/MediaInfo/EmbeddedImageProvider.cs b/MediaBrowser.Providers/MediaInfo/EmbeddedImageProvider.cs
index 9b63971a9c..186e55f1dc 100644
--- a/MediaBrowser.Providers/MediaInfo/EmbeddedImageProvider.cs
+++ b/MediaBrowser.Providers/MediaInfo/EmbeddedImageProvider.cs
@@ -15,6 +15,7 @@ using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Net;
+using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.MediaInfo
{
@@ -45,14 +46,17 @@ namespace MediaBrowser.Providers.MediaInfo
};
private readonly IMediaEncoder _mediaEncoder;
+ private readonly ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// The media encoder for extracting attached/embedded images.
- public EmbeddedImageProvider(IMediaEncoder mediaEncoder)
+ /// The logger.
+ public EmbeddedImageProvider(IMediaEncoder mediaEncoder, ILogger logger)
{
_mediaEncoder = mediaEncoder;
+ _logger = logger;
}
///
@@ -114,9 +118,15 @@ namespace MediaBrowser.Providers.MediaInfo
ImageType.Primary => _primaryImageFileNames,
ImageType.Backdrop => _backdropImageFileNames,
ImageType.Logo => _logoImageFileNames,
- _ => _primaryImageFileNames
+ _ => Array.Empty()
};
+ if (imageFileNames.Length == 0)
+ {
+ _logger.LogWarning("Attempted to load unexpected image type: {Type}", type);
+ return new DynamicImageResponse { HasImage = false };
+ }
+
// Try attachments first
var attachmentStream = item.GetMediaSources(false)
.SelectMany(source => source.MediaAttachments)
@@ -156,13 +166,21 @@ namespace MediaBrowser.Providers.MediaInfo
}
}
+ 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, ".jpg", cancellationToken)
+ await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, format, cancellationToken)
.ConfigureAwait(false);
return new DynamicImageResponse
{
- Format = ImageFormat.Jpg,
+ Format = format,
HasImage = true,
Path = extractedImagePath,
Protocol = MediaProtocol.File
@@ -180,10 +198,6 @@ namespace MediaBrowser.Providers.MediaInfo
extension = ".jpg";
}
- string extractedAttachmentPath =
- await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, extension, cancellationToken)
- .ConfigureAwait(false);
-
ImageFormat format = extension switch
{
".bmp" => ImageFormat.Bmp,
@@ -194,6 +208,10 @@ namespace MediaBrowser.Providers.MediaInfo
_ => ImageFormat.Jpg
};
+ string extractedAttachmentPath =
+ await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, format, cancellationToken)
+ .ConfigureAwait(false);
+
return new DynamicImageResponse
{
Format = format,
diff --git a/tests/Jellyfin.Providers.Tests/MediaInfo/EmbeddedImageProviderTests.cs b/tests/Jellyfin.Providers.Tests/MediaInfo/EmbeddedImageProviderTests.cs
index b194e38855..38eac28a22 100644
--- a/tests/Jellyfin.Providers.Tests/MediaInfo/EmbeddedImageProviderTests.cs
+++ b/tests/Jellyfin.Providers.Tests/MediaInfo/EmbeddedImageProviderTests.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@@ -10,6 +11,7 @@ using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Providers.MediaInfo;
+using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
@@ -17,47 +19,25 @@ namespace Jellyfin.Providers.Tests.MediaInfo
{
public class EmbeddedImageProviderTests
{
- private static TheoryData GetSupportedImages_UnsupportedBaseItems_ReturnsEmpty_TestData()
- {
- return new ()
- {
- new AudioBook(),
- new BoxSet(),
- new Series(),
- new Season(),
- };
- }
-
[Theory]
- [MemberData(nameof(GetSupportedImages_UnsupportedBaseItems_ReturnsEmpty_TestData))]
- public void GetSupportedImages_UnsupportedBaseItems_ReturnsEmpty(BaseItem item)
+ [InlineData(typeof(AudioBook))]
+ [InlineData(typeof(BoxSet))]
+ [InlineData(typeof(Series))]
+ [InlineData(typeof(Season))]
+ [InlineData(typeof(Episode), ImageType.Primary)]
+ [InlineData(typeof(Movie), ImageType.Logo, ImageType.Backdrop, ImageType.Primary)]
+ public void GetSupportedImages_AnyBaseItem_ReturnsExpected(Type type, params ImageType[] expected)
{
- var embeddedImageProvider = GetEmbeddedImageProvider(null);
- Assert.Empty(embeddedImageProvider.GetSupportedImages(item));
- }
-
- private static TheoryData> GetSupportedImages_SupportedBaseItems_ReturnsPopulated_TestData()
- {
- return new TheoryData>
- {
- { new Episode(), new List { ImageType.Primary } },
- { new Movie(), new List { ImageType.Logo, ImageType.Backdrop, ImageType.Primary } },
- };
- }
-
- [Theory]
- [MemberData(nameof(GetSupportedImages_SupportedBaseItems_ReturnsPopulated_TestData))]
- public void GetSupportedImages_SupportedBaseItems_ReturnsPopulated(BaseItem item, IEnumerable expected)
- {
- var embeddedImageProvider = GetEmbeddedImageProvider(null);
+ BaseItem item = (BaseItem)Activator.CreateInstance(type)!;
+ var embeddedImageProvider = new EmbeddedImageProvider(Mock.Of(), new NullLogger());
var actual = embeddedImageProvider.GetSupportedImages(item);
Assert.Equal(expected.OrderBy(i => i.ToString()), actual.OrderBy(i => i.ToString()));
}
[Fact]
- public async void GetImage_InputWithNoStreams_ReturnsNoImage()
+ public async void GetImage_NoStreams_ReturnsNoImage()
{
- var embeddedImageProvider = GetEmbeddedImageProvider(null);
+ var embeddedImageProvider = new EmbeddedImageProvider(null, new NullLogger());
var input = GetMovie(new List(), new List());
@@ -66,136 +46,96 @@ namespace Jellyfin.Providers.Tests.MediaInfo
Assert.False(actual.HasImage);
}
- [Fact]
- public async void GetImage_InputWithUnlabeledAttachments_ReturnsNoImage()
+ [Theory]
+ [InlineData("chapter", null, 1, ImageType.Chapter, null)] // unexpected type, nothing found
+ [InlineData("unmatched", null, 1, ImageType.Primary, null)] // doesn't default on no match
+ [InlineData("clearlogo.png", null, 1, ImageType.Logo, ImageFormat.Png)] // extract extension from name
+ [InlineData("backdrop", "image/bmp", 2, ImageType.Backdrop, ImageFormat.Bmp)] // extract extension from mimetype
+ [InlineData("poster", null, 3, ImageType.Primary, ImageFormat.Jpg)] // default extension to jpg
+ public async void GetImage_Attachment_ReturnsCorrectSelection(string filename, string mimetype, int targetIndex, ImageType type, ImageFormat? expectedFormat)
{
- var embeddedImageProvider = GetEmbeddedImageProvider(null);
-
- // add an attachment without a filename - has a list to look through but finds nothing
- var input = GetMovie(
- new List { new () },
- new List());
-
- var actual = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
- Assert.NotNull(actual);
- Assert.False(actual.HasImage);
- }
-
- [Fact]
- public async void GetImage_InputWithLabeledAttachments_ReturnsCorrectSelection()
- {
- // first tests file extension detection, second uses mimetype, third defaults to jpg
- MediaAttachment sampleAttachment1 = new () { FileName = "clearlogo.png", Index = 1 };
- MediaAttachment sampleAttachment2 = new () { FileName = "backdrop", MimeType = "image/bmp", Index = 2 };
- MediaAttachment sampleAttachment3 = new () { FileName = "poster", Index = 3 };
- string targetPath1 = "path1.png";
- string targetPath2 = "path2.bmp";
- string targetPath3 = "path2.jpg";
+ var attachments = new List();
+ string pathPrefix = "path";
+ for (int i = 1; i <= targetIndex; i++)
+ {
+ var name = i == targetIndex ? filename : "unmatched";
+ attachments.Add(new ()
+ {
+ FileName = name,
+ MimeType = mimetype,
+ Index = i
+ });
+ }
var mediaEncoder = new Mock(MockBehavior.Strict);
- mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), 1, ".png", CancellationToken.None))
- .Returns(Task.FromResult(targetPath1));
- mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), 2, ".bmp", CancellationToken.None))
- .Returns(Task.FromResult(targetPath2));
- mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), 3, ".jpg", CancellationToken.None))
- .Returns(Task.FromResult(targetPath3));
- var embeddedImageProvider = GetEmbeddedImageProvider(mediaEncoder.Object);
+ mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()))
+ .Returns((_, _, _, _, index, ext, _) => Task.FromResult(pathPrefix + index + "." + ext));
+ var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object, new NullLogger());
- var input = GetMovie(
- new List { sampleAttachment1, sampleAttachment2, sampleAttachment3 },
- new List());
+ var input = GetMovie(attachments, new List());
- var actualLogo = await embeddedImageProvider.GetImage(input, ImageType.Logo, CancellationToken.None);
- Assert.NotNull(actualLogo);
- Assert.True(actualLogo.HasImage);
- Assert.Equal(targetPath1, actualLogo.Path);
- Assert.Equal(ImageFormat.Png, actualLogo.Format);
-
- var actualBackdrop = await embeddedImageProvider.GetImage(input, ImageType.Backdrop, CancellationToken.None);
- Assert.NotNull(actualBackdrop);
- Assert.True(actualBackdrop.HasImage);
- Assert.Equal(targetPath2, actualBackdrop.Path);
- Assert.Equal(ImageFormat.Bmp, actualBackdrop.Format);
-
- var actualPrimary = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
- Assert.NotNull(actualPrimary);
- Assert.True(actualPrimary.HasImage);
- Assert.Equal(targetPath3, actualPrimary.Path);
- Assert.Equal(ImageFormat.Jpg, actualPrimary.Format);
- }
-
- [Fact]
- public async void GetImage_InputWithUnlabeledEmbeddedImages_BackdropReturnsNoImage()
- {
- var embeddedImageProvider = GetEmbeddedImageProvider(null);
-
- var input = GetMovie(
- new List(),
- new List { new () { Type = MediaStreamType.EmbeddedImage } });
-
- var actual = await embeddedImageProvider.GetImage(input, ImageType.Backdrop, CancellationToken.None);
+ var actual = await embeddedImageProvider.GetImage(input, type, CancellationToken.None);
Assert.NotNull(actual);
- Assert.False(actual.HasImage);
+ if (expectedFormat == null)
+ {
+ Assert.False(actual.HasImage);
+ }
+ else
+ {
+ Assert.True(actual.HasImage);
+ Assert.Equal(pathPrefix + targetIndex + "." + expectedFormat, actual.Path, StringComparer.OrdinalIgnoreCase);
+ Assert.Equal(expectedFormat, actual.Format);
+ }
}
- [Fact]
- public async void GetImage_InputWithUnlabeledEmbeddedImages_PrimaryReturnsImage()
+ [Theory]
+ [InlineData("chapter", null, 1, ImageType.Chapter, null)] // unexpected type, nothing found
+ [InlineData(null, null, 1, ImageType.Backdrop, null)] // no label, can only find primary
+ [InlineData(null, null, 1, ImageType.Primary, ImageFormat.Jpg)] // no label, finds primary
+ [InlineData("backdrop", null, 2, ImageType.Backdrop, ImageFormat.Jpg)] // uses label to find index 2, not just pulling first stream
+ [InlineData("cover", null, 2, ImageType.Primary, ImageFormat.Jpg)] // uses label to find index 2, not just pulling first stream
+ [InlineData(null, "mjpeg", 1, ImageType.Primary, ImageFormat.Jpg)]
+ [InlineData(null, "png", 1, ImageType.Primary, ImageFormat.Png)]
+ [InlineData(null, "gif", 1, ImageType.Primary, ImageFormat.Gif)]
+ public async void GetImage_Embedded_ReturnsCorrectSelection(string label, string? codec, int targetIndex, ImageType type, ImageFormat? expectedFormat)
{
- MediaStream sampleStream = new () { Type = MediaStreamType.EmbeddedImage, Index = 1 };
- string targetPath = "path";
+ var streams = new List();
+ for (int i = 1; i <= targetIndex; i++)
+ {
+ var comment = i == targetIndex ? label : "unmatched";
+ streams.Add(new ()
+ {
+ Type = MediaStreamType.EmbeddedImage,
+ Index = i,
+ Comment = comment,
+ Codec = codec
+ });
+ }
+ var pathPrefix = "path";
var mediaEncoder = new Mock(MockBehavior.Strict);
- mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny(), It.IsAny(), It.IsAny(), sampleStream, 1, ".jpg", CancellationToken.None))
- .Returns(Task.FromResult(targetPath));
- var embeddedImageProvider = GetEmbeddedImageProvider(mediaEncoder.Object);
+ mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()))
+ .Returns((_, _, _, stream, index, ext, _) =>
+ {
+ Assert.Equal(streams[index - 1], stream);
+ return Task.FromResult(pathPrefix + index + "." + ext);
+ });
+ var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object, new NullLogger());
- var input = GetMovie(
- new List(),
- new List { sampleStream });
+ var input = GetMovie(new List(), streams);
- var actual = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
+ var actual = await embeddedImageProvider.GetImage(input, type, CancellationToken.None);
Assert.NotNull(actual);
- Assert.True(actual.HasImage);
- Assert.Equal(targetPath, actual.Path);
- Assert.Equal(ImageFormat.Jpg, actual.Format);
- }
-
- [Fact]
- public async void GetImage_InputWithLabeledEmbeddedImages_ReturnsCorrectSelection()
- {
- // primary is second stream to ensure it's not defaulting, backdrop is first
- MediaStream sampleStream1 = new () { Type = MediaStreamType.EmbeddedImage, Index = 1, Comment = "backdrop" };
- MediaStream sampleStream2 = new () { Type = MediaStreamType.EmbeddedImage, Index = 2, Comment = "cover" };
- string targetPath1 = "path1.jpg";
- string targetPath2 = "path2.jpg";
-
- var mediaEncoder = new Mock(MockBehavior.Strict);
- mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny(), It.IsAny(), It.IsAny(), sampleStream1, 1, ".jpg", CancellationToken.None))
- .Returns(Task.FromResult(targetPath1));
- mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny(), It.IsAny(), It.IsAny(), sampleStream2, 2, ".jpg", CancellationToken.None))
- .Returns(Task.FromResult(targetPath2));
- var embeddedImageProvider = GetEmbeddedImageProvider(mediaEncoder.Object);
-
- var input = GetMovie(
- new List(),
- new List { sampleStream1, sampleStream2 });
-
- var actualPrimary = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
- Assert.NotNull(actualPrimary);
- Assert.True(actualPrimary.HasImage);
- Assert.Equal(targetPath2, actualPrimary.Path);
- Assert.Equal(ImageFormat.Jpg, actualPrimary.Format);
-
- var actualBackdrop = await embeddedImageProvider.GetImage(input, ImageType.Backdrop, CancellationToken.None);
- Assert.NotNull(actualBackdrop);
- Assert.True(actualBackdrop.HasImage);
- Assert.Equal(targetPath1, actualBackdrop.Path);
- Assert.Equal(ImageFormat.Jpg, actualBackdrop.Format);
- }
-
- private static EmbeddedImageProvider GetEmbeddedImageProvider(IMediaEncoder? mediaEncoder)
- {
- return new EmbeddedImageProvider(mediaEncoder);
+ if (expectedFormat == null)
+ {
+ Assert.False(actual.HasImage);
+ }
+ else
+ {
+ Assert.True(actual.HasImage);
+ Assert.Equal(pathPrefix + targetIndex + "." + expectedFormat, actual.Path, StringComparer.OrdinalIgnoreCase);
+ Assert.Equal(expectedFormat, actual.Format);
+ }
}
private static Movie GetMovie(List mediaAttachments, List mediaStreams)