diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index b626600fa..a8874b6d0 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -53,7 +53,7 @@ namespace MediaBrowser.MediaEncoding.Encoder private readonly int DefaultImageExtractionTimeoutMs; private readonly string StartupOptionFFmpegPath; - private readonly SemaphoreSlim _thumbnailResourcePool = new SemaphoreSlim(1, 1); + private readonly SemaphoreSlim _thumbnailResourcePool = new SemaphoreSlim(2, 2); private readonly List _runningProcesses = new List(); private readonly ILocalizationManager _localization; @@ -582,19 +582,27 @@ namespace MediaBrowser.MediaEncoding.Encoder { bool ranToCompletion; - StartProcess(processWrapper); - - var timeoutMs = ConfigurationManager.Configuration.ImageExtractionTimeoutMs; - if (timeoutMs <= 0) + await _thumbnailResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + try { - timeoutMs = DefaultImageExtractionTimeoutMs; + StartProcess(processWrapper); + + var timeoutMs = ConfigurationManager.Configuration.ImageExtractionTimeoutMs; + if (timeoutMs <= 0) + { + timeoutMs = DefaultImageExtractionTimeoutMs; + } + + ranToCompletion = await process.WaitForExitAsync(timeoutMs).ConfigureAwait(false); + + if (!ranToCompletion) + { + StopProcess(processWrapper, 1000); + } } - - ranToCompletion = await process.WaitForExitAsync(timeoutMs).ConfigureAwait(false); - - if (!ranToCompletion) + finally { - StopProcess(processWrapper, 1000); + _thumbnailResourcePool.Release(); } var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1; @@ -625,7 +633,8 @@ namespace MediaBrowser.MediaEncoding.Encoder return time.ToString(@"hh\:mm\:ss\.fff", UsCulture); } - public async Task ExtractVideoImagesOnInterval(string[] inputFiles, + public async Task ExtractVideoImagesOnInterval( + string[] inputFiles, string container, MediaStream videoStream, MediaProtocol protocol, @@ -636,8 +645,6 @@ namespace MediaBrowser.MediaEncoding.Encoder int? maxWidth, CancellationToken cancellationToken) { - var resourcePool = _thumbnailResourcePool; - var inputArgument = GetInputArgument(inputFiles, protocol); var vf = "fps=fps=1/" + interval.TotalSeconds.ToString(UsCulture); @@ -701,7 +708,7 @@ namespace MediaBrowser.MediaEncoding.Encoder _logger.LogInformation(process.StartInfo.FileName + " " + process.StartInfo.Arguments); - await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); + await _thumbnailResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); bool ranToCompletion = false; @@ -742,7 +749,7 @@ namespace MediaBrowser.MediaEncoding.Encoder } finally { - resourcePool.Release(); + _thumbnailResourcePool.Release(); } var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;