Skip to content

Commit

Permalink
YoutubeViewer: Move youtube-dl support to its own pairer.
Browse files Browse the repository at this point in the history
This effectively allows you to use youtube-dl entirely to find all of the urls
and formats of a specific video. Thanks to these changes encrypted videos now
use the format decided by the user.
  • Loading branch information
Jookia committed May 31, 2015
1 parent 14e3be1 commit 329e2cf
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions lib/WWW/YoutubeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,30 @@ sub get_video_tops {
... # NEEDS WORK!!!
}

sub _get_ytdl_url {
sub _get_pairs_from_ytdl {
my ($self, $videoID) = @_;

return '' if ((state $x = $self->proxy_system('youtube-dl', '--version')) == 0);
my @array;

return @array if ((state $x = $self->proxy_system('youtube-dl', '--version')) != 0);

chomp(my $url = $self->proxy_stdout('youtube-dl',
'--get-url', '--format', 'best',
'"http://www.youtube.com/watch?v=' . $videoID . '"'));
my @urls = $self->proxy_stdout('youtube-dl',
'--get-url', '--all-formats',
'"http://www.youtube.com/watch?v=' . $videoID . '"');

return $url;
foreach my $url (@urls) {
foreach my $pair (split(/&/, $url)) {
$pair =~ s{^itag=(?=\w+=)}{}im;
my ($key, $value) = split(/=/, $pair);
$key // next;
push(@array, {
'itag' => $value,
'url' => $url
});
}
}

return @array;
}

sub _get_pairs_from_info_data {
Expand Down Expand Up @@ -581,23 +595,23 @@ sub _get_pairs_from_info_data {
$hash_ref->{url} .= "&signature=$hash_ref->{sig}";
}
elsif (exists $hash_ref->{s}) { # has an encrypted signature :(
# Unfortunately, this streaming URLs doesn't work with 'mplayer', but they work with 'mpv' and 'vlc'
my $url = $self->_get_ytdl_url($videoID);
if ($url ne '') {
foreach my $item (@array) {
if (exists $item->{url}) {
$item->{url} = $url;
my @pairs = $self->_get_pairs_from_ytdl($videoID);
foreach my $item (@pairs) {
foreach my $ref (@array) {
if (defined($ref->{itag}) && ($ref->{itag} eq $item->{itag})) {
$ref->{url} = $item->{url};
}
}
last;
}
last;
}
}
}

return @array;
}


=head2 get_streaming_urls($videoID)
Returns a list of streaming URLs for a videoID.
Expand Down

0 comments on commit 329e2cf

Please sign in to comment.