From 029be321d1c951abdf79cfea038f50ec399fdfb9 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Fri, 6 May 2022 23:18:45 +0200 Subject: [PATCH] Respect limited opus sampling rates when building trancoding command --- .../MediaEncoding/EncodingHelper.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs index 1851a9f0c5..54b41a6d25 100644 --- a/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs +++ b/MediaBrowser.Controller/MediaEncoding/EncodingHelper.cs @@ -5370,12 +5370,22 @@ namespace MediaBrowser.Controller.MediaEncoding audioTranscodeParams.Add("-ac " + state.OutputAudioChannels.Value.ToString(CultureInfo.InvariantCulture)); } - // opus will fail on 44100 if (!string.Equals(state.OutputAudioCodec, "opus", StringComparison.OrdinalIgnoreCase)) { - if (state.OutputAudioSampleRate.HasValue) + // opus only supports specific sampling rates + var sampleRate = state.OutputAudioSampleRate; + if (sampleRate.HasValue) { - audioTranscodeParams.Add("-ar " + state.OutputAudioSampleRate.Value.ToString(CultureInfo.InvariantCulture)); + var sampleRateValue = sampleRate.Value switch + { + <= 8000 => 8000, + <= 12000 => 12000, + <= 16000 => 16000, + <= 24000 => 24000, + _ => 48000 + }; + + audioTranscodeParams.Add("-ar " + sampleRateValue.ToString(CultureInfo.InvariantCulture)); } }