aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding/Encoder
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.MediaEncoding/Encoder')
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs21
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/JobLogger.cs2
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs74
3 files changed, 58 insertions, 39 deletions
diff --git a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs
index ecf5d72d5..5a00c3d3f 100644
--- a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs
@@ -248,7 +248,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
protected virtual void DeleteFiles(EncodingJob job)
{
- File.Delete(job.OutputFilePath);
+ FileSystem.DeleteFile(job.OutputFilePath);
}
private void OnTranscodeBeginning(EncodingJob job)
@@ -280,13 +280,14 @@ namespace MediaBrowser.MediaEncoding.Encoder
private string GetOutputFilePath(EncodingJob state)
{
- var folder = ConfigurationManager.ApplicationPaths.TranscodingTempPath;
+ var folder = string.IsNullOrWhiteSpace(state.Options.OutputDirectory) ?
+ ConfigurationManager.ApplicationPaths.TranscodingTempPath :
+ state.Options.OutputDirectory;
var outputFileExtension = GetOutputFileExtension(state);
- var context = state.Options.Context;
var filename = state.Id + (outputFileExtension ?? string.Empty).ToLower();
- return Path.Combine(folder, context.ToString().ToLower(), filename);
+ return Path.Combine(folder, filename);
}
protected virtual string GetOutputFileExtension(EncodingJob state)
@@ -460,7 +461,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
// {
// if (SupportsThrottleWithStream)
// {
- // var url = "http://localhost:" + ServerConfigurationManager.Configuration.HttpServerPortNumber.ToString(UsCulture) + "/mediabrowser/videos/" + job.Request.Id + "/stream?static=true&Throttle=true&mediaSourceId=" + job.Request.MediaSourceId;
+ // var url = "http://localhost:" + ServerConfigurationManager.Configuration.HttpServerPortNumber.ToString(UsCulture) + "/videos/" + job.Request.Id + "/stream?static=true&Throttle=true&mediaSourceId=" + job.Request.MediaSourceId;
// url += "&transcodingJobId=" + transcodingJobId;
@@ -630,13 +631,13 @@ namespace MediaBrowser.MediaEncoding.Encoder
switch (qualitySetting)
{
case EncodingQuality.HighSpeed:
- param += " -crf 23";
+ param += " -subq 0 -crf 23";
break;
case EncodingQuality.HighQuality:
- param += " -crf 20";
+ param += " -subq 3 -crf 20";
break;
case EncodingQuality.MaxQuality:
- param += " -crf 18";
+ param += " -subq 6 -crf 18";
break;
}
}
@@ -739,7 +740,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
param += " -level " + state.Options.Level.Value.ToString(UsCulture);
}
- return param;
+ return "-pix_fmt yuv420p " + param;
}
protected string GetVideoBitrateParam(EncodingJob state, string videoCodec, bool isHls)
@@ -1014,7 +1015,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
if (!string.IsNullOrEmpty(state.SubtitleStream.Language))
{
- var charenc = SubtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath, state.SubtitleStream.Language);
+ var charenc = SubtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath);
if (!string.IsNullOrEmpty(charenc))
{
diff --git a/MediaBrowser.MediaEncoding/Encoder/JobLogger.cs b/MediaBrowser.MediaEncoding/Encoder/JobLogger.cs
index 6be870519..cb6e58f17 100644
--- a/MediaBrowser.MediaEncoding/Encoder/JobLogger.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/JobLogger.cs
@@ -1,4 +1,4 @@
-using MediaBrowser.Common.Extensions;
+using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Logging;
using System;
using System.Globalization;
diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
index a4ab1c551..b75d7bee3 100644
--- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
@@ -37,6 +37,11 @@ namespace MediaBrowser.MediaEncoding.Encoder
private readonly IJsonSerializer _jsonSerializer;
/// <summary>
+ /// The _thumbnail resource pool
+ /// </summary>
+ private readonly SemaphoreSlim _thumbnailResourcePool = new SemaphoreSlim(1, 1);
+
+ /// <summary>
/// The video image resource pool
/// </summary>
private readonly SemaphoreSlim _videoImageResourcePool = new SemaphoreSlim(1, 1);
@@ -326,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);
@@ -357,6 +362,8 @@ namespace MediaBrowser.MediaEncoding.Encoder
}
};
+ _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
+
await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
process.Start();
@@ -456,7 +463,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
int? maxWidth,
CancellationToken cancellationToken)
{
- var resourcePool = _videoImageResourcePool;
+ var resourcePool = _thumbnailResourcePool;
var inputArgument = GetInputArgument(inputFiles, protocol);
@@ -472,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);
@@ -499,41 +506,52 @@ namespace MediaBrowser.MediaEncoding.Encoder
await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
- process.Start();
-
- // 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 ranToCompletion;
- 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;