Advertisement
Urik_Kane

ffmpeg_batch_rewrap

Dec 7th, 2020 (edited)
2,529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.68 KB | None | 0 0
  1. @echo off
  2. SetLocal EnableDelayedExpansion
  3. set title1=FFmpeg batch rewrap
  4. title %title1%
  5.  
  6. REM this script can accept multiple input files. input can be folder(s) too. only %supported_formats% will be processed.
  7. REM if input is a folder, and has subfolders, output should preserve the subdirectory tree in destination
  8. REM all destination folders will get created automatically
  9.  
  10. REM for output path, it accepts either complete path like E:\converted files or a subfolder. If no drive letter like E:, it will treat it as subfolder of input
  11.  
  12.  
  13.  
  14. if "%~1"=="" ( echo ERROR: NO INPUT & echo  & pause & goto :eof ) else ( title %title1%^: "%~nx1")
  15.  
  16. :: you can override exe paths here. for example, set "ffmpeg=C:\ffmpeg\bin\ffmpeg.exe"
  17. set "ffmpeg=ffmpeg"
  18. set "ffprobe=ffprobe"
  19.  
  20. :: note: if you put ffmpeg.exe/ffprobe.exe and this script in the same folder, that would also work
  21.  
  22. :: only these file extensions will be processed by the script
  23. set supported_formats=avi,mp4,mkv,mov,mts,m2ts,xmf,wmv
  24.  
  25. :: whether to play a chime sound after completion
  26. set playsound=true
  27.  
  28. :: the sound to play. you can change
  29. set "chime=C:\Windows\Media\tada.wav"
  30.  
  31. :: whether to open the output folder after completion
  32. set open_folder=true
  33.  
  34. :: whether to add suffix to output files. for example, set "suffix=_fixed"
  35. set suffix=
  36.  
  37. :: optional default subfolder to output to. for example, set "subfolder=converted"
  38. set subfolder=
  39.  
  40. :: override output extension, then it will not ask
  41. set ext=
  42.  
  43. :CheckSubfolderVar
  44. if not "%subfolder%"=="" (
  45.     set subfolder=%subfolder: =%
  46.     for %%A in ("!subfolder!") do ( set "subfolder=%%~A" & goto :CheckFFMPEG)
  47. )
  48. set subfolder=
  49.  
  50. :CheckFFMPEG
  51. if not exist "%ffmpeg%" (
  52.     if exist "%~dp0ffmpeg.exe" set "ffmpeg=%~dp0ffmpeg.exe" & goto :CheckFFPROBE
  53.     ffmpeg>nul 2>&1
  54.     if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFMPEG NOT FOUND / INSTALLED. EXITING." & echo  & pause & goto :eof
  55. )
  56.  
  57. :CheckFFPROBE
  58. if not exist "%ffprobe%" (
  59.     if exist "%~dp0ffprobe.exe" set "ffprobe=%~dp0ffprobe.exe" & goto :CountFiles
  60.     ffprobe>nul 2>&1
  61.     if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFPROBE NOT FOUND / INSTALLED. EXITING." & echo  & pause & goto :eof
  62. )
  63.  
  64. :CountFiles
  65. :: replace bad characters for batch
  66. set files=%*
  67. set files=%files:,=\comma\%
  68. set files=%files:(=\obr\%
  69. set files=%files:)=\cbr\%
  70.  
  71. :: count files
  72. set total=0
  73. for %%F in (%files%) do (
  74.     if "%%~xF"=="" (
  75.         CALL :CountFolder "%%~F"
  76.     ) else (
  77.         for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A set /a total=!total!+1)
  78.     )
  79. )
  80. if %total%==0 echo  & CALL :PrintMessage "NO FILES TO PROCESS" "EXITING" & pause & goto :eof
  81. if %total% GTR 1 title %title1%^: %total% files
  82.  
  83.  
  84. if not "%ext%"=="" goto :EnterPath
  85. :EnterExtension
  86. set ext=
  87. echo.
  88. set /p ext="output extension "
  89. for %%A in (!ext!) do ( if not ""%%~xA=="" set "ext=%%~xA")
  90. if not "!ext!"=="" (
  91.     set "ext=!ext: =!"
  92.     set "ext=!ext:.=!"
  93.     for /f "delims=abcdefghijklmnopqrstuvwxyz1234567890" %%A in ('echo !ext!') do (
  94.         echo ERROR: input incorrect
  95.         goto :EnterExtension
  96.     )
  97. ) else goto :EnterExtension
  98. for %%X in (mov,mp4) do ( if /i "%ext%"=="%%X" ( set "format_options=-movflags +faststart" & goto :EnterPath))
  99.  
  100. :EnterPath
  101. if "%subfolder%"=="" ( set "msg=output path = ") else ( set "msg=output path (default %subfolder%) ")
  102. set p=
  103. echo.
  104. set /p p="%msg%"
  105. if not "%p%"=="" (
  106.     for %%F in ("!p!") do (
  107.         set "outfolder=%%~F"
  108.         goto :ConformPath
  109.     )
  110. )
  111.  
  112. :ConformPath
  113. echo.
  114. if not "!outfolder!"=="" (
  115.     if not "!outfolder:~-1!"=="\" (
  116.         if "!outfolder:~-1!"=="/" set "outfolder=!outfolder:~0,-1!"
  117.         set "outfolder=!outfolder!\"
  118.         for %%A in ("%outfolder%") do (
  119.             echo "%%~dA"| find /i "%~d1" > nul 2>&1
  120.             if not !ErrorLevel!==0 ( set openfolder=true & goto :BatchProcess)
  121.         )
  122.         for %%A in ("%outfolder%") do (
  123.             echo "%%~dpA"| find /i "%~dp1" > nul 2>&1
  124.             if not !ErrorLevel!==0 ( set openfolder=true & goto :BatchProcess)
  125.         )
  126.     )
  127. )
  128.  
  129. :BatchProcess
  130. set current=
  131. for %%F in (%files%) do (
  132.     set "file=%%~F"
  133.     set "file=!file:\comma\=,!"
  134.     set "file=!file:\cbr\=)!"
  135.     set "file=!file:\obr\=(!"
  136.     if "%%~xF"=="" (
  137.         CALL :ProcessFolder "!file!"
  138.     ) else (
  139.         for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A CALL :ProcessFile "!file!")
  140.     )
  141. )
  142. title Finished
  143. if defined openfolder if %open_folder%==true explorer "%outfolder%"
  144. if %playsound%==true powershell -c (New-Object Media.SoundPlayer "%chime%").PlaySync();
  145. goto :eof
  146.  
  147. :: SUBROUTINES
  148. :CountFolder
  149. set "f=%~1"
  150. set "f=!f:\comma\=,!"
  151. set "f=!f:\cbr\=)!"
  152. set "f=!f:\obr\=(!"
  153. for /f "tokens=*" %%F in ('dir "!f!" /s /b /a:-d') do ( for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A set /a total=!total!+1))
  154. goto :eof
  155.  
  156. :ProcessFolder
  157. for /f "tokens=*" %%F in ('dir "%~1" /s /b /a:-d') do (
  158.     for %%A in (%supported_formats%) do (
  159.         if /i %%~xF==.%%A (
  160.             if not "%outfolder%"=="" ( set "foldertree=%%~dpF" & set "foldertree=!foldertree:%~dp1=!")
  161.             CALL :ProcessFile "%%~F"
  162.         )
  163.     )
  164. )
  165. set foldertree=
  166. goto :eof
  167.  
  168. :: MAIN ROUTINE
  169. :ProcessFile
  170. if "!current!"=="" ( set current=1) else ( set /a current=!current!+1)
  171. if %total% GTR 1 set "ratio= !current!/!total!"
  172. title %title1%^:!ratio! "%~nx1"
  173. if not "%cd%\"=="%~dp1" cd /d "%~dp1"
  174.  
  175. :Checkoutfolder
  176. if not "%outfolder%"=="" (
  177.     set "outpath=%outfolder%!foldertree!"
  178. ) else (
  179.     if "%subfolder%"=="" ( set "outpath=%~dp1") else ( set "outpath=%~dp1%subfolder%\")
  180. )
  181. if not exist "!outpath!" mkdir "!outpath!"
  182.  
  183. :Add_Postfix
  184. if exist "!outpath!%~n1%suffix%.mp4" (
  185.     for /L %%N in (1,1,1000) do (
  186.         if not exist "!outpath!%~n1%suffix%_%%N.mp4" ( set "postfix=_%%N" & goto :FFmpeg)
  187.     )
  188. )
  189.  
  190. :FFmpeg
  191. "%ffmpeg%" -hide_banner -i "%~1" -map 0:v -map 0:a:? -c:v copy -c:a copy %format_options% "!outpath!%~n1%suffix%!postfix!.%ext%"
  192. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement