Advertisement
hl2guide

Video Converter 1.0A - PowerShell

Jul 30th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Name: Convert Videos using FFMpeg or Handbrake CLI
  2. # Version: 1.0A
  3. # Date Last Modified: 30 July 2017
  4. # Author: hl2guide
  5. # SOURCES: http://www.waynezim.com/2014/06/use-powershell-to-batch-convert-videos-using-handbrake/
  6.  
  7. # Outputs an optimized x264 encoded MP4 video
  8.  
  9. # 1) Source Folder:
  10. $folder = 'C:\Temp'
  11. # 2) Source File Extension:
  12. $sourceFileExtension = 'wmv'
  13. # 3) Tool ('handbrake' or 'ffmpeg'):
  14. $tool = 'ffmpeg'
  15. # Folder for tools (ffmpeg and handbrake) [ffmpeg.exe and HandBrakeCLI.exe]
  16. $toolFolder = 'Y:\Batch Scripts\Commands'
  17.  
  18. # Gets list of files
  19. $filelist = Get-ChildItem $folder -filter *.$sourceFileExtension -recurse
  20.  
  21. # Counts the number of files
  22. $num = $filelist | measure
  23. $filecount = $num.count
  24.  
  25. # Sets tool EXE
  26. if($tool -eq 'handbrake')
  27. {
  28.     $process = $toolFolder+'\HandBrakeCLI.exe'
  29. }
  30. elseif($tool -eq 'ffmpeg')
  31. {
  32.     $process = $toolFolder+'\ffmpeg.exe'
  33. }
  34. else
  35. {
  36.     Write-Host 'Tool to use is not set, exiting.'
  37.     exit
  38. }
  39.  
  40. # Loops through file list
  41. $i = 0;
  42. ForEach ($file in $filelist)
  43. {
  44.     $i++;
  45.     $oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension
  46.     $newfile = $file.DirectoryName + "\" + $file.BaseName + '.mp4'
  47.  
  48.     # Calculates Progress
  49.     $progress = ($i / $filecount) * 100
  50.     $progress = [Math]::Round($progress,2)
  51.  
  52.     # Shows Status
  53.     Clear-Host
  54.     Write-Host '-------------------------------------------------------------------------------' -ForegroundColor White
  55.     Write-Host "Handbrake Batch Encoding ($tool)" -ForegroundColor Cyan
  56.     Write-Host "Processing - $oldfile" -ForegroundColor Yellow
  57.     Write-Host "Destination - $newfile" -ForegroundColor Green
  58.     Write-Host "File $i of $filecount - $progress%" -ForegroundColor Magenta
  59.     Write-Host '-------------------------------------------------------------------------------' -ForegroundColor White
  60.  
  61.     # Checks tool
  62.     if($tool -eq 'handbrake')
  63.     {
  64.         # Arguments:
  65.         $arguments='--encoder x264 --encoder-preset slow --optimize -B 190'
  66.         # Perform Video Conversion using Handbrake CLI
  67.         # Write-Host "-i `"$oldfile`" `"$newfile`" $arguments"
  68.         Start-Process $process -ArgumentList "-i `"$oldfile`" -o `"$newfile`" -f mp4 $arguments --verbose=0" -Wait -NoNewWindow
  69.     }
  70.     elseif($tool -eq 'ffmpeg')
  71.     {
  72.         # Arguments:
  73.         $arguments='-c:v libx264 -c:a copy -preset slow -crf 22 -hide_banner'
  74.         # Perform Video Conversion using FFMpeg CLI
  75.         # Write-Host "-i `"$oldfile`" `"$newfile`" $arguments"
  76.         Start-Process $process -ArgumentList "-i `"$oldfile`" `"$newfile`" $arguments" -Wait -NoNewWindow
  77.     }
  78. }
  79.  
  80. # Shows final messages
  81. Write-Host "$filecount videos converted from .$sourceFileExtension to .mp4." -ForegroundColor Green
  82. Write-Host "Folder: $folder"
  83. Write-Host
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement