Difference between revisions of "PowerShell: Download YouTube Video"

From DanIT
Jump to navigation Jump to search
m
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Overview=
+
==Components==
Long story short, YouTube makes it not so easy to download their videos.</br>
+
These parts are needed to ''Make Your Own'' '''YouTube Downloader''':</br>
So regardless what application you use to download them, it is always gonna require or include the following base components:</br>
 
  
==='yt-dlp'===
+
===Interface===
yt-dlp.exe is a command line application to download from YouTube, and needs to be regularly updated (as YouTube keeps changing the system).</br>
+
The [[PowerShell: Download YouTube Video#Script|Download-YoutubeVideo.ps1]] script.</br>
This application is open-source (https://github.com/yt-dlp/yt-dlp/).</br>
+
Useful to have a simple menu that uses all components in one place.</br>
  
==='ffmpeg'===
+
===yt-dlp===
ffmpeg.exe is a command line application that is used to convert the videos to .mp4 format (if needed).</br>
+
Required to download Video and Audio from YouTube.</br>
This application is open-source (https://github.com/FFmpeg/FFmpeg).</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>
  
===Quality===
+
Needs to be regularly updated (as YouTube keeps changing their system).</br>
YouTube devides videos up into multiple components, seperating video from audio, and having different quality levels.</br>
+
You can make an Update-button by creating a shortcut to: <code>\yt-dlp\yt-dlp.exe -U</code>
 +
 
 +
===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===
Cookies from your browser are sometimes required to download, as YouTube requires identification for restricted videos or users.</br>
+
Sometimes required, as YouTube requires account identification on restricted videos or restricted client IPs.</br>
 
 
=Setup=
 
Create a new folder, and put the following in there:
 
  
# Download '''yt-dlp.exe''' from https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe
+
</br>
# Download '''ffmpeg.exe''' from https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip (found in the 'bin' folder).
+
==Script==
# Create a new shortcut, with location '''yt-dlp.exe -U''', and name it '''Update YT-DLP'''
+
'''Folder Layout'''</br>
# Create a new folder called '''Downloads'''
+
[[File:YouTubeDownloader Folder.png||Folder Layout]]</br>
# Open Notepad, copy the PowerShell code below, and save it as '''Download.ps1'''
 
  
<pre>
+
[[Media:Download-YoutubeVideo.ps1|Download-YoutubeVideo.ps1]]</br>
param([string]$URL, [string]$Cookies, [string]$Quality, [string]$Location)
+
<PRE>
 +
param([string]$URL, [string]$Cookies, [string]$Formats, [string]$Location)
  
 
function Download-YoutubeVideo
 
function Download-YoutubeVideo
Line 33: Line 37:
 
     if(!$URL)
 
     if(!$URL)
 
     {
 
     {
         Write-Host ""; Write-Host "Enter YouTube URL: " -ForegroundColor Yellow -NoNewline; $URL = Read-Host
+
         Write-Host "Enter YouTube URL: " -ForegroundColor Cyan -NoNewline; $URL = Read-Host
 
     }
 
     }
  
 
     if(!$Cookies)
 
     if(!$Cookies)
 
     {
 
     {
         Write-Host ""; Write-Host "Select Cookies" -ForegroundColor Yellow
+
         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 "[C] for Chrome, [F] for Firefox, [L] for local cookies.txt, empty to skip: " -ForegroundColor Yellow -NoNewline; $Cookies = Read-Host
+
        Write-Host "`nChoose Cookies: " -ForegroundColor Cyan -NoNewline; $Cookies = Read-Host
  
         if($Cookies -eq "C") { $CookiesArgs = @('--cookies-from-browser', 'chrome') }
+
         $File = $PSScriptRoot+'\Cookies\browser.txt';
        if($Cookies -eq "F") { $CookiesArgs = @('--cookies-from-browser', 'firefox') }
+
        switch -Regex ($Cookies)
        if($Cookies -eq "L") { $CookiesArgs = @('--cookies', 'cookies.txt') }
+
        {
 +
            '^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")) }
 +
        }
 
     }
 
     }
  
     if(!$Quality)
+
    $Args += @('--js-runtimes', "node:$PSScriptRoot\node\node.exe")
 +
 
 +
     if(!$Formats)
 
     {
 
     {
         ./yt-dlp.exe $CookiesArgs -F $URL
+
        Write-Host "`n`nLoading all available formats..." -ForegroundColor Yellow
 +
         ./yt-dlp/yt-dlp.exe --quiet --no-warnings $Args --list-formats $URL
  
         Write-Host ""; Write-Host "Select Quality" -ForegroundColor Yellow
+
         Write-Host "`n`n[VideoID+AudioID] For Video and Audio`n[Enter] To automatically select BestVideo+BestAudio" -ForegroundColor Yellow
         Write-Host "Video ID + Audio ID, empty for best: " -ForegroundColor Yellow -NoNewline; $Quality = Read-Host
+
         Write-Host "`nChoose Format ID(s): " -ForegroundColor Cyan -NoNewline; $Formats = Read-Host
  
         if(!$Quality) { $Quality = 'bestvideo+bestaudio' }
+
         if(!$Formats) { $Formats = 'bestvideo+bestaudio' }
 +
        $Args += @('--format', "$Formats")
 
     }
 
     }
  
Line 59: Line 73:
 
     {
 
     {
 
         $Location = $PSScriptRoot + '\Downloads'
 
         $Location = $PSScriptRoot + '\Downloads'
 +
        $Args += @('--paths', $Location)
 
     }
 
     }
  
     ./yt-dlp.exe --format $Quality --merge-output-format mp4 $CookiesArgs --paths $Location --output '%(title)s.%(ext)s' $URL
+
    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 ""; Write-Host -ForegroundColor Green "Done!"; Write-Host ""
+
     Write-Host -ForegroundColor Green "`nDownload has finished!`n`n"
  
 
     Remove-Variable * -ErrorAction SilentlyContinue
 
     Remove-Variable * -ErrorAction SilentlyContinue
Line 70: Line 86:
  
 
Download-YoutubeVideo
 
Download-YoutubeVideo
</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]]
 +
Run the '''Download-YoutubeVideo.ps1''' script, 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 download without cookies.</br>
  
===Quality===
+
===-Formats===
Enter the ID(s) of the format(s) that you want to download from the list.</br>
+
A list of available Video and Audio Format ID's for this URL will be shown.</br>
If you want to download Video and Audio and combine them, enter like: 'Video ID'+'Audio ID'.</br>
+
Enter the '''<Video Format ID>''' '''+''' '''<Audio Format ID>'''</br>
Leave empty to automatically choose 'bestvideo+bestaudio'.</br>
 
  
=About=
+
'''Leave empty''' to choose [https://github.com/yt-dlp/yt-dlp#format-selection 'bestvideo+bestaudio'].</br>
Setting up this system is IMO the best way to do it for Windows, in terms of balancing security and ease of use and edit.</br>
 
  
* Make sure to try Update YT-DLP first (with the shortcut) when encountering issues.
+
</br>
* A list of available formats for the given URL will automatically be shown.
+
The script will automatically start over after the download has finished.</br>
* Downloads will automatically be converted to .mp4 (if needed), using ffmpeg.
 
* After download completion, you will automatically be prompted for a next download.
 
* Script has parameters, so can also be used by other scripts and automations/jobs.
 
* Everything is open-source.
 

Latest revision as of 08:19, 27 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 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

Folder Layout
Folder Layout

Download-YoutubeVideo.ps1

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

Download-YouTubeVideo.ps1

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.