Advertisement
anonit

Powershell to batch convert videos using FFMPEG

Aug 6th, 2017
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## ConvertVideo.ps1
  2.  
  3. ## Requires FFMPEG https://www.ffmpeg.org/download.html
  4.  
  5. ## Batch converts a directory of files to the a given format
  6. ## eg
  7. ## Given a directory with files:
  8. ## vid1.mkv, vid2.mkv, vid3.mkv
  9. ## Running the command ConvertVideo.mp4 -filepath %PATHTOFILES% -convertto MP4
  10. ## the results would be:
  11. ## vid1.mp4, vid2.mp4, vid3.mp4
  12.  
  13. ## Usage:
  14. ## ConvertVideo.ps1 -filepath %PATHTOFILES% -convertto %EXT% [-ffmpeglocation %PATHTOFFMPEG%]
  15. ## -filepath is the path to the files to be converted, spaces must be quoted, trailing slash must be added
  16. ## -convertto is the new extension.  Extension must be supported by ffmpeg https://www.ffmpeg.org/documentation.html
  17. ## -ffmpeglocation (optional) is the filepath to ffmpeg.exe.  Default is "c:\program files\ffmpeg\ffmpeg.exe"
  18.  
  19. ## eg:
  20. ## ConvertVideo.ps1 -filepath c:\scripts\videos\ -convertto MP4
  21. ## ConvertVideo.ps1 -filepath "c:\batch files\videos\" -convertto FLV
  22. ## ConvertVideo.ps1 -filepath "c:\batch files\videos\" -convertto FLV -ffmpeglocation "c:\program files\audio files\ffmpeg\ffmpeg.exe"
  23.  
  24.  
  25.  
  26. param(
  27.     [string]$filepath=$(throw "-filepath parameter is required"),
  28.     [string]$convertto=$(throw "-convertto parameter is required"),
  29.     [string]$ffmpeglocation="c:\program files\ffmpeg\ffmpeg.exe"
  30. )
  31.  
  32.  
  33. # ---------------------------------------------------------------------------#
  34.  
  35. $ffmpegInputFile="-i"
  36. $ffmpegCodec="-codec"
  37. $ffmpegCopy="copy"
  38.  
  39. $listoffiles=get-childitem $filepath | sort lastwritetime
  40.  
  41. ## check location of ffmpeg
  42.  
  43. if (test-path  $ffmpeglocation)
  44. {
  45.     foreach ($file in $listoffiles)
  46.     {
  47.         ## perform the conversion
  48.         $newname=$file.directoryname+"\"+$file.basename+"."+$convertto
  49.         & $ffmpeglocation $ffmpegInputFile $file.fullname $ffmpegCodec $ffmpegcopy $newname
  50.       }
  51. }
  52. else
  53. {
  54.     throw "ffmpeg.exe not found - https://www.ffmpeg.org/download.html"
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement