<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://htyp.org/mw/index.php?action=history&amp;feed=atom&amp;title=YouTube%2FDWPS%2Fsource</id>
	<title>YouTube/DWPS/source - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://htyp.org/mw/index.php?action=history&amp;feed=atom&amp;title=YouTube%2FDWPS%2Fsource"/>
	<link rel="alternate" type="text/html" href="https://htyp.org/mw/index.php?title=YouTube/DWPS/source&amp;action=history"/>
	<updated>2026-07-08T03:29:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://htyp.org/mw/index.php?title=YouTube/DWPS/source&amp;diff=20151&amp;oldid=prev</id>
		<title>Woozle: ...in case the page goes away</title>
		<link rel="alternate" type="text/html" href="https://htyp.org/mw/index.php?title=YouTube/DWPS/source&amp;diff=20151&amp;oldid=prev"/>
		<updated>2014-08-13T12:48:30Z</updated>

		<summary type="html">&lt;p&gt;...in case the page goes away&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Retrieved 2014-08-13 from [https://calomel.org/youtube_wget.html here].&lt;br /&gt;
&amp;lt;perl&amp;gt;&lt;br /&gt;
#!/usr/bin/perl -T&lt;br /&gt;
&lt;br /&gt;
use strict;&lt;br /&gt;
use warnings;&lt;br /&gt;
&lt;br /&gt;
#&lt;br /&gt;
##  Calomel.org  ,:,  Download Youtube videos and music using wget&lt;br /&gt;
##    Script Name : youtube_wget_video.pl&lt;br /&gt;
##    Version     : 0.42&lt;br /&gt;
##    Valid from  : March 2014&lt;br /&gt;
##    URL Page    : https://calomel.org/youtube_wget.html&lt;br /&gt;
##    OS Support  : Linux, Mac OSX, OpenBSD, FreeBSD or any system with perl&lt;br /&gt;
#                `:`&lt;br /&gt;
## Two arguments&lt;br /&gt;
##    $1 Youtube URL from the browser&lt;br /&gt;
##    $2 prefix to the file name of the video (optional)&lt;br /&gt;
#&lt;br /&gt;
&lt;br /&gt;
############  options  ##########################################&lt;br /&gt;
&lt;br /&gt;
# Option: what file type do you want to download? The string is used to search&lt;br /&gt;
# in the youtube URL so you can choose mp4, webm, avi or flv.  mp4 seems to&lt;br /&gt;
# work on the most players like android, ipod, ipad, iphones, vlc and mplayer.&lt;br /&gt;
my $fileType = &amp;quot;mp4&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
# Option: what visual resolution or quality do you want to download? List&lt;br /&gt;
# multiple values just in case the highest quality video is not available, the&lt;br /&gt;
# script will look for the next resolution. You can choose &amp;quot;highres&amp;quot; for 4k,&lt;br /&gt;
# &amp;quot;hd1080&amp;quot; for 1080p, &amp;quot;hd720&amp;quot; for 720p, &amp;quot;itag=18&amp;quot; which means standard&lt;br /&gt;
# definition 640x380 and &amp;quot;itag=17&amp;quot; which is mobile resolution 144p (176x144).&lt;br /&gt;
# The script will always prefer to download the highest resolution video format&lt;br /&gt;
# from the list if available.&lt;br /&gt;
my $resolution = &amp;quot;hd720,itag=18&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
# Option: How many times should the script retry the download if wget fails for&lt;br /&gt;
# any reason? Do not make this too high as a reoccurring error will just hit&lt;br /&gt;
# youtube over and over again. &lt;br /&gt;
my $retryTimes = 20;&lt;br /&gt;
&lt;br /&gt;
# Option: do you want the resolution of the video in the file name? zero(0) is&lt;br /&gt;
# no and one(1) is yes. This option simply puts &amp;quot;_hd1080.mp4&amp;quot; or similar at the&lt;br /&gt;
# end of the file name.&lt;br /&gt;
my $resolutionFilename = 0;&lt;br /&gt;
&lt;br /&gt;
# Option: Force all communication with YouTube to use SSL (https) links. The&lt;br /&gt;
# script will simply convert all URL&amp;#039;s you pass to the script to use https&lt;br /&gt;
# instead of http. Encryption better protects your privacy and may help avoid&lt;br /&gt;
# ISP rate limiting. &lt;br /&gt;
my $forceSSL = 1;&lt;br /&gt;
&lt;br /&gt;
# Option: turn on DEBUG mode. Use this to reverse engineering this code if you are&lt;br /&gt;
# making changes or you are building your own youtube download script.&lt;br /&gt;
my $DEBUG=0;&lt;br /&gt;
&lt;br /&gt;
#################################################################&lt;br /&gt;
&lt;br /&gt;
# initialize retry loop and resolution variables&lt;br /&gt;
$ENV{PATH} = &amp;quot;/bin:/usr/bin:/usr/local/bin:/opt/local/bin&amp;quot;;&lt;br /&gt;
my $prefix = &amp;quot;&amp;quot;;&lt;br /&gt;
my $retry = 1;&lt;br /&gt;
my $retryCounter = 0;&lt;br /&gt;
my $resFile = &amp;quot;unknown&amp;quot;;&lt;br /&gt;
my $user_url = &amp;quot;&amp;quot;;&lt;br /&gt;
my $user_prefix = &amp;quot;&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
# collect the URL from the command line argument&lt;br /&gt;
chomp($user_url = $ARGV[0]);&lt;br /&gt;
my $url = &amp;quot;$1&amp;quot; if ($user_url =~ m/^([a-zA-Z0-9\_\-\&amp;amp;\?\=\:\.\/]+)$/ or die &amp;quot;\nError: Illegal characters in YouTube URL\n\n&amp;quot; );&lt;br /&gt;
&lt;br /&gt;
# declare the user defined file name prefix if specified&lt;br /&gt;
if (defined($ARGV[1])) {&lt;br /&gt;
   chomp($user_prefix = $ARGV[1]);&lt;br /&gt;
   $prefix = &amp;quot;$1&amp;quot; if ($user_prefix =~ m/^([a-zA-Z0-9\_\-\.\ ]+)$/ or die &amp;quot;\nError: Illegal characters in filename prefix\n\n&amp;quot; );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# while loop to retry downloading the video if the script fails for any reason&lt;br /&gt;
while ( $retry != 0 &amp;amp;&amp;amp; $retryCounter &amp;lt; $retryTimes ) {&lt;br /&gt;
&lt;br /&gt;
# Force SSL (https) download of the html page&lt;br /&gt;
$url =~ s/http:\/\//https:\/\//gi if ($forceSSL == 1);&lt;br /&gt;
&lt;br /&gt;
# download the html from the youtube page containing the page title and video&lt;br /&gt;
# url. The page title will be used for the local video file name and the url&lt;br /&gt;
# will be sanitized and passed to wget for the download.&lt;br /&gt;
my $html = `wget -4Ncq --convert-links=off --no-cookies --timeout=90 --user-agent=&amp;#039;&amp;#039; --no-check-certificate &amp;quot;$url&amp;quot; -O-`  or die  &amp;quot;\nThere was a problem downloading the HTML page.\n\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
# format the title of the page to use as the file name&lt;br /&gt;
my ($title) = $html =~ m/&amp;lt;title&amp;gt;(.+)&amp;lt;\/title&amp;gt;/si;&lt;br /&gt;
$title =~ s/[^\w\d]+/_/g or die &amp;quot;\nError: we could not find the title of the HTML page. Check the URL.\n\n&amp;quot;;&lt;br /&gt;
$title =~ s/_youtube//ig;&lt;br /&gt;
$title =~ s/^_//ig;&lt;br /&gt;
$title = lc ($title);&lt;br /&gt;
$title =~ s/_amp//ig;&lt;br /&gt;
&lt;br /&gt;
# filter the URL of the video from the HTML page&lt;br /&gt;
my ($download) = $html =~ /&amp;quot;url_encoded_fmt_stream_map&amp;quot;(.*)/ig;&lt;br /&gt;
&lt;br /&gt;
# Print all of the separated strings in the HTML page&lt;br /&gt;
#print &amp;quot;\n$download\n\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
&lt;br /&gt;
# This is where we look through the HTML code and select the file type and&lt;br /&gt;
# video quality. &lt;br /&gt;
my @urls = split(&amp;#039;,&amp;#039;, $download);&lt;br /&gt;
OUTERLOOP:&lt;br /&gt;
foreach my $val (@urls) {&lt;br /&gt;
#   print &amp;quot;\n$val\n\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
    if ( $val =~ /$fileType/ ) {&lt;br /&gt;
       my @res = split(&amp;#039;,&amp;#039;, $resolution);&lt;br /&gt;
       foreach my $ress (@res) {&lt;br /&gt;
         if ( $val =~ /$ress/ ) {&lt;br /&gt;
         print &amp;quot;\n  html to url seperation complete.\n\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
         print &amp;quot;$val\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
         $resFile = $ress;&lt;br /&gt;
         $resFile = &amp;quot;sd640&amp;quot; if ( $ress =~ /itag=18/ );&lt;br /&gt;
         $resFile = &amp;quot;mobil176&amp;quot; if ( $ress =~ /itag=17/ );&lt;br /&gt;
         $download = $val;&lt;br /&gt;
         last OUTERLOOP;&lt;br /&gt;
         }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# clean up the url by translating unicode and removing unwanted strings&lt;br /&gt;
print &amp;quot;\n  Re-formatting url for wget...\n\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
$download =~ s/\:\ \&amp;quot;//;&lt;br /&gt;
$download =~ s/%3A/:/g;&lt;br /&gt;
$download =~ s/%2F/\//g;&lt;br /&gt;
$download =~ s/%3F/\?/g;&lt;br /&gt;
$download =~ s/%3D/\=/g;&lt;br /&gt;
$download =~ s/%252C/%2C/g;&lt;br /&gt;
$download =~ s/%26/\&amp;amp;/g;&lt;br /&gt;
$download =~ s/sig=/signature=/g;&lt;br /&gt;
$download =~ s/\\u0026/\&amp;amp;/g;&lt;br /&gt;
$download =~ s/(type=[^&amp;amp;]+)//g;&lt;br /&gt;
$download =~ s/(fallback_host=[^&amp;amp;]+)//g;&lt;br /&gt;
$download =~ s/(quality=[^&amp;amp;]+)//g;&lt;br /&gt;
&lt;br /&gt;
# clean up the url &lt;br /&gt;
my ($youtubeurl) = $download =~ /(http?:.+)/;&lt;br /&gt;
&lt;br /&gt;
# url title additon&lt;br /&gt;
my ($titleurl) = $html =~ m/&amp;lt;title&amp;gt;(.+)&amp;lt;\/title&amp;gt;/si;&lt;br /&gt;
$titleurl =~ s/ - YouTube//ig;&lt;br /&gt;
$titleurl =~ s/ /%20/ig;&lt;br /&gt;
&lt;br /&gt;
# combine the youtube url and title string&lt;br /&gt;
$download = &amp;quot;$youtubeurl\&amp;amp;title=$titleurl&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
# a bit more cleanup as youtube &lt;br /&gt;
#$download =~ s/&amp;amp;+/&amp;amp;/g;&lt;br /&gt;
#$download =~ s/&amp;amp;itag=\d+&amp;amp;signature=/&amp;amp;signature=/g;&lt;br /&gt;
&lt;br /&gt;
# combine file variables into the full file name&lt;br /&gt;
my $filename = &amp;quot;unknown&amp;quot;;&lt;br /&gt;
if ( $resolutionFilename == 1 ) {&lt;br /&gt;
   $filename = &amp;quot;$prefix$title\_$resFile.$fileType&amp;quot;;&lt;br /&gt;
  } else {&lt;br /&gt;
   $filename = &amp;quot;$prefix$title.$fileType&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# Process check: Are we currently downloading this exact same video? Two of the&lt;br /&gt;
# same wget processes will overwrite themselves and corrupt the video.&lt;br /&gt;
my $running = `ps auwww | grep [w]get | grep -c &amp;quot;$filename&amp;quot;`;&lt;br /&gt;
print &amp;quot;\n  Is the same file already being downloaded? $running\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
if ($running &amp;gt;= 1)&lt;br /&gt;
  {&lt;br /&gt;
   print &amp;quot;\n  Already $running process, exiting.&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
   exit 0;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
# Force SSL (https) download of the video file.&lt;br /&gt;
$download =~ s/http:\/\//https:\/\//g if ($forceSSL == 1);&lt;br /&gt;
&lt;br /&gt;
# Print the long, sanitized youtube url for testing and debugging&lt;br /&gt;
print &amp;quot;\n  The following url will be passed to wget:\n\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
print &amp;quot;\n$download\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
&lt;br /&gt;
# print the file name of the video being downloaded for the user &lt;br /&gt;
print &amp;quot;\n Download: $filename\n\n&amp;quot; if ($retryCounter &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
# Background the script before wget starts downloading. Use &amp;quot;ps&amp;quot; if you need to&lt;br /&gt;
# look for the process running or use &amp;quot;ls -al&amp;quot; to look at the file size and&lt;br /&gt;
# date.&lt;br /&gt;
fork and exit;&lt;br /&gt;
&lt;br /&gt;
# Download the video &lt;br /&gt;
system(&amp;quot;wget&amp;quot;, &amp;quot;-4Ncq&amp;quot;, &amp;quot;--convert-links=off&amp;quot;, &amp;quot;--no-cookies&amp;quot;, &amp;quot;--timeout=90&amp;quot;, &amp;quot;--no-check-certificate&amp;quot;, &amp;quot;--user-agent=&amp;#039;&amp;#039;&amp;quot; , &amp;quot;$download&amp;quot;, &amp;quot;-O&amp;quot;, &amp;quot;$filename&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
# Print the error code of wget&lt;br /&gt;
print &amp;quot;\n  wget error code: $?\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
&lt;br /&gt;
# Exit Status: Check if the file exists and we received the correct error code&lt;br /&gt;
# from wget system call. If the download experienced any problems the script&lt;br /&gt;
# will run again and try continue the download until the retryTimes count limit&lt;br /&gt;
# is reached.&lt;br /&gt;
&lt;br /&gt;
if( $? == 0 &amp;amp;&amp;amp; -e &amp;quot;$filename&amp;quot; &amp;amp;&amp;amp; ! -z &amp;quot;$filename&amp;quot; )&lt;br /&gt;
   {&lt;br /&gt;
      print &amp;quot;\n  Finished: $filename\n\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
    # print &amp;quot;\n  Success: $filename\n\n&amp;quot;;&lt;br /&gt;
      $retry = 0;&lt;br /&gt;
   }&lt;br /&gt;
 else&lt;br /&gt;
   {&lt;br /&gt;
      print STDERR &amp;quot;\n  FAILED: $filename\n\n&amp;quot; if ($DEBUG == 1);&lt;br /&gt;
    # print &amp;quot;\n  FAILED: $filename\n\n&amp;quot;;&lt;br /&gt;
      $retry = 1;&lt;br /&gt;
      $retryCounter++;&lt;br /&gt;
    # sleep $retryCounter;&lt;br /&gt;
      sleep 1;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#### EOF #####&lt;br /&gt;
&amp;lt;/perl&amp;gt;&lt;/div&gt;</summary>
		<author><name>Woozle</name></author>
	</entry>
</feed>