nurjns

YouTube Audio Downloader

Aug 23rd, 2025 (edited)
8,298
0
Never
1
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.2 - 23.08.2026 - @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. $fileAge = (Get-Date) - (Get-Item $ytDlp).LastWriteTime
  25. if ($fileAge.TotalDays -gt 14) {
  26.     Write-Host "Hinweis: yt-dlp.exe ist aelter als 2 Wochen. Ein Update wird empfohlen:" -ForegroundColor Yellow
  27.     Write-Host "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe"
  28.     Write-Host ""
  29. }
  30.  
  31. # Kanal-Link interaktiv abfragen
  32. $channelUrl = Read-Host "YouTube Video-URL oder Kanal-URL"
  33.  
  34. # Sicherstellen, dass der Ausgabeordner existiert
  35. if (!(Test-Path $outputDir)) {
  36.     New-Item -ItemType Directory -Force -Path $outputDir | Out-Null
  37. }
  38.  
  39. # Download-Befehl mit yt-dlp (max. 50 Videos)
  40. $arguments = @(
  41.     $channelUrl,
  42.     "--download-archive", "`"$archiveFile`"",
  43.     "-o", "`"$outputDir/%(title)s [%(id)s].%(ext)s`"",
  44.     "-x",
  45.     "--audio-format", "mp3",
  46.     "--ffmpeg-location", "`"$PSScriptRoot`"",
  47.     "--max-downloads", "50"
  48. )
  49.  
  50. Write-Host "Starte Download mit yt-dlp..."
  51. & $ytDlp @arguments
  52.  
  53. if ($LASTEXITCODE -eq 101) {
  54.     Write-Host "Limit von 50 Videos erreicht! Fuer weitere Downloads Script neu starten." -ForegroundColor Yellow
  55. }
  56.  
  57. # Hole Liste aller Video-IDs, die im Archiv stehen (also schon geladen wurden)
  58. $downloadedIds = Get-Content $archiveFile | ForEach-Object { $_ -replace '^youtube ', '' }
  59.  
  60. # Schleife durch YouTube-Kanal
  61. foreach ($id in $downloadedIds) {
  62.     try {
  63.         $json = & $ytDlp -j --skip-download -- "$id" 2>$null | ConvertFrom-Json
  64.         if ($null -eq $json) {
  65.             Write-Warning "Keine Metadaten fuer ID $id gefunden"
  66.             continue
  67.         }
  68.  
  69.         $uploadDate = $json.upload_date
  70.         $title = $json.title
  71.  
  72.         # ungültige Zeichen durch "_" ersetzen
  73.         $invalidChars = [System.IO.Path]::GetInvalidFileNameChars() -join ''
  74.         $regex = "[{0}]" -f [Regex]::Escape($invalidChars)
  75.         $safeTitle = $title -replace $regex, '_'
  76.  
  77.         $fileName = "$outputDir\$($safeTitle) [$id].mp3"
  78.  
  79.         $fileFound = $false
  80.         $actualFileName = ""
  81.  
  82.         if (Test-Path $fileName) {
  83.             $fileFound = $true
  84.             $actualFileName = $fileName
  85.         }
  86.         else {
  87.             $searchPattern = "*[$id].*"
  88.             $matchingFiles = Get-ChildItem -Path $outputDir -Filter $searchPattern -ErrorAction SilentlyContinue
  89.             if ($matchingFiles.Count -gt 0) {
  90.                 $actualFileName = $matchingFiles[0].FullName
  91.                 $fileFound = $true
  92.                 Write-Host "Datei gefunden als: $($matchingFiles[0].Name)" -ForegroundColor Yellow
  93.             }
  94.         }
  95.  
  96.         if (!$fileFound) {
  97.             Write-Warning "Datei nicht gefunden fuer ID $id. Gesucht: $fileName"
  98.             continue
  99.         }
  100.  
  101.         if ($uploadDate -match '^\d{8}$') {
  102.             $year = $uploadDate.Substring(0, 4)
  103.             $month = $uploadDate.Substring(4, 2)
  104.             $day = $uploadDate.Substring(6, 2)
  105.  
  106.             try {
  107.                 $dt = Get-Date -Year $year -Month $month -Day $day -Hour 0 -Minute 0 -Second 0
  108.                 $file = Get-Item -LiteralPath $actualFileName
  109.                 if ($file -is [System.Array]) {
  110.                     $file = $file[0]
  111.                 }
  112.  
  113.                 if ($file.CreationTime.Date -ne $dt.Date -or $file.LastWriteTime.Date -ne $dt.Date -or $file.LastAccessTime.Date -ne $dt.Date) {
  114.                     Set-ItemProperty -LiteralPath $actualFileName -Name CreationTime -Value $dt
  115.                     Set-ItemProperty -LiteralPath $actualFileName -Name LastWriteTime -Value $dt
  116.                     Set-ItemProperty -LiteralPath $actualFileName -Name LastAccessTime -Value $dt
  117.                     Write-Host "Metadaten gesetzt fuer: $($file.Name)" -ForegroundColor Green
  118.                 }
  119.                 else {
  120.                     Write-Host "Metadaten bereits korrekt fuer: $($file.Name)" -ForegroundColor DarkGray
  121.                 }
  122.             }
  123.             catch {
  124.                 Write-Error "Fehler beim Setzen der Zeitstempel fuer $actualFileName : $($_.Exception.Message)"
  125.             }
  126.         }
  127.         else {
  128.             Write-Warning "Ungueltiges Upload-Datum Format fuer $fileName : $uploadDate"
  129.         }
  130.     }
  131.     catch {
  132.         Write-Error "Fehler beim Verarbeiten von ID $id : $($_.Exception.Message)"
  133.     }
  134. }
  135.  
  136. Write-Host "Fertig!" -ForegroundColor Green
  137. pause
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment