From 85bbb408464a877fa03bcfe541771856de1980e2 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 23 Nov 2023 17:25:23 +0200 Subject: [PATCH] Don't fail if can't extract three-letter language code on subs --- YoutubeExplode.Converter/Converter.cs | 2 +- .../Utils/Extensions/LanguageExtensions.cs | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/YoutubeExplode.Converter/Converter.cs b/YoutubeExplode.Converter/Converter.cs index a7923ba6..b19e86a2 100644 --- a/YoutubeExplode.Converter/Converter.cs +++ b/YoutubeExplode.Converter/Converter.cs @@ -135,7 +135,7 @@ private async ValueTask ProcessAsync( { arguments .Add($"-metadata:s:s:{i}") - .Add($"language={subtitleInput.Info.Language.GetThreeLetterCode()}") + .Add($"language={subtitleInput.Info.Language.TryGetThreeLetterCode() ?? subtitleInput.Info.Language.GetTwoLetterCode()}") .Add($"-metadata:s:s:{i}") .Add($"title={subtitleInput.Info.Language.Name}"); } diff --git a/YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs b/YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs index 2fda32b3..7449e7b0 100644 --- a/YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs +++ b/YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs @@ -1,5 +1,4 @@ -using System; -using YoutubeExplode.Videos.ClosedCaptions; +using YoutubeExplode.Videos.ClosedCaptions; namespace YoutubeExplode.Converter.Utils.Extensions; @@ -11,7 +10,7 @@ public static string GetTwoLetterCode(this Language language) return dashIndex >= 0 ? language.Code[..dashIndex] : language.Code; } - public static string GetThreeLetterCode(this Language language) => + public static string? TryGetThreeLetterCode(this Language language) => language.GetTwoLetterCode().ToLowerInvariant() switch { "aa" => "aar", @@ -204,6 +203,6 @@ public static string GetThreeLetterCode(this Language language) => "za" => "zha", "zh" => "zho", "zu" => "zul", - var code => throw new InvalidOperationException($"Unrecognized language code '{code}'.") + _ => null }; }