सबसे पहले, यदि आपके पास वेज नहीं है , तो इसे स्थापित करें:
sudo apt-get install wget
इस पर्ल स्क्रिप्ट का उपयोग करें:
#!/usr/bin/perl -T
use strict;
use warnings;
#
## Calomel.org ,:, Download YouTube videos and music using wget
## Script Name : youtube_wget_video.pl
## Version : 0.38
## Valid from : March 2014
## URL Page : https://calomel.org/youtube_wget.html
## OS Support : Linux, Mac OSX, OpenBSD, FreeBSD or any system with perl
# `:`
## Two arguments
## $1 YouTube URL from the browser
## $2 Prefix to the file name of the video (optional)
#
############ options ##########################################
# Option: what file type do you want to download? The string is used to search
# in the YouTube URL so you can choose mp4, webm, avi or flv. mp4 seems to
# work on the most players like Android, iPod, iPad, iPhones, VLC media player
# and MPlayer.
my $fileType = "mp4";
# Option: what visual resolution or quality do you want to download? List
# multiple values just in case the highest quality video is not available, the
# script will look for the next resolution. You can choose "highres" for 4k,
# "hd1080" for 1080p, "hd720" for 720p, "itag=18" which means standard
# definition 640x380 and "itag=17" which is mobile resolution 144p (176x144).
# The script will always prefer to download the highest resolution video format
# from the list if available.
my $resolution = "hd720,itag=18";
# Option: How many times should the script retry the download if wget fails for
# any reason? Do not make this too high as a reoccurring error will just hit
# YouTube over and over again.
my $retryTimes = 5;
# Option: do you want the resolution of the video in the file name? zero(0) is
# no and one(1) is yes. This option simply puts "_hd1080.mp4" or similar at the
# end of the file name.
my $resolutionFilename = 0;
# Option: turn on DEBUG mode. Use this to reverse engineering this code if you are
# making changes or you are building your own YouTube download script.
my $DEBUG=0;
#################################################################
## Initialize retry loop and resolution variables
$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 = "";
## Collect the URL from the command line argument
chomp($user_url = $ARGV[0]);
my $url = "$1" if ($user_url =~ m/^([a-zA-Z0-9\_\-\&\?\=\:\.\/]+)$/ or die "\nError: Illegal characters in YouTube URL\n\n" );
## Declare the user defined file name prefix if specified
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" );
}
## Retry getting the video if the script fails for any reason
while ( $retry != 0 && $retryCounter < $retryTimes ) {
## Download the html code from the YouTube page suppling the page title and the
## video URL. The page title will be used for the local video file name and the
## URL will be sanitized and passed to wget for the download.
my $html = `wget -4Ncq -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.\n\n";
## Format the title of the page to use as the file name
my ($title) = $html =~ m/<title>(.+)<\/title>/si;
$title =~ s/[^\w\d]+/_/g or die "\nError: we could not find the title of the HTML page. Check the URL.\n\n";
$title =~ s/_youtube//ig;
$title =~ s/^_//ig;
$title = lc ($title);
$title =~ s/_amp//ig;
## Collect the URL of the video from the HTML page
my ($download) = $html =~ /"url_encoded_fmt_stream_map"(.*)/ig;
# Print all of the separated strings in the HTML page
print "\n$download\n\n" if ($DEBUG == 1);
# This is where we look through the HTMl code and select the file type and
# video quality.
my @urls = split(',', $download);
OUTERLOOP:
foreach my $val (@urls) {
# print "\n$val\n\n";
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 = "mobil176" if ( $ress =~ /itag=17/ );
$download = $val;
last OUTERLOOP;
}
}
}
}
## Clean up the URL by translating unicode and removing unwanted strings
$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;
## Clean up the URL
my ($youtubeurl) = $download =~ /(http?:.+)/;
## URL title additon
my ($titleurl) = $html =~ m/<title>(.+)<\/title>/si;
$titleurl =~ s/ - YouTube//ig;
$titleurl =~ s/ /%20/ig;
## Combine the YouTube URL and title string
$download = "$youtubeurl\&title=$titleurl";
## A bit more cleanup as YouTube
$download =~ s/&+/&/g;
$download =~ s/&itag=\d+&signature=/&signature=/g;
## Combine file variables into the full file name
my $filename = "unknown";
if ($resolutionFilename == 1) {
$filename = "$prefix$title\_$resFile.$fileType";
}
else {
$filename = "$prefix$title.$fileType";
}
## Process check: Are we currently downloading this exact same video? Two of the
## same wget processes will overwrite themselves and corrupt the video.
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 $running process, exiting." if ($DEBUG == 1);
exit 0;
};
## Print the long, sanitized YouTube URL for testing and debugging
print "\n$download\n" if ($DEBUG == 1);
## Print the file name of the video collected from the web page title for us to see on the cli
print "\n Download: $filename\n\n";
## Background the script. Use "ps" if you need to look for the process
## running or use "ls -al" to look at the file size and date.
fork and exit;
## Download the video
system("wget", "-4Ncq", "-e", "convert-links=off", "--load-cookies", "/dev/null", "--tries=10", "--timeout=20", "--no-check-certificate", "$download", "-O", "$filename");
## Print the error code of wget
print " error code: $?\n" if ($DEBUG == 1);
## Exit Status: Check if the file exists and we received the correct error code
## from system call. If the download experienced any problems the script will run again and try
## continue the download till the file is downloaded.
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;
}
} # while
#### EOF #####
इस स्क्रिप्ट को .txt फ़ाइल में कॉपी करें और नाम बदलें youtube_wget.pl
।
निम्न आदेश का उपयोग करते हुए, इसे निष्पादन योग्य बनाएं:
chmod 755 youtube_wget.pl
जब आप टर्मिनल में डाउनलोड करना चाहते हैं, तो टाइप करें:
./youtube_wget.pl http://www.youtube.com/watch?v=ejkm5uGoxs4
YouTube URL को अपने YouTube वीडियो URL में बदलें।
स्थान सहेजें: वीडियो आपकी वर्तमान निर्देशिका में सहेजा जाएगा।
स्रोत: YouTube डाउनलोडर Wget पर्ल स्क्रिप्ट