From 49df4a31b18ad0f9f62d36c645b3592c7600f90c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 1 Feb 2015 15:05:39 -0500 Subject: [PATCH] update thumbnail generation --- MediaBrowser.Api/PluginService.cs | 5 +- .../Encoder/MediaEncoder.cs | 67 +++++++++++-------- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/MediaBrowser.Api/PluginService.cs b/MediaBrowser.Api/PluginService.cs index f9098f5bf..f5c8935f8 100644 --- a/MediaBrowser.Api/PluginService.cs +++ b/MediaBrowser.Api/PluginService.cs @@ -1,5 +1,4 @@ -using System.Threading; -using MediaBrowser.Common; +using MediaBrowser.Common; using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Security; using MediaBrowser.Common.Updates; @@ -8,12 +7,12 @@ using MediaBrowser.Model.Entities; using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Serialization; using ServiceStack; -using ServiceStack.Text.Controller; using ServiceStack.Web; using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Api diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index cb6e9f21e..b75d7bee3 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -331,8 +331,8 @@ namespace MediaBrowser.MediaEncoding.Encoder // -f image2 -f webp // Use ffmpeg to sample 100 (we can drop this if required using thumbnail=50 for 50 frames) frames and pick the best thumbnail. Have a fall back just in case. - var args = useIFrame ? string.Format("-i {0} -threads 0 -v quiet -vframes 1 -vf \"{2},thumbnail=30\" -f image2 \"{1}\"", inputPath, "-", vf) : - string.Format("-i {0} -threads 0 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, "-", vf); + var args = useIFrame ? string.Format("-i {0} -threads 1 -v quiet -vframes 1 -vf \"{2},thumbnail=30\" -f image2 \"{1}\"", inputPath, "-", vf) : + string.Format("-i {0} -threads 1 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, "-", vf); var probeSize = GetProbeSizeArgument(new[] { inputPath }, protocol); @@ -363,7 +363,7 @@ namespace MediaBrowser.MediaEncoding.Encoder }; _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); - + await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); process.Start(); @@ -479,7 +479,7 @@ namespace MediaBrowser.MediaEncoding.Encoder Directory.CreateDirectory(targetDirectory); var outputPath = Path.Combine(targetDirectory, filenamePrefix + "%05d.jpg"); - var args = string.Format("-i {0} -threads 0 -v quiet -vf \"{2}\" -f image2 \"{1}\"", inputArgument, outputPath, vf); + var args = string.Format("-i {0} -threads 1 -v quiet -vf \"{2}\" -f image2 \"{1}\"", inputArgument, outputPath, vf); var probeSize = GetProbeSizeArgument(new[] { inputArgument }, protocol); @@ -506,41 +506,52 @@ namespace MediaBrowser.MediaEncoding.Encoder await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); - process.Start(); + bool ranToCompletion; - // Need to give ffmpeg enough time to make all the thumbnails, which could be a while, - // but we still need to detect if the process hangs. - // Making the assumption that as long as new jpegs are showing up, everything is good. - - bool isResponsive = true; - int lastCount = 0; - - while (isResponsive && !process.WaitForExit(120000)) + try { - int jpegCount = Directory.GetFiles(targetDirectory, "*.jpg").Count(); - isResponsive = (jpegCount > lastCount); - lastCount = jpegCount; - } + process.Start(); - bool ranToCompletion = process.HasExited; + // Need to give ffmpeg enough time to make all the thumbnails, which could be a while, + // but we still need to detect if the process hangs. + // Making the assumption that as long as new jpegs are showing up, everything is good. - if (!ranToCompletion) - { - try + bool isResponsive = true; + int lastCount = 0; + + while (isResponsive && !process.WaitForExit(30000)) { - _logger.Info("Killing ffmpeg process"); + cancellationToken.ThrowIfCancellationRequested(); - process.StandardInput.WriteLine("q"); + int jpegCount = Directory.GetFiles(targetDirectory) + .Count(i => string.Equals(Path.GetExtension(i), ".jpg", StringComparison.OrdinalIgnoreCase)); - process.WaitForExit(1000); + isResponsive = (jpegCount > lastCount); + lastCount = jpegCount; } - catch (Exception ex) + + ranToCompletion = process.HasExited; + + if (!ranToCompletion) { - _logger.ErrorException("Error killing process", ex); + try + { + _logger.Info("Killing ffmpeg process"); + + process.StandardInput.WriteLine("q"); + + process.WaitForExit(1000); + } + catch (Exception ex) + { + _logger.ErrorException("Error killing process", ex); + } } } - - resourcePool.Release(); + finally + { + resourcePool.Release(); + } var exitCode = ranToCompletion ? process.ExitCode : -1;