Skip to content

Commit

Permalink
Expect YouTube to provide 3-letter language codes by itself sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Nov 29, 2023
1 parent 5d1d867 commit 1100e1c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion YoutubeExplode.Converter/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
21 changes: 14 additions & 7 deletions YoutubeExplode.Converter/Utils/Extensions/LanguageExtensions.cs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -205,4 +211,5 @@ public static string GetTwoLetterCode(this Language language)
"zu" => "zul",
_ => null
};
}
}
14 changes: 13 additions & 1 deletion YoutubeExplode.Converter/Utils/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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];
}
}
1 change: 0 additions & 1 deletion YoutubeExplode/Utils/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static string SubstringUntil(
)
{
var index = str.IndexOf(sub, comparison);

return index < 0 ? str : str[..index];
}

Expand Down
3 changes: 2 additions & 1 deletion YoutubeExplode/Videos/ClosedCaptions/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace YoutubeExplode.Videos.ClosedCaptions;
public readonly partial struct Language
{
/// <summary>
/// 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').
/// </summary>
public string Code { get; }

Expand Down

0 comments on commit 1100e1c

Please sign in to comment.