From 1100e1c368a36c61f975b8e1d7532d925bc9e6e5 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:09:04 +0200 Subject: [PATCH] Expect YouTube to provide 3-letter language codes by itself sometimes --- YoutubeExplode.Converter/Converter.cs | 2 +- .../Utils/Extensions/LanguageExtensions.cs | 21 ++++++++++++------- .../Utils/Extensions/StringExtensions.cs | 14 ++++++++++++- .../Utils/Extensions/StringExtensions.cs | 1 - .../Videos/ClosedCaptions/Language.cs | 3 ++- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/YoutubeExplode.Converter/Converter.cs b/YoutubeExplode.Converter/Converter.cs index b53268f4..2c16f531 100644 --- a/YoutubeExplode.Converter/Converter.cs +++ b/YoutubeExplode.Converter/Converter.cs @@ -137,7 +137,7 @@ private async ValueTask ProcessAsync( // three-letter codes, so we'll try to convert to that first. var languageCode = subtitleInput.Info.Language.TryGetThreeLetterCode() - ?? subtitleInput.Info.Language.GetTwoLetterCode(); + ?? subtitleInput.Info.Language.Code; arguments .Add($"-metadata:s:s:{i}") diff --git a/YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs b/YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs index 7449e7b0..8b999eb4 100644 --- a/YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs +++ b/YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs @@ -1,17 +1,23 @@ -using YoutubeExplode.Videos.ClosedCaptions; +using System; +using YoutubeExplode.Videos.ClosedCaptions; namespace YoutubeExplode.Converter.Utils.Extensions; internal static class LanguageExtensions { - public static string GetTwoLetterCode(this Language language) + public static string? TryGetThreeLetterCode(this Language language) { - var dashIndex = language.Code.IndexOf('-'); - return dashIndex >= 0 ? language.Code[..dashIndex] : language.Code; - } + // YouTube provides either a two-letter or a three-letter language code, + // which may or may not also contain a region identifier. + var regionNeutralLanguageCode = language + .Code + .SubstringUntil("-", StringComparison.OrdinalIgnoreCase); + + // Already a three-letter code + if (regionNeutralLanguageCode.Length == 3) + return regionNeutralLanguageCode; - public static string? TryGetThreeLetterCode(this Language language) => - language.GetTwoLetterCode().ToLowerInvariant() switch + return regionNeutralLanguageCode.ToLowerInvariant() switch { "aa" => "aar", "ab" => "abk", @@ -205,4 +211,5 @@ public static string GetTwoLetterCode(this Language language) "zu" => "zul", _ => null }; + } } diff --git a/YoutubeExplode.Converter/Utils/Extensions/StringExtensions.cs b/YoutubeExplode.Converter/Utils/Extensions/StringExtensions.cs index 3be3a493..4a1c6252 100644 --- a/YoutubeExplode.Converter/Utils/Extensions/StringExtensions.cs +++ b/YoutubeExplode.Converter/Utils/Extensions/StringExtensions.cs @@ -1,7 +1,19 @@ -namespace YoutubeExplode.Converter.Utils.Extensions; +using System; + +namespace YoutubeExplode.Converter.Utils.Extensions; internal static class StringExtensions { public static string? NullIfWhiteSpace(this string s) => !string.IsNullOrWhiteSpace(s) ? s : null; + + public static string SubstringUntil( + this string str, + string sub, + StringComparison comparison = StringComparison.Ordinal + ) + { + var index = str.IndexOf(sub, comparison); + return index < 0 ? str : str[..index]; + } } diff --git a/YoutubeExplode/Utils/Extensions/StringExtensions.cs b/YoutubeExplode/Utils/Extensions/StringExtensions.cs index f23e6041..b067efc1 100644 --- a/YoutubeExplode/Utils/Extensions/StringExtensions.cs +++ b/YoutubeExplode/Utils/Extensions/StringExtensions.cs @@ -18,7 +18,6 @@ public static string SubstringUntil( ) { var index = str.IndexOf(sub, comparison); - return index < 0 ? str : str[..index]; } diff --git a/YoutubeExplode/Videos/ClosedCaptions/Language.cs b/YoutubeExplode/Videos/ClosedCaptions/Language.cs index 0907295b..7f05b4cc 100644 --- a/YoutubeExplode/Videos/ClosedCaptions/Language.cs +++ b/YoutubeExplode/Videos/ClosedCaptions/Language.cs @@ -9,7 +9,8 @@ namespace YoutubeExplode.Videos.ClosedCaptions; public readonly partial struct Language { /// - /// Two-letter (ISO 639-1) language code, possibly with a regional identifier (e.g. 'en' or 'en-US'). + /// Two-letter or three-letter language code, possibly with a regional identifier + /// (e.g. 'en' or 'en-US' or 'eng'). /// public string Code { get; }