Created
August 3, 2025 09:15
-
-
Save exoticknight/dbf66c3090f42ec76a6ef8719fed2af6 to your computer and use it in GitHub Desktop.
yt-dlp subtitles downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Batch YouTube Subtitle Downloader | |
| # A PowerShell script for downloading subtitles from multiple YouTube videos using yt-dlp | |
| # | |
| # Author: exoticknight | |
| # Version: 1.0 | |
| # Requirements: yt-dlp must be installed and available in PATH | |
| # | |
| # ============================================================================= | |
| # DESCRIPTION | |
| # ============================================================================= | |
| # This script automates the process of downloading subtitles from multiple | |
| # YouTube videos. It takes a list of YouTube video IDs and executes yt-dlp | |
| # commands for each video with a configurable delay between downloads. | |
| # | |
| # ============================================================================= | |
| # HOW IT WORKS | |
| # ============================================================================= | |
| # 1. The script reads a list of YouTube video IDs from a multi-line string | |
| # 2. For each video ID, it constructs a yt-dlp command with specified parameters | |
| # 3. Commands are executed sequentially with real-time output display | |
| # 4. Each command's exit code is monitored to determine success/failure | |
| # 5. A delay is added between downloads to avoid overwhelming the server | |
| # 6. A comprehensive summary report is generated at the end | |
| # | |
| # ============================================================================= | |
| # CONFIGURATION PARAMETERS | |
| # ============================================================================= | |
| # YouTube Video IDs - Add your video IDs here, one per line | |
| $videoIds = @" | |
| xxxxxxxx | |
| "@ | |
| # Subtitle language configuration | |
| # Common language codes: | |
| # en = English, zh-Hans = Chinese Simplified, zh-Hant = Chinese Traditional | |
| # es = Spanish, fr = French, de = German, ja = Japanese, ko = Korean | |
| # ar = Arabic, ru = Russian, pt = Portuguese, it = Italian | |
| $subtitleLanguage = "en" | |
| # Delay between downloads (in seconds) - Recommended: 1-3 seconds | |
| $delayBetweenDownloads = 1 | |
| # Additional yt-dlp options (modify as needed) | |
| # --skip-download: Only download subtitles, not the video | |
| # --write-auto-sub: Download auto-generated subtitles if manual ones aren't available | |
| # --sub-format: Preferred subtitle format (best/vtt/srt/ass) | |
| $ytdlpOptions = "--skip-download" | |
| # ============================================================================= | |
| # USAGE INSTRUCTIONS | |
| # ============================================================================= | |
| # 1. Install yt-dlp: | |
| # - Windows: pip install yt-dlp OR download from GitHub releases | |
| # - Ensure yt-dlp is available in your PATH | |
| # | |
| # 2. Configure this script: | |
| # - Replace the video IDs in $videoIds with your target videos | |
| # - Set $subtitleLanguage to your desired language code | |
| # - Adjust $delayBetweenDownloads if needed | |
| # | |
| # 3. Run the script: | |
| # - Save this script as "batch-subtitle-downloader.ps1" | |
| # - Open PowerShell in your desired download directory | |
| # - Execute: .\batch-subtitle-downloader.ps1 | |
| # | |
| # 4. Monitor the output: | |
| # - Watch real-time progress for each video | |
| # - Check the summary report at the end | |
| # - Review any failed downloads and retry if necessary | |
| # | |
| # ============================================================================= | |
| # SCRIPT EXECUTION | |
| # ============================================================================= | |
| # Build the yt-dlp command template | |
| $commandTemplate = 'yt-dlp --sub-lang "{0}" {1} youtu.be/{2}' -f $subtitleLanguage, $ytdlpOptions, '{0}' | |
| # Initialize tracking arrays | |
| $errorCommands = @() | |
| $executedCommands = @() | |
| # Process video IDs and clean up the list | |
| $videoIdList = $videoIds -split "`n" | Where-Object { $_.Trim() -ne "" } | ForEach-Object { $_.Trim() } | |
| # Generate command list | |
| $commandLines = @() | |
| foreach ($videoId in $videoIdList) { | |
| $command = $commandTemplate -f $videoId | |
| $commandLines += $command | |
| } | |
| # Display startup information | |
| Write-Host ("=" * 80) -ForegroundColor Green | |
| Write-Host "π¬ BATCH YOUTUBE SUBTITLE DOWNLOADER" -ForegroundColor Green | |
| Write-Host ("=" * 80) -ForegroundColor Green | |
| Write-Host "π Total videos to process: $($videoIdList.Count)" -ForegroundColor Cyan | |
| Write-Host "π Subtitle language: $subtitleLanguage" -ForegroundColor Cyan | |
| Write-Host "β±οΈ Delay between downloads: $delayBetweenDownloads second(s)" -ForegroundColor Cyan | |
| Write-Host "π§ Command template: $commandTemplate" -ForegroundColor Yellow | |
| Write-Host "π Output directory: $(Get-Location)" -ForegroundColor Gray | |
| Write-Host "" | |
| # Execute commands for each video | |
| for ($i = 0; $i -lt $commandLines.Count; $i++) { | |
| $currentCommand = $commandLines[$i] | |
| $currentVideoId = $videoIdList[$i] | |
| $commandNumber = $i + 1 | |
| Write-Host "`n" + ("=" * 60) -ForegroundColor Yellow | |
| Write-Host "[$commandNumber/$($commandLines.Count)] Processing Video: $currentVideoId" -ForegroundColor Cyan | |
| Write-Host "π YouTube URL: https://youtu.be/$currentVideoId" -ForegroundColor Blue | |
| Write-Host "β‘ Executing: $currentCommand" -ForegroundColor White | |
| Write-Host ("=" * 60) -ForegroundColor Yellow | |
| try { | |
| # Record start time | |
| $startTime = Get-Date | |
| # Execute command in current window with real-time output | |
| Invoke-Expression $currentCommand | |
| $exitCode = $LASTEXITCODE | |
| # Record end time and calculate duration | |
| $endTime = Get-Date | |
| $duration = $endTime - $startTime | |
| Write-Host "`n" + ("-" * 40) -ForegroundColor Gray | |
| Write-Host "β±οΈ Execution time: $($duration.TotalSeconds.ToString('F1')) seconds" -ForegroundColor Gray | |
| Write-Host "π’ Exit code: $exitCode" -ForegroundColor $(if ($exitCode -eq 0) { "Green" } else { "Red" }) | |
| # Determine status based on exit code | |
| if ($exitCode -eq 0) { | |
| Write-Host "β Command completed successfully" -ForegroundColor Green | |
| $status = "Success" | |
| } else { | |
| Write-Host "β Command failed (Exit code: $exitCode)" -ForegroundColor Red | |
| $status = "Failed" | |
| # Record failed video | |
| $errorCommands += [PSCustomObject]@{ | |
| Number = $commandNumber | |
| VideoId = $currentVideoId | |
| ExitCode = $exitCode | |
| Command = $currentCommand | |
| Duration = $duration.TotalSeconds | |
| } | |
| } | |
| $executedCommands += [PSCustomObject]@{ | |
| Number = $commandNumber | |
| VideoId = $currentVideoId | |
| Status = $status | |
| ExitCode = $exitCode | |
| Duration = $duration.TotalSeconds | |
| } | |
| } | |
| catch { | |
| Write-Host "`nβ PowerShell execution error: $($_.Exception.Message)" -ForegroundColor Red | |
| $errorCommands += [PSCustomObject]@{ | |
| Number = $commandNumber | |
| VideoId = $currentVideoId | |
| ExitCode = -1 | |
| Command = $currentCommand | |
| Error = $_.Exception.Message | |
| Duration = 0 | |
| } | |
| $executedCommands += [PSCustomObject]@{ | |
| Number = $commandNumber | |
| VideoId = $currentVideoId | |
| Status = "Exception" | |
| ExitCode = -1 | |
| Duration = 0 | |
| } | |
| } | |
| # Add delay between downloads (except for the last video) | |
| if ($i -lt $commandLines.Count - 1) { | |
| Write-Host "`nβ³ Waiting $delayBetweenDownloads second(s) before next video..." -ForegroundColor DarkGray | |
| Start-Sleep -Seconds $delayBetweenDownloads | |
| } | |
| } | |
| # ============================================================================= | |
| # EXECUTION SUMMARY REPORT | |
| # ============================================================================= | |
| Write-Host "`n" + ("=" * 80) -ForegroundColor Green | |
| Write-Host "π BATCH PROCESSING COMPLETED - SUMMARY REPORT" -ForegroundColor Green | |
| Write-Host ("=" * 80) -ForegroundColor Green | |
| # Display results table | |
| Write-Host "`nπ Execution Results Overview:" -ForegroundColor Cyan | |
| $executedCommands | Format-Table -Property Number, VideoId, Status, ExitCode, @{Name="Duration(s)"; Expression={$_.Duration.ToString("F1")}} -AutoSize | |
| # Calculate statistics | |
| $successCount = ($executedCommands | Where-Object { $_.Status -eq "Success" }).Count | |
| $failedCount = ($executedCommands | Where-Object { $_.Status -eq "Failed" }).Count | |
| $exceptionCount = ($executedCommands | Where-Object { $_.Status -eq "Exception" }).Count | |
| $totalTime = ($executedCommands | Measure-Object -Property Duration -Sum).Sum | |
| Write-Host "π Processing Statistics:" -ForegroundColor Yellow | |
| Write-Host " π¬ Total videos processed: $($executedCommands.Count)" | |
| Write-Host " β Successful downloads: $successCount" -ForegroundColor Green | |
| Write-Host " β Failed downloads: $failedCount" -ForegroundColor Red | |
| Write-Host " π₯ Execution exceptions: $exceptionCount" -ForegroundColor Magenta | |
| Write-Host " β±οΈ Total execution time: $($totalTime.ToString('F1')) seconds" -ForegroundColor Gray | |
| Write-Host " π Success rate: $(($successCount / $executedCommands.Count * 100).ToString('F1'))%" -ForegroundColor Cyan | |
| # Display failed videos details if any | |
| if ($errorCommands.Count -gt 0) { | |
| Write-Host "`nβ οΈ Failed Downloads Details:" -ForegroundColor Red | |
| Write-Host ("-" * 50) -ForegroundColor Red | |
| foreach ($errorCmd in $errorCommands) { | |
| Write-Host "π΄ Video $($errorCmd.Number): $($errorCmd.VideoId)" -ForegroundColor Red | |
| Write-Host " π URL: https://youtu.be/$($errorCmd.VideoId)" -ForegroundColor Blue | |
| Write-Host " π€ Exit code: $($errorCmd.ExitCode)" -ForegroundColor Yellow | |
| if ($errorCmd.Error) { | |
| Write-Host " β Error: $($errorCmd.Error)" -ForegroundColor Red | |
| } | |
| if ($errorCmd.Duration) { | |
| Write-Host " β±οΈ Duration: $($errorCmd.Duration.ToString('F1'))s" -ForegroundColor Gray | |
| } | |
| Write-Host "" | |
| } | |
| Write-Host "π Failed Video IDs (for retry):" -ForegroundColor Red | |
| $failedIds = $errorCommands | ForEach-Object { $_.VideoId } | |
| Write-Host ($failedIds -join "`n") -ForegroundColor Red | |
| Write-Host "`nπ‘ Troubleshooting Tips:" -ForegroundColor Yellow | |
| Write-Host " β’ Check if yt-dlp is properly installed and updated" -ForegroundColor White | |
| Write-Host " β’ Verify video IDs are correct and videos are public" -ForegroundColor White | |
| Write-Host " β’ Some videos may not have subtitles in the requested language" -ForegroundColor White | |
| Write-Host " β’ Try adding --write-auto-sub for auto-generated subtitles" -ForegroundColor White | |
| } else { | |
| Write-Host "`nπ ALL VIDEOS PROCESSED SUCCESSFULLY!" -ForegroundColor Green | |
| Write-Host "π― All subtitle downloads completed without errors." -ForegroundColor Green | |
| } | |
| Write-Host "`nπΌ Output Information:" -ForegroundColor Cyan | |
| Write-Host "π Subtitle files saved to: $(Get-Location)" -ForegroundColor Gray | |
| Write-Host "π Common subtitle formats: .vtt, .srt, .ass" -ForegroundColor Gray | |
| Write-Host "π Check the directory for downloaded subtitle files" -ForegroundColor Gray | |
| Write-Host "`n⨠Script execution completed! Thank you for using Batch YouTube Subtitle Downloader." -ForegroundColor Green | |
| Write-Host ("=" * 80) -ForegroundColor Green | |
| # ============================================================================= | |
| # END OF SCRIPT | |
| # ============================================================================= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment