Skip to content

Commit a272c15

Browse files
committed
Pass the "clientScreen" => "EMBED" argument only when failling without it.
Fixes the URL extraction for videos that have embedding disabled.
1 parent 269a23a commit a272c15

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

lib/WWW/YoutubeViewer.pm

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -917,18 +917,12 @@ sub _old_extract_streaming_urls {
917917
}
918918

919919
sub _extract_streaming_urls {
920-
my ($self, $info, $videoID) = @_;
921-
922-
if (exists $info->{url_encoded_fmt_stream_map}) {
923-
return $self->_old_extract_streaming_urls($info, $videoID);
924-
}
920+
my ($self, $json, $videoID) = @_;
925921

926922
if ($self->get_debug) {
927923
say STDERR ":: Using `player_response` to extract the streaming URLs...";
928924
}
929925

930-
my $json = $self->parse_json_string($info->{player_response} // return);
931-
932926
if ($self->get_debug >= 2) {
933927
require Data::Dump;
934928
Data::Dump::pp($json);
@@ -986,7 +980,7 @@ sub _extract_streaming_urls {
986980
}
987981

988982
sub _get_youtubei_content {
989-
my ($self, $endpoint, $videoID) = @_;
983+
my ($self, $endpoint, $videoID, %args) = @_;
990984

991985
# Valid endpoints: browse, player, next
992986

@@ -1004,9 +998,9 @@ sub _get_youtubei_content {
1004998
"hl" => "en",
1005999
"gl" => "US",
10061000
"clientName" => "WEB",
1007-
"clientScreen" => "EMBED",
10081001
"clientVersion" =>
10091002
sprintf("2.%s.05.00", Time::Piece->new(time)->strftime("%Y%m%d")),
1003+
%args,
10101004
}
10111005
}
10121006
}
@@ -1026,12 +1020,12 @@ sub _old_get_video_info {
10261020
}
10271021

10281022
sub _get_video_info {
1029-
my ($self, $videoID) = @_;
1023+
my ($self, $videoID, %args) = @_;
10301024

10311025
my ($content, %info);
10321026

10331027
for (1 .. 1) {
1034-
$content = $self->_get_youtubei_content('player', $videoID) // return $self->_old_get_video_info($videoID);
1028+
$content = $self->_get_youtubei_content('player', $videoID, %args) // return $self->_old_get_video_info($videoID);
10351029
%info = (player_response => $content);
10361030
}
10371031

@@ -1138,37 +1132,36 @@ Returns a list of streaming URLs for a videoID.
11381132
sub get_streaming_urls {
11391133
my ($self, $videoID) = @_;
11401134

1141-
my %info = $self->_get_video_info($videoID);
1142-
my @streaming_urls = $self->_extract_streaming_urls(\%info, $videoID);
1135+
my %info = $self->_get_video_info($videoID);
1136+
my $json = defined($info{player_response}) ? $self->parse_json_string($info{player_response}) : {};
11431137

1144-
my @caption_urls;
1138+
if (not defined $json->{streamingData}) {
1139+
say STDERR ":: Trying to bypass age-restricted gate..." if $self->get_debug;
1140+
%info = $self->_get_video_info($videoID, "clientScreen" => "EMBED");
1141+
$json = defined($info{player_response}) ? $self->parse_json_string($info{player_response}) : {};
1142+
}
11451143

1146-
if (defined $info{player_response}) {
1144+
my @streaming_urls = $self->_extract_streaming_urls($json, $videoID);
11471145

1148-
my $captions_json = $info{player_response}; # don't run uri_unescape() on this
1149-
my $caption_data = $self->parse_json_string($captions_json);
1146+
my @caption_urls;
11501147

1151-
if (eval { ref($caption_data->{captions}{playerCaptionsTracklistRenderer}{captionTracks}) eq 'ARRAY' }) {
1148+
if (eval { ref($json->{captions}{playerCaptionsTracklistRenderer}{captionTracks}) eq 'ARRAY' }) {
11521149

1153-
my @caption_tracks = @{$caption_data->{captions}{playerCaptionsTracklistRenderer}{captionTracks}};
1154-
my @human_made_cc = grep { ($_->{kind} // '') ne 'asr' } @caption_tracks;
1150+
my @caption_tracks = @{$json->{captions}{playerCaptionsTracklistRenderer}{captionTracks}};
1151+
my @human_made_cc = grep { ($_->{kind} // '') ne 'asr' } @caption_tracks;
11551152

1156-
push @caption_urls, @human_made_cc, @caption_tracks;
1153+
push @caption_urls, @human_made_cc, @caption_tracks;
11571154

1158-
foreach my $caption (@caption_urls) {
1159-
$caption->{baseUrl} =~ s{\bfmt=srv[0-9]\b}{fmt=srv1}g;
1160-
}
1161-
1162-
push @caption_urls, $self->_make_translated_captions(\@caption_urls);
1155+
foreach my $caption (@caption_urls) {
1156+
$caption->{baseUrl} =~ s{\bfmt=srv[0-9]\b}{fmt=srv1}g;
11631157
}
11641158

1165-
# Try again with youtube-dl
1166-
if (!@streaming_urls or (($caption_data->{playabilityStatus}{status} // '') =~ /fail|error/i)) {
1167-
@streaming_urls = $self->_fallback_extract_urls($videoID);
1168-
push @caption_urls, $self->_fallback_extract_captions($videoID);
1169-
}
1159+
push @caption_urls, $self->_make_translated_captions(\@caption_urls);
11701160
}
1171-
else {
1161+
1162+
# Try again with youtube-dl
1163+
if (!@streaming_urls or (($json->{playabilityStatus}{status} // '') =~ /fail|error/i)) {
1164+
@streaming_urls = $self->_fallback_extract_urls($videoID);
11721165
push @caption_urls, $self->_fallback_extract_captions($videoID);
11731166
}
11741167

@@ -1177,12 +1170,6 @@ sub get_streaming_urls {
11771170
say STDERR ":: Found $count streaming URLs...";
11781171
}
11791172

1180-
# Try again with youtube-dl
1181-
if (!@streaming_urls or (($info{status} // '') =~ /fail|error/i)) {
1182-
@streaming_urls = $self->_fallback_extract_urls($videoID);
1183-
push @caption_urls, $self->_fallback_extract_captions($videoID);
1184-
}
1185-
11861173
if ($self->get_prefer_mp4 or $self->get_prefer_av1) {
11871174

11881175
my @video_urls;

0 commit comments

Comments
 (0)