Difference between revisions of "PowerShell: Download YouTube Video"

From DanIT
Jump to navigation Jump to search
m (Dan moved page PowerShell: Download Youtube Video to PowerShell: Download YouTube Video without leaving a redirect)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=Overview=
 
=Overview=
 
Long story short, YouTube makes it not so easy to download their videos.</br>
 
Long story short, YouTube makes it not so easy to download their videos.</br>
So regardless what application you use to download them, it is always gonna require or include the following base components.</br>
+
So regardless what application you use to download, it is always going to require or include the following base components:</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>
 
  
 
==='yt-dlp'===
 
==='yt-dlp'===
Line 10: Line 8:
  
 
==='ffmpeg'===
 
==='ffmpeg'===
ffmpeg.exe is a command line application that is used to convert the videos to .mp4 format (if needed).</br>
+
YouTube stores videos as multiple components, seperating video from audio, and having different quality levels.</br>
 +
 
 +
ffmpeg.exe is a command line application to merge the video with audio into one file, and to convert the format if needed.</br>
 
This application is open-source (https://github.com/FFmpeg/FFmpeg).</br>
 
This application is open-source (https://github.com/FFmpeg/FFmpeg).</br>
  
===Quality===
+
===Cookies===
YouTube devides videos up into multiple components, seperating video from audio, and having different quality levels.</br>
+
Cookies from your YouTube session from browser are sometimes required to download, as YouTube requires identification for restricted videos or users.</br>
  
===Cookies===
+
=Script=
Cookies from your browser are sometimes required to download, as YouTube requires identification for restricted videos or users.</br>
+
Setting up this PowerShell Menu Script 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.
 +
* A list of available formats for the given URL will automatically be shown.
 +
* 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.
  
 
=Setup=
 
=Setup=
Line 35: Line 42:
 
     if(!$URL)
 
     if(!$URL)
 
     {
 
     {
         Write-Host ""; Write-Host "Enter YouTube URL: " -ForegroundColor Yellow -NoNewline; $URL = Read-Host
+
         Write-Host "`nEnter YouTube URL: " -ForegroundColor Yellow -NoNewline; $URL = Read-Host
 
     }
 
     }
  
 
     if(!$Cookies)
 
     if(!$Cookies)
 
     {
 
     {
         Write-Host ""; Write-Host "Select Cookies" -ForegroundColor Yellow
+
         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
 
         Write-Host "[C] for Chrome, [F] for Firefox, [L] for local cookies.txt, empty to skip: " -ForegroundColor Yellow -NoNewline; $Cookies = Read-Host
  
Line 50: Line 57:
 
     if(!$Quality)
 
     if(!$Quality)
 
     {
 
     {
 +
        Write-Host ""
 
         ./yt-dlp.exe $CookiesArgs -F $URL
 
         ./yt-dlp.exe $CookiesArgs -F $URL
  
         Write-Host ""; Write-Host "Select Quality" -ForegroundColor Yellow
+
         Write-Host "`nSelect Format ID(s) for Quality" -ForegroundColor Yellow
         Write-Host "Video ID + Audio ID, empty for best: " -ForegroundColor Yellow -NoNewline; $Quality = Read-Host
+
         Write-Host "'Video ID + Audio ID', empty for best video and audio: " -ForegroundColor Yellow -NoNewline; $Quality = Read-Host
  
 
         if(!$Quality) { $Quality = 'bestvideo+bestaudio' }
 
         if(!$Quality) { $Quality = 'bestvideo+bestaudio' }
Line 63: Line 71:
 
     }
 
     }
  
 +
    Write-Host ""
 
     ./yt-dlp.exe --format $Quality --merge-output-format mp4 $CookiesArgs --paths $Location --output '%(title)s.%(ext)s' $URL
 
     ./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 ""
+
     Write-Host -ForegroundColor Green "`nDone!"
  
 
     Remove-Variable * -ErrorAction SilentlyContinue
 
     Remove-Variable * -ErrorAction SilentlyContinue
Line 74: Line 83:
 
</pre>
 
</pre>
  
=Script=
+
=Use=
* Make sure to try Update YT-DLP first (with the shortcut) when encountering issues.
 
* A list of available formats for the given URL will automatically be shown.
 
* Downloads will automatically be converted to .mp4 (if needed), using ffmpeg.
 
* Script has parameters, so can also be used by other scripts and automations/jobs.
 
* Everything is open-source.
 
 
 
=Use Script=
 
 
Open the '''Download.ps1''' file, and answer the following prompts:</br>
 
Open the '''Download.ps1''' file, and answer the following prompts:</br>
  

Latest revision as of 22:55, 22 December 2025

Overview

Long story short, YouTube makes it not so easy to download their videos.
So regardless what application you use to download, it is always going to require or include the following base components:

'yt-dlp'

yt-dlp.exe is a command line application to download from YouTube, and needs to be regularly updated (as YouTube keeps changing the system).
This application is open-source (https://github.com/yt-dlp/yt-dlp/).

'ffmpeg'

YouTube stores videos as multiple components, seperating video from audio, and having different quality levels.

ffmpeg.exe is a command line application to merge the video with audio into one file, and to convert the format if needed.
This application is open-source (https://github.com/FFmpeg/FFmpeg).

Cookies

Cookies from your YouTube session from browser are sometimes required to download, as YouTube requires identification for restricted videos or users.

Script

Setting up this PowerShell Menu Script is IMO the best way to do it for Windows, in terms of balancing security and ease of use and edit.

  • Make sure to try Update YT-DLP first (with the shortcut) when encountering issues.
  • A list of available formats for the given URL will automatically be shown.
  • 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.

Setup

Create a new folder, and put the following in there:

  1. Download yt-dlp.exe from https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe
  2. Download ffmpeg.exe from https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip (found in the 'bin' folder).
  3. Create a new shortcut, with location yt-dlp.exe -U, and name it Update YT-DLP
  4. Create a new folder called Downloads
  5. 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 "`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") { $CookiesArgs = @('--cookies-from-browser', 'chrome') }
        if($Cookies -eq "F") { $CookiesArgs = @('--cookies-from-browser', 'firefox') }
        if($Cookies -eq "L") { $CookiesArgs = @('--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'
    }

    Write-Host ""
    ./yt-dlp.exe --format $Quality --merge-output-format mp4 $CookiesArgs --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'.