mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-07-08 23:00:51 +02:00
added image encoder methods
This commit is contained in:
parent
eda0939b90
commit
1664de62c0
|
@ -13,6 +13,8 @@ namespace MediaBrowser.Controller.MediaEncoding
|
||||||
|
|
||||||
public int? MaxHeight { get; set; }
|
public int? MaxHeight { get; set; }
|
||||||
|
|
||||||
|
public int? Quality { get; set; }
|
||||||
|
|
||||||
public string Format { get; set; }
|
public string Format { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using MediaBrowser.Controller.MediaEncoding;
|
using MediaBrowser.Common.IO;
|
||||||
|
using MediaBrowser.Controller.MediaEncoding;
|
||||||
using MediaBrowser.Model.Logging;
|
using MediaBrowser.Model.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
@ -13,20 +14,39 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
{
|
{
|
||||||
private readonly string _ffmpegPath;
|
private readonly string _ffmpegPath;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
private readonly IFileSystem _fileSystem;
|
||||||
|
|
||||||
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
||||||
|
|
||||||
private static readonly SemaphoreSlim ResourcePool = new SemaphoreSlim(5, 5);
|
private static readonly SemaphoreSlim ResourcePool = new SemaphoreSlim(10, 10);
|
||||||
|
|
||||||
public ImageEncoder(string ffmpegPath, ILogger logger)
|
public ImageEncoder(string ffmpegPath, ILogger logger, IFileSystem fileSystem)
|
||||||
{
|
{
|
||||||
_ffmpegPath = ffmpegPath;
|
_ffmpegPath = ffmpegPath;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_fileSystem = fileSystem;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
|
public async Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
ValidateInput(options);
|
ValidateInput(options);
|
||||||
|
|
||||||
|
await ResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await EncodeImageInternal(options, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ResourcePool.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<Stream> EncodeImageInternal(ImageEncodingOptions options, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ValidateInput(options);
|
||||||
|
|
||||||
var process = new Process
|
var process = new Process
|
||||||
{
|
{
|
||||||
StartInfo = new ProcessStartInfo
|
StartInfo = new ProcessStartInfo
|
||||||
|
@ -38,11 +58,12 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
WindowStyle = ProcessWindowStyle.Hidden,
|
WindowStyle = ProcessWindowStyle.Hidden,
|
||||||
ErrorDialog = false,
|
ErrorDialog = false,
|
||||||
RedirectStandardOutput = true,
|
RedirectStandardOutput = true,
|
||||||
RedirectStandardError = true
|
RedirectStandardError = true,
|
||||||
|
WorkingDirectory = Path.GetDirectoryName(options.InputPath)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
await ResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
_logger.Debug("ffmpeg " + process.StartInfo.Arguments);
|
||||||
|
|
||||||
process.Start();
|
process.Start();
|
||||||
|
|
||||||
|
@ -74,8 +95,6 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ResourcePool.Release();
|
|
||||||
|
|
||||||
var exitCode = ranToCompletion ? process.ExitCode : -1;
|
var exitCode = ranToCompletion ? process.ExitCode : -1;
|
||||||
|
|
||||||
process.Dispose();
|
process.Dispose();
|
||||||
|
@ -98,12 +117,17 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
private string GetArguments(ImageEncodingOptions options)
|
private string GetArguments(ImageEncodingOptions options)
|
||||||
{
|
{
|
||||||
var vfScale = GetFilterGraph(options);
|
var vfScale = GetFilterGraph(options);
|
||||||
var outputFormat = GetOutputFormat(options);
|
var outputFormat = GetOutputFormat(options.Format);
|
||||||
|
|
||||||
return string.Format("-i file:\"{0}\" {1} -f {2}",
|
var quality = (options.Quality ?? 100) * .3;
|
||||||
options.InputPath,
|
quality = 31 - quality;
|
||||||
|
var qualityValue = Convert.ToInt32(Math.Max(quality, 1));
|
||||||
|
|
||||||
|
return string.Format("-f image2 -i file:\"{3}\" -q:v {0} {1} -f image2pipe -vcodec {2} -",
|
||||||
|
qualityValue.ToString(_usCulture),
|
||||||
vfScale,
|
vfScale,
|
||||||
outputFormat);
|
outputFormat,
|
||||||
|
Path.GetFileName(options.InputPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetFilterGraph(ImageEncodingOptions options)
|
private string GetFilterGraph(ImageEncodingOptions options)
|
||||||
|
@ -121,7 +145,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
|
|
||||||
if (options.MaxWidth.HasValue)
|
if (options.MaxWidth.HasValue)
|
||||||
{
|
{
|
||||||
widthScale = "min(iw," + options.MaxWidth.Value.ToString(_usCulture) + ")";
|
widthScale = "min(iw\\," + options.MaxWidth.Value.ToString(_usCulture) + ")";
|
||||||
}
|
}
|
||||||
else if (options.Width.HasValue)
|
else if (options.Width.HasValue)
|
||||||
{
|
{
|
||||||
|
@ -130,7 +154,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
|
|
||||||
if (options.MaxHeight.HasValue)
|
if (options.MaxHeight.HasValue)
|
||||||
{
|
{
|
||||||
heightScale = "min(ih," + options.MaxHeight.Value.ToString(_usCulture) + ")";
|
heightScale = "min(ih\\," + options.MaxHeight.Value.ToString(_usCulture) + ")";
|
||||||
}
|
}
|
||||||
else if (options.Height.HasValue)
|
else if (options.Height.HasValue)
|
||||||
{
|
{
|
||||||
|
@ -145,9 +169,14 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
scaleMethod);
|
scaleMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetOutputFormat(ImageEncodingOptions options)
|
private string GetOutputFormat(string format)
|
||||||
{
|
{
|
||||||
return options.Format;
|
if (string.Equals(format, "jpeg", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
string.Equals(format, "jpg", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return "mjpeg";
|
||||||
|
}
|
||||||
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ValidateInput(ImageEncodingOptions options)
|
private void ValidateInput(ImageEncodingOptions options)
|
||||||
|
|
|
@ -879,45 +879,6 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
return memoryStream;
|
return memoryStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Starts the and wait for process.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="process">The process.</param>
|
|
||||||
/// <param name="timeout">The timeout.</param>
|
|
||||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
||||||
private bool StartAndWaitForProcess(Process process, int timeout = 10000)
|
|
||||||
{
|
|
||||||
process.Start();
|
|
||||||
|
|
||||||
var ranToCompletion = process.WaitForExit(timeout);
|
|
||||||
|
|
||||||
if (!ranToCompletion)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_logger.Info("Killing ffmpeg process");
|
|
||||||
|
|
||||||
process.Kill();
|
|
||||||
|
|
||||||
process.WaitForExit(1000);
|
|
||||||
}
|
|
||||||
catch (Win32Exception ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Error killing process", ex);
|
|
||||||
}
|
|
||||||
catch (InvalidOperationException ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Error killing process", ex);
|
|
||||||
}
|
|
||||||
catch (NotSupportedException ex)
|
|
||||||
{
|
|
||||||
_logger.ErrorException("Error killing process", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ranToCompletion;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the file input argument.
|
/// Gets the file input argument.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -950,7 +911,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
|
||||||
|
|
||||||
public Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
|
public Task<Stream> EncodeImage(ImageEncodingOptions options, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return new ImageEncoder(FFMpegPath, _logger).EncodeImage(options, cancellationToken);
|
return new ImageEncoder(FFMpegPath, _logger, _fileSystem).EncodeImage(options, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -3,6 +3,7 @@ using MediaBrowser.Common.IO;
|
||||||
using MediaBrowser.Controller;
|
using MediaBrowser.Controller;
|
||||||
using MediaBrowser.Controller.Drawing;
|
using MediaBrowser.Controller.Drawing;
|
||||||
using MediaBrowser.Controller.Entities;
|
using MediaBrowser.Controller.Entities;
|
||||||
|
using MediaBrowser.Controller.MediaEncoding;
|
||||||
using MediaBrowser.Controller.Providers;
|
using MediaBrowser.Controller.Providers;
|
||||||
using MediaBrowser.Model.Drawing;
|
using MediaBrowser.Model.Drawing;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Entities;
|
||||||
|
@ -52,12 +53,14 @@ namespace MediaBrowser.Server.Implementations.Drawing
|
||||||
private readonly IFileSystem _fileSystem;
|
private readonly IFileSystem _fileSystem;
|
||||||
private readonly IJsonSerializer _jsonSerializer;
|
private readonly IJsonSerializer _jsonSerializer;
|
||||||
private readonly IServerApplicationPaths _appPaths;
|
private readonly IServerApplicationPaths _appPaths;
|
||||||
|
private readonly IMediaEncoder _mediaEncoder;
|
||||||
|
|
||||||
public ImageProcessor(ILogger logger, IServerApplicationPaths appPaths, IFileSystem fileSystem, IJsonSerializer jsonSerializer)
|
public ImageProcessor(ILogger logger, IServerApplicationPaths appPaths, IFileSystem fileSystem, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_fileSystem = fileSystem;
|
_fileSystem = fileSystem;
|
||||||
_jsonSerializer = jsonSerializer;
|
_jsonSerializer = jsonSerializer;
|
||||||
|
_mediaEncoder = mediaEncoder;
|
||||||
_appPaths = appPaths;
|
_appPaths = appPaths;
|
||||||
|
|
||||||
_saveImageSizeTimer = new Timer(SaveImageSizeCallback, null, Timeout.Infinite, Timeout.Infinite);
|
_saveImageSizeTimer = new Timer(SaveImageSizeCallback, null, Timeout.Infinite, Timeout.Infinite);
|
||||||
|
@ -213,6 +216,39 @@ namespace MediaBrowser.Server.Implementations.Drawing
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed.HasValue;
|
||||||
|
|
||||||
|
if (!hasPostProcessing)
|
||||||
|
{
|
||||||
|
using (var outputStream = await _mediaEncoder.EncodeImage(new ImageEncodingOptions
|
||||||
|
{
|
||||||
|
InputPath = originalImagePath,
|
||||||
|
MaxHeight = options.MaxHeight,
|
||||||
|
MaxWidth = options.MaxWidth,
|
||||||
|
Height = options.Height,
|
||||||
|
Width = options.Width,
|
||||||
|
Quality = options.Quality,
|
||||||
|
Format = options.OutputFormat == ImageOutputFormat.Original ? Path.GetExtension(originalImagePath).TrimStart('.') : options.OutputFormat.ToString().ToLower()
|
||||||
|
|
||||||
|
}, CancellationToken.None).ConfigureAwait(false))
|
||||||
|
{
|
||||||
|
using (var outputMemoryStream = new MemoryStream())
|
||||||
|
{
|
||||||
|
// Save to the memory stream
|
||||||
|
await outputStream.CopyToAsync(outputMemoryStream).ConfigureAwait(false);
|
||||||
|
|
||||||
|
var bytes = outputMemoryStream.ToArray();
|
||||||
|
|
||||||
|
await toStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// kick off a task to cache the result
|
||||||
|
await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
||||||
{
|
{
|
||||||
// Copy to memory stream to avoid Image locking file
|
// Copy to memory stream to avoid Image locking file
|
||||||
|
@ -241,7 +277,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
|
||||||
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
|
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
|
||||||
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||||
thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||||
thumbnailGraph.CompositingMode = string.IsNullOrEmpty(options.BackgroundColor) && !options.UnplayedCount.HasValue && !options.AddPlayedIndicator && !options.PercentPlayed.HasValue ?
|
thumbnailGraph.CompositingMode = !hasPostProcessing ?
|
||||||
CompositingMode.SourceCopy :
|
CompositingMode.SourceCopy :
|
||||||
CompositingMode.SourceOver;
|
CompositingMode.SourceOver;
|
||||||
|
|
||||||
|
@ -263,7 +299,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
|
||||||
await toStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
await toStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
||||||
|
|
||||||
// kick off a task to cache the result
|
// kick off a task to cache the result
|
||||||
CacheResizedImage(cacheFilePath, bytes, semaphore);
|
await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,11 +308,9 @@ namespace MediaBrowser.Server.Implementations.Drawing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
finally
|
||||||
{
|
{
|
||||||
semaphore.Release();
|
semaphore.Release();
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,33 +319,26 @@ namespace MediaBrowser.Server.Implementations.Drawing
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cacheFilePath">The cache file path.</param>
|
/// <param name="cacheFilePath">The cache file path.</param>
|
||||||
/// <param name="bytes">The bytes.</param>
|
/// <param name="bytes">The bytes.</param>
|
||||||
/// <param name="semaphore">The semaphore.</param>
|
/// <returns>Task.</returns>
|
||||||
private void CacheResizedImage(string cacheFilePath, byte[] bytes, SemaphoreSlim semaphore)
|
private async Task CacheResizedImage(string cacheFilePath, byte[] bytes)
|
||||||
{
|
{
|
||||||
Task.Run(async () =>
|
try
|
||||||
{
|
{
|
||||||
try
|
var parentPath = Path.GetDirectoryName(cacheFilePath);
|
||||||
{
|
|
||||||
var parentPath = Path.GetDirectoryName(cacheFilePath);
|
|
||||||
|
|
||||||
Directory.CreateDirectory(parentPath);
|
Directory.CreateDirectory(parentPath);
|
||||||
|
|
||||||
// Save to the cache location
|
// Save to the cache location
|
||||||
using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
|
using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
|
||||||
{
|
|
||||||
// Save to the filestream
|
|
||||||
await cacheFileStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
_logger.ErrorException("Error writing to image cache file {0}", ex, cacheFilePath);
|
// Save to the filestream
|
||||||
|
await cacheFileStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
finally
|
}
|
||||||
{
|
catch (Exception ex)
|
||||||
semaphore.Release();
|
{
|
||||||
}
|
_logger.ErrorException("Error writing to image cache file {0}", ex, cacheFilePath);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -473,7 +473,13 @@ namespace MediaBrowser.ServerApplication
|
||||||
LocalizationManager = new LocalizationManager(ServerConfigurationManager, FileSystemManager);
|
LocalizationManager = new LocalizationManager(ServerConfigurationManager, FileSystemManager);
|
||||||
RegisterSingleInstance(LocalizationManager);
|
RegisterSingleInstance(LocalizationManager);
|
||||||
|
|
||||||
ImageProcessor = new ImageProcessor(LogManager.GetLogger("ImageProcessor"), ServerConfigurationManager.ApplicationPaths, FileSystemManager, JsonSerializer);
|
var innerProgress = new ActionableProgress<double>();
|
||||||
|
innerProgress.RegisterAction(p => progress.Report((.75 * p) + 15));
|
||||||
|
|
||||||
|
await RegisterMediaEncoder(innerProgress).ConfigureAwait(false);
|
||||||
|
progress.Report(90);
|
||||||
|
|
||||||
|
ImageProcessor = new ImageProcessor(LogManager.GetLogger("ImageProcessor"), ServerConfigurationManager.ApplicationPaths, FileSystemManager, JsonSerializer, MediaEncoder);
|
||||||
RegisterSingleInstance(ImageProcessor);
|
RegisterSingleInstance(ImageProcessor);
|
||||||
|
|
||||||
DtoService = new DtoService(Logger, LibraryManager, UserManager, UserDataManager, ItemRepository, ImageProcessor, ServerConfigurationManager, FileSystemManager, ProviderManager);
|
DtoService = new DtoService(Logger, LibraryManager, UserManager, UserDataManager, ItemRepository, ImageProcessor, ServerConfigurationManager, FileSystemManager, ProviderManager);
|
||||||
|
@ -487,12 +493,6 @@ namespace MediaBrowser.ServerApplication
|
||||||
|
|
||||||
progress.Report(15);
|
progress.Report(15);
|
||||||
|
|
||||||
var innerProgress = new ActionableProgress<double>();
|
|
||||||
innerProgress.RegisterAction(p => progress.Report((.75 * p) + 15));
|
|
||||||
|
|
||||||
await RegisterMediaEncoder(innerProgress).ConfigureAwait(false);
|
|
||||||
progress.Report(90);
|
|
||||||
|
|
||||||
EncodingManager = new EncodingManager(ServerConfigurationManager, FileSystemManager, Logger, ItemRepository,
|
EncodingManager = new EncodingManager(ServerConfigurationManager, FileSystemManager, Logger, ItemRepository,
|
||||||
MediaEncoder);
|
MediaEncoder);
|
||||||
RegisterSingleInstance(EncodingManager);
|
RegisterSingleInstance(EncodingManager);
|
||||||
|
|
|
@ -267,4 +267,7 @@ Global
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
|
GlobalSection(Performance) = preSolution
|
||||||
|
HasPerformanceSessions = true
|
||||||
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
Loading…
Reference in a new issue