EurenikZ

YouTube Audio Downloader

Aug 23rd, 2025 (edited)
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  2. # YouTube Audio Downloader
  3. #
  4. # 1. Script als z. B. "download.ps1" mit Kodierung "UTF-8" anlegen.
  5. # 2. yt-dlp hier herunterladen und in den selben Ordner des Scripts kopieren: https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe
  6. # 3. Beim Ausführen des Scripts die Video- oder Kanal-URL angegeben. Das Script lädt alle Videos als MP3 herunter und passt das Datum zum Upload-Datum an. Bereits heruntergeladene Audios werden übersprungen.
  7. # 4. Es werden maximal 50 Videos wegen dem Rate-Limit von YouTube heruntergeladen. Nach Abschluss der Downloads wird das ursprüngliche Upload-Datum auf die Audio-Dateien gesetzt.
  8. #
  9. # Version 1.0.1 - 22.09.2025 - @nurjns
  10. # ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  11.  
  12. # Pfade definieren und prüfen
  13. $outputDir = Join-Path $PSScriptRoot 'audios'
  14. $archiveFile = Join-Path $PSScriptRoot 'downloaded.txt'
  15. $ytDlp = Join-Path $PSScriptRoot 'yt-dlp.exe'
  16.  
  17. if (!(Test-Path $ytDlp)) {
  18.     Write-Host "Fehler: yt-dlp.exe wurde im Skriptordner nicht gefunden: $ytDlp" -ForegroundColor Red
  19.     Write-Host "Hier herunterladen: https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe"
  20.     Pause
  21.     return
  22. }
  23.  
  24. # Kanal-Link interaktiv abfragen
  25. $channelUrl = Read-Host "YouTube Video-URL oder Kanal-URL"
  26.  
  27. # Sicherstellen, dass der Ausgabeordner existiert
  28. if (!(Test-Path $outputDir)) {
  29.     New-Item -ItemType Directory -Force -Path $outputDir | Out-Null
  30. }
  31.  
  32. # Download-Befehl mit yt-dlp (max. 50 Videos)
  33. $arguments = @(
  34.     $channelUrl,
  35.     "--download-archive", "`"$archiveFile`"",
  36.     "-o", "`"$outputDir/%(title)s [%(id)s].%(ext)s`"",
  37.     "-x",
  38.     "--audio-format", "mp3",
  39.     "--ffmpeg-location", "`"$PSScriptRoot`"",
  40.     "--max-downloads", "50"
  41. )
  42.  
  43. Write-Host "Starte Download mit yt-dlp..."
  44. & $ytDlp @arguments
  45.  
  46. if ($LASTEXITCODE -eq 101) {
  47.     Write-Host "Limit von 50 Videos erreicht! Fuer weitere Downloads Script neu starten." -ForegroundColor Yellow
  48. }
  49.  
  50. # Hole Liste aller Video-IDs, die im Archiv stehen (also schon geladen wurden)
  51. $downloadedIds = Get-Content $archiveFile | ForEach-Object { $_ -replace '^youtube ', '' }
  52.  
  53. # Schleife durch YouTube-Kanal
  54. foreach ($id in $downloadedIds) {
  55.     try {
  56.         $json = & $ytDlp -j --skip-download -- "$id" 2>$null | ConvertFrom-Json
  57.         if ($null -eq $json) {
  58.             Write-Warning "Keine Metadaten fuer ID $id gefunden"
  59.             continue
  60.         }
  61.  
  62.         $uploadDate = $json.upload_date
  63.         $title = $json.title
  64.  
  65.         # ungültige Zeichen durch "_" ersetzen
  66.         $invalidChars = [System.IO.Path]::GetInvalidFileNameChars() -join ''
  67.         $regex = "[{0}]" -f [Regex]::Escape($invalidChars)
  68.         $safeTitle = $title -replace $regex, '_'
  69.  
  70.         $fileName = "$outputDir\$($safeTitle) [$id].mp3"
  71.  
  72.         $fileFound = $false
  73.         $actualFileName = ""
  74.  
  75.         if (Test-Path $fileName) {
  76.             $fileFound = $true
  77.             $actualFileName = $fileName
  78.         }
  79.         else {
  80.             $searchPattern = "*[$id].*"
  81.             $matchingFiles = Get-ChildItem -Path $outputDir -Filter $searchPattern -ErrorAction SilentlyContinue
  82.             if ($matchingFiles.Count -gt 0) {
  83.                 $actualFileName = $matchingFiles[0].FullName
  84.                 $fileFound = $true
  85.                 Write-Host "Datei gefunden als: $($matchingFiles[0].Name)" -ForegroundColor Yellow
  86.             }
  87.         }
  88.  
  89.         if (!$fileFound) {
  90.             Write-Warning "Datei nicht gefunden fuer ID $id. Gesucht: $fileName"
  91.             continue
  92.         }
  93.  
  94.         if ($uploadDate -match '^\d{8}$') {
  95.             $year = $uploadDate.Substring(0, 4)
  96.             $month = $uploadDate.Substring(4, 2)
  97.             $day = $uploadDate.Substring(6, 2)
  98.  
  99.             try {
  100.                 $dt = Get-Date -Year $year -Month $month -Day $day -Hour 0 -Minute 0 -Second 0
  101.                 $file = Get-Item -LiteralPath $actualFileName
  102.                 if ($file -is [System.Array]) {
  103.                     $file = $file[0]
  104.                 }
  105.  
  106.                 if ($file.CreationTime.Date -ne $dt.Date -or $file.LastWriteTime.Date -ne $dt.Date -or $file.LastAccessTime.Date -ne $dt.Date) {
  107.                     Set-ItemProperty -LiteralPath $actualFileName -Name CreationTime -Value $dt
  108.                     Set-ItemProperty -LiteralPath $actualFileName -Name LastWriteTime -Value $dt
  109.                     Set-ItemProperty -LiteralPath $actualFileName -Name LastAccessTime -Value $dt
  110.                     Write-Host "Metadaten gesetzt fuer: $($file.Name)" -ForegroundColor Green
  111.                 }
  112.                 else {
  113.                     Write-Host "Metadaten bereits korrekt fuer: $($file.Name)" -ForegroundColor DarkGray
  114.                 }
  115.             }
  116.             catch {
  117.                 Write-Error "Fehler beim Setzen der Zeitstempel fuer $actualFileName : $($_.Exception.Message)"
  118.             }
  119.         }
  120.         else {
  121.             Write-Warning "Ungueltiges Upload-Datum Format fuer $fileName : $uploadDate"
  122.         }
  123.     }
  124.     catch {
  125.         Write-Error "Fehler beim Verarbeiten von ID $id : $($_.Exception.Message)"
  126.     }
  127. }
  128.  
  129. Write-Host "Fertig!" -ForegroundColor Green
  130. pause
Advertisement
Add Comment
Please, Sign In to add comment