Skip to content

Commit

Permalink
Added support for the 'audio' format;
Browse files Browse the repository at this point in the history
Improved the 'itag finder' system for a given resolution;
YoutubeViewer.pm accepts URLs which doesn't contain the signature (sig);
  • Loading branch information
Daniel Șuteu committed Nov 7, 2013
1 parent ebae448 commit 2c57ea0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 57 deletions.
2 changes: 1 addition & 1 deletion WWW-YoutubeViewer/MANIFEST
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ lib/WWW/YoutubeViewer/ParseXML.pm
lib/WWW/YoutubeViewer/RegularExpressions.pm lib/WWW/YoutubeViewer/RegularExpressions.pm
lib/WWW/YoutubeViewer/Utils.pm lib/WWW/YoutubeViewer/Utils.pm
Makefile.PL Makefile.PL
MANIFEST MANIFEST This list of files
META.json META.json
META.yml META.yml
README README
Expand Down
2 changes: 1 addition & 1 deletion WWW-YoutubeViewer/META.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Șuteu \"Trizen\" Daniel <trizenx@gmail.com>" "Șuteu \"Trizen\" Daniel <trizenx@gmail.com>"
], ],
"dynamic_config" : 1, "dynamic_config" : 1,
"generated_by" : "Module::Build version 0.4005, CPAN::Meta::Converter version 2.131560", "generated_by" : "Module::Build version 0.4008, CPAN::Meta::Converter version 2.132830",
"license" : [ "license" : [
"perl_5" "perl_5"
], ],
Expand Down
2 changes: 1 addition & 1 deletion WWW-YoutubeViewer/META.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ build_requires:
configure_requires: configure_requires:
Module::Build: 0 Module::Build: 0
dynamic_config: 1 dynamic_config: 1
generated_by: 'Module::Build version 0.4005, CPAN::Meta::Converter version 2.131560' generated_by: 'Module::Build version 0.4008, CPAN::Meta::Converter version 2.132830'
license: perl license: perl
meta-spec: meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html url: http://module-build.sourceforge.net/META-spec-v1.4.html
Expand Down
2 changes: 1 addition & 1 deletion WWW-YoutubeViewer/Makefile.PL
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
# Note: this file was auto-generated by Module::Build::Compat version 0.4005 # Note: this file was auto-generated by Module::Build::Compat version 0.4008
require 5.016; require 5.016;
use ExtUtils::MakeMaker; use ExtUtils::MakeMaker;
WriteMakefile WriteMakefile
Expand Down
30 changes: 11 additions & 19 deletions WWW-YoutubeViewer/bin/youtube-viewer
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#------------------------------------------------------- #-------------------------------------------------------
# Appname: youtube-viewer # Appname: youtube-viewer
# Created on: 02 June 2010 # Created on: 02 June 2010
# Latest edit on: 06 September 2013 # Latest edit on: 07 November 2013
# Websites: http://trizen.googlecode.com # Websites: http://trizen.googlecode.com
# https://github.com/trizen/youtube-viewer # https://github.com/trizen/youtube-viewer
#------------------------------------------------------- #-------------------------------------------------------
Expand Down Expand Up @@ -463,9 +463,9 @@ YouTube options:
--hd : search only for videos available in at least 720p --hd : search only for videos available in at least 720p
--page=i : get results starting with a specific page --page=i : get results starting with a specific page
--results=[1-50] : how many results to display per page --results=[1-50] : how many results to display per page
-2 -3 -4 -7 -1 : resolutions: 240p, 360p, 480p, 720p or 1080p -2 -3 -4 -7 -1 : resolutions: 240p, 360p, 480p, 720p and 1080p
--resolution=RES : resolutions: original, 1080, 720, 480, 360, 340, --resolution=RES : available resolutions: original, 1080p, 720p,
240, 180, 144 480p, 360p, 240p, 180p, 144p, audio
-F --favorites:s : show the latest favorited videos * -F --favorites:s : show the latest favorited videos *
-R --recommended : show the recommended videos for you * -R --recommended : show the recommended videos for you *
-S --subscriptions:s : show the new subscription videos * -S --subscriptions:s : show the new subscription videos *
Expand All @@ -480,6 +480,7 @@ MPlayer options:
-f --fullscreen! : set the fullscreen mode for mplayer (-fs) -f --fullscreen! : set the fullscreen mode for mplayer (-fs)
-n --novideo! : play the music only without a video in the foreground -n --novideo! : play the music only without a video in the foreground
-l --use-lc : use a lower cache for MPlayer (for slow connections) -l --use-lc : use a lower cache for MPlayer (for slow connections)
--nocache : use no cache while streaming videos (for fast connections)
--cache=i : set the cache for MPlayer (set: $opt{cache}) --cache=i : set the cache for MPlayer (set: $opt{cache})
--cache-min=i : set the cache-min for MPlayer (set: $opt{cache_min}) --cache-min=i : set the cache-min for MPlayer (set: $opt{cache_min})
--lcache=i : set the lower-cache for MPlayer (set: $opt{lower_cache}) --lcache=i : set the lower-cache for MPlayer (set: $opt{lower_cache})
Expand Down Expand Up @@ -516,7 +517,7 @@ Other options:
--get-captions! : download the closed captions (with gcap) --get-captions! : download the closed captions (with gcap)
--captions-dir=s : the directory where to download the .srt files --captions-dir=s : the directory where to download the .srt files
--gdata-url=s : print video results from a valid GData URL --gdata-url=s : print video results from a valid GData URL
--get-term-width : adjust the text for your terminal width --get-term-width! : adjust the text for your terminal width
--login : will prompt you for login --login : will prompt you for login
--logout : will delete the authentication key --logout : will delete the authentication key
--http_proxy=s : HTTP proxy to use, format 'http://domain.tld:port/'. --http_proxy=s : HTTP proxy to use, format 'http://domain.tld:port/'.
Expand Down Expand Up @@ -602,17 +603,7 @@ NOTE: Categories and region IDs are case sensitive!
example, but the mplayer arguments doesn't work for vlc. example, but the mplayer arguments doesn't work for vlc.
The full command should be: $execname -mplayer=vlc -mplayer-arg=-q The full command should be: $execname -mplayer=vlc -mplayer-arg=-q
2. Available resolutions: original, 1080, 720, 480, 360, 340, 240, 180, 144 2. lower_cache and lower_cache_min values are used when -l (--use-lower-cache)
1080 = mp4
720 = mp4 (unless webm is prefered)
480 = flv (unless webm is prefered)
360 = flv (unless webm is prefered)
340 = mp4 (actually is 360p)
240 = flv
180 = 3gpp (mp4v, mp4a)
144 = 3gpp (mp4v, mp4a)
3. lower_cache and lower_cache_min values are used when -l (--use-lower-cache)
is specified as argument, or if the resolution of a video is lower than 720p. is specified as argument, or if the resolution of a video is lower than 720p.
** Arguments ** Arguments
Expand Down Expand Up @@ -1120,6 +1111,7 @@ sub parse_arguments {


# MPlayer # MPlayer
'mplayer=s' => \$opt{mplayer}, 'mplayer=s' => \$opt{mplayer},
'nocache!' => \$opt{nocache},
'cache=i' => \$opt{cache}, 'cache=i' => \$opt{cache},
'cache-min|cache_min=i' => \$opt{cache_min}, 'cache-min|cache_min=i' => \$opt{cache_min},
'lcache|lower-cache|lower_cache=i' => \$opt{lower_cache}, 'lcache|lower-cache|lower_cache=i' => \$opt{lower_cache},
Expand All @@ -1141,7 +1133,7 @@ sub parse_arguments {
'rp|rem-played|remove_played_file!' => \$opt{remove_played_file}, 'rp|rem-played|remove_played_file!' => \$opt{remove_played_file},
'clobber!' => \$opt{clobber}, 'clobber!' => \$opt{clobber},
'info|i|show-info=s' => \$opt{show_video_info}, 'info|i|show-info=s' => \$opt{show_video_info},
'get-term-width' => \$opt{adj_for_term_width}, 'get-term-width!' => \$opt{adj_for_term_width},
'page=i' => \$opt{page}, 'page=i' => \$opt{page},
'novideo|n!' => \$opt{novideo}, 'novideo|n!' => \$opt{novideo},
'results=i' => \$opt{results}, 'results=i' => \$opt{results},
Expand Down Expand Up @@ -2467,10 +2459,10 @@ sub update_mplayer_arguments {


if ($opt{use_lower_cache} if ($opt{use_lower_cache}
or not $resolution ~~ [qw(original 1080 720)]) { or not $resolution ~~ [qw(original 1080 720)]) {
set_mplayer_arguments($opt{lower_cache}, $opt{lower_cache_min}); set_mplayer_arguments($opt{nocache} ? (32, 0) : ($opt{lower_cache}, $opt{lower_cache_min}));
} }
else { else {
set_mplayer_arguments($opt{cache}, $opt{cache_min}); set_mplayer_arguments($opt{nocache} ? (32, 0) : ($opt{cache}, $opt{cache_min}));
} }
} }


Expand Down
7 changes: 4 additions & 3 deletions WWW-YoutubeViewer/lib/WWW/YoutubeViewer.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use strict;
use autouse 'XML::Fast' => qw{ xml2hash($;%) }; use autouse 'XML::Fast' => qw{ xml2hash($;%) };
use autouse 'URI::Escape' => qw{ uri_escape uri_escape_utf8 uri_unescape }; use autouse 'URI::Escape' => qw{ uri_escape uri_escape_utf8 uri_unescape };



=head1 NAME =head1 NAME
WWW::YoutubeViewer - A very easy interface to YouTube. WWW::YoutubeViewer - A very easy interface to YouTube.
Expand Down Expand Up @@ -1122,10 +1121,12 @@ sub _get_pairs_from_info_data {
} }


foreach my $hash_ref (@array) { foreach my $hash_ref (@array) {
if (exists $hash_ref->{url} and exists $hash_ref->{sig}) { if (exists $hash_ref->{url}) {


# Add signature # Add signature
$hash_ref->{url} .= "&signature=$hash_ref->{sig}"; if (exists $hash_ref->{sig}) {
$hash_ref->{url} .= "&signature=$hash_ref->{sig}";
}


# Add proxy (if defined http_proxy) # Add proxy (if defined http_proxy)
if (defined(my $proxy_url = $self->get_http_proxy)) { if (defined(my $proxy_url = $self->get_http_proxy)) {
Expand Down
71 changes: 40 additions & 31 deletions WWW-YoutubeViewer/lib/WWW/YoutubeViewer/Itags.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ Get a HASH ref with the YouTube itags. {resolution => {type => itag}}.
=cut =cut


sub get_itags { sub get_itags {
return return scalar {
scalar { 'original' => [38],
'original' => {normal => 38}, '1080' => [37, 46], # 137 -- no audio
'1080' => {normal => 37, webm => 46}, '720' => [22, 45], # 136 -- no audio
'720' => {normal => 22, webm => 45}, '480' => [35, 44], # 135 -- no audio
'480' => {normal => 35, webm => 44}, '360' => [34, 18, 43], # 134 -- no audio
'360' => {normal => 34, webm => 43}, '240' => [5], # 133 -- no audio
'340' => {normal => 18}, '180' => [36],
'240' => {normal => 5}, '144' => [17], # 160 -- no audio
'180' => {normal => 36}, 'audio' => [139, 140, 141, 171, 172],
'144' => {normal => 17}, };
};
} }


=head2 get_resolutions() =head2 get_resolutions()
Expand All @@ -68,11 +67,12 @@ Get a HASH ref with the itags as keys and resolutions as values.


sub get_resolutions { sub get_resolutions {
my ($self) = @_; my ($self) = @_;

state $itags = $self->get_itags(); state $itags = $self->get_itags();
return scalar { return scalar {
map { map {
my $res = $_; my $res = $_;
map { $itags->{$res}{$_} => $res } keys %{$itags->{$_}} map { $itags->{$res}[$_] => $res } 0 .. $#{$itags->{$_}}
} keys %{$itags} } keys %{$itags}
}; };
} }
Expand All @@ -87,8 +87,11 @@ sub find_streaming_url {
my ($self, $urls_ref, $prefer_webm, $resolution) = @_; my ($self, $urls_ref, $prefer_webm, $resolution) = @_;


state $itags = $self->get_itags(); state $itags = $self->get_itags();
state $resolutions = $self->get_resolutions(); state $resolutions = $self->get_resolutions($itags);
state $webm_itags = [map { $_->{webm} } grep { exists $_->{webm} } values %{$itags}];
if (defined($resolution) and $resolution =~ /^([0-9]+)/) {
$resolution = $1;
}


my $wanted_itag = defined $resolution ? $itags->{$resolution} : undef; my $wanted_itag = defined $resolution ? $itags->{$resolution} : undef;


Expand All @@ -97,21 +100,10 @@ sub find_streaming_url {
if (exists $url_ref->{itag} && exists $url_ref->{url}) { if (exists $url_ref->{itag} && exists $url_ref->{url}) {


if (defined $wanted_itag) { if (defined $wanted_itag) {
( if (($prefer_webm and $url_ref->{itag} == $wanted_itag->[-1]) or ($url_ref->{itag} ~~ $wanted_itag)) {
( ## ok
$url_ref->{itag} == ( }
$prefer_webm && exists $wanted_itag->{webm} else {
? $wanted_itag->{webm}
: $wanted_itag->{normal}
)
)
|| ($url_ref->{itag} == $wanted_itag->{normal})
)
|| next;
}

if (not $prefer_webm) {
if ($url_ref->{itag} ~~ $webm_itags) {
next; next;
} }
} }
Expand All @@ -122,7 +114,24 @@ sub find_streaming_url {
} }
} }


return unless defined $streaming; if (not defined $streaming) {

foreach my $res (qw(original 1080 720 480 360 240 180 144)) {

foreach my $url (@{$urls_ref}) {
if (exists $url->{itag} and exists $url->{url}) {

if ($url->{itag} ~~ $itags->{$res}) {
$streaming = $url;
last;
}

}
}
last if defined($streaming);
}
}

return wantarray ? ($streaming, $resolutions->{$streaming->{itag}}) : $streaming; return wantarray ? ($streaming, $resolutions->{$streaming->{itag}}) : $streaming;
} }


Expand Down

0 comments on commit 2c57ea0

Please sign in to comment.