aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2017-02-02 11:02:01 -0500
committerLuke Pulverenti <luke.pulverenti@gmail.com>2017-02-02 11:02:01 -0500
commit5edaf12d40188bc4d3b9544fcd1905e6ec1c459a (patch)
tree2eaa2c93b6e90297f0adb183d4a0353b4c896023 /MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs
parent39e8e3cbe7865ba618c67c612a191b134f5ad186 (diff)
move encoding methods to shared classes
Diffstat (limited to 'MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs')
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs798
1 files changed, 7 insertions, 791 deletions
diff --git a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs
index 7654ec1d7..b0b37f2d6 100644
--- a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs
@@ -36,6 +36,8 @@ namespace MediaBrowser.MediaEncoding.Encoder
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
+ protected EncodingHelper EncodingHelper;
+
protected BaseEncoder(MediaEncoder mediaEncoder,
ILogger logger,
IServerConfigurationManager configurationManager,
@@ -56,6 +58,8 @@ namespace MediaBrowser.MediaEncoding.Encoder
SubtitleEncoder = subtitleEncoder;
MediaSourceManager = mediaSourceManager;
ProcessFactory = processFactory;
+
+ EncodingHelper = new EncodingHelper(MediaEncoder, ConfigurationManager, FileSystem, SubtitleEncoder);
}
public async Task<EncodingJob> Start(EncodingJobOptions options,
@@ -63,7 +67,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
CancellationToken cancellationToken)
{
var encodingJob = await new EncodingJobFactory(Logger, LibraryManager, MediaSourceManager, ConfigurationManager)
- .CreateJob(options, IsVideoEncoder, progress, cancellationToken).ConfigureAwait(false);
+ .CreateJob(options, EncodingHelper, IsVideoEncoder, progress, cancellationToken).ConfigureAwait(false);
encodingJob.OutputFilePath = GetOutputFilePath(encodingJob);
FileSystem.CreateDirectory(Path.GetDirectoryName(encodingJob.OutputFilePath));
@@ -286,72 +290,6 @@ namespace MediaBrowser.MediaEncoding.Encoder
}
/// <summary>
- /// Gets the number of threads.
- /// </summary>
- /// <returns>System.Int32.</returns>
- protected int GetNumberOfThreads(EncodingJob job, bool isWebm)
- {
- return job.Options.CpuCoreLimit ?? 0;
- }
-
- protected string GetInputModifier(EncodingJob state, bool genPts = true)
- {
- var inputModifier = string.Empty;
-
- var probeSize = GetProbeSizeArgument(state);
- inputModifier += " " + probeSize;
- inputModifier = inputModifier.Trim();
-
- var userAgentParam = GetUserAgentParam(state);
-
- if (!string.IsNullOrWhiteSpace(userAgentParam))
- {
- inputModifier += " " + userAgentParam;
- }
-
- inputModifier = inputModifier.Trim();
-
- inputModifier += " " + GetFastSeekCommandLineParameter(state.Options);
- inputModifier = inputModifier.Trim();
-
- if (state.IsVideoRequest && genPts)
- {
- inputModifier += " -fflags +genpts";
- }
-
- if (!string.IsNullOrEmpty(state.InputAudioSync))
- {
- inputModifier += " -async " + state.InputAudioSync;
- }
-
- if (!string.IsNullOrEmpty(state.InputVideoSync))
- {
- inputModifier += " -vsync " + state.InputVideoSync;
- }
-
- if (state.ReadInputAtNativeFramerate)
- {
- inputModifier += " -re";
- }
-
- var videoDecoder = GetVideoDecoder(state);
- if (!string.IsNullOrWhiteSpace(videoDecoder))
- {
- inputModifier += " " + videoDecoder;
- }
-
- //if (state.IsVideoRequest)
- //{
- // if (string.Equals(state.OutputContainer, "mkv", StringComparison.OrdinalIgnoreCase))
- // {
- // //inputModifier += " -noaccurate_seek";
- // }
- //}
-
- return inputModifier;
- }
-
- /// <summary>
/// Gets the name of the output video codec
/// </summary>
/// <param name="state">The state.</param>
@@ -405,130 +343,6 @@ namespace MediaBrowser.MediaEncoding.Encoder
return null;
}
- private string GetUserAgentParam(EncodingJob state)
- {
- string useragent = null;
-
- state.RemoteHttpHeaders.TryGetValue("User-Agent", out useragent);
-
- if (!string.IsNullOrWhiteSpace(useragent))
- {
- return "-user-agent \"" + useragent + "\"";
- }
-
- return string.Empty;
- }
-
- /// <summary>
- /// Gets the probe size argument.
- /// </summary>
- /// <param name="state">The state.</param>
- /// <returns>System.String.</returns>
- private string GetProbeSizeArgument(EncodingJob state)
- {
- if (state.PlayableStreamFileNames.Count > 0)
- {
- return MediaEncoder.GetProbeSizeAndAnalyzeDurationArgument(state.PlayableStreamFileNames.ToArray(), state.InputProtocol);
- }
-
- return MediaEncoder.GetProbeSizeAndAnalyzeDurationArgument(new[] { state.MediaPath }, state.InputProtocol);
- }
-
- /// <summary>
- /// Gets the fast seek command line parameter.
- /// </summary>
- /// <param name="request">The request.</param>
- /// <returns>System.String.</returns>
- /// <value>The fast seek command line parameter.</value>
- protected string GetFastSeekCommandLineParameter(EncodingJobOptions request)
- {
- var time = request.StartTimeTicks ?? 0;
-
- if (time > 0)
- {
- return string.Format("-ss {0}", MediaEncoder.GetTimeParameter(time));
- }
-
- return string.Empty;
- }
-
- /// <summary>
- /// Gets the input argument.
- /// </summary>
- /// <param name="state">The state.</param>
- /// <returns>System.String.</returns>
- protected string GetInputArgument(EncodingJob state)
- {
- var arg = string.Format("-i {0}", GetInputPathArgument(state));
-
- if (state.SubtitleStream != null && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode)
- {
- if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
- {
- if (state.VideoStream != null && state.VideoStream.Width.HasValue)
- {
- // This is hacky but not sure how to get the exact subtitle resolution
- double height = state.VideoStream.Width.Value;
- height /= 16;
- height *= 9;
-
- arg += string.Format(" -canvas_size {0}:{1}", state.VideoStream.Width.Value.ToString(CultureInfo.InvariantCulture), Convert.ToInt32(height).ToString(CultureInfo.InvariantCulture));
- }
-
- var subtitlePath = state.SubtitleStream.Path;
-
- if (string.Equals(Path.GetExtension(subtitlePath), ".sub", StringComparison.OrdinalIgnoreCase))
- {
- var idxFile = Path.ChangeExtension(subtitlePath, ".idx");
- if (FileSystem.FileExists(idxFile))
- {
- subtitlePath = idxFile;
- }
- }
-
- arg += " -i \"" + subtitlePath + "\"";
- }
- }
-
- if (state.IsVideoRequest)
- {
- var encodingOptions = GetEncodingOptions();
- var videoEncoder = EncodingJobFactory.GetVideoEncoder(MediaEncoder, state, encodingOptions);
- if (videoEncoder.IndexOf("vaapi", StringComparison.OrdinalIgnoreCase) != -1)
- {
- var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode;
- var hwOutputFormat = "vaapi";
-
- if (hasGraphicalSubs)
- {
- hwOutputFormat = "yuv420p";
- }
-
- arg = "-hwaccel vaapi -hwaccel_output_format " + hwOutputFormat + " -vaapi_device " + encodingOptions.VaapiDevice + " " + arg;
- }
- }
-
- return arg.Trim();
- }
-
- private string GetInputPathArgument(EncodingJob state)
- {
- var protocol = state.InputProtocol;
- var mediaPath = state.MediaPath ?? string.Empty;
-
- var inputPath = new[] { mediaPath };
-
- if (state.IsInputVideo)
- {
- if (!(state.VideoType == VideoType.Iso && state.IsoMount == null))
- {
- inputPath = MediaEncoderHelpers.GetInputArgument(FileSystem, mediaPath, state.InputProtocol, state.IsoMount, state.PlayableStreamFileNames);
- }
- }
-
- return MediaEncoder.GetInputArgument(inputPath, protocol);
- }
-
private async Task AcquireResources(EncodingJob state, CancellationToken cancellationToken)
{
if (state.VideoType == VideoType.Iso && state.IsoType.HasValue && IsoManager.CanMount(state.MediaPath))
@@ -544,11 +358,11 @@ namespace MediaBrowser.MediaEncoding.Encoder
}, false, cancellationToken).ConfigureAwait(false);
- AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, state.Options);
+ EncodingHelper.AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, null);
if (state.IsVideoRequest)
{
- EncodingJobFactory.TryStreamCopy(state, state.Options);
+ EncodingHelper.TryStreamCopy(state);
}
}
@@ -557,603 +371,5 @@ namespace MediaBrowser.MediaEncoding.Encoder
await Task.Delay(state.MediaSource.BufferMs.Value, cancellationToken).ConfigureAwait(false);
}
}
-
- private void AttachMediaSourceInfo(EncodingJob state,
- MediaSourceInfo mediaSource,
- EncodingJobOptions videoRequest)
- {
- EncodingJobFactory.AttachMediaSourceInfo(state, mediaSource, videoRequest);
- }
-
- /// <summary>
- /// Gets the internal graphical subtitle param.
- /// </summary>
- /// <param name="state">The state.</param>
- /// <param name="outputVideoCodec">The output video codec.</param>
- /// <returns>System.String.</returns>
- protected async Task<string> GetGraphicalSubtitleParam(EncodingJob state, string outputVideoCodec)
- {
- var outputSizeParam = string.Empty;
-
- var request = state.Options;
-
- // Add resolution params, if specified
- if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
- {
- outputSizeParam = await GetOutputSizeParam(state, outputVideoCodec).ConfigureAwait(false);
- outputSizeParam = outputSizeParam.TrimEnd('"');
-
- if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
- {
- outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("format", StringComparison.OrdinalIgnoreCase));
- }
- else
- {
- outputSizeParam = "," + outputSizeParam.Substring(outputSizeParam.IndexOf("scale", StringComparison.OrdinalIgnoreCase));
- }
- }
-
- if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase) && outputSizeParam.Length == 0)
- {
- outputSizeParam = ",format=nv12|vaapi,hwupload";
- }
-
- var videoSizeParam = string.Empty;
-
- if (state.VideoStream != null && state.VideoStream.Width.HasValue && state.VideoStream.Height.HasValue)
- {
- videoSizeParam = string.Format(",scale={0}:{1}", state.VideoStream.Width.Value.ToString(UsCulture), state.VideoStream.Height.Value.ToString(UsCulture));
- }
-
- var mapPrefix = state.SubtitleStream.IsExternal ?
- 1 :
- 0;
-
- var subtitleStreamIndex = state.SubtitleStream.IsExternal
- ? 0
- : state.SubtitleStream.Index;
-
- return string.Format(" -filter_complex \"[{0}:{1}]format=yuva444p{4},lut=u=128:v=128:y=gammaval(.3)[sub] ; [0:{2}] [sub] overlay{3}\"",
- mapPrefix.ToString(UsCulture),
- subtitleStreamIndex.ToString(UsCulture),
- state.VideoStream.Index.ToString(UsCulture),
- outputSizeParam,
- videoSizeParam);
- }
-
- /// <summary>
- /// Gets the video bitrate to specify on the command line
- /// </summary>
- /// <param name="state">The state.</param>
- /// <param name="videoEncoder">The video codec.</param>
- /// <returns>System.String.</returns>
- protected string GetVideoQualityParam(EncodingJob state, string videoEncoder)
- {
- var param = string.Empty;
-
- var isVc1 = state.VideoStream != null &&
- string.Equals(state.VideoStream.Codec, "vc1", StringComparison.OrdinalIgnoreCase);
-
- if (string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase))
- {
- param = "-preset superfast";
-
- param += " -crf 23";
- }
-
- else if (string.Equals(videoEncoder, "libx265", StringComparison.OrdinalIgnoreCase))
- {
- param = "-preset fast";
-
- param += " -crf 28";
- }
-
- // h264 (h264_qsv)
- else if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase))
- {
- param = "-preset 7 -look_ahead 0";
-
- }
-
- // h264 (h264_nvenc)
- else if (string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase))
- {
- param = "-preset llhq";
- }
-
- // webm
- else if (string.Equals(videoEncoder, "libvpx", StringComparison.OrdinalIgnoreCase))
- {
- // Values 0-3, 0 being highest quality but slower
- var profileScore = 0;
-
- string crf;
- var qmin = "0";
- var qmax = "50";
-
- crf = "10";
-
- if (isVc1)
- {
- profileScore++;
- }
-
- // Max of 2
- profileScore = Math.Min(profileScore, 2);
-
- // http://www.webmproject.org/docs/encoder-parameters/
- param = string.Format("-speed 16 -quality good -profile:v {0} -slices 8 -crf {1} -qmin {2} -qmax {3}",
- profileScore.ToString(UsCulture),
- crf,
- qmin,
- qmax);
- }
-
- else if (string.Equals(videoEncoder, "mpeg4", StringComparison.OrdinalIgnoreCase))
- {
- param = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
- }
-
- // asf/wmv
- else if (string.Equals(videoEncoder, "wmv2", StringComparison.OrdinalIgnoreCase))
- {
- param = "-qmin 2";
- }
-
- else if (string.Equals(videoEncoder, "msmpeg4", StringComparison.OrdinalIgnoreCase))
- {
- param = "-mbd 2";
- }
-
- param += GetVideoBitrateParam(state, videoEncoder);
-
- var framerate = GetFramerateParam(state);
- if (framerate.HasValue)
- {
- param += string.Format(" -r {0}", framerate.Value.ToString(UsCulture));
- }
-
- if (!string.IsNullOrEmpty(state.OutputVideoSync))
- {
- param += " -vsync " + state.OutputVideoSync;
- }
-
- if (!string.IsNullOrEmpty(state.Options.Profile))
- {
- if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase) &&
- !string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
- {
- // not supported by h264_omx
- param += " -profile:v " + state.Options.Profile;
- }
- }
-
- var levelString = state.Options.Level.HasValue ? state.Options.Level.Value.ToString(CultureInfo.InvariantCulture) : null;
-
- if (!string.IsNullOrEmpty(levelString))
- {
- levelString = NormalizeTranscodingLevel(state.OutputVideoCodec, levelString);
-
- // h264_qsv and h264_nvenc expect levels to be expressed as a decimal. libx264 supports decimal and non-decimal format
- // also needed for libx264 due to https://trac.ffmpeg.org/ticket/3307
- if (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) ||
- string.Equals(videoEncoder, "h264_nvenc", StringComparison.OrdinalIgnoreCase) ||
- string.Equals(videoEncoder, "libx264", StringComparison.OrdinalIgnoreCase))
- {
- switch (levelString)
- {
- case "30":
- param += " -level 3.0";
- break;
- case "31":
- param += " -level 3.1";
- break;
- case "32":
- param += " -level 3.2";
- break;
- case "40":
- param += " -level 4.0";
- break;
- case "41":
- param += " -level 4.1";
- break;
- case "42":
- param += " -level 4.2";
- break;
- case "50":
- param += " -level 5.0";
- break;
- case "51":
- param += " -level 5.1";
- break;
- case "52":
- param += " -level 5.2";
- break;
- default:
- param += " -level " + levelString;
- break;
- }
- }
- else if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase))
- {
- param += " -level " + levelString;
- }
- }
-
- if (!string.Equals(videoEncoder, "h264_omx", StringComparison.OrdinalIgnoreCase) &&
- !string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) &&
- !string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
- {
- param = "-pix_fmt yuv420p " + param;
- }
-
- return param;
- }
-
- private string NormalizeTranscodingLevel(string videoCodec, string level)
- {
- double requestLevel;
-
- // Clients may direct play higher than level 41, but there's no reason to transcode higher
- if (double.TryParse(level, NumberStyles.Any, UsCulture, out requestLevel))
- {
- if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
- {
- if (requestLevel > 41)
- {
- return "41";
- }
- }
- }
-
- return level;
- }
-
- protected string GetVideoBitrateParam(EncodingJob state, string videoCodec)
- {
- var bitrate = state.OutputVideoBitrate;
-
- if (bitrate.HasValue)
- {
- if (string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase))
- {
- // With vpx when crf is used, b:v becomes a max rate
- // https://trac.ffmpeg.org/wiki/vpxEncodingGuide. But higher bitrate source files -b:v causes judder so limite the bitrate but dont allow it to "saturate" the bitrate. So dont contrain it down just up.
- return string.Format(" -maxrate:v {0} -bufsize:v ({0}*2) -b:v {0}", bitrate.Value.ToString(UsCulture));
- }
-
- if (string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
- {
- return string.Format(" -b:v {0}", bitrate.Value.ToString(UsCulture));
- }
-
- if (string.Equals(videoCodec, "libx264", StringComparison.OrdinalIgnoreCase))
- {
- // h264
- return string.Format(" -maxrate {0} -bufsize {1}",
- bitrate.Value.ToString(UsCulture),
- (bitrate.Value * 2).ToString(UsCulture));
- }
-
- // h264
- return string.Format(" -b:v {0} -maxrate {0} -bufsize {1}",
- bitrate.Value.ToString(UsCulture),
- (bitrate.Value * 2).ToString(UsCulture));
- }
-
- return string.Empty;
- }
-
- protected double? GetFramerateParam(EncodingJob state)
- {
- if (state.Options != null)
- {
- if (state.Options.Framerate.HasValue)
- {
- return state.Options.Framerate.Value;
- }
-
- var maxrate = state.Options.MaxFramerate;
-
- if (maxrate.HasValue && state.VideoStream != null)
- {
- var contentRate = state.VideoStream.AverageFrameRate ?? state.VideoStream.RealFrameRate;
-
- if (contentRate.HasValue && contentRate.Value > maxrate.Value)
- {
- return maxrate;
- }
- }
- }
-
- return null;
- }
-
- /// <summary>
- /// Gets the map args.
- /// </summary>
- /// <param name="state">The state.</param>
- /// <returns>System.String.</returns>
- protected virtual string GetMapArgs(EncodingJob state)
- {
- // If we don't have known media info
- // If input is video, use -sn to drop subtitles
- // Otherwise just return empty
- if (state.VideoStream == null && state.AudioStream == null)
- {
- return state.IsInputVideo ? "-sn" : string.Empty;
- }
-
- // We have media info, but we don't know the stream indexes
- if (state.VideoStream != null && state.VideoStream.Index == -1)
- {
- return "-sn";
- }
-
- // We have media info, but we don't know the stream indexes
- if (state.AudioStream != null && state.AudioStream.Index == -1)
- {
- return state.IsInputVideo ? "-sn" : string.Empty;
- }
-
- var args = string.Empty;
-
- if (state.VideoStream != null)
- {
- args += string.Format("-map 0:{0}", state.VideoStream.Index);
- }
- else
- {
- // No known video stream
- args += "-vn";
- }
-
- if (state.AudioStream != null)
- {
- args += string.Format(" -map 0:{0}", state.AudioStream.Index);
- }
-
- else
- {
- args += " -map -0:a";
- }
-
- if (state.SubtitleStream == null || state.Options.SubtitleMethod == SubtitleDeliveryMethod.Hls)
- {
- args += " -map -0:s";
- }
- else if (state.SubtitleStream.IsExternal && !state.SubtitleStream.IsTextSubtitleStream)
- {
- args += " -map 1:0 -sn";
- }
-
- return args;
- }
-
- /// <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;
- }
-
- /// <summary>
- /// If we're going to put a fixed size on the command line, this will calculate it
- /// </summary>
- /// <param name="state">The state.</param>
- /// <param name="outputVideoCodec">The output video codec.</param>
- /// <param name="allowTimeStampCopy">if set to <c>true</c> [allow time stamp copy].</param>
- /// <returns>System.String.</returns>
- protected async Task<string> GetOutputSizeParam(EncodingJob state,
- string outputVideoCodec,
- bool allowTimeStampCopy = true)
- {
- // http://sonnati.wordpress.com/2012/10/19/ffmpeg-the-swiss-army-knife-of-internet-streaming-part-vi/
-
- var request = state.Options;
-
- var filters = new List<string>();
-
- if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
- {
- filters.Add("format=nv12|vaapi");
- filters.Add("hwupload");
- }
- else if (state.DeInterlace && !string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
- {
- filters.Add("yadif=0:-1:0");
- }
-
- if (string.Equals(outputVideoCodec, "h264_vaapi", StringComparison.OrdinalIgnoreCase))
- {
- // Work around vaapi's reduced scaling features
- var scaler = "scale_vaapi";
-
- // Given the input dimensions (inputWidth, inputHeight), determine the output dimensions
- // (outputWidth, outputHeight). The user may request precise output dimensions or maximum
- // output dimensions. Output dimensions are guaranteed to be even.
- decimal inputWidth = Convert.ToDecimal(state.VideoStream.Width);
- decimal inputHeight = Convert.ToDecimal(state.VideoStream.Height);
- decimal outputWidth = request.Width.HasValue ? Convert.ToDecimal(request.Width.Value) : inputWidth;
- decimal outputHeight = request.Height.HasValue ? Convert.ToDecimal(request.Height.Value) : inputHeight;
- decimal maximumWidth = request.MaxWidth.HasValue ? Convert.ToDecimal(request.MaxWidth.Value) : outputWidth;
- decimal maximumHeight = request.MaxHeight.HasValue ? Convert.ToDecimal(request.MaxHeight.Value) : outputHeight;
-
- if (outputWidth > maximumWidth || outputHeight > maximumHeight)
- {
- var scale = Math.Min(maximumWidth / outputWidth, maximumHeight / outputHeight);
- outputWidth = Math.Min(maximumWidth, Math.Truncate(outputWidth * scale));
- outputHeight = Math.Min(maximumHeight, Math.Truncate(outputHeight * scale));
- }
-
- outputWidth = 2 * Math.Truncate(outputWidth / 2);
- outputHeight = 2 * Math.Truncate(outputHeight / 2);
-
- if (outputWidth != inputWidth || outputHeight != inputHeight)
- {
- filters.Add(string.Format("{0}=w={1}:h={2}", scaler, outputWidth.ToString(UsCulture), outputHeight.ToString(UsCulture)));
- }
- }
- else
- {
- // If fixed dimensions were supplied
- if (request.Width.HasValue && request.Height.HasValue)
- {
- var widthParam = request.Width.Value.ToString(UsCulture);
- var heightParam = request.Height.Value.ToString(UsCulture);
-
- filters.Add(string.Format("scale=trunc({0}/2)*2:trunc({1}/2)*2", widthParam, heightParam));
- }
-
- // If Max dimensions were supplied, for width selects lowest even number between input width and width req size and selects lowest even number from in width*display aspect and requested size
- else if (request.MaxWidth.HasValue && request.MaxHeight.HasValue)
- {
- var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
- var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
-
- filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,min({0}\\,{1}*dar))/2)*2:trunc(min(max(iw/dar\\,ih)\\,min({0}/dar\\,{1}))/2)*2", maxWidthParam, maxHeightParam));
- }
-
- // If a fixed width was requested
- else if (request.Width.HasValue)
- {
- var widthParam = request.Width.Value.ToString(UsCulture);
-
- filters.Add(string.Format("scale={0}:trunc(ow/a/2)*2", widthParam));
- }
-
- // If a fixed height was requested
- else if (request.Height.HasValue)
- {
- var heightParam = request.Height.Value.ToString(UsCulture);
-
- filters.Add(string.Format("scale=trunc(oh*a/2)*2:{0}", heightParam));
- }
-
- // If a max width was requested
- else if (request.MaxWidth.HasValue)
- {
- var maxWidthParam = request.MaxWidth.Value.ToString(UsCulture);
-
- filters.Add(string.Format("scale=trunc(min(max(iw\\,ih*dar)\\,{0})/2)*2:trunc(ow/dar/2)*2", maxWidthParam));
- }
-
- // If a max height was requested
- else if (request.MaxHeight.HasValue)
- {
- var maxHeightParam = request.MaxHeight.Value.ToString(UsCulture);
-
- filters.Add(string.Format("scale=trunc(oh*a/2)*2:min(max(iw/dar\\,ih)\\,{0})", maxHeightParam));
- }
- }
-
- var output = string.Empty;
-
- if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode)
- {
- var subParam = await GetTextSubtitleParam(state).ConfigureAwait(false);
-
- filters.Add(subParam);
-
- if (allowTimeStampCopy)
- {
- output += " -copyts";
- }
- }
-
- if (filters.Count > 0)
- {
- output += string.Format(" -vf \"{0}\"", string.Join(",", filters.ToArray()));
- }
-
- return output;
- }
-
- /// <summary>
- /// Gets the text subtitle param.
- /// </summary>
- /// <param name="state">The state.</param>
- /// <returns>System.String.</returns>
- protected async Task<string> GetTextSubtitleParam(EncodingJob state)
- {
- var seconds = Math.Round(TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds);
-
- if (state.SubtitleStream.IsExternal)
- {
- var subtitlePath = state.SubtitleStream.Path;
-
- var charsetParam = string.Empty;
-
- if (!string.IsNullOrEmpty(state.SubtitleStream.Language))
- {
- var charenc = await SubtitleEncoder.GetSubtitleFileCharacterSet(subtitlePath, state.SubtitleStream.Language, state.MediaSource.Protocol, CancellationToken.None).ConfigureAwait(false);
-
- if (!string.IsNullOrEmpty(charenc))
- {
- charsetParam = ":charenc=" + charenc;
- }
- }
-
- // TODO: Perhaps also use original_size=1920x800 ??
- return string.Format("subtitles=filename='{0}'{1},setpts=PTS -{2}/TB",
- MediaEncoder.EscapeSubtitleFilterPath(subtitlePath),
- charsetParam,
- seconds.ToString(UsCulture));
- }
-
- var mediaPath = state.MediaPath ?? string.Empty;
-
- return string.Format("subtitles='{0}:si={1}',setpts=PTS -{2}/TB",
- MediaEncoder.EscapeSubtitleFilterPath(mediaPath),
- state.InternalSubtitleStreamOffset.ToString(UsCulture),
- seconds.ToString(UsCulture));
- }
-
- protected string GetAudioFilterParam(EncodingJob state, bool isHls)
- {
- var volParam = string.Empty;
- var audioSampleRate = string.Empty;
-
- var channels = state.OutputAudioChannels;
-
- // Boost volume to 200% when downsampling from 6ch to 2ch
- if (channels.HasValue && channels.Value <= 2)
- {
- if (state.AudioStream != null && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5 && !GetEncodingOptions().DownMixAudioBoost.Equals(1))
- {
- volParam = ",volume=" + GetEncodingOptions().DownMixAudioBoost.ToString(UsCulture);
- }
- }
-
- if (state.OutputAudioSampleRate.HasValue)
- {
- audioSampleRate = state.OutputAudioSampleRate.Value + ":";
- }
-
- var adelay = isHls ? "adelay=1," : string.Empty;
-
- var pts = string.Empty;
-
- if (state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.Options.SubtitleMethod == SubtitleDeliveryMethod.Encode && !state.Options.CopyTimestamps)
- {
- var seconds = TimeSpan.FromTicks(state.Options.StartTimeTicks ?? 0).TotalSeconds;
-
- pts = string.Format(",asetpts=PTS-{0}/TB", Math.Round(seconds).ToString(UsCulture));
- }
-
- return string.Format("-af \"{0}aresample={1}async={4}{2}{3}\"",
-
- adelay,
- audioSampleRate,
- volParam,
- pts,
- state.OutputAudioSync);
- }
}
}