diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index 37ab123667..b62af554e7 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -16,6 +16,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.IO; +using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Net; using MediaBrowser.Model.Diagnostics; using MediaBrowser.Model.IO; @@ -829,23 +830,4 @@ namespace MediaBrowser.Api } } } - - /// - /// Enum TranscodingJobType - /// - public enum TranscodingJobType - { - /// - /// The progressive - /// - Progressive, - /// - /// The HLS - /// - Hls, - /// - /// The dash - /// - Dash - } } diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index bd4442a590..43f25145e0 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -890,6 +890,11 @@ namespace MediaBrowser.Api.Playback.Hls args += " -copyts"; } + if (!string.IsNullOrEmpty(state.OutputVideoSync)) + { + args += " -vsync " + state.OutputVideoSync; + } + return args; } @@ -932,7 +937,7 @@ namespace MediaBrowser.Api.Playback.Hls } var videoCodec = EncodingHelper.GetVideoEncoder(state, ApiEntryPoint.Instance.GetEncodingOptions()); - var breakOnNonKeyFrames = state.Request.BreakOnNonKeyFrames && string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase); + var breakOnNonKeyFrames = state.EnableBreakOnNonKeyFrames(videoCodec); var breakOnNonKeyFramesArg = breakOnNonKeyFrames ? " -break_non_keyframes 1" : ""; diff --git a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs index 03291670b5..ae049a83a4 100644 --- a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs +++ b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using MediaBrowser.Common.IO; +using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Model.IO; using MediaBrowser.Model.Services; diff --git a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs index 8b828ddfe8..71b1dc1b15 100644 --- a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs @@ -85,36 +85,42 @@ namespace MediaBrowser.Api.Playback.Hls if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase)) { // if h264_mp4toannexb is ever added, do not use it for live tv - if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) + if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && + !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) { args += " -bsf:v h264_mp4toannexb"; } - args += " -flags -global_header"; - return args; } + else + { + var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"", + state.SegmentLength.ToString(UsCulture)); - var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"", - state.SegmentLength.ToString(UsCulture)); + var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode; - var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode; + var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions(); + args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultH264Preset()) + keyFrameArg; - var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions(); - args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultH264Preset()) + keyFrameArg; + // Add resolution params, if specified + if (!hasGraphicalSubs) + { + args += EncodingHelper.GetOutputSizeParam(state, codec); + } - // Add resolution params, if specified - if (!hasGraphicalSubs) - { - args += EncodingHelper.GetOutputSizeParam(state, codec); + // This is for internal graphical subs + if (hasGraphicalSubs) + { + args += EncodingHelper.GetGraphicalSubtitleParam(state, codec); + } } - // This is for internal graphical subs - if (hasGraphicalSubs) + args += " -flags -global_header"; + + if (!string.IsNullOrEmpty(state.OutputVideoSync)) { - args += EncodingHelper.GetGraphicalSubtitleParam(state, codec); + args += " -vsync " + state.OutputVideoSync; } - args += " -flags -global_header"; - return args; } diff --git a/MediaBrowser.Api/Playback/StreamRequest.cs b/MediaBrowser.Api/Playback/StreamRequest.cs index d02b4bae63..176b68f375 100644 --- a/MediaBrowser.Api/Playback/StreamRequest.cs +++ b/MediaBrowser.Api/Playback/StreamRequest.cs @@ -42,7 +42,6 @@ namespace MediaBrowser.Api.Playback public int? SegmentLength { get; set; } public int? MinSegments { get; set; } - public bool BreakOnNonKeyFrames { get; set; } } public class VideoStreamRequest : StreamRequest diff --git a/MediaBrowser.Api/Playback/StreamState.cs b/MediaBrowser.Api/Playback/StreamState.cs index 2f5f6227a9..7f146466d4 100644 --- a/MediaBrowser.Api/Playback/StreamState.cs +++ b/MediaBrowser.Api/Playback/StreamState.cs @@ -104,14 +104,6 @@ namespace MediaBrowser.Api.Playback } } - public bool IsSegmentedLiveStream - { - get - { - return TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue; - } - } - public int HlsListSize { get @@ -121,14 +113,12 @@ namespace MediaBrowser.Api.Playback } public string UserAgent { get; set; } - public TranscodingJobType TranscodingType { get; set; } public StreamState(IMediaSourceManager mediaSourceManager, ILogger logger, TranscodingJobType transcodingType) - : base(logger) + : base(logger, transcodingType) { _mediaSourceManager = mediaSourceManager; _logger = logger; - TranscodingType = transcodingType; } public string MimeType { get; set; } diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs index 90ec5aac7d..78e54289ea 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs @@ -663,11 +663,6 @@ namespace MediaBrowser.Controller.MediaEncoding param += string.Format(" -r {0}", framerate.Value.ToString(_usCulture)); } - if (!string.IsNullOrEmpty(state.OutputVideoSync)) - { - param += " -vsync " + state.OutputVideoSync; - } - var request = state.BaseRequest; if (!string.IsNullOrEmpty(request.Profile)) @@ -1571,10 +1566,14 @@ namespace MediaBrowser.Controller.MediaEncoding if (state.IsVideoRequest) { + var outputVideoCodec = GetVideoEncoder(state, encodingOptions); + // Important: If this is ever re-enabled, make sure not to use it with wtv because it breaks seeking - if (string.Equals(state.OutputContainer, "mkv", StringComparison.OrdinalIgnoreCase) && state.CopyTimestamps) + if (!string.Equals(state.InputContainer, "wtv", StringComparison.OrdinalIgnoreCase) && + state.TranscodingType != TranscodingJobType.Progressive && + state.EnableBreakOnNonKeyFrames(outputVideoCodec)) { - //inputModifier += " -noaccurate_seek"; + inputModifier += " -noaccurate_seek"; } if (!string.IsNullOrWhiteSpace(state.InputContainer) && state.VideoType == VideoType.VideoFile && string.IsNullOrWhiteSpace(encodingOptions.HardwareAccelerationType)) @@ -1878,7 +1877,9 @@ namespace MediaBrowser.Controller.MediaEncoding if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase)) { - if (state.VideoStream != null && IsH264(state.VideoStream) && string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) + if (state.VideoStream != null && IsH264(state.VideoStream) && + string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) && + !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase)) { args += " -bsf:v h264_mp4toannexb"; } @@ -1892,51 +1893,56 @@ namespace MediaBrowser.Controller.MediaEncoding { args += " -flags -global_header -fflags +genpts"; } - - return args; } + else + { + var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"", + 5.ToString(_usCulture)); - var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"", - 5.ToString(_usCulture)); - - args += keyFrameArg; + args += keyFrameArg; - var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode; + var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode; - var hasCopyTs = false; - // Add resolution params, if specified - if (!hasGraphicalSubs) - { - var outputSizeParam = GetOutputSizeParam(state, videoCodec); - args += outputSizeParam; - hasCopyTs = outputSizeParam.IndexOf("copyts", StringComparison.OrdinalIgnoreCase) != -1; - } + var hasCopyTs = false; + // Add resolution params, if specified + if (!hasGraphicalSubs) + { + var outputSizeParam = GetOutputSizeParam(state, videoCodec); + args += outputSizeParam; + hasCopyTs = outputSizeParam.IndexOf("copyts", StringComparison.OrdinalIgnoreCase) != -1; + } - if (state.RunTimeTicks.HasValue && state.BaseRequest.CopyTimestamps) - { - if (!hasCopyTs) + if (state.RunTimeTicks.HasValue && state.BaseRequest.CopyTimestamps) { - args += " -copyts"; + if (!hasCopyTs) + { + args += " -copyts"; + } + args += " -avoid_negative_ts disabled -start_at_zero"; } - args += " -avoid_negative_ts disabled -start_at_zero"; - } - var qualityParam = GetVideoQualityParam(state, videoCodec, encodingOptions, defaultH264Preset); + var qualityParam = GetVideoQualityParam(state, videoCodec, encodingOptions, defaultH264Preset); - if (!string.IsNullOrEmpty(qualityParam)) - { - args += " " + qualityParam.Trim(); - } + if (!string.IsNullOrEmpty(qualityParam)) + { + args += " " + qualityParam.Trim(); + } - // This is for internal graphical subs - if (hasGraphicalSubs) - { - args += GetGraphicalSubtitleParam(state, videoCodec); + // This is for internal graphical subs + if (hasGraphicalSubs) + { + args += GetGraphicalSubtitleParam(state, videoCodec); + } + + if (!state.RunTimeTicks.HasValue) + { + args += " -flags -global_header"; + } } - if (!state.RunTimeTicks.HasValue) + if (!string.IsNullOrEmpty(state.OutputVideoSync)) { - args += " -flags -global_header"; + args += " -vsync " + state.OutputVideoSync; } return args; diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingJobInfo.cs b/MediaBrowser.Controller/MediaEncoding/EncodingJobInfo.cs index f5878864b6..4e0f223b77 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingJobInfo.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingJobInfo.cs @@ -41,7 +41,20 @@ namespace MediaBrowser.Controller.MediaEncoding public string OutputContainer { get; set; } - public string OutputVideoSync = "-1"; + public string OutputVideoSync + { + get + { + // For live tv + recordings + if (string.Equals(InputContainer, "mpegts", StringComparison.OrdinalIgnoreCase) || + string.Equals(InputContainer, "ts", StringComparison.OrdinalIgnoreCase)) + { + return "cfr"; + } + + return "-1"; + } + } public string OutputAudioSync = "1"; public string InputAudioSync { get; set; } public string InputVideoSync { get; set; } @@ -72,10 +85,12 @@ namespace MediaBrowser.Controller.MediaEncoding public int? OutputAudioSampleRate; public bool DeInterlace { get; set; } public bool IsVideoRequest { get; set; } + public TranscodingJobType TranscodingType { get; set; } - public EncodingJobInfo(ILogger logger) + public EncodingJobInfo(ILogger logger, TranscodingJobType jobType) { _logger = logger; + TranscodingType = jobType; RemoteHttpHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase); PlayableStreamFileNames = new List(); SupportedAudioCodecs = new List(); @@ -83,6 +98,29 @@ namespace MediaBrowser.Controller.MediaEncoding SupportedSubtitleCodecs = new List(); } + public bool IsSegmentedLiveStream + { + get + { + return TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue; + } + } + + public bool EnableBreakOnNonKeyFrames(string videoCodec) + { + if (TranscodingType != TranscodingJobType.Progressive) + { + if (IsSegmentedLiveStream) + { + return false; + } + + return BaseRequest.BreakOnNonKeyFrames && string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase); + } + + return false; + } + /// /// Predicts the audio sample rate that will be in the output stream /// @@ -118,4 +156,23 @@ namespace MediaBrowser.Controller.MediaEncoding public abstract void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate); } + + /// + /// Enum TranscodingJobType + /// + public enum TranscodingJobType + { + /// + /// The progressive + /// + Progressive, + /// + /// The HLS + /// + Hls, + /// + /// The dash + /// + Dash + } } diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs b/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs index 30deae842a..632c350ad0 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingJobOptions.cs @@ -74,6 +74,7 @@ namespace MediaBrowser.Controller.MediaEncoding public bool AllowVideoStreamCopy { get; set; } public bool AllowAudioStreamCopy { get; set; } + public bool BreakOnNonKeyFrames { get; set; } /// /// Gets or sets the audio sample rate. diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs index 98adb6e205..f74dbce985 100644 --- a/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs @@ -57,7 +57,7 @@ namespace MediaBrowser.MediaEncoding.Encoder private readonly IMediaSourceManager _mediaSourceManager; public EncodingJob(ILogger logger, IMediaSourceManager mediaSourceManager) : - base(logger) + base(logger, TranscodingJobType.Progressive) { _logger = logger; _mediaSourceManager = mediaSourceManager;