From 964ad105414a72e06e7a217e1d8fea4870f2aa57 Mon Sep 17 00:00:00 2001 From: Mate Herber Date: Thu, 5 Mar 2026 11:42:35 +0100 Subject: [PATCH] Refactor: Simplify AudioCodecHelper and GetBestAudioStream - Replace nested if/else chain with flat tuple pattern match - Replace manual loop with MaxBy in GetBestAudioStream - Remove unnecessary GetAudioCodecRank wrapper --- .../MediaFiles/MediaInfo/AudioCodec.cs | 192 +++++------------- .../MediaInfo/VideoFileInfoReader.cs | 31 +-- 2 files changed, 55 insertions(+), 168 deletions(-) diff --git a/src/NzbDrone.Core/MediaFiles/MediaInfo/AudioCodec.cs b/src/NzbDrone.Core/MediaFiles/MediaInfo/AudioCodec.cs index ef0cad3bc9..7656bd8b94 100644 --- a/src/NzbDrone.Core/MediaFiles/MediaInfo/AudioCodec.cs +++ b/src/NzbDrone.Core/MediaFiles/MediaInfo/AudioCodec.cs @@ -30,147 +30,63 @@ public static class AudioCodecHelper { public static AudioCodec Resolve(string format, string codecID, string profile) { - format = format ?? string.Empty; - codecID = codecID ?? string.Empty; - profile = profile ?? string.Empty; + format ??= string.Empty; + codecID ??= string.Empty; + profile ??= string.Empty; - if (codecID == "thd+") + return (codecID, format, profile) switch { - return AudioCodec.TrueHDAtmos; - } - - if (format == "truehd") - { - return AudioCodec.TrueHD; - } - - if (format == "flac") - { - return AudioCodec.FLAC; - } - - if (format == "dts") - { - if (profile == "DTS:X") - { - return AudioCodec.DTS_X; - } - - if (profile == "DTS-HD MA") - { - return AudioCodec.DTS_HD_MA; - } - - if (profile == "DTS-ES") - { - return AudioCodec.DTS_ES; - } - - if (profile == "DTS-HD HRA") - { - return AudioCodec.DTS_HD_HRA; - } - - if (profile == "DTS Express") - { - return AudioCodec.DTS_Express; - } - - if (profile == "DTS 96/24") - { - return AudioCodec.DTS_9624; - } - - return AudioCodec.DTS; - } - - if (codecID == "ec+3") - { - return AudioCodec.EAC3Atmos; - } - - if (format == "eac3") - { - return AudioCodec.EAC3; - } - - if (format == "ac3") - { - return AudioCodec.AC3; - } - - if (format == "aac") - { - if (codecID == "A_AAC/MPEG4/LC/SBR") - { - return AudioCodec.HE_AAC; - } - - return AudioCodec.AAC; - } - - if (format == "mp3") - { - return AudioCodec.MP3; - } - - if (format == "mp2") - { - return AudioCodec.MP2; - } - - if (format == "opus") - { - return AudioCodec.Opus; - } - - if (format.StartsWith("pcm_") || format.StartsWith("adpcm_")) - { - return AudioCodec.PCM; - } - - if (format == "vorbis") - { - return AudioCodec.Vorbis; - } - - if (format == "wmav1" || - format == "wmav2" || - format == "wmapro") - { - return AudioCodec.WMA; - } - - return AudioCodec.Unknown; - } - - public static string GetDisplayName(AudioCodec codec) - { - return codec switch - { - AudioCodec.TrueHDAtmos => "TrueHD Atmos", - AudioCodec.TrueHD => "TrueHD", - AudioCodec.DTS_X => "DTS-X", - AudioCodec.DTS_HD_MA => "DTS-HD MA", - AudioCodec.DTS_HD_HRA => "DTS-HD HRA", - AudioCodec.DTS_ES => "DTS-ES", - AudioCodec.DTS_Express => "DTS Express", - AudioCodec.DTS_9624 => "DTS 96/24", - AudioCodec.DTS => "DTS", - AudioCodec.EAC3Atmos => "EAC3 Atmos", - AudioCodec.EAC3 => "EAC3", - AudioCodec.FLAC => "FLAC", - AudioCodec.AC3 => "AC3", - AudioCodec.HE_AAC => "HE-AAC", - AudioCodec.AAC => "AAC", - AudioCodec.MP3 => "MP3", - AudioCodec.MP2 => "MP2", - AudioCodec.Opus => "Opus", - AudioCodec.PCM => "PCM", - AudioCodec.Vorbis => "Vorbis", - AudioCodec.WMA => "WMA", - _ => string.Empty + ("thd+", _, _) => AudioCodec.TrueHDAtmos, + ("ec+3", _, _) => AudioCodec.EAC3Atmos, + (_, "truehd", _) => AudioCodec.TrueHD, + (_, "flac", _) => AudioCodec.FLAC, + (_, "dts", "DTS:X") => AudioCodec.DTS_X, + (_, "dts", "DTS-HD MA") => AudioCodec.DTS_HD_MA, + (_, "dts", "DTS-HD HRA") => AudioCodec.DTS_HD_HRA, + (_, "dts", "DTS-ES") => AudioCodec.DTS_ES, + (_, "dts", "DTS Express") => AudioCodec.DTS_Express, + (_, "dts", "DTS 96/24") => AudioCodec.DTS_9624, + (_, "dts", _) => AudioCodec.DTS, + (_, "eac3", _) => AudioCodec.EAC3, + (_, "ac3", _) => AudioCodec.AC3, + ("A_AAC/MPEG4/LC/SBR", "aac", _) => AudioCodec.HE_AAC, + (_, "aac", _) => AudioCodec.AAC, + (_, "mp3", _) => AudioCodec.MP3, + (_, "mp2", _) => AudioCodec.MP2, + (_, "opus", _) => AudioCodec.Opus, + (_, "vorbis", _) => AudioCodec.Vorbis, + (_, "wmav1", _) => AudioCodec.WMA, + (_, "wmav2", _) => AudioCodec.WMA, + (_, "wmapro", _) => AudioCodec.WMA, + _ when format.StartsWith("pcm_") || format.StartsWith("adpcm_") => AudioCodec.PCM, + _ => AudioCodec.Unknown }; } + + public static string GetDisplayName(AudioCodec codec) => codec switch + { + AudioCodec.TrueHDAtmos => "TrueHD Atmos", + AudioCodec.TrueHD => "TrueHD", + AudioCodec.DTS_X => "DTS-X", + AudioCodec.DTS_HD_MA => "DTS-HD MA", + AudioCodec.DTS_HD_HRA => "DTS-HD HRA", + AudioCodec.DTS_ES => "DTS-ES", + AudioCodec.DTS_Express => "DTS Express", + AudioCodec.DTS_9624 => "DTS 96/24", + AudioCodec.DTS => "DTS", + AudioCodec.EAC3Atmos => "EAC3 Atmos", + AudioCodec.EAC3 => "EAC3", + AudioCodec.FLAC => "FLAC", + AudioCodec.AC3 => "AC3", + AudioCodec.HE_AAC => "HE-AAC", + AudioCodec.AAC => "AAC", + AudioCodec.MP3 => "MP3", + AudioCodec.MP2 => "MP2", + AudioCodec.Opus => "Opus", + AudioCodec.PCM => "PCM", + AudioCodec.Vorbis => "Vorbis", + AudioCodec.WMA => "WMA", + _ => string.Empty + }; } } diff --git a/src/NzbDrone.Core/MediaFiles/MediaInfo/VideoFileInfoReader.cs b/src/NzbDrone.Core/MediaFiles/MediaInfo/VideoFileInfoReader.cs index 4b3d01bd79..aa4ee66bcb 100644 --- a/src/NzbDrone.Core/MediaFiles/MediaInfo/VideoFileInfoReader.cs +++ b/src/NzbDrone.Core/MediaFiles/MediaInfo/VideoFileInfoReader.cs @@ -201,36 +201,7 @@ private FFProbePixelFormat GetPixelFormat(string format) private static AudioStream GetBestAudioStream(List audioStreams) { - if (audioStreams == null || audioStreams.Count == 0) - { - return null; - } - - if (audioStreams.Count == 1) - { - return audioStreams[0]; - } - - AudioStream best = null; - var bestRank = -1; - - foreach (var stream in audioStreams) - { - var rank = GetAudioCodecRank(stream.CodecName, stream.CodecTagString, stream.Profile); - - if (rank > bestRank) - { - bestRank = rank; - best = stream; - } - } - - return best; - } - - private static int GetAudioCodecRank(string format, string codecID, string profile) - { - return (int)AudioCodecHelper.Resolve(format, codecID, profile); + return audioStreams?.MaxBy(stream => AudioCodecHelper.Resolve(stream.CodecName, stream.CodecTagString, stream.Profile)); } public static HdrFormat GetHdrFormat(int bitDepth, string colorPrimaries, string transferFunction, List sideData)