试试这个
use strict;
use warnings;
my $fileType = "mp4";
my $resolution = "hd1080,hd720,itag=18";
my $retryTimes = 5;
my $resolutionFilename = 0;
my $DEBUG=0;
$ENV{ENV} ="";
$ENV{PATH} = "/bin:/usr/bin:/usr/local/bin";
my $prefix = "";
my $retry = 1;
my $retryCounter = 0;
my $resFile = "unknown";
my $user_url = "";
my $user_prefix = "";
my $url = "";
if (defined($ARGV[0])) {
chomp($user_url = $ARGV[0]);
$url = "$1" if ($user_url =~ m/^([a-zA-Z0-9\-\&\?\=\:\.\/\_]+)$/ or die "\nError: Illegal characters in YouTube URL\n\n" );
} else {
print "\nError: You Must specify a Youtube URL\n\n";
exit;
}
if (defined($ARGV[1])) {
chomp($user_prefix = $ARGV[1]);
$prefix = "$1" if ($user_prefix =~ m/^([a-zA-Z0-9\_\-\.\ ]+)$/ or die "\nError: Illegal characters in filename prefix\n\n" );
}
while ( $retry != 0 && $retryCounter < $retryTimes ) {
my $html = `wget -Ncq -e convert-links=off --keep-session-cookies --save-cookies /dev/null --no-check-certificate "$url" -O-` or die "\nThere was a problem downloading the HTML file or the video is not open to the public.\n\n";
my ($title) = $html =~ m/<title>(.+)<\/title>/si;
$title =~ s/[^\w\d]+/_/g or die "\nError: title of the HTML page not found. Check the URL.\n\n";
$title =~ s/_youtube//ig;
$title =~ s/^_//ig;
$title = lc ($title);
$title =~ s/_amp//ig;
my ($download) = $html =~ /"url_encoded_fmt_stream_map"(.*)/ig;
print "\n$download\n\n" if ($DEBUG == 1);
my @urls = split(',', $download);
OUTERLOOP:
foreach my $val (@urls) {
if ( $val =~ /$fileType/ ) {
my @res = split(',', $resolution);
foreach my $ress (@res) {
if ( $val =~ /$ress/ ) {
print "\n\nGOOD\n\n" if ($DEBUG == 1);
print "$val\n" if ($DEBUG == 1);
$resFile = $ress;
$resFile = "sd640" if ( $ress =~ /itag=18/ );
$resFile = "mb144" if ( $ress =~ /itag=17/ );
$download = $val;
last OUTERLOOP;
}
}
}
}
$download =~ s/\:\ \"//;
$download =~ s/%3A/:/g;
$download =~ s/%2F/\//g;
$download =~ s/%3F/\?/g;
$download =~ s/%3D/\=/g;
$download =~ s/%252C/%2C/g;
$download =~ s/%26/\&/g;
$download =~ s/sig=/signature=/g;
$download =~ s/\\u0026/\&/g;
$download =~ s/(type=[^&]+)//g;
$download =~ s/(fallback_host=[^&]+)//g;
$download =~ s/(quality=[^&]+)//g;
my ($signature) = $download =~ /(signature=[^&]+)/;
my ($youtubeurl) = $download =~ /(http?:.+)/;
$youtubeurl =~ s/&signature.+$//;
$download = "$youtubeurl\&$signature";
$download =~ s/&+/&/g;
$download =~ s/&itag=\d+&signature=/&signature=/g;
my $filename = "unknown";
if ( $resolutionFilename == 1 ) {
$filename = "$prefix$title\_$resFile.$fileType";
} else {
$filename = "$prefix$title.$fileType";
}
my $running = `ps auwww | grep [w]get | grep -c "$filename"`;
print "\nNumber of the same wgets running: $running\n" if ($DEBUG == 1);
if ($running >= 1)
{
print "\nAlready downloading the same filename, exiting: $filename\n";
exit 0;
};
print "\n$download\n" if ($DEBUG == 1);
print "\n Download: $filename\n\n";
fork and exit;
system("wget", "-Ncq", "-e", "convert-links=off", "--load-cookies", "/dev/null", "--tries=200", "--timeout=20", "--no-check-certificate", "$download", "-O", "$filename");
print " error code: $?\n" if ($DEBUG == 1);
if( $? == 0 && -e "$filename" && ! -z "$filename" )
{
print " Finished: $filename\n\n" if ($DEBUG == 1);
$retry = 0;
}
else
{
print STDERR "\n FAILED: $filename\n\n" if ($DEBUG == 1);
$retry = 1;
$retryCounter++;
sleep $retryCounter;
}
}