Skip to content

Commit d8f474e

Browse files
committed
When there exists a human-made closed-caption, translate it into different languages.
Extends previous commmit.
1 parent 8387794 commit d8f474e

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

lib/WWW/YoutubeViewer.pm

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,47 +1053,37 @@ sub get_streaming_urls {
10531053
}
10541054
}
10551055
}
1056-
}
10571056

1058-
# When there exists a human-made subtitle, use that for translation
1059-
if (@caption_urls and $has_subtitles) {
1060-
foreach my $caption (@caption_urls) {
1061-
$caption->{baseUrl} =~ s/&kind=asr&/&/;
1062-
}
1057+
last if $has_subtitles;
10631058
}
10641059

10651060
# Auto-translated captions
1066-
if (@caption_urls and not defined $ytdl_info->{automatic_captions}) {
1061+
if ($has_subtitles) {
10671062

10681063
if ($self->get_debug) {
10691064
say STDERR ":: Generating translated closed-caption URLs...";
10701065
}
10711066

1067+
my %trans_languages = map { $_->{languageCode} => 1 } @caption_urls;
1068+
1069+
my @languages = qw(
1070+
af am ar az be bg bn bs ca ceb co cs cy da de el en eo es et eu fa fi fil
1071+
fr fy ga gd gl gu ha haw hi hmn hr ht hu hy id ig is it iw ja jv ka kk km
1072+
kn ko ku ky la lb lo lt lv mg mi mk ml mn mr ms mt my ne nl no ny or pa pl
1073+
ps pt ro ru rw sd si sk sl sm sn so sq sr st su sv sw ta te tg th tk tr tt
1074+
ug uk ur uz vi xh yi yo zh-Hans zh-Hant zu
1075+
);
1076+
1077+
@languages = grep { not exists $trans_languages{$_} } @languages;
1078+
10721079
my @asr;
10731080
foreach my $caption (@caption_urls) {
1074-
if ($caption->{baseUrl} =~ /\basr_langs=([^&]+)/) {
1075-
my @languages = split(/%2C/, $1);
1076-
1077-
push @languages, qw(
1078-
af am ar az be bg bn bs ca ceb co cs cy da de el en eo es et eu fa fi fil
1079-
fr fy ga gd gl gu ha haw hi hmn hr ht hu hy id ig is it iw ja jv ka kk km
1080-
kn ko ku ky la lb lo lt lv mg mi mk ml mn mr ms mt my ne nl no ny or pa pl
1081-
ps pt ro ru rw sd si sk sl sm sn so sq sr st su sv sw ta te tg th tk tr tt
1082-
ug uk ur uz vi xh yi yo zh-Hans zh-Hant zu
1083-
);
1084-
1085-
@languages = do {
1086-
my %seen;
1087-
grep { $_ ne $caption->{languageCode} } grep { !$seen{$_}++ } @languages;
1088-
};
1089-
1090-
foreach my $lang_code (@languages) {
1091-
my %caption_copy = %$caption;
1092-
$caption_copy{languageCode} = $lang_code;
1093-
$caption_copy{kind} = 'asr';
1094-
$caption_copy{baseUrl} = $caption_copy{baseUrl} . "&tlang=$lang_code";
1095-
push @asr, \%caption_copy;
1096-
}
1081+
foreach my $lang_code (@languages) {
1082+
my %caption_copy = %$caption;
1083+
$caption_copy{languageCode} = $lang_code;
1084+
$caption_copy{kind} = 'asr';
1085+
$caption_copy{baseUrl} = $caption_copy{baseUrl} . "&tlang=$lang_code";
1086+
push @asr, \%caption_copy;
10971087
}
10981088
}
10991089

0 commit comments

Comments
 (0)