Advertisement
Chrisdbhr

Whisper Transcribe to subtitle all media on folder from powershell

Oct 28th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.39 KB | Software | 0 0
  1. Add-Type -AssemblyName System.Windows.Forms
  2.  
  3. # Caminho para salvar a última pasta usada
  4. $lastFolderPathFile = "$HOME\lastFolderPath.txt"
  5.  
  6. # Recupera a última pasta usada, se disponível
  7. if (Test-Path -Path $lastFolderPathFile) {
  8.     $lastFolderPath = Get-Content -Path $lastFolderPathFile
  9. } else {
  10.     $lastFolderPath = [System.Environment]::GetFolderPath("MyDocuments")
  11. }
  12.  
  13. # 1. Abre o seletor de pasta atualizado
  14. $folderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
  15. $folderBrowserDialog.Description = "Selecione a pasta contendo os arquivos de mídia"
  16. $folderBrowserDialog.SelectedPath = $lastFolderPath
  17. $folderBrowserDialog.ShowNewFolderButton = $false
  18.  
  19. if ($folderBrowserDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
  20.     $folderPath = $folderBrowserDialog.SelectedPath
  21.     # Salva a última pasta usada para futuras execuções
  22.     $folderPath | Out-File -FilePath $lastFolderPathFile -Force
  23. } else {
  24.     # Toca um som de erro e encerra o script sem pausar
  25.     $player = New-Object System.Media.SoundPlayer "C:\Windows\Media\Windows Error.wav"
  26.     $player.PlaySync()
  27.     exit
  28. }
  29.  
  30. # 2. Percorre todos os arquivos de mídia na pasta selecionada e suas subpastas, ordenados por tamanho
  31. $mediaExtensions = @(".mp3", ".wav", ".mp4", ".mov", ".avi", ".mkv", ".ogg")
  32. $mediaFiles = Get-ChildItem -Path $folderPath -Recurse | Where-Object { $mediaExtensions -contains $_.Extension } | Sort-Object Length
  33.  
  34. foreach ($file in $mediaFiles) {
  35.     $outputPath = Split-Path -Path $file.FullName
  36.     $outputFile = [System.IO.Path]::ChangeExtension($file.FullName, ".srt")
  37.  
  38.     # Verifica se o arquivo .srt já existe
  39.     if (-Not (Test-Path -Path $outputFile)) {
  40.         # 3. Executa o Whisper para transcrever o arquivo
  41.         Write-Output "Transcrevendo $($file.FullName)..."
  42.         & python -m whisper "$($file.FullName)" --language pt --model turbo -o "$outputPath" -f srt
  43.  
  44.         # Exibe mensagem de conclusão para cada arquivo
  45.         Write-Output "Transcrição concluída para $($file.Name)."
  46.     } else {
  47.         Write-Output "Arquivo .srt já existe para $($file.Name), ignorando..."
  48.     }
  49. }
  50.  
  51. # 4. Toca um som de conclusão com volume reduzido e abre a pasta inicial ao finalizar o processo
  52. $player = New-Object System.Media.SoundPlayer "C:\Windows\Media\notify.wav"
  53. $player.PlaySync()
  54. Start-Process -FilePath "explorer.exe" -ArgumentList "$folderPath"
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement