This script requires PowerShell v2 in it’s final version (as included in Windows 7 RTM).
This script will download any Youtube video in it’s best available quality as an MP4 file.
Syntax: GetYouTube Video-ID
Example: GetYouTube irp8CNj9qBI
Of course, one can also send a list of video ids to the script via a prepared text file, like so:
foreach ($video in get-content list.txt) { getyoutube.ps1 $video }
Here is the script GetYouTube.ps1:
Import-Module bitstransfer
$ErrorActionPreference = "SilentlyContinue"
$v=$args[0]
#$v="irp8CNj9qBI"
#Grab Youtube Page
$s=(New-Object System.Net.WebClient).DownloadString("http://www.youtube.com/watch?v=" + $v)
#extract token
$t=$s | % {$_.substring($_.IndexOf("`"t`": `"")+6,44)}
#extract title
$r="<title\b[^>]*>YouTube - (.*?)</title>" ; $f = $s | ?{ $_ -match $r } | %{ $_ -match $r | out-null ; $matches[1] }
#Try downloading 720p version of video
$u = "http://www.youtube.com/get_video?fmt=22&video_id=" + $v + "&t=" + $t
$o = $Env:Userprofile + "\Videos\" + $f + "(HQ).mp4"
Start-BitsTransfer –source $u -destination $o
#Try downloading regular mp4 version of video, if HQ version failed
if (!$?) {
$u = "http://www.youtube.com/get_video?fmt=18&video_id=" + $v + "&t=" + $t
$o = $Env:Userprofile + "\Videos\" + $f + ".mp4"
Start-BitsTransfer –source $u -destination $o
}