diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index 63fa5f1f5e..8f3042e2ad 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -18,6 +18,7 @@ using System.Threading.Tasks; using MediaBrowser.Model.IO; using Emby.Drawing.Common; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Model.Net; using MediaBrowser.Model.Threading; using MediaBrowser.Model.Extensions; @@ -56,22 +57,24 @@ namespace Emby.Drawing private readonly IServerApplicationPaths _appPaths; private IImageEncoder _imageEncoder; private readonly Func _libraryManager; + private readonly Func _mediaEncoder; public ImageProcessor(ILogger logger, IServerApplicationPaths appPaths, IFileSystem fileSystem, IJsonSerializer jsonSerializer, IImageEncoder imageEncoder, - Func libraryManager, ITimerFactory timerFactory) + Func libraryManager, ITimerFactory timerFactory, Func mediaEncoder) { _logger = logger; _fileSystem = fileSystem; _jsonSerializer = jsonSerializer; _imageEncoder = imageEncoder; _libraryManager = libraryManager; + _mediaEncoder = mediaEncoder; _appPaths = appPaths; - ImageEnhancers = new IImageEnhancer[] {}; + ImageEnhancers = new IImageEnhancer[] { }; _saveImageSizeTimer = timerFactory.Create(SaveImageSizeCallback, null, Timeout.Infinite, Timeout.Infinite); ImageHelper.ImageProcessor = this; @@ -120,7 +123,36 @@ namespace Emby.Drawing { get { - return _imageEncoder.SupportedInputFormats; + return new string[] + { + "tiff", + "jpeg", + "jpg", + "png", + "aiff", + "cr2", + "crw", + "dng", + + // Remove until supported + //"nef", + "orf", + "pef", + "arw", + "webp", + "gif", + "bmp", + "erf", + "raf", + "rw2", + "nrw", + "dng", + "ico", + "astc", + "ktx", + "pkm", + "wbmp" + }; } } @@ -203,6 +235,10 @@ namespace Emby.Drawing return new Tuple(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified); } + var supportedImageInfo = await GetSupportedImage(originalImagePath, dateModified).ConfigureAwait(false); + originalImagePath = supportedImageInfo.Item1; + dateModified = supportedImageInfo.Item2; + if (options.Enhancers.Count > 0) { if (item == null) @@ -663,6 +699,42 @@ namespace Emby.Drawing return string.Join("|", cacheKeys.ToArray(cacheKeys.Count)).GetMD5().ToString("N"); } + private async Task> GetSupportedImage(string originalImagePath, DateTime dateModified) + { + var inputFormat = (Path.GetExtension(originalImagePath) ?? string.Empty) + .TrimStart('.') + .Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase); + + if (!_imageEncoder.SupportedInputFormats.Contains(inputFormat, StringComparer.OrdinalIgnoreCase)) + { + try + { + var filename = (originalImagePath + dateModified.Ticks.ToString(UsCulture)).GetMD5().ToString("N"); + + var outputPath = Path.Combine(_appPaths.ImageCachePath, "converted-images", filename + ".webp"); + + var file = _fileSystem.GetFileInfo(outputPath); + if (!file.Exists) + { + await _mediaEncoder().ConvertImage(originalImagePath, outputPath).ConfigureAwait(false); + dateModified = _fileSystem.GetLastWriteTimeUtc(outputPath); + } + else + { + dateModified = file.LastWriteTimeUtc; + } + + originalImagePath = outputPath; + } + catch (Exception ex) + { + _logger.ErrorException("Image conversion failed for {0}", ex, originalImagePath); + } + } + + return new Tuple(originalImagePath, dateModified); + } + /// /// Gets the enhanced image. /// diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 6441fe4f2d..aaca22fe9b 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -1202,7 +1202,7 @@ namespace Emby.Server.Implementations private IImageProcessor GetImageProcessor() { - return new ImageProcessor(LogManager.GetLogger("ImageProcessor"), ServerConfigurationManager.ApplicationPaths, FileSystemManager, JsonSerializer, ImageEncoder, () => LibraryManager, TimerFactory); + return new ImageProcessor(LogManager.GetLogger("ImageProcessor"), ServerConfigurationManager.ApplicationPaths, FileSystemManager, JsonSerializer, ImageEncoder, () => LibraryManager, TimerFactory, () => MediaEncoder); } protected virtual FFMpegInstallInfo GetFfmpegInstallInfo() diff --git a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs index 05bb35771e..803b189d4c 100644 --- a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs +++ b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs @@ -102,6 +102,8 @@ namespace MediaBrowser.Controller.MediaEncoding IProgress progress, CancellationToken cancellationToken); + Task ConvertImage(string inputPath, string outputPath); + /// /// Escapes the subtitle filter path. ///