Video Downloader

PowerShell script to download videos from clipboard URLs.

# Requires PowerShell 5.1+

# 1. Define target download directory

$downloadDir = "$env:USERPROFILE\Downloads"

# Ensure the download directory exists and set it as working directory

if (Test-Path -Path $downloadDir) {
Set-Location -Path $downloadDir
}

# 2. Check for yt-dlp dependency

if (-not (Get-Command "yt-dlp" -ErrorAction SilentlyContinue)) {
Write-Host "[ERROR] 'yt-dlp' was not found in your PATH." -ForegroundColor Red
Write-Host "Please install yt-dlp (e.g., 'winget install yt-dlp') and try again.`n" -ForegroundColor Yellow
[System.Console]::ReadKey() | Out-Null
exit
}

# 3. Safely retrieve URL from clipboard

$url = (Get-Clipboard -ErrorAction SilentlyContinue)

if ([string]::IsNullOrWhiteSpace($url)) {
Write-Host "`n[WARNING] Clipboard is empty or does not contain text.`n" -ForegroundColor Yellow
Write-Host "Closing window..."
exit
}

$url = $url.Trim()

# 4. Validate URL format

if ($url -match '^https?://[^\s]+') {

Write-Host "Found URL: $url" -ForegroundColor Cyan
Write-Host "Starting download to: $downloadDir`n" -ForegroundColor Green

    # Array of arguments for robust parsing without escaping issues
    $ytArgs = @(
        '--no-playlist',
        '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
        $url
    )

    # Directly execute yt-dlp to stream progress output into the current console
    & yt-dlp @ytArgs

    if ($LASTEXITCODE -eq 0) {
        Write-Host "`nDownload completed successfully!" -ForegroundColor Green
        Write-Host "Press any key to open the Downloads folder..." -ForegroundColor Gray
        [System.Console]::ReadKey() | Out-Null

        Invoke-Item -Path $downloadDir
    } else {
        Write-Host "`n[ERROR] yt-dlp encountered an error during download (Exit Code: $LASTEXITCODE)." -ForegroundColor Red
        Write-Host "Press any key to exit..." -ForegroundColor Gray
        [System.Console]::ReadKey() | Out-Null
    }

} else {
Write-Host "`n[WARNING] No valid HTTP/HTTPS URL found in clipboard." -ForegroundColor Yellow
    Write-Host "Clipboard text was: '$url'`n" -ForegroundColor Gray
}

Write-Host "Closing window..."
exit