aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.MediaEncoding')
-rw-r--r--MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs74
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs32
-rw-r--r--MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs42
-rw-r--r--MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs2
-rw-r--r--MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs5
5 files changed, 114 insertions, 41 deletions
diff --git a/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs b/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs
index 5bae4fbd5..6ca994fb7 100644
--- a/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs
+++ b/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
+using System.Globalization;
using System.Linq;
using BDInfo;
+using Jellyfin.Extensions;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
@@ -60,21 +62,20 @@ public class BdInfoExaminer : IBlurayExaminer
var sortedStreams = playlist.SortedStreams;
var mediaStreams = new List<MediaStream>(sortedStreams.Count);
- foreach (var stream in sortedStreams)
+ for (int i = 0; i < sortedStreams.Count; i++)
{
+ var stream = sortedStreams[i];
switch (stream)
{
case TSVideoStream videoStream:
- AddVideoStream(mediaStreams, videoStream);
+ AddVideoStream(mediaStreams, i, videoStream);
break;
case TSAudioStream audioStream:
- AddAudioStream(mediaStreams, audioStream);
+ AddAudioStream(mediaStreams, i, audioStream);
break;
- case TSTextStream textStream:
- AddSubtitleStream(mediaStreams, textStream);
- break;
- case TSGraphicsStream graphicStream:
- AddSubtitleStream(mediaStreams, graphicStream);
+ case TSTextStream:
+ case TSGraphicsStream:
+ AddSubtitleStream(mediaStreams, i, stream);
break;
}
}
@@ -96,18 +97,19 @@ public class BdInfoExaminer : IBlurayExaminer
/// Adds the video stream.
/// </summary>
/// <param name="streams">The streams.</param>
+ /// <param name="index">The stream index.</param>
/// <param name="videoStream">The video stream.</param>
- private void AddVideoStream(List<MediaStream> streams, TSVideoStream videoStream)
+ private void AddVideoStream(List<MediaStream> streams, int index, TSVideoStream videoStream)
{
var mediaStream = new MediaStream
{
BitRate = Convert.ToInt32(videoStream.BitRate),
Width = videoStream.Width,
Height = videoStream.Height,
- Codec = videoStream.CodecShortName,
+ Codec = GetNormalizedCodec(videoStream),
IsInterlaced = videoStream.IsInterlaced,
Type = MediaStreamType.Video,
- Index = streams.Count
+ Index = index
};
if (videoStream.FrameRateDenominator > 0)
@@ -125,17 +127,19 @@ public class BdInfoExaminer : IBlurayExaminer
/// Adds the audio stream.
/// </summary>
/// <param name="streams">The streams.</param>
+ /// <param name="index">The stream index.</param>
/// <param name="audioStream">The audio stream.</param>
- private void AddAudioStream(List<MediaStream> streams, TSAudioStream audioStream)
+ private void AddAudioStream(List<MediaStream> streams, int index, TSAudioStream audioStream)
{
var stream = new MediaStream
{
- Codec = audioStream.CodecShortName,
+ Codec = GetNormalizedCodec(audioStream),
Language = audioStream.LanguageCode,
- Channels = audioStream.ChannelCount,
+ ChannelLayout = string.Format(CultureInfo.InvariantCulture, "{0:D}.{1:D}", audioStream.ChannelCount, audioStream.LFE),
+ Channels = audioStream.ChannelCount + audioStream.LFE,
SampleRate = audioStream.SampleRate,
Type = MediaStreamType.Audio,
- Index = streams.Count
+ Index = index
};
var bitrate = Convert.ToInt32(audioStream.BitRate);
@@ -145,11 +149,6 @@ public class BdInfoExaminer : IBlurayExaminer
stream.BitRate = bitrate;
}
- if (audioStream.LFE > 0)
- {
- stream.Channels = audioStream.ChannelCount + 1;
- }
-
streams.Add(stream);
}
@@ -157,31 +156,28 @@ public class BdInfoExaminer : IBlurayExaminer
/// Adds the subtitle stream.
/// </summary>
/// <param name="streams">The streams.</param>
- /// <param name="textStream">The text stream.</param>
- private void AddSubtitleStream(List<MediaStream> streams, TSTextStream textStream)
+ /// <param name="index">The stream index.</param>
+ /// <param name="stream">The stream.</param>
+ private void AddSubtitleStream(List<MediaStream> streams, int index, TSStream stream)
{
streams.Add(new MediaStream
{
- Language = textStream.LanguageCode,
- Codec = textStream.CodecShortName,
+ Language = stream.LanguageCode,
+ Codec = GetNormalizedCodec(stream),
Type = MediaStreamType.Subtitle,
- Index = streams.Count
+ Index = index
});
}
- /// <summary>
- /// Adds the subtitle stream.
- /// </summary>
- /// <param name="streams">The streams.</param>
- /// <param name="textStream">The text stream.</param>
- private void AddSubtitleStream(List<MediaStream> streams, TSGraphicsStream textStream)
- {
- streams.Add(new MediaStream
+ private string GetNormalizedCodec(TSStream stream)
+ => stream.StreamType switch
{
- Language = textStream.LanguageCode,
- Codec = textStream.CodecShortName,
- Type = MediaStreamType.Subtitle,
- Index = streams.Count
- });
- }
+ TSStreamType.MPEG1_VIDEO => "mpeg1video",
+ TSStreamType.MPEG2_VIDEO => "mpeg2video",
+ TSStreamType.VC1_VIDEO => "vc1",
+ TSStreamType.AC3_PLUS_AUDIO or TSStreamType.AC3_PLUS_SECONDARY_AUDIO => "eac3",
+ TSStreamType.DTS_AUDIO or TSStreamType.DTS_HD_AUDIO or TSStreamType.DTS_HD_MASTER_AUDIO or TSStreamType.DTS_HD_SECONDARY_AUDIO => "dts",
+ TSStreamType.PRESENTATION_GRAPHICS => "pgssub",
+ _ => stream.CodecShortName
+ };
}
diff --git a/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs b/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs
index 30bb21dcb..a865b0e4c 100644
--- a/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs
@@ -27,6 +27,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
"msmpeg4",
"dca",
"ac3",
+ "ac4",
"aac",
"mp3",
"flac",
@@ -94,6 +95,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
"h264_v4l2m2m",
"h264_videotoolbox",
"hevc_videotoolbox",
+ "mjpeg_videotoolbox",
"h264_rkmpp",
"hevc_rkmpp"
};
@@ -499,6 +501,11 @@ namespace MediaBrowser.MediaEncoding.Encoder
return output.Contains(keyDesc, StringComparison.Ordinal);
}
+ public bool CheckSupportedHwaccelFlag(string flag)
+ {
+ return !string.IsNullOrEmpty(flag) && GetProcessExitCode(_encoderPath, $"-loglevel quiet -hwaccel_flags +{flag} -hide_banner -f lavfi -i nullsrc=s=1x1:d=100 -f null -");
+ }
+
private IEnumerable<string> GetCodecs(Codec codec)
{
string codecstr = codec == Codec.Encoder ? "encoders" : "decoders";
@@ -604,6 +611,31 @@ namespace MediaBrowser.MediaEncoding.Encoder
}
}
+ private bool GetProcessExitCode(string path, string arguments)
+ {
+ using var process = new Process();
+ process.StartInfo = new ProcessStartInfo(path, arguments)
+ {
+ CreateNoWindow = true,
+ UseShellExecute = false,
+ WindowStyle = ProcessWindowStyle.Hidden,
+ ErrorDialog = false
+ };
+ _logger.LogDebug("Running {Path} {Arguments}", path, arguments);
+
+ try
+ {
+ process.Start();
+ process.WaitForExit();
+ return process.ExitCode == 0;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError("Running {Path} {Arguments} failed with exception {Exception}", path, arguments, ex.Message);
+ return false;
+ }
+ }
+
[GeneratedRegex("^\\s\\S{6}\\s(?<codec>[\\w|-]+)\\s+.+$", RegexOptions.Multiline)]
private static partial Regex CodecRegex();
diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
index d2aaba906..5cfead502 100644
--- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs
@@ -74,6 +74,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
private IDictionary<int, bool> _filtersWithOption = new Dictionary<int, bool>();
private bool _isPkeyPauseSupported = false;
+ private bool _isLowPriorityHwDecodeSupported = false;
private bool _isVaapiDeviceAmd = false;
private bool _isVaapiDeviceInteliHD = false;
@@ -194,6 +195,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
_threads = EncodingHelper.GetNumberOfThreads(null, options, null);
_isPkeyPauseSupported = validator.CheckSupportedRuntimeKey("p pause transcoding");
+ _isLowPriorityHwDecodeSupported = validator.CheckSupportedHwaccelFlag("low_priority");
// Check the Vaapi device vendor
if (OperatingSystem.IsLinux()
@@ -813,12 +815,28 @@ namespace MediaBrowser.MediaEncoding.Encoder
int? threads,
int? qualityScale,
ProcessPriorityClass? priority,
+ bool enableKeyFrameOnlyExtraction,
EncodingHelper encodingHelper,
CancellationToken cancellationToken)
{
var options = allowHwAccel ? _configurationManager.GetEncodingOptions() : new EncodingOptions();
threads ??= _threads;
+ if (allowHwAccel && enableKeyFrameOnlyExtraction)
+ {
+ var supportsKeyFrameOnly = (string.Equals(options.HardwareAccelerationType, "nvenc", StringComparison.OrdinalIgnoreCase) && options.EnableEnhancedNvdecDecoder)
+ || (string.Equals(options.HardwareAccelerationType, "amf", StringComparison.OrdinalIgnoreCase) && OperatingSystem.IsWindows())
+ || (string.Equals(options.HardwareAccelerationType, "qsv", StringComparison.OrdinalIgnoreCase) && options.PreferSystemNativeHwDecoder)
+ || string.Equals(options.HardwareAccelerationType, "vaapi", StringComparison.OrdinalIgnoreCase)
+ || string.Equals(options.HardwareAccelerationType, "videotoolbox", StringComparison.OrdinalIgnoreCase);
+ if (!supportsKeyFrameOnly)
+ {
+ // Disable hardware acceleration when the hardware decoder does not support keyframe only mode.
+ allowHwAccel = false;
+ options = new EncodingOptions();
+ }
+ }
+
// A new EncodingOptions instance must be used as to not disable HW acceleration for all of Jellyfin.
// Additionally, we must set a few fields without defaults to prevent null pointer exceptions.
if (!allowHwAccel)
@@ -868,6 +886,17 @@ namespace MediaBrowser.MediaEncoding.Encoder
inputArg = "-threads " + threads + " " + inputArg; // HW accel args set a different input thread count, only set if disabled
}
+ if (options.HardwareAccelerationType.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase) && _isLowPriorityHwDecodeSupported)
+ {
+ // VideoToolbox supports low priority decoding, which is useful for trickplay
+ inputArg = "-hwaccel_flags +low_priority " + inputArg;
+ }
+
+ if (enableKeyFrameOnlyExtraction)
+ {
+ inputArg = "-skip_frame nokey " + inputArg;
+ }
+
var filterParam = encodingHelper.GetVideoProcessingFilterParam(jobState, options, vidEncoder).Trim();
if (string.IsNullOrWhiteSpace(filterParam))
{
@@ -900,6 +929,14 @@ namespace MediaBrowser.MediaEncoding.Encoder
encoderQuality = (100 - ((qualityScale - 1) * (100 / 30))) / 118;
}
+ if (vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase))
+ {
+ // videotoolbox's mjpeg encoder uses jpeg quality scaled to QP2LAMBDA (118) instead of ffmpeg defined qscale
+ // ffmpeg qscale is a value from 1-31, with 1 being best quality and 31 being worst
+ // jpeg quality is a value from 0-100, with 0 being worst quality and 100 being best
+ encoderQuality = 118 - ((qualityScale - 1) * (118 / 30));
+ }
+
// Output arguments
var targetDirectory = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(targetDirectory);
@@ -908,12 +945,13 @@ namespace MediaBrowser.MediaEncoding.Encoder
// Final command arguments
var args = string.Format(
CultureInfo.InvariantCulture,
- "-loglevel error {0} -an -sn {1} -threads {2} -c:v {3} {4}-f {5} \"{6}\"",
+ "-loglevel error {0} -an -sn {1} -threads {2} -c:v {3} {4}{5}-f {6} \"{7}\"",
inputArg,
filterParam,
outputThreads.GetValueOrDefault(_threads),
vidEncoder,
qualityScale.HasValue ? "-qscale:v " + encoderQuality.Value.ToString(CultureInfo.InvariantCulture) + " " : string.Empty,
+ vidEncoder.Contains("videotoolbox", StringComparison.InvariantCultureIgnoreCase) ? "-allow_sw 1 " : string.Empty, // allow_sw fallback for some intel macs
"image2",
outputPath);
@@ -1212,7 +1250,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
var duration = TimeSpan.FromTicks(mediaInfoResult.RunTimeTicks.Value).TotalSeconds;
// Add file path stanza to concat configuration
- sw.WriteLine("file '{0}'", path);
+ sw.WriteLine("file '{0}'", path.Replace("'", @"'\''", StringComparison.Ordinal));
// Add duration stanza to concat configuration
sw.WriteLine("duration {0}", duration);
diff --git a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
index 3aafb733d..5a5eb6e61 100644
--- a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
+++ b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs
@@ -721,6 +721,8 @@ namespace MediaBrowser.MediaEncoding.Probing
if (streamInfo.CodecType == CodecType.Audio)
{
stream.Type = MediaStreamType.Audio;
+ stream.LocalizedDefault = _localization.GetLocalizedString("Default");
+ stream.LocalizedExternal = _localization.GetLocalizedString("External");
stream.Channels = streamInfo.Channels;
diff --git a/MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs b/MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs
index 0b09e57b5..67a2dddb8 100644
--- a/MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs
+++ b/MediaBrowser.MediaEncoding/Transcoding/TranscodeManager.cs
@@ -470,6 +470,11 @@ public sealed class TranscodeManager : ITranscodeManager, IDisposable
: "FFmpeg.DirectStream-";
}
+ if (state.VideoRequest is null && EncodingHelper.IsCopyCodec(state.OutputAudioCodec))
+ {
+ logFilePrefix = "FFmpeg.Remux-";
+ }
+
var logFilePath = Path.Combine(
_serverConfigurationManager.ApplicationPaths.LogDirectoryPath,
$"{logFilePrefix}{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{state.Request.MediaSourceId}_{Guid.NewGuid().ToString()[..8]}.log");