Skip to content

Instantly share code, notes, and snippets.

@samundra
Forked from bhill77/laracast.pl
Created October 12, 2016 19:13
Show Gist options
  • Save samundra/d2e21de0cd3becb1df8b005cc57c3773 to your computer and use it in GitHub Desktop.
Save samundra/d2e21de0cd3becb1df8b005cc57c3773 to your computer and use it in GitHub Desktop.
#!/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.45
## Valid from : September 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 and mplayer.
my $fileType = "mp4";
# 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 = 1;
# 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=1;
#################################################################
# initialize global variables and sanitize the path
$ENV{PATH} = "/bin:/usr/bin:/usr/local/bin:/opt/local/bin";
my $prefix = "";
my $retry = 1;
my $retryCounter = 0;
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" );
}
# while loop to retry downloading the video if the script fails for any reason
while ( $retry != 0 && $retryCounter < $retryTimes ) {
# Force SSL (https) download of the html page
# $url =~ s/http:\/\//https:\/\//gi if ($forceSSL == 1);
# download the html from the youtube page containing the page title and 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 --convert-links=off --load-cookies cookies.txt --timeout=90 --user-agent='' --no-check-certificate "$url" -O-` or die "\nThere was a problem downloading the HTML page.\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;
$title = lc ($title);
$title =~ s/^_//ig;
# filter the URL of the video from the HTML page
my ($download) = $html =~ m{(?:src)="(.*?hd\.mp4.*?)"}ig;
# Print all of the separated strings in the HTML page
# print "\n$download\n\n" if ($DEBUG == 1);
# clean up the url
$download = "https:" . $download;
# combine file variables into the full file name
my $filename = "$prefix$title.$fileType";
# Print the long, sanitized youtube url for testing and debugging
print "\n The following url will be passed to youtube-dl:\n\n" if ($DEBUG == 1);
print "\n$download\n" if ($DEBUG == 1);
# print the file name of the video being downloaded for the user
print "\n Download: $filename\n\n" if ($retryCounter < 1);
# Background the script before wget starts downloading. 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("torify", "youtube-dl", "--continue", "$download", "--output", "$filename");
system("youtube-dl", "--continue", "$download", "--output", "$filename");
# Print the error code of wget
print "\n youtube-dl error code: $?\n" if ($DEBUG == 1);
# Exit Status: Check if the file exists and we received the correct error code
# from wget system call. If the download experienced any problems the script
# will run again and try continue the download until the retryTimes count limit
# is reached.
if( $? == 0 && -e "$filename" && ! -z "$filename" )
{
print "\n Finished: $filename\n\n" if ($DEBUG == 1);
# print "\n Success: $filename\n\n";
$retry = 0;
}
else
{
print STDERR "\n FAILED: $filename\n\n" if ($DEBUG == 1);
# print "\n FAILED: $filename\n\n";
$retry = 1;
$retryCounter++;
# sleep $retryCounter;
sleep 1;
}
}
#### EOF #####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment