Difference between revisions of "PowerShell: Download YouTube Video"
m |
m |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Components== | ==Components== | ||
| − | These parts are needed to | + | These parts are needed to ''Make Your Own'' '''YouTube Downloader''':</br> |
===Interface=== | ===Interface=== | ||
| Line 9: | Line 9: | ||
Required to download Video and Audio from YouTube.</br> | 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> | "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 their system).</br> | ||
You can make an Update-button by creating a shortcut to: <code>\yt-dlp\yt-dlp.exe -U</code> | You can make an Update-button by creating a shortcut to: <code>\yt-dlp\yt-dlp.exe -U</code> | ||
| Line 24: | Line 24: | ||
Sometimes required, as YouTube requires account identification on restricted videos or restricted client IPs.</br> | Sometimes required, as YouTube requires account identification on restricted videos or restricted client IPs.</br> | ||
| + | </br> | ||
==Script== | ==Script== | ||
'''Folder Layout'''</br> | '''Folder Layout'''</br> | ||
[[File:YouTubeDownloader Folder.png||Folder Layout]]</br> | [[File:YouTubeDownloader Folder.png||Folder Layout]]</br> | ||
| − | + | [[Media:Download-YoutubeVideo.ps1|Download-YoutubeVideo.ps1]]</br> | |
<PRE> | <PRE> | ||
param([string]$URL, [string]$Cookies, [string]$Formats, [string]$Location) | param([string]$URL, [string]$Cookies, [string]$Formats, [string]$Location) | ||
| Line 36: | Line 37: | ||
if(!$URL) | if(!$URL) | ||
{ | { | ||
| − | + | Write-Host "Enter YouTube URL: " -ForegroundColor Cyan -NoNewline; $URL = Read-Host | |
| − | Write-Host "Enter YouTube URL: " -ForegroundColor | ||
} | } | ||
if(!$Cookies) | if(!$Cookies) | ||
{ | { | ||
| − | Write-Host "`n | + | Write-Host "`n`n[E] Extract from Edge`n[C] Extract from Chrome`n[F] Extract from Firefox`n[R] Use last Browser Extract`n[M] Use manual cookies.txt`n[Enter] to Skip Cookies" -ForegroundColor Yellow |
| − | Write-Host " | + | Write-Host "`nChoose Cookies: " -ForegroundColor Cyan -NoNewline; $Cookies = Read-Host |
| + | $File = $PSScriptRoot+'\Cookies\browser.txt'; | ||
switch -Regex ($Cookies) | switch -Regex ($Cookies) | ||
{ | { | ||
| − | '^C' { $Args += @('--cookies-from-browser', 'chrome') } | + | '^E' { Remove-Item $File; $Args += @('--cookies-from-browser', 'edge', '--cookies', $File) } |
| − | '^F' { $Args += @('--cookies-from-browser', 'firefox') } | + | '^C' { Remove-Item $File; $Args += @('--cookies-from-browser', 'chrome', '--cookies', $File) } |
| − | '^ | + | '^F' { Remove-Item $File; $Args += @('--cookies-from-browser', 'firefox', '--cookies', $File) } |
| + | '^R' { $Args += @('--cookies', $File) } | ||
| + | '^M' { $Args += @('--cookies', $File.Replace("browser.txt", "cookies.txt")) } | ||
} | } | ||
} | } | ||
| + | |||
| + | $Args += @('--js-runtimes', "node:$PSScriptRoot\node\node.exe") | ||
if(!$Formats) | if(!$Formats) | ||
{ | { | ||
| − | Write-Host "`nLoading all available formats..." -ForegroundColor Yellow | + | Write-Host "`n`nLoading all available formats..." -ForegroundColor Yellow |
| − | ./yt-dlp/yt-dlp.exe --quiet --no-warnings | + | ./yt-dlp/yt-dlp.exe --quiet --no-warnings $Args --list-formats $URL |
| − | Write-Host "`n | + | Write-Host "`n`n[VideoID+AudioID] For Video and Audio`n[Enter] To automatically select BestVideo+BestAudio" -ForegroundColor Yellow |
| − | Write-Host " | + | Write-Host "`nChoose Format ID(s): " -ForegroundColor Cyan -NoNewline; $Formats = Read-Host |
if(!$Formats) { $Formats = 'bestvideo+bestaudio' } | if(!$Formats) { $Formats = 'bestvideo+bestaudio' } | ||
| Line 72: | Line 77: | ||
Write-Host "`nDownloading started..." -ForegroundColor Yellow | Write-Host "`nDownloading started..." -ForegroundColor Yellow | ||
| − | ./yt-dlp/yt-dlp.exe | + | ./yt-dlp/yt-dlp.exe --ffmpeg-location "$PSScriptRoot\ffmpeg\ffmpeg.exe" --merge-output-format mp4 --output '[%(id)s] %(title)s.%(ext)s' $Args $URL |
| − | Write-Host -ForegroundColor Green "` | + | Write-Host -ForegroundColor Green "`nDownload has finished!`n`n" |
| − | Remove-Variable * -ErrorAction SilentlyContinue | + | Remove-Variable * -ErrorAction SilentlyContinue |
Download-YoutubeVideo | Download-YoutubeVideo | ||
} | } | ||
| Line 101: | Line 106: | ||
Enter the '''<Video Format ID>''' '''+''' '''<Audio Format ID>'''</br> | Enter the '''<Video Format ID>''' '''+''' '''<Audio Format ID>'''</br> | ||
| − | '''Leave empty''' to choose 'bestvideo+bestaudio'.</br> | + | '''Leave empty''' to choose [https://github.com/yt-dlp/yt-dlp#format-selection 'bestvideo+bestaudio'].</br> |
| + | |||
| + | </br> | ||
| + | The script will automatically start over after the download has finished.</br> | ||
Latest revision as of 08:19, 27 April 2026
Contents
Components
These parts are needed to Make Your Own YouTube Downloader:
Interface
The Download-YoutubeVideo.ps1 script.
Useful to have a simple menu that uses all components in one place.
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 their system).
You can make an Update-button by creating a shortcut to: \yt-dlp\yt-dlp.exe -U
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]$Formats, [string]$Location)
function Download-YoutubeVideo
{
if(!$URL)
{
Write-Host "Enter YouTube URL: " -ForegroundColor Cyan -NoNewline; $URL = Read-Host
}
if(!$Cookies)
{
Write-Host "`n`n[E] Extract from Edge`n[C] Extract from Chrome`n[F] Extract from Firefox`n[R] Use last Browser Extract`n[M] Use manual cookies.txt`n[Enter] to Skip Cookies" -ForegroundColor Yellow
Write-Host "`nChoose Cookies: " -ForegroundColor Cyan -NoNewline; $Cookies = Read-Host
$File = $PSScriptRoot+'\Cookies\browser.txt';
switch -Regex ($Cookies)
{
'^E' { Remove-Item $File; $Args += @('--cookies-from-browser', 'edge', '--cookies', $File) }
'^C' { Remove-Item $File; $Args += @('--cookies-from-browser', 'chrome', '--cookies', $File) }
'^F' { Remove-Item $File; $Args += @('--cookies-from-browser', 'firefox', '--cookies', $File) }
'^R' { $Args += @('--cookies', $File) }
'^M' { $Args += @('--cookies', $File.Replace("browser.txt", "cookies.txt")) }
}
}
$Args += @('--js-runtimes', "node:$PSScriptRoot\node\node.exe")
if(!$Formats)
{
Write-Host "`n`nLoading all available formats..." -ForegroundColor Yellow
./yt-dlp/yt-dlp.exe --quiet --no-warnings $Args --list-formats $URL
Write-Host "`n`n[VideoID+AudioID] For Video and Audio`n[Enter] To automatically select BestVideo+BestAudio" -ForegroundColor Yellow
Write-Host "`nChoose Format ID(s): " -ForegroundColor Cyan -NoNewline; $Formats = Read-Host
if(!$Formats) { $Formats = 'bestvideo+bestaudio' }
$Args += @('--format', "$Formats")
}
if(!$Location)
{
$Location = $PSScriptRoot + '\Downloads'
$Args += @('--paths', $Location)
}
Write-Host "`nDownloading started..." -ForegroundColor Yellow
./yt-dlp/yt-dlp.exe --ffmpeg-location "$PSScriptRoot\ffmpeg\ffmpeg.exe" --merge-output-format mp4 --output '[%(id)s] %(title)s.%(ext)s' $Args $URL
Write-Host -ForegroundColor Green "`nDownload has finished!`n`n"
Remove-Variable * -ErrorAction SilentlyContinue
Download-YoutubeVideo
}
Download-YoutubeVideo
Use
Run the Download-YoutubeVideo.ps1 script, and answer the following prompts:
-URL
Enter the <URL> link of the YouTube video.
-Cookies
<C> to extract from Chrome
<F> to extract from Firefox
<L> to extract from local file 'cookies.txt'
Leave empty to download without cookies.
-Formats
A list of available Video and Audio Format ID's for this URL will be shown.
Enter the <Video Format ID> + <Audio Format ID>
Leave empty to choose 'bestvideo+bestaudio'.
The script will automatically start over after the download has finished.

