Skip to content

Commit a4f6ce9

Browse files
committed
Extract closed-captions with youtube-dl when this info is not available in player_response.
This includes videos flagged as "inappropriate or offensive to some audiences".
1 parent e648353 commit a4f6ce9

File tree

1 file changed

+62
-51
lines changed

1 file changed

+62
-51
lines changed

lib/WWW/PipeViewer.pm

+62-51
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ sub set_lwp_useragent {
351351
my $cookies = HTTP::Cookies->new();
352352

353353
# Consent cookie
354-
$cookies->set_cookie(0, "CONSENT", "YES+cb-m.20210516-15-p0.en+FX+687",
354+
$cookies->set_cookie(0, "CONSENT", "YES+cb-m.20210615-14-p0.en+FX+096",
355355
"/", ".youtube.com", undef, 0, 1, '21' . join('', map { int(rand(10)) } 1 .. 8),
356356
0, {});
357357

@@ -1196,6 +1196,64 @@ sub _make_translated_captions {
11961196
return @asr;
11971197
}
11981198

1199+
sub _fallback_extract_captions {
1200+
my ($self, $videoID) = @_;
1201+
1202+
if ($self->get_debug) {
1203+
say STDERR ":: Extracting closed-caption URLs with `youtube-dl`...";
1204+
}
1205+
1206+
# Extract closed-caption URLs with youtube-dl if our code failed
1207+
my $ytdl_info = $self->_info_from_ytdl($videoID);
1208+
1209+
my @caption_urls;
1210+
1211+
if (defined($ytdl_info) and ref($ytdl_info) eq 'HASH') {
1212+
1213+
my $has_subtitles = 0;
1214+
1215+
foreach my $key (qw(subtitles automatic_captions)) {
1216+
1217+
my $ccaps = $ytdl_info->{$key} // next;
1218+
1219+
ref($ccaps) eq 'HASH' or next;
1220+
1221+
foreach my $lang_code (sort keys %$ccaps) {
1222+
1223+
my ($caption_info) = grep { $_->{ext} eq 'srv1' } @{$ccaps->{$lang_code}};
1224+
1225+
if (defined($caption_info) and ref($caption_info) eq 'HASH' and defined($caption_info->{url})) {
1226+
1227+
push @caption_urls,
1228+
scalar {
1229+
kind => ($key eq 'automatic_captions' ? 'asr' : ''),
1230+
languageCode => $lang_code,
1231+
baseUrl => $caption_info->{url},
1232+
};
1233+
1234+
if ($key eq 'subtitles') {
1235+
$has_subtitles = 1;
1236+
}
1237+
}
1238+
}
1239+
1240+
last if $has_subtitles;
1241+
}
1242+
1243+
# Auto-translated captions
1244+
if ($has_subtitles) {
1245+
1246+
if ($self->get_debug) {
1247+
say STDERR ":: Generating translated closed-caption URLs...";
1248+
}
1249+
1250+
push @caption_urls, $self->_make_translated_captions(\@caption_urls);
1251+
}
1252+
}
1253+
1254+
return @caption_urls;
1255+
}
1256+
11991257
=head2 get_streaming_urls($videoID)
12001258
12011259
Returns a list of streaming URLs for a videoID.
@@ -1233,59 +1291,11 @@ sub get_streaming_urls {
12331291
# Try again with youtube-dl
12341292
if (!@streaming_urls or (($caption_data->{playabilityStatus}{status} // '') =~ /fail|error/i)) {
12351293
@streaming_urls = $self->_fallback_extract_urls($videoID);
1294+
push @caption_urls, $self->_fallback_extract_captions($videoID);
12361295
}
12371296
}
12381297
else {
1239-
1240-
if ($self->get_debug) {
1241-
say STDERR ":: Extracting closed-caption URLs with `youtube-dl`...";
1242-
}
1243-
1244-
# Extract closed-caption URLs with youtube-dl if our code failed
1245-
my $ytdl_info = $self->_info_from_ytdl($videoID);
1246-
1247-
if (defined($ytdl_info) and ref($ytdl_info) eq 'HASH') {
1248-
1249-
my $has_subtitles = 0;
1250-
1251-
foreach my $key (qw(subtitles automatic_captions)) {
1252-
1253-
my $ccaps = $ytdl_info->{$key} // next;
1254-
1255-
ref($ccaps) eq 'HASH' or next;
1256-
1257-
foreach my $lang_code (sort keys %$ccaps) {
1258-
1259-
my ($caption_info) = grep { $_->{ext} eq 'srv1' } @{$ccaps->{$lang_code}};
1260-
1261-
if (defined($caption_info) and ref($caption_info) eq 'HASH' and defined($caption_info->{url})) {
1262-
1263-
push @caption_urls,
1264-
scalar {
1265-
kind => ($key eq 'automatic_captions' ? 'asr' : ''),
1266-
languageCode => $lang_code,
1267-
baseUrl => $caption_info->{url},
1268-
};
1269-
1270-
if ($key eq 'subtitles') {
1271-
$has_subtitles = 1;
1272-
}
1273-
}
1274-
}
1275-
1276-
last if $has_subtitles;
1277-
}
1278-
1279-
# Auto-translated captions
1280-
if ($has_subtitles) {
1281-
1282-
if ($self->get_debug) {
1283-
say STDERR ":: Generating translated closed-caption URLs...";
1284-
}
1285-
1286-
push @caption_urls, $self->_make_translated_captions(\@caption_urls);
1287-
}
1288-
}
1298+
push @caption_urls, $self->_fallback_extract_captions($videoID);
12891299
}
12901300

12911301
if ($self->get_debug) {
@@ -1296,6 +1306,7 @@ sub get_streaming_urls {
12961306
# Try again with youtube-dl
12971307
if (!@streaming_urls or (($info{status} // '') =~ /fail|error/i)) {
12981308
@streaming_urls = $self->_fallback_extract_urls($videoID);
1309+
push @caption_urls, $self->_fallback_extract_captions($videoID);
12991310
}
13001311

13011312
if ($self->get_prefer_mp4 or $self->get_prefer_av1) {

0 commit comments

Comments
 (0)