Minor changes to encoding code

* Don't wait in intervals of 100ms for the file to exist
This commit is contained in:
Bond_009 2019-01-29 19:30:52 +01:00
parent 34da7de47d
commit 66eabcdd39
5 changed files with 110 additions and 205 deletions

View File

@ -105,7 +105,7 @@ namespace Emby.Server.Implementations.Diagnostics
{ {
return _process.WaitForExit(timeMs); return _process.WaitForExit(timeMs);
} }
public Task<bool> WaitForExitAsync(int timeMs) public Task<bool> WaitForExitAsync(int timeMs)
{ {
//Note: For this function to work correctly, the option EnableRisingEvents needs to be set to true. //Note: For this function to work correctly, the option EnableRisingEvents needs to be set to true.

View File

@ -10,19 +10,13 @@ using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO; using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo; using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Session; using MediaBrowser.Model.Session;
using Microsoft.Extensions.Logging;
using System.IO;
using MediaBrowser.Model.Net; using MediaBrowser.Model.Net;
using MediaBrowser.Controller.Library;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.MediaEncoding namespace MediaBrowser.Controller.MediaEncoding
{ {
// For now, a common base class until the API and MediaEncoding classes are unified // For now, a common base class until the API and MediaEncoding classes are unified
public class EncodingJobInfo public class EncodingJobInfo
{ {
protected readonly IMediaSourceManager MediaSourceManager;
public MediaStream VideoStream { get; set; } public MediaStream VideoStream { get; set; }
public VideoType VideoType { get; set; } public VideoType VideoType { get; set; }
public Dictionary<string, string> RemoteHttpHeaders { get; set; } public Dictionary<string, string> RemoteHttpHeaders { get; set; }
@ -49,7 +43,6 @@ namespace MediaBrowser.Controller.MediaEncoding
public string OutputFilePath { get; set; } public string OutputFilePath { get; set; }
public string MimeType { get; set; } public string MimeType { get; set; }
public long? EncodingDurationTicks { get; set; }
public string GetMimeType(string outputPath, bool enableStreamDefault = true) public string GetMimeType(string outputPath, bool enableStreamDefault = true)
{ {
@ -68,7 +61,12 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (_transcodeReasons == null) if (_transcodeReasons == null)
{ {
_transcodeReasons = (BaseRequest.TranscodeReasons ?? string.Empty) if (BaseRequest.TranscodeReasons == null)
{
return Array.Empty<TranscodeReason>();
}
_transcodeReasons = BaseRequest.TranscodeReasons
.Split(',') .Split(',')
.Where(i => !string.IsNullOrEmpty(i)) .Where(i => !string.IsNullOrEmpty(i))
.Select(v => (TranscodeReason)Enum.Parse(typeof(TranscodeReason), v, true)) .Select(v => (TranscodeReason)Enum.Parse(typeof(TranscodeReason), v, true))
@ -98,7 +96,8 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
// For live tv + in progress recordings // For live tv + in progress recordings
if (string.Equals(InputContainer, "mpegts", StringComparison.OrdinalIgnoreCase) || string.Equals(InputContainer, "ts", StringComparison.OrdinalIgnoreCase)) if (string.Equals(InputContainer, "mpegts", StringComparison.OrdinalIgnoreCase)
|| string.Equals(InputContainer, "ts", StringComparison.OrdinalIgnoreCase))
{ {
if (!MediaSource.RunTimeTicks.HasValue) if (!MediaSource.RunTimeTicks.HasValue)
{ {
@ -155,15 +154,7 @@ namespace MediaBrowser.Controller.MediaEncoding
} }
} }
if (forceDeinterlaceIfSourceIsInterlaced) return forceDeinterlaceIfSourceIsInterlaced && isInputInterlaced;
{
if (isInputInterlaced)
{
return true;
}
}
return false;
} }
public string[] GetRequestedProfiles(string codec) public string[] GetRequestedProfiles(string codec)
@ -211,7 +202,8 @@ namespace MediaBrowser.Controller.MediaEncoding
if (!string.IsNullOrEmpty(codec)) if (!string.IsNullOrEmpty(codec))
{ {
var value = BaseRequest.GetOption(codec, "maxrefframes"); var value = BaseRequest.GetOption(codec, "maxrefframes");
if (!string.IsNullOrEmpty(value) && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)) if (!string.IsNullOrEmpty(value)
&& int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
{ {
return result; return result;
} }
@ -230,7 +222,8 @@ namespace MediaBrowser.Controller.MediaEncoding
if (!string.IsNullOrEmpty(codec)) if (!string.IsNullOrEmpty(codec))
{ {
var value = BaseRequest.GetOption(codec, "videobitdepth"); var value = BaseRequest.GetOption(codec, "videobitdepth");
if (!string.IsNullOrEmpty(value) && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)) if (!string.IsNullOrEmpty(value)
&& int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
{ {
return result; return result;
} }
@ -249,7 +242,8 @@ namespace MediaBrowser.Controller.MediaEncoding
if (!string.IsNullOrEmpty(codec)) if (!string.IsNullOrEmpty(codec))
{ {
var value = BaseRequest.GetOption(codec, "audiobitdepth"); var value = BaseRequest.GetOption(codec, "audiobitdepth");
if (!string.IsNullOrEmpty(value) && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)) if (!string.IsNullOrEmpty(value)
&& int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
{ {
return result; return result;
} }
@ -264,6 +258,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
return BaseRequest.MaxAudioChannels; return BaseRequest.MaxAudioChannels;
} }
if (BaseRequest.AudioChannels.HasValue) if (BaseRequest.AudioChannels.HasValue)
{ {
return BaseRequest.AudioChannels; return BaseRequest.AudioChannels;
@ -272,7 +267,8 @@ namespace MediaBrowser.Controller.MediaEncoding
if (!string.IsNullOrEmpty(codec)) if (!string.IsNullOrEmpty(codec))
{ {
var value = BaseRequest.GetOption(codec, "audiochannels"); var value = BaseRequest.GetOption(codec, "audiochannels");
if (!string.IsNullOrEmpty(value) && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)) if (!string.IsNullOrEmpty(value)
&& int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
{ {
return result; return result;
} }
@ -294,7 +290,8 @@ namespace MediaBrowser.Controller.MediaEncoding
SupportedSubtitleCodecs = Array.Empty<string>(); SupportedSubtitleCodecs = Array.Empty<string>();
} }
public bool IsSegmentedLiveStream => TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue; public bool IsSegmentedLiveStream
=> TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue;
public bool EnableBreakOnNonKeyFrames(string videoCodec) public bool EnableBreakOnNonKeyFrames(string videoCodec)
{ {
@ -428,11 +425,12 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? null : VideoStream.Level; return VideoStream?.Level;
} }
var level = GetRequestedLevel(ActualOutputVideoCodec); var level = GetRequestedLevel(ActualOutputVideoCodec);
if (!string.IsNullOrEmpty(level) && double.TryParse(level, NumberStyles.Any, CultureInfo.InvariantCulture, out var result)) if (!string.IsNullOrEmpty(level)
&& double.TryParse(level, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
{ {
return result; return result;
} }
@ -450,7 +448,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? null : VideoStream.BitDepth; return VideoStream?.BitDepth;
} }
return null; return null;
@ -467,7 +465,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? null : VideoStream.RefFrames; return VideoStream?.RefFrames;
} }
return null; return null;
@ -494,13 +492,14 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
get get
{ {
var defaultValue = string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ? if (BaseRequest.Static)
{
return InputTimestamp;
}
return string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
TransportStreamTimestamp.Valid : TransportStreamTimestamp.Valid :
TransportStreamTimestamp.None; TransportStreamTimestamp.None;
return !BaseRequest.Static
? defaultValue
: InputTimestamp;
} }
} }
@ -513,7 +512,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? null : VideoStream.PacketLength; return VideoStream?.PacketLength;
} }
return null; return null;
@ -529,7 +528,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? null : VideoStream.Profile; return VideoStream?.Profile;
} }
var requestedProfile = GetRequestedProfiles(ActualOutputVideoCodec).FirstOrDefault(); var requestedProfile = GetRequestedProfiles(ActualOutputVideoCodec).FirstOrDefault();
@ -542,42 +541,13 @@ namespace MediaBrowser.Controller.MediaEncoding
} }
} }
/// <summary>
/// Predicts the audio sample rate that will be in the output stream
/// </summary>
public string TargetVideoRange
{
get
{
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{
return VideoStream == null ? null : VideoStream.VideoRange;
}
return "SDR";
}
}
public string TargetAudioProfile
{
get
{
if (BaseRequest.Static || string.Equals(OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase))
{
return AudioStream == null ? null : AudioStream.Profile;
}
return null;
}
}
public string TargetVideoCodecTag public string TargetVideoCodecTag
{ {
get get
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? null : VideoStream.CodecTag; return VideoStream?.CodecTag;
} }
return null; return null;
@ -590,7 +560,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? null : VideoStream.IsAnamorphic; return VideoStream?.IsAnamorphic;
} }
return false; return false;
@ -605,14 +575,7 @@ namespace MediaBrowser.Controller.MediaEncoding
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
var stream = VideoStream; return VideoStream?.Codec;
if (stream != null)
{
return stream.Codec;
}
return null;
} }
return codec; return codec;
@ -627,14 +590,7 @@ namespace MediaBrowser.Controller.MediaEncoding
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
var stream = AudioStream; return AudioStream?.Codec;
if (stream != null)
{
return stream.Codec;
}
return null;
} }
return codec; return codec;
@ -647,7 +603,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? (bool?)null : VideoStream.IsInterlaced; return VideoStream?.IsInterlaced;
} }
if (DeInterlace(ActualOutputVideoCodec, true)) if (DeInterlace(ActualOutputVideoCodec, true))
@ -655,7 +611,7 @@ namespace MediaBrowser.Controller.MediaEncoding
return false; return false;
} }
return VideoStream == null ? (bool?)null : VideoStream.IsInterlaced; return VideoStream?.IsInterlaced;
} }
} }
@ -665,7 +621,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase))
{ {
return VideoStream == null ? null : VideoStream.IsAVC; return VideoStream?.IsAVC;
} }
return false; return false;

View File

@ -1,5 +1,5 @@
using System; using System;
using System.Globalization; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
@ -31,11 +31,10 @@ namespace MediaBrowser.MediaEncoding.Encoder
protected readonly IMediaSourceManager MediaSourceManager; protected readonly IMediaSourceManager MediaSourceManager;
protected IProcessFactory ProcessFactory; protected IProcessFactory ProcessFactory;
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
protected EncodingHelper EncodingHelper; protected EncodingHelper EncodingHelper;
protected BaseEncoder(MediaEncoder mediaEncoder, protected BaseEncoder(
MediaEncoder mediaEncoder,
ILogger logger, ILogger logger,
IServerConfigurationManager configurationManager, IServerConfigurationManager configurationManager,
IFileSystem fileSystem, IFileSystem fileSystem,
@ -43,7 +42,8 @@ namespace MediaBrowser.MediaEncoding.Encoder
ILibraryManager libraryManager, ILibraryManager libraryManager,
ISessionManager sessionManager, ISessionManager sessionManager,
ISubtitleEncoder subtitleEncoder, ISubtitleEncoder subtitleEncoder,
IMediaSourceManager mediaSourceManager, IProcessFactory processFactory) IMediaSourceManager mediaSourceManager,
IProcessFactory processFactory)
{ {
MediaEncoder = mediaEncoder; MediaEncoder = mediaEncoder;
Logger = logger; Logger = logger;
@ -59,11 +59,12 @@ namespace MediaBrowser.MediaEncoding.Encoder
EncodingHelper = new EncodingHelper(MediaEncoder, FileSystem, SubtitleEncoder); EncodingHelper = new EncodingHelper(MediaEncoder, FileSystem, SubtitleEncoder);
} }
public async Task<EncodingJob> Start(EncodingJobOptions options, public async Task<EncodingJob> Start(
EncodingJobOptions options,
IProgress<double> progress, IProgress<double> progress,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var encodingJob = await new EncodingJobFactory(Logger, LibraryManager, MediaSourceManager, ConfigurationManager, MediaEncoder) var encodingJob = await new EncodingJobFactory(Logger, LibraryManager, MediaSourceManager, MediaEncoder)
.CreateJob(options, EncodingHelper, IsVideoEncoder, progress, cancellationToken).ConfigureAwait(false); .CreateJob(options, EncodingHelper, IsVideoEncoder, progress, cancellationToken).ConfigureAwait(false);
encodingJob.OutputFilePath = GetOutputFilePath(encodingJob); encodingJob.OutputFilePath = GetOutputFilePath(encodingJob);
@ -75,8 +76,9 @@ namespace MediaBrowser.MediaEncoding.Encoder
var commandLineArgs = GetCommandLineArguments(encodingJob); var commandLineArgs = GetCommandLineArguments(encodingJob);
var process = ProcessFactory.Create(new ProcessOptions Process process = Process.Start(new ProcessStartInfo
{ {
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true, CreateNoWindow = true,
UseShellExecute = false, UseShellExecute = false,
@ -88,11 +90,11 @@ namespace MediaBrowser.MediaEncoding.Encoder
FileName = MediaEncoder.EncoderPath, FileName = MediaEncoder.EncoderPath,
Arguments = commandLineArgs, Arguments = commandLineArgs,
IsHidden = true, ErrorDialog = false
ErrorDialog = false,
EnableRaisingEvents = true
}); });
process.EnableRaisingEvents = true;
var workingDirectory = GetWorkingDirectory(options); var workingDirectory = GetWorkingDirectory(options);
if (!string.IsNullOrWhiteSpace(workingDirectory)) if (!string.IsNullOrWhiteSpace(workingDirectory))
{ {
@ -128,29 +130,60 @@ namespace MediaBrowser.MediaEncoding.Encoder
throw; throw;
} }
cancellationToken.Register(() => Cancel(process, encodingJob)); cancellationToken.Register(async () => await Cancel(process, encodingJob));
// MUST read both stdout and stderr asynchronously or a deadlock may occurr // MUST read both stdout and stderr asynchronously or a deadlock may occur
//process.BeginOutputReadLine(); //process.BeginOutputReadLine();
// Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
new JobLogger(Logger).StartStreamingLog(encodingJob, process.StandardError.BaseStream, encodingJob.LogFileStream); new JobLogger(Logger).StartStreamingLog(encodingJob, process.StandardError.BaseStream, encodingJob.LogFileStream);
// Wait for the file to exist before proceeeding // Wait for the file to or for the process to stop
while (!File.Exists(encodingJob.OutputFilePath) && !encodingJob.HasExited) Task file = WaitForFileAsync(encodingJob.OutputFilePath);
{ await Task.WhenAny(encodingJob.TaskCompletionSource.Task, file).ConfigureAwait(false);
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
return encodingJob; return encodingJob;
} }
private void Cancel(IProcess process, EncodingJob job) public static Task WaitForFileAsync(string path)
{
if (File.Exists(path))
{
return Task.CompletedTask;
}
var tcs = new TaskCompletionSource<bool>();
FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(path));
watcher.Created += (s, e) =>
{
if (e.Name == Path.GetFileName(path))
{
watcher.Dispose();
tcs.TrySetResult(true);
}
};
watcher.Renamed += (s, e) =>
{
if (e.Name == Path.GetFileName(path))
{
watcher.Dispose();
tcs.TrySetResult(true);
}
};
watcher.EnableRaisingEvents = true;
return tcs.Task;
}
private async Task Cancel(Process process, EncodingJob job)
{ {
Logger.LogInformation("Killing ffmpeg process for {0}", job.OutputFilePath); Logger.LogInformation("Killing ffmpeg process for {0}", job.OutputFilePath);
//process.Kill(); //process.Kill();
process.StandardInput.WriteLine("q"); await process.StandardInput.WriteLineAsync("q");
job.IsCancelled = true; job.IsCancelled = true;
} }
@ -160,28 +193,28 @@ namespace MediaBrowser.MediaEncoding.Encoder
/// </summary> /// </summary>
/// <param name="process">The process.</param> /// <param name="process">The process.</param>
/// <param name="job">The job.</param> /// <param name="job">The job.</param>
private void OnFfMpegProcessExited(IProcess process, EncodingJob job) private void OnFfMpegProcessExited(Process process, EncodingJob job)
{ {
job.HasExited = true; job.HasExited = true;
Logger.LogDebug("Disposing stream resources"); Logger.LogDebug("Disposing stream resources");
job.Dispose(); job.Dispose();
var isSuccesful = false; var isSuccessful = false;
try try
{ {
var exitCode = process.ExitCode; var exitCode = process.ExitCode;
Logger.LogInformation("FFMpeg exited with code {0}", exitCode); Logger.LogInformation("FFMpeg exited with code {0}", exitCode);
isSuccesful = exitCode == 0; isSuccessful = exitCode == 0;
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.LogError(ex, "FFMpeg exited with an error."); Logger.LogError(ex, "FFMpeg exited with an error.");
} }
if (isSuccesful && !job.IsCancelled) if (isSuccessful && !job.IsCancelled)
{ {
job.TaskCompletionSource.TrySetResult(true); job.TaskCompletionSource.TrySetResult(true);
} }

View File

@ -4,7 +4,6 @@ using System.Threading.Tasks;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Net;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace MediaBrowser.MediaEncoding.Encoder namespace MediaBrowser.MediaEncoding.Encoder
@ -40,7 +39,6 @@ namespace MediaBrowser.MediaEncoding.Encoder
_mediaSourceManager = mediaSourceManager; _mediaSourceManager = mediaSourceManager;
Id = Guid.NewGuid(); Id = Guid.NewGuid();
_logger = logger;
TaskCompletionSource = new TaskCompletionSource<bool>(); TaskCompletionSource = new TaskCompletionSource<bool>();
} }

View File

@ -1,13 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -19,17 +16,13 @@ namespace MediaBrowser.MediaEncoding.Encoder
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager; private readonly ILibraryManager _libraryManager;
private readonly IMediaSourceManager _mediaSourceManager; private readonly IMediaSourceManager _mediaSourceManager;
private readonly IConfigurationManager _config;
private readonly IMediaEncoder _mediaEncoder; private readonly IMediaEncoder _mediaEncoder;
protected static readonly CultureInfo UsCulture = new CultureInfo("en-US"); public EncodingJobFactory(ILogger logger, ILibraryManager libraryManager, IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder)
public EncodingJobFactory(ILogger logger, ILibraryManager libraryManager, IMediaSourceManager mediaSourceManager, IConfigurationManager config, IMediaEncoder mediaEncoder)
{ {
_logger = logger; _logger = logger;
_libraryManager = libraryManager; _libraryManager = libraryManager;
_mediaSourceManager = mediaSourceManager; _mediaSourceManager = mediaSourceManager;
_config = config;
_mediaEncoder = mediaEncoder; _mediaEncoder = mediaEncoder;
} }
@ -118,11 +111,11 @@ namespace MediaBrowser.MediaEncoding.Encoder
if (state.OutputVideoBitrate.HasValue) if (state.OutputVideoBitrate.HasValue)
{ {
var resolution = ResolutionNormalizer.Normalize( var resolution = ResolutionNormalizer.Normalize(
state.VideoStream == null ? (int?)null : state.VideoStream.BitRate, state.VideoStream?.BitRate,
state.VideoStream == null ? (int?)null : state.VideoStream.Width, state.VideoStream?.Width,
state.VideoStream == null ? (int?)null : state.VideoStream.Height, state.VideoStream?.Height,
state.OutputVideoBitrate.Value, state.OutputVideoBitrate.Value,
state.VideoStream == null ? null : state.VideoStream.Codec, state.VideoStream?.Codec,
state.OutputVideoCodec, state.OutputVideoCodec,
videoRequest.MaxWidth, videoRequest.MaxWidth,
videoRequest.MaxHeight); videoRequest.MaxHeight);
@ -144,40 +137,6 @@ namespace MediaBrowser.MediaEncoding.Encoder
return state; return state;
} }
protected EncodingOptions GetEncodingOptions()
{
return _config.GetConfiguration<EncodingOptions>("encoding");
}
/// <summary>
/// Infers the video codec.
/// </summary>
/// <param name="container">The container.</param>
/// <returns>System.Nullable{VideoCodecs}.</returns>
private static string InferVideoCodec(string container)
{
var ext = "." + (container ?? string.Empty);
if (string.Equals(ext, ".asf", StringComparison.OrdinalIgnoreCase))
{
return "wmv";
}
if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
{
return "vpx";
}
if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
{
return "theora";
}
if (string.Equals(ext, ".m3u8", StringComparison.OrdinalIgnoreCase) || string.Equals(ext, ".ts", StringComparison.OrdinalIgnoreCase))
{
return "h264";
}
return "copy";
}
private string InferAudioCodec(string container) private string InferAudioCodec(string container)
{ {
var ext = "." + (container ?? string.Empty); var ext = "." + (container ?? string.Empty);
@ -186,31 +145,19 @@ namespace MediaBrowser.MediaEncoding.Encoder
{ {
return "mp3"; return "mp3";
} }
if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase)) else if (string.Equals(ext, ".aac", StringComparison.OrdinalIgnoreCase))
{ {
return "aac"; return "aac";
} }
if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase)) else if (string.Equals(ext, ".wma", StringComparison.OrdinalIgnoreCase))
{ {
return "wma"; return "wma";
} }
if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase)) else if (string.Equals(ext, ".ogg", StringComparison.OrdinalIgnoreCase)
{ || string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase)
return "vorbis"; || string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase)
} || string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase)
if (string.Equals(ext, ".oga", StringComparison.OrdinalIgnoreCase)) || string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
{
return "vorbis";
}
if (string.Equals(ext, ".ogv", StringComparison.OrdinalIgnoreCase))
{
return "vorbis";
}
if (string.Equals(ext, ".webm", StringComparison.OrdinalIgnoreCase))
{
return "vorbis";
}
if (string.Equals(ext, ".webma", StringComparison.OrdinalIgnoreCase))
{ {
return "vorbis"; return "vorbis";
} }
@ -218,35 +165,6 @@ namespace MediaBrowser.MediaEncoding.Encoder
return "copy"; return "copy";
} }
/// <summary>
/// Determines whether the specified stream is H264.
/// </summary>
/// <param name="stream">The stream.</param>
/// <returns><c>true</c> if the specified stream is H264; otherwise, <c>false</c>.</returns>
protected bool IsH264(MediaStream stream)
{
var codec = stream.Codec ?? string.Empty;
return codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 ||
codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1;
}
private static int GetVideoProfileScore(string profile)
{
var list = new List<string>
{
"Constrained Baseline",
"Baseline",
"Extended",
"Main",
"High",
"Progressive High",
"Constrained High"
};
return Array.FindIndex(list.ToArray(), t => string.Equals(t, profile, StringComparison.OrdinalIgnoreCase));
}
private void ApplyDeviceProfileSettings(EncodingJob state) private void ApplyDeviceProfileSettings(EncodingJob state)
{ {
var profile = state.Options.DeviceProfile; var profile = state.Options.DeviceProfile;