Difference between revisions of "PowerShell: Download YouTube Video"
m (Dan moved page PowerShell: Download Youtube Video to PowerShell: Download YouTube Video without leaving a redirect) |
|||
| Line 1: | Line 1: | ||
| − | = | + | =Components= |
| − | + | These parts are needed to '''Make Your Own YouTube Downloader''':</br> | |
| − | |||
| − | === | + | ===Interface=== |
| − | + | Useful to control all components from one place.</br> | |
| − | This | + | This [[PowerShell: Download YouTube Video#Script|PowerShell script]] functions as a menu.</br> |
| − | === | + | ===yt-dlp=== |
| − | YouTube | + | Required to download Video and Audio from YouTube.</br> |
| + | "yt-dlp.exe" is an [https://github.com/yt-dlp/yt-dlp open-source] command line application, and can be downloaded from: https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe</br> | ||
| + | Needs to be regularly updated (as YouTube keeps changing the system).</br> | ||
| − | ffmpeg.exe is | + | ===FFmpeg=== |
| − | + | Required to merge Video and Audio, and to convert formats.</br> | |
| + | "ffmpeg.exe" is an [https://github.com/FFmpeg/FFmpeg open-source] command line application, and can be downloaded from: https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip</br> | ||
| + | |||
| + | ===Node.js=== | ||
| + | Required to simulate a JavaScript environment, so YouTube does not think you are a bot.</br> | ||
| + | "node.exe" is an [https://github.com/nodejs/node open-source] command line application, and can be downloaded from: https://nodejs.org/en/download</br> | ||
===Cookies=== | ===Cookies=== | ||
| − | + | Sometimes required, as YouTube requires account identification on restricted videos or restricted client IPs.</br> | |
=Script= | =Script= | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
<pre> | <pre> | ||
| − | param([string]$URL, [string]$Cookies, [string]$Quality, [string]$Location) | + | param([string]$URL, [string]$Cookies, [string]$Quality, [string]$Location, [string]$Node) |
function Download-YoutubeVideo | function Download-YoutubeVideo | ||
| Line 50: | Line 38: | ||
Write-Host "[C] for Chrome, [F] for Firefox, [L] for local cookies.txt, empty to skip: " -ForegroundColor Yellow -NoNewline; $Cookies = Read-Host | 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") { $ | + | if($Cookies -eq "C") { $Args = @('--cookies-from-browser', 'chrome') } |
| − | if($Cookies -eq "F") { $ | + | if($Cookies -eq "F") { $Args = @('--cookies-from-browser', 'firefox') } |
| − | if($Cookies -eq "L") { $ | + | if($Cookies -eq "L") { $Args = @('--cookies', 'cookies.txt') } |
} | } | ||
| Line 69: | Line 57: | ||
{ | { | ||
$Location = $PSScriptRoot + '\Downloads' | $Location = $PSScriptRoot + '\Downloads' | ||
| + | } | ||
| + | |||
| + | if(!$Node) | ||
| + | { | ||
| + | $Node = $PSScriptRoot + '\node\node.exe' | ||
| + | $Args += @('--js-runtimes', "node:$Node") | ||
} | } | ||
Write-Host "" | Write-Host "" | ||
| − | ./yt-dlp.exe --format $Quality --merge-output-format mp4 $ | + | ./yt-dlp.exe --format $Quality --merge-output-format mp4 $Args --paths $Location --output '%(title)s.%(ext)s' $URL |
Write-Host -ForegroundColor Green "`nDone!" | Write-Host -ForegroundColor Green "`nDone!" | ||
Revision as of 16:31, 6 April 2026
Contents
Components
These parts are needed to Make Your Own YouTube Downloader:
Interface
Useful to control all components from one place.
This PowerShell script functions as a menu.
yt-dlp
Required to download Video and Audio from YouTube.
"yt-dlp.exe" is an open-source command line application, and can be downloaded from: https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe
Needs to be regularly updated (as YouTube keeps changing the system).
FFmpeg
Required to merge Video and Audio, and to convert formats.
"ffmpeg.exe" is an open-source command line application, and can be downloaded from: https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip
Node.js
Required to simulate a JavaScript environment, so YouTube does not think you are a bot.
"node.exe" is an open-source command line application, and can be downloaded from: https://nodejs.org/en/download
Cookies
Sometimes required, as YouTube requires account identification on restricted videos or restricted client IPs.
Script
param([string]$URL, [string]$Cookies, [string]$Quality, [string]$Location, [string]$Node)
function Download-YoutubeVideo
{
if(!$URL)
{
Write-Host "`nEnter YouTube URL: " -ForegroundColor Yellow -NoNewline; $URL = Read-Host
}
if(!$Cookies)
{
Write-Host "`nSelect 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") { $Args = @('--cookies-from-browser', 'chrome') }
if($Cookies -eq "F") { $Args = @('--cookies-from-browser', 'firefox') }
if($Cookies -eq "L") { $Args = @('--cookies', 'cookies.txt') }
}
if(!$Quality)
{
Write-Host ""
./yt-dlp.exe $CookiesArgs -F $URL
Write-Host "`nSelect Format ID(s) for Quality" -ForegroundColor Yellow
Write-Host "'Video ID + Audio ID', empty for best video and audio: " -ForegroundColor Yellow -NoNewline; $Quality = Read-Host
if(!$Quality) { $Quality = 'bestvideo+bestaudio' }
}
if(!$Location)
{
$Location = $PSScriptRoot + '\Downloads'
}
if(!$Node)
{
$Node = $PSScriptRoot + '\node\node.exe'
$Args += @('--js-runtimes', "node:$Node")
}
Write-Host ""
./yt-dlp.exe --format $Quality --merge-output-format mp4 $Args --paths $Location --output '%(title)s.%(ext)s' $URL
Write-Host -ForegroundColor Green "`nDone!"
Remove-Variable * -ErrorAction SilentlyContinue
Download-YoutubeVideo
}
Download-YoutubeVideo
Use
Open the Download.ps1 file, and answer the following prompts:
URL
The link of the YouTube video
Cookies
'C' for extract from Chrome
'F' for extract from Firefox
'L' for extract from local file 'cookies.txt'
Leave empty for attempt to download without cookies
Quality
Enter the ID(s) of the format(s) that you want to download from the list.
If you want to download Video and Audio and combine them, enter like: 'Video ID'+'Audio ID'.
Leave empty to automatically choose 'bestvideo+bestaudio'.