Use ImageFormat instead of string for extension

This commit is contained in:
Joe Rogers 2021-11-12 16:11:15 +01:00
parent de9bf327c6
commit f73a7a6ed8
No known key found for this signature in database
GPG Key ID: 0074AD57B8FDBBB4
4 changed files with 38 additions and 34 deletions

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo; using MediaBrowser.Model.MediaInfo;
@ -95,10 +96,10 @@ namespace MediaBrowser.Controller.MediaEncoding
/// <param name="mediaSource">Media source information.</param> /// <param name="mediaSource">Media source information.</param>
/// <param name="imageStream">Media stream information.</param> /// <param name="imageStream">Media stream information.</param>
/// <param name="imageStreamIndex">Index of the stream to extract from.</param> /// <param name="imageStreamIndex">Index of the stream to extract from.</param>
/// <param name="outputExtension">The extension of the file to write, including the '.'.</param> /// <param name="targetFormat">The format of the file to write.</param>
/// <param name="cancellationToken">CancellationToken to use for operation.</param> /// <param name="cancellationToken">CancellationToken to use for operation.</param>
/// <returns>Location of video image.</returns> /// <returns>Location of video image.</returns>
Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, string outputExtension, CancellationToken cancellationToken); Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, ImageFormat? targetFormat, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Gets the media info. /// Gets the media info.

View File

@ -19,6 +19,7 @@ using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.MediaEncoding.Probing; using MediaBrowser.MediaEncoding.Probing;
using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization; using MediaBrowser.Model.Globalization;
@ -478,17 +479,17 @@ namespace MediaBrowser.MediaEncoding.Encoder
Protocol = MediaProtocol.File 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<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream videoStream, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken) public Task<string> 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<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, string outputExtension, CancellationToken cancellationToken) public Task<string> 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<string> ExtractImage( private async Task<string> ExtractImage(
@ -500,7 +501,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
bool isAudio, bool isAudio,
Video3DFormat? threedFormat, Video3DFormat? threedFormat,
TimeSpan? offset, TimeSpan? offset,
string outputExtension, ImageFormat? targetFormat,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var inputArgument = GetInputArgument(inputFile, mediaSource); 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. // The failure of HDR extraction usually occurs when using custom ffmpeg that does not contain the zscale filter.
try 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) catch (ArgumentException)
{ {
@ -523,7 +524,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
try 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) catch (ArgumentException)
{ {
@ -536,7 +537,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
try 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) 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<string> ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int? imageStreamIndex, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, bool allowTonemap, string outputExtension, CancellationToken cancellationToken) private async Task<string> 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)) if (string.IsNullOrEmpty(inputPath))
{ {
throw new ArgumentNullException(nameof(inputPath)); throw new ArgumentNullException(nameof(inputPath));
} }
if (string.IsNullOrEmpty(outputExtension)) var outputExtension = targetFormat switch
{ {
outputExtension = ".jpg"; ImageFormat.Bmp => ".bmp",
} ImageFormat.Gif => ".gif",
else if (outputExtension[0] != '.') ImageFormat.Jpg => ".jpg",
{ ImageFormat.Png => ".png",
outputExtension = "." + outputExtension; ImageFormat.Webp => ".webp",
} _ => ".jpg"
};
var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + outputExtension); var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + outputExtension);
Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath)); Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));

View File

@ -156,13 +156,14 @@ namespace MediaBrowser.Providers.MediaInfo
} }
} }
var format = ImageFormat.Jpg;
string extractedImagePath = 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); .ConfigureAwait(false);
return new DynamicImageResponse return new DynamicImageResponse
{ {
Format = ImageFormat.Jpg, Format = format,
HasImage = true, HasImage = true,
Path = extractedImagePath, Path = extractedImagePath,
Protocol = MediaProtocol.File Protocol = MediaProtocol.File
@ -180,10 +181,6 @@ namespace MediaBrowser.Providers.MediaInfo
extension = ".jpg"; extension = ".jpg";
} }
string extractedAttachmentPath =
await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, extension, cancellationToken)
.ConfigureAwait(false);
ImageFormat format = extension switch ImageFormat format = extension switch
{ {
".bmp" => ImageFormat.Bmp, ".bmp" => ImageFormat.Bmp,
@ -194,6 +191,10 @@ namespace MediaBrowser.Providers.MediaInfo
_ => ImageFormat.Jpg _ => ImageFormat.Jpg
}; };
string extractedAttachmentPath =
await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, format, cancellationToken)
.ConfigureAwait(false);
return new DynamicImageResponse return new DynamicImageResponse
{ {
Format = format, Format = format,

View File

@ -57,7 +57,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
for (int i = 1; i <= targetIndex; i++) for (int i = 1; i <= targetIndex; i++)
{ {
var name = i == targetIndex ? filename : "unmatched"; var name = i == targetIndex ? filename : "unmatched";
attachments.Add(new() attachments.Add(new ()
{ {
FileName = name, FileName = name,
MimeType = mimetype, MimeType = mimetype,
@ -66,8 +66,8 @@ namespace Jellyfin.Providers.Tests.MediaInfo
} }
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict); var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<ImageFormat>(), It.IsAny<CancellationToken>()))
.Returns<string, string, MediaSourceInfo, MediaStream, int, string, CancellationToken>((_, _, _, _, index, ext, _) => Task.FromResult(pathPrefix + index + ext)); .Returns<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, _, index, ext, _) => Task.FromResult(pathPrefix + index + "." + ext));
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object); var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object);
var input = GetMovie(attachments, new List<MediaStream>()); var input = GetMovie(attachments, new List<MediaStream>());
@ -81,7 +81,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
else else
{ {
Assert.True(actual.HasImage); Assert.True(actual.HasImage);
Assert.Equal(pathPrefix + targetIndex + "." + format, actual.Path, StringComparer.InvariantCultureIgnoreCase); Assert.Equal(pathPrefix + targetIndex + "." + format, actual.Path, StringComparer.OrdinalIgnoreCase);
Assert.Equal(format, actual.Format); Assert.Equal(format, actual.Format);
} }
} }
@ -97,7 +97,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
for (int i = 1; i <= targetIndex; i++) for (int i = 1; i <= targetIndex; i++)
{ {
var comment = i == targetIndex ? label : "unmatched"; var comment = i == targetIndex ? label : "unmatched";
streams.Add(new() streams.Add(new ()
{ {
Type = MediaStreamType.EmbeddedImage, Type = MediaStreamType.EmbeddedImage,
Index = i, Index = i,
@ -107,11 +107,11 @@ namespace Jellyfin.Providers.Tests.MediaInfo
var pathPrefix = "path"; var pathPrefix = "path";
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict); var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<string>(), It.IsAny<CancellationToken>())) mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<ImageFormat>(), It.IsAny<CancellationToken>()))
.Returns<string, string, MediaSourceInfo, MediaStream, int, string, CancellationToken>((_, _, _, stream, index, ext, _) => .Returns<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, stream, index, ext, _) =>
{ {
Assert.Equal(streams[index - 1], stream); Assert.Equal(streams[index - 1], stream);
return Task.FromResult(pathPrefix + index + ext); return Task.FromResult(pathPrefix + index + "." + ext);
}); });
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object); var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object);
@ -122,7 +122,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
Assert.Equal(hasImage, actual.HasImage); Assert.Equal(hasImage, actual.HasImage);
if (hasImage) if (hasImage)
{ {
Assert.Equal(pathPrefix + targetIndex + ".jpg", actual.Path); Assert.Equal(pathPrefix + targetIndex + ".jpg", actual.Path, StringComparer.OrdinalIgnoreCase);
Assert.Equal(ImageFormat.Jpg, actual.Format); Assert.Equal(ImageFormat.Jpg, actual.Format);
} }
} }