PowerShell: Download YouTube Video
Contents
Why
Because I am probably not the only one who is tired about all those different YouTube download applications that never work perfectly,
constantly have to be redownloaded or recompiled, clunky GUI's, untrustable closed source apps, limited/payed version, etc..
yt-dlp
If you do not have experience yet with downloading YouTube videos, YouTube deliberately makes it hard for people to download their videos,
but basically everything is based on 'yt-dlp' (https://github.com/yt-dlp/yt-dlp).
yt-dlp.exe is a command line application, and needs to be regularly updated (as YouTube keeps changing the system).
This can be done be running yt-dlp.exe with the -U parameter
ffmpeg
ffmpeg.exe isn't required to download YouTube video's, but it is if you want to convert them to .mp4 format.
Start
Create a new folder, and put the following files in there:
- Download yt-dlp.exe from https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe
- Download ffmpeg.exe from https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip (found in the 'bin' folder).
- Create a new shortcut, with location yt-dlp.exe -U, and name it Update YT-DLP
- Create a new folder called Downloads
- Open Notepad, copy the PowerShell code below, and save it as Download.ps1
param([string]$URL, [string]$Cookies, [string]$Quality, [string]$Location)
function Download-YoutubeVideo
{
if(!$URL)
{
Write-Host ""; Write-Host "Enter YouTube URL: " -ForegroundColor Yellow -NoNewline; $URL = Read-Host
}
if(!$Cookies)
{
Write-Host ""; Write-Host "Select Cookies" -ForegroundColor Yellow
Write-Host "[C] for Chrome, [F] for Firefox, [L] for local cookies.txt, empty to skip: " -ForegroundColor Yellow -NoNewline; $Cookies = Read-Host
if($Cookies -eq "C") { $CookiesArgs = @('--cookies-from-browser', 'chrome') }
if($Cookies -eq "F") { $CookiesArgs = @('--cookies-from-browser', 'firefox') }
if($Cookies -eq "L") { $CookiesArgs = @('--cookies', 'cookies.txt') }
}
if(!$Quality)
{
./yt-dlp.exe $CookiesArgs -F $URL
Write-Host ""; Write-Host "Select Quality" -ForegroundColor Yellow
Write-Host "Video ID + Audio ID, empty for best: " -ForegroundColor Yellow -NoNewline; $Quality = Read-Host
if(!$Quality) { $Quality = 'bestvideo+bestaudio' }
}
if(!$Location)
{
$Location = $PSScriptRoot + '\Downloads'
}
./yt-dlp.exe --format $Quality --merge-output-format mp4 $CookiesArgs --paths $Location --output '%(title)s.%(ext)s' $URL
Write-Host ""; Write-Host -ForegroundColor Green "Done!"; Write-Host ""
Remove-Variable * -ErrorAction SilentlyContinue
Download-YoutubeVideo
}
Download-YoutubeVideo