Refactor copy codec checks

This commit is contained in:
xumix 2020-05-26 00:58:19 +03:00 committed by xumix
parent 777c9c7bc9
commit 46420dfd68
8 changed files with 56 additions and 38 deletions

View File

@ -284,8 +284,8 @@ namespace MediaBrowser.Api
Width = state.OutputWidth, Width = state.OutputWidth,
Height = state.OutputHeight, Height = state.OutputHeight,
AudioChannels = state.OutputAudioChannels, AudioChannels = state.OutputAudioChannels,
IsAudioDirect = string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase), IsAudioDirect = EncodingHelper.IsCopyCodec(state.OutputAudioCodec),
IsVideoDirect = string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase), IsVideoDirect = EncodingHelper.IsCopyCodec(state.OutputVideoCodec),
TranscodeReasons = state.TranscodeReasons TranscodeReasons = state.TranscodeReasons
}); });
} }

View File

@ -193,7 +193,7 @@ namespace MediaBrowser.Api.Playback
await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false); await AcquireResources(state, cancellationTokenSource).ConfigureAwait(false);
if (state.VideoRequest != null && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (state.VideoRequest != null && !EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
{ {
var auth = AuthorizationContext.GetAuthorizationInfo(Request); var auth = AuthorizationContext.GetAuthorizationInfo(Request);
if (auth.User != null && !auth.User.Policy.EnableVideoPlaybackTranscoding) if (auth.User != null && !auth.User.Policy.EnableVideoPlaybackTranscoding)
@ -243,9 +243,9 @@ namespace MediaBrowser.Api.Playback
var logFilePrefix = "ffmpeg-transcode"; var logFilePrefix = "ffmpeg-transcode";
if (state.VideoRequest != null if (state.VideoRequest != null
&& string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) && EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
{ {
logFilePrefix = string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase) logFilePrefix = EncodingHelper.IsCopyCodec(state.OutputAudioCodec)
? "ffmpeg-remux" : "ffmpeg-directstream"; ? "ffmpeg-remux" : "ffmpeg-directstream";
} }
@ -328,7 +328,7 @@ namespace MediaBrowser.Api.Playback
state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks && state.RunTimeTicks.Value >= TimeSpan.FromMinutes(5).Ticks &&
state.IsInputVideo && state.IsInputVideo &&
state.VideoType == VideoType.VideoFile && state.VideoType == VideoType.VideoFile &&
!string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase); !EncodingHelper.IsCopyCodec(state.OutputVideoCodec);
} }
return false; return false;
@ -791,7 +791,7 @@ namespace MediaBrowser.Api.Playback
EncodingHelper.TryStreamCopy(state); EncodingHelper.TryStreamCopy(state);
} }
if (state.OutputVideoBitrate.HasValue && !string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (state.OutputVideoBitrate.HasValue && !EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
{ {
var resolution = ResolutionNormalizer.Normalize( var resolution = ResolutionNormalizer.Normalize(
state.VideoStream?.BitRate, state.VideoStream?.BitRate,

View File

@ -700,12 +700,12 @@ namespace MediaBrowser.Api.Playback.Hls
return false; return false;
} }
if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
{ {
return false; return false;
} }
if (string.Equals(state.OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(state.OutputAudioCodec))
{ {
return false; return false;
} }
@ -728,7 +728,7 @@ namespace MediaBrowser.Api.Playback.Hls
private int? GetOutputVideoCodecLevel(StreamState state) private int? GetOutputVideoCodecLevel(StreamState state)
{ {
string levelString; string levelString;
if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase) if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec)
&& state.VideoStream.Level.HasValue) && state.VideoStream.Level.HasValue)
{ {
levelString = state.VideoStream?.Level.ToString(); levelString = state.VideoStream?.Level.ToString();
@ -1008,7 +1008,7 @@ namespace MediaBrowser.Api.Playback.Hls
if (!state.IsOutputVideo) if (!state.IsOutputVideo)
{ {
if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(audioCodec))
{ {
return "-acodec copy"; return "-acodec copy";
} }
@ -1036,11 +1036,11 @@ namespace MediaBrowser.Api.Playback.Hls
return string.Join(" ", audioTranscodeParams.ToArray()); return string.Join(" ", audioTranscodeParams.ToArray());
} }
if (string.Equals(audioCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(audioCodec))
{ {
var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions); var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions);
if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase) && state.EnableBreakOnNonKeyFrames(videoCodec)) if (EncodingHelper.IsCopyCodec(videoCodec) && state.EnableBreakOnNonKeyFrames(videoCodec))
{ {
return "-codec:a:0 copy -copypriorss:a:0 0"; return "-codec:a:0 copy -copypriorss:a:0 0";
} }
@ -1091,7 +1091,7 @@ namespace MediaBrowser.Api.Playback.Hls
// } // }
// See if we can save come cpu cycles by avoiding encoding // See if we can save come cpu cycles by avoiding encoding
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(codec))
{ {
if (state.VideoStream != null && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) if (state.VideoStream != null && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
{ {

View File

@ -72,7 +72,7 @@ namespace MediaBrowser.Api.Playback.Hls
{ {
var codec = EncodingHelper.GetAudioEncoder(state); var codec = EncodingHelper.GetAudioEncoder(state);
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(codec))
{ {
return "-codec:a:0 copy"; return "-codec:a:0 copy";
} }

View File

@ -42,7 +42,7 @@ namespace MediaBrowser.Api.Playback
return Request.SegmentLength.Value; return Request.SegmentLength.Value;
} }
if (string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
var userAgent = UserAgent ?? string.Empty; var userAgent = UserAgent ?? string.Empty;

View File

@ -1338,7 +1338,7 @@ namespace MediaBrowser.Controller.MediaEncoding
transcoderChannelLimit = 6; transcoderChannelLimit = 6;
} }
var isTranscodingAudio = !string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase); var isTranscodingAudio = !EncodingHelper.IsCopyCodec(codec);
int? resultChannels = state.GetRequestedAudioChannels(codec); int? resultChannels = state.GetRequestedAudioChannels(codec);
if (isTranscodingAudio) if (isTranscodingAudio)
@ -1734,7 +1734,8 @@ namespace MediaBrowser.Controller.MediaEncoding
var hasTextSubs = state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode; var hasTextSubs = state.SubtitleStream != null && state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;
if (string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase) || (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) && !hasTextSubs) if ((string.Equals(videoEncoder, "h264_vaapi", StringComparison.OrdinalIgnoreCase)
|| (string.Equals(videoEncoder, "h264_qsv", StringComparison.OrdinalIgnoreCase) && !hasTextSubs))
&& width.HasValue && width.HasValue
&& height.HasValue) && height.HasValue)
{ {
@ -1991,7 +1992,7 @@ namespace MediaBrowser.Controller.MediaEncoding
filters.Add("hwupload"); filters.Add("hwupload");
} }
// When the input may or may not be hardware QSV decodable // When the input may or may not be hardware QSV decodable
else if (string.Equals(outputVideoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase)) else if (string.Equals(outputVideoCodec, "h264_qsv", StringComparison.OrdinalIgnoreCase))
{ {
if (!hasTextSubs) if (!hasTextSubs)
@ -2248,7 +2249,7 @@ namespace MediaBrowser.Controller.MediaEncoding
flags.Add("+ignidx"); flags.Add("+ignidx");
} }
if (state.GenPtsInput || string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (state.GenPtsInput || EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
{ {
flags.Add("+genpts"); flags.Add("+genpts");
} }
@ -2511,7 +2512,7 @@ namespace MediaBrowser.Controller.MediaEncoding
/// </summary> /// </summary>
protected string GetHardwareAcceleratedVideoDecoder(EncodingJobInfo state, EncodingOptions encodingOptions) protected string GetHardwareAcceleratedVideoDecoder(EncodingJobInfo state, EncodingOptions encodingOptions)
{ {
if (string.Equals(state.OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(state.OutputVideoCodec))
{ {
return null; return null;
} }
@ -2799,7 +2800,7 @@ namespace MediaBrowser.Controller.MediaEncoding
args += " -mpegts_m2ts_mode 1"; args += " -mpegts_m2ts_mode 1";
} }
if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(videoCodec))
{ {
if (state.VideoStream != null if (state.VideoStream != null
&& string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) && string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase)
@ -2901,7 +2902,7 @@ namespace MediaBrowser.Controller.MediaEncoding
var args = "-codec:a:0 " + codec; var args = "-codec:a:0 " + codec;
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(codec))
{ {
return args; return args;
} }
@ -2973,5 +2974,10 @@ namespace MediaBrowser.Controller.MediaEncoding
string.Empty, string.Empty,
string.Empty).Trim(); string.Empty).Trim();
} }
public static bool IsCopyCodec(string codec)
{
return string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase);
}
} }
} }

View File

@ -302,7 +302,7 @@ namespace MediaBrowser.Controller.MediaEncoding
return false; return false;
} }
return BaseRequest.BreakOnNonKeyFrames && string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase); return BaseRequest.BreakOnNonKeyFrames && EncodingHelper.IsCopyCodec(videoCodec);
} }
return false; return false;
@ -367,7 +367,7 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
if (BaseRequest.Static if (BaseRequest.Static
|| string.Equals(OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) || EncodingHelper.IsCopyCodec(OutputAudioCodec))
{ {
if (AudioStream != null) if (AudioStream != null)
{ {
@ -390,7 +390,7 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
if (BaseRequest.Static if (BaseRequest.Static
|| string.Equals(OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) || EncodingHelper.IsCopyCodec(OutputAudioCodec))
{ {
if (AudioStream != null) if (AudioStream != null)
{ {
@ -409,7 +409,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
get get
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.Level; return VideoStream?.Level;
} }
@ -433,7 +433,7 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
if (BaseRequest.Static if (BaseRequest.Static
|| string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.BitDepth; return VideoStream?.BitDepth;
} }
@ -451,7 +451,7 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
if (BaseRequest.Static if (BaseRequest.Static
|| string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.RefFrames; return VideoStream?.RefFrames;
} }
@ -468,7 +468,7 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
if (BaseRequest.Static if (BaseRequest.Static
|| string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream == null ? null : (VideoStream.AverageFrameRate ?? VideoStream.RealFrameRate); return VideoStream == null ? null : (VideoStream.AverageFrameRate ?? VideoStream.RealFrameRate);
} }
@ -499,7 +499,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
get get
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.PacketLength; return VideoStream?.PacketLength;
} }
@ -515,7 +515,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
get get
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.Profile; return VideoStream?.Profile;
} }
@ -535,7 +535,7 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
if (BaseRequest.Static if (BaseRequest.Static
|| string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.CodecTag; return VideoStream?.CodecTag;
} }
@ -549,7 +549,7 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
if (BaseRequest.Static if (BaseRequest.Static
|| string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.IsAnamorphic; return VideoStream?.IsAnamorphic;
} }
@ -562,7 +562,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
get get
{ {
if (string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.Codec; return VideoStream?.Codec;
} }
@ -575,7 +575,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
get get
{ {
if (string.Equals(OutputAudioCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (EncodingHelper.IsCopyCodec(OutputAudioCodec))
{ {
return AudioStream?.Codec; return AudioStream?.Codec;
} }
@ -589,7 +589,7 @@ namespace MediaBrowser.Controller.MediaEncoding
get get
{ {
if (BaseRequest.Static if (BaseRequest.Static
|| string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.IsInterlaced; return VideoStream?.IsInterlaced;
} }
@ -607,7 +607,7 @@ namespace MediaBrowser.Controller.MediaEncoding
{ {
get get
{ {
if (BaseRequest.Static || string.Equals(OutputVideoCodec, "copy", StringComparison.OrdinalIgnoreCase)) if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
{ {
return VideoStream?.IsAVC; return VideoStream?.IsAVC;
} }

View File

@ -5,10 +5,15 @@ namespace MediaBrowser.Model.Configuration
public class EncodingOptions public class EncodingOptions
{ {
public int EncodingThreadCount { get; set; } public int EncodingThreadCount { get; set; }
public string TranscodingTempPath { get; set; } public string TranscodingTempPath { get; set; }
public double DownMixAudioBoost { get; set; } public double DownMixAudioBoost { get; set; }
public bool EnableThrottling { get; set; } public bool EnableThrottling { get; set; }
public int ThrottleDelaySeconds { get; set; } public int ThrottleDelaySeconds { get; set; }
public string HardwareAccelerationType { get; set; } public string HardwareAccelerationType { get; set; }
/// <summary> /// <summary>
@ -20,12 +25,19 @@ namespace MediaBrowser.Model.Configuration
/// The current FFmpeg path being used by the system and displayed on the transcode page. /// The current FFmpeg path being used by the system and displayed on the transcode page.
/// </summary> /// </summary>
public string EncoderAppPathDisplay { get; set; } public string EncoderAppPathDisplay { get; set; }
public string VaapiDevice { get; set; } public string VaapiDevice { get; set; }
public int H264Crf { get; set; } public int H264Crf { get; set; }
public int H265Crf { get; set; } public int H265Crf { get; set; }
public string EncoderPreset { get; set; } public string EncoderPreset { get; set; }
public string DeinterlaceMethod { get; set; } public string DeinterlaceMethod { get; set; }
public bool EnableHardwareEncoding { get; set; } public bool EnableHardwareEncoding { get; set; }
public bool EnableSubtitleExtraction { get; set; } public bool EnableSubtitleExtraction { get; set; }
public string[] HardwareDecodingCodecs { get; set; } public string[] HardwareDecodingCodecs { get; set; }