Difference between revisions of "PowerShell: Download YouTube Video"

From DanIT
Jump to navigation Jump to search
Line 1: Line 1:
=Components=
+
==Components==
 
These parts are needed to '''Make Your Own YouTube Downloader'''</br>
 
These parts are needed to '''Make Your Own YouTube Downloader'''</br>
  
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>
  
=Script=
+
==Script==
 
'''Folder Layout'''</br>
 
'''Folder Layout'''</br>
 
[[File:YouTubeDownloader Folder.png||Folder Layout]]</br>
 
[[File:YouTubeDownloader Folder.png||Folder Layout]]</br>
Line 83: Line 83:
 
</PRE>
 
</PRE>
  
=Use=
+
==Use==
Open the '''Download.ps1''' file, and answer the following prompts:</br>
+
[[File:Download-YouTubeVideo Preview.png|thumb|right|800px|Download-YouTubeVideo.ps1]]
 +
Open the '''Download-YoutubeVideo.ps1''' file, and answer the following prompts:</br>
  
===URL===
+
===-URL===
The link of the YouTube video
+
Enter the '''<URL>''' link of the YouTube video.</br>
  
===Cookies===
+
===-Cookies===
'C' for extract from Chrome</br>
+
'''<C>''' to extract from Chrome</br>
'F' for extract from Firefox</br>
+
'''<F>''' to extract from Firefox</br>
'L' for extract from local file 'cookies.txt'</br>
+
'''<L>''' to extract from local file 'cookies.txt'</br>
Leave empty for attempt to download without cookies</br>
+
 
 +
'''Leave empty''' to attempt download without cookies.</br>
 +
 
 +
===-Formats===
 +
A list of available Video and Audio Format ID's for this URL will be shown.</br>
 +
Enter the '''<Video Format ID>''' '''+''' '''<Audio Format ID>'''</br>
  
===Quality===
+
'''Leave empty''' to automatically choose 'bestvideo+bestaudio'.</br>
Enter the ID(s) of the format(s) that you want to download from the list.</br>
 
If you want to download Video and Audio and combine them, enter like: 'Video ID'+'Audio ID'.</br>
 
Leave empty to automatically choose 'bestvideo+bestaudio'.</br>
 

Revision as of 02:02, 18 April 2026

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 the 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

Folder Layout
Folder Layout

PowerShell

param([string]$URL, [string]$Cookies, [string]$Formats, [string]$Location)

function Download-YoutubeVideo
{
    if(!$URL)
    {
        Write-Host "`n-URL <URL>" -ForegroundColor Yellow
        Write-Host "Enter YouTube URL: " -ForegroundColor Yellow -NoNewline; $URL = Read-Host
    }

    if(!$Cookies)
    {
        Write-Host "`n-Cookies <[C]hrome> <[F]irefox> <[L]ocal> (empty to Skip)" -ForegroundColor Yellow
        Write-Host "Select Cookies: " -ForegroundColor Yellow -NoNewline; $Cookies = Read-Host

        switch -Regex ($Cookies)
        {
            '^C' { $Args += @('--cookies-from-browser', 'chrome') }
            '^F' { $Args += @('--cookies-from-browser', 'firefox') }
            '^L' { $Args += @('--cookies', 'cookies.txt') }
        }
    }

    if(!$Formats)
    {
        Write-Host "`nLoading all available formats..." -ForegroundColor Yellow
        ./yt-dlp/yt-dlp.exe --quiet --no-warnings --js-runtimes "node:$PSScriptRoot\node\node.exe" $Args --list-formats $URL

        Write-Host "`n-Formats <[Video ID] + [Audio ID]> (empty for Best video and audio)" -ForegroundColor Yellow
        Write-Host "Select Format ID(s): " -ForegroundColor Yellow -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 --js-runtimes "node:$PSScriptRoot\node\node.exe" --ffmpeg-location "$PSScriptRoot\ffmpeg\ffmpeg.exe" --merge-output-format mp4 --output '%(title)s.%(ext)s' $Args $URL

    Write-Host -ForegroundColor Green "`nDone!"

    Remove-Variable * -ErrorAction SilentlyContinue # -Exclude 'Cookies'
    Download-YoutubeVideo
}

Download-YoutubeVideo

Use

Download-YouTubeVideo.ps1

Open the Download-YoutubeVideo.ps1 file, 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 attempt 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 automatically choose 'bestvideo+bestaudio'.