Urik_Kane

nvenc_5M_Mark

Feb 17th, 2021 (edited)
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.98 KB | None | 0 0
  1. @echo off
  2. SetLocal EnableDelayedExpansion
  3. set title1=H.264 NVENC VBR
  4. title %title1%
  5.  
  6. if "%~1"=="" ( echo ERROR: NO INPUT & echo  & pause & goto :eof ) else ( title %title1%^: "%~nx1")
  7.  
  8. :: you can override exe paths here. for example, set "ffmpeg=C:\ffmpeg\bin\ffmpeg.exe"
  9. set "ffmpeg=ffmpeg.exe"
  10. set "ffprobe=ffprobe.exe"
  11.  
  12. :: only these file extensions will be processed by the script
  13. set supported_formats=avi,bik,flv,m2ts,m4p,m4v,mkv,mov,mp2,mp4,mpg,mpeg,mpe,mpv,mts,qt,swf,wmv,xmf,webm
  14.  
  15. :: whether to play a chime sound after completion
  16. set playsound=true
  17.  
  18. :: the sound to play. you can change
  19. set "chime=C:\Windows\Media\tada.wav"
  20.  
  21. :: whether to open the output folder after completion
  22. set open_folder=true
  23.  
  24. :: whether to add suffix to output files. for example, set "suffix=_fixed"
  25. set suffix=
  26.  
  27. :: optional default subfolder to output to. for example, set "subfolder=converted"
  28. set subfolder=
  29.  
  30. set bitrate=5
  31.  
  32. if "%bitrate%"=="" echo ERROR^: bitrate value not defined & pause>nul & goto :eof
  33. for /f "delims=1234567890" %%A in ('echo/%bitrate%') do ( echo ERROR^: bad bitrate value %bitrate% & pause>nul & goto :eof)
  34.  
  35. set title1=H.264 NVENC VBR %bitrate% Mbit/s
  36. title %title1%
  37.  
  38. :: load override settings from optional settings.ini located in the same folder as this script
  39. if exist "%~dp0settings.ini" ( for /f "tokens=* eol=;" %%L in ('type "%~dp0settings.ini"') do set "%%~L")
  40.  
  41. :CheckSubfolderVar
  42. if not "%subfolder%"=="" (
  43.     set subfolder=%subfolder: =%
  44.     for %%A in ("!subfolder!") do ( set "subfolder=%%~A" & goto :CheckFFMPEG)
  45. )
  46. set subfolder=
  47.  
  48. :CheckFFMPEG
  49. if not exist "%ffmpeg%" (
  50.     if exist "%~dp0ffmpeg.exe" set "ffmpeg=%~dp0ffmpeg.exe" & goto :CheckFFPROBE
  51.     ffmpeg.exe>nul 2>&1
  52.     if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFMPEG NOT FOUND / INSTALLED. EXITING." & echo  & pause & goto :eof
  53. )
  54.  
  55. :CheckFFPROBE
  56. if not exist "%ffprobe%" (
  57.     if exist "%~dp0ffprobe.exe" set "ffprobe=%~dp0ffprobe.exe" & goto :CountFiles
  58.     ffprobe.exe>nul 2>&1
  59.     if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFPROBE NOT FOUND / INSTALLED. EXITING." & echo  & pause & goto :eof
  60. )
  61.  
  62. :CountFiles
  63. :: replace bad characters for batch
  64. set files=%*
  65. set files=%files:,=-_comma_-%
  66. set files=%files:(=-_opbr_-%
  67. set files=%files:)=-_clbr_-%
  68.  
  69. :: count files
  70. set total=0
  71. for %%F in (%*) do ( if "%%~xF"=="" ( CALL :CountFolder "%%~F") else ( for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A set /a total=!total!+1)))
  72. if %total%==0 echo  & CALL :PrintMessage "NO FILES TO PROCESS" "EXITING" & pause & goto :eof
  73. if %total% GTR 1 title %title1%^: %total% files
  74.  
  75. :CalcRates
  76. set /a bufsize=%bitrate% * 2
  77.  
  78. :EnterPath
  79. if "%subfolder%"=="" ( set "msg=output path = ") else ( set "msg=output path (default %subfolder%) ")
  80. set p=
  81. echo.
  82. set /p p="%msg%"
  83. if not "%p%"=="" (
  84.     for %%F in ("!p!") do (
  85.         set "outfolder=%%~F"
  86.         goto :ConformPath
  87.     )
  88. )
  89.  
  90. :ConformPath
  91. echo.
  92. if not "!outfolder!"=="" (
  93.     if not "!outfolder:~-1!"=="\" (
  94.         if "!outfolder:~-1!"=="/" set "outfolder=!outfolder:~0,-1!"
  95.         set "outfolder=!outfolder!\"
  96.         for %%A in ("%outfolder%") do (
  97.             echo "%%~dA"| find /i "%~d1" > nul 2>&1
  98.             if not !ErrorLevel!==0 ( set "openfolder=true" & goto :BatchProcess)
  99.         )
  100.         for %%A in ("%outfolder%") do (
  101.             echo "%%~dpA"| find /i "%~dp1" > nul 2>&1
  102.             if not !ErrorLevel!==0 ( set "openfolder=true" & goto :BatchProcess)
  103.         )
  104.     )
  105. )
  106.  
  107. :BatchProcess
  108. set current=
  109. for %%F in (%files%) do (
  110.     set "file=%%~F"
  111.     set "file=!file:-_comma_-=,!"
  112.     set "file=!file:-_clbr_-=)!"
  113.     set "file=!file:-_opbr_-=(!"
  114.     if "%%~xF"=="" (
  115.         CALL :ProcessFolder "!file!"
  116.     ) else (
  117.         for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A CALL :ProcessFile "!file!")
  118.     )
  119. )
  120. title Finished
  121. if defined openfolder if %open_folder%==true explorer "%outfolder%"
  122. if %playsound%==true if "%goterror%"=="" powershell -c (New-Object Media.SoundPlayer "%chime%").PlaySync();
  123. goto :eof
  124.  
  125. :: SUBROUTINES
  126. :CountFolder
  127. set "f=%~1"
  128. set "f=!f:-_comma_-=,!"
  129. set "f=!f:-_clbr_-=)!"
  130. set "f=!f:-_opbr_-=(!"
  131. 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))
  132. goto :eof
  133.  
  134. :ProcessFolder
  135. for /f "tokens=*" %%F in ('dir "%~1" /b /s /a:-d /o:n') do (
  136.     for %%A in (%supported_formats%) do (
  137.         if /i %%~xF==.%%A (
  138.             if not "%outfolder%"=="" ( set "foldertree=%%~dpF" & set "foldertree=!foldertree:%~dp1=!")
  139.             CALL :ProcessFile "%%~F"
  140.         )
  141.     )
  142. )
  143. set foldertree=
  144. goto :eof
  145.  
  146. :: MAIN ROUTINE
  147. :ProcessFile
  148. if "!current!"=="" ( set current=1) else ( set /a current=!current!+1)
  149. if %total% GTR 1 set "ratio= !current!/!total!"
  150. title %title1%^:!ratio! "%~nx1"
  151. echo "%~1"|find "?" > nul 2>&1 && ( CALL :PrintMessage "ERROR: Unresolvable non-ASCII characters in" "%~1" "Either set your Windows locale setting accordingly," "or remove those characters from paths/names" & echo  & set "goterror=true" & pause & goto :eof)
  152. echo "%~1"|find "(" > nul 2>&1 && ( CALL :PrintMessage "ERROR: Bad character ( in" "%~1" "Exiting." & echo  & set "goterror=true" & pause & goto :eof)
  153. echo "%~1"|find ")" > nul 2>&1 && ( CALL :PrintMessage "ERROR: Bad character ) in" "%~1" "Exiting." & echo  & set "goterror=true" & pause & goto :eof)
  154. echo "%~1"|find "," > nul 2>&1 && ( CALL :PrintMessage "ERROR: Bad character , in" "%~1" "Exiting." & echo  & set "goterror=true" & pause & goto :eof)
  155. if not "%cd%\"=="%~dp1" cd /d "%~dp1"
  156.  
  157. :SetAudio
  158. for /f "tokens=*" %%L in ('""%ffprobe%" -hide_banner -show_streams -select_streams a -show_entries stream^=codec_name "%~1""') do (
  159.     echo "%%L"| find /i "codec_name" >nul 2>&1
  160.     if !ErrorLevel!==0 for /f "tokens=2 delims==" %%C in ("%%L") do set "audiocodec=%%C"
  161. )
  162. if "!audiocodec!"=="aac" ( set "audiosettings=-c:a copy") else ( set "audiosettings=-c:a aac -b:a 320k")
  163.  
  164. :Checkoutfolder
  165. if not "%outfolder%"=="" (
  166.     set "outpath=%outfolder%!foldertree!"
  167. ) else (
  168.     if "%subfolder%"=="" ( set "outpath=%~dp1") else ( set "outpath=%~dp1%subfolder%\")
  169. )
  170. if not exist "!outpath!" mkdir "!outpath!"
  171.  
  172. :Add_Postfix
  173. if exist "!outpath!%~n1%suffix%.mp4" (
  174.     for /L %%N in (1,1,1000) do (
  175.         if not exist "!outpath!%~n1%suffix%_%%N.mp4" ( set "postfix=_%%N" & goto :FFmpeg)
  176.     )
  177. )
  178.  
  179. :FFmpeg
  180. "%ffmpeg%" -hide_banner -hwaccel_output_format cuda -i "%~1" -map 0:v -map 0:a:? -c:v h264_nvenc -preset slow -profile:v high -bf 2 -g 30 -rc-lookahead 32 -rc vbr -b:v %bitrate%M -bufsize !bufsize!M !audiosettings! -movflags +faststart "!outpath!%~n1%suffix%!postfix!.mp4"
  181. if not !errorlevel!==0 CALL :ErrorSubroutine "!outpath!%~n1%suffix%!postfix!.%ext%"
  182. goto :eof
  183.  
  184. :: if output file is empty, deletes it. prints some messages. stores goterror var.
  185. :ErrorSubroutine
  186. if "%~z1"=="0" del %1 & ( echo  & echo ERROR: FILE OUTPUT FAILED & echo.)
  187. set goterror=true
  188. pause
  189. goto :eof
  190.  
  191. :: UTILITIES
  192.  
  193. :PrintMessage
  194. echo.
  195. echo ---------------------------------------------------------------
  196. echo  %~1
  197. if not "%~2"=="" echo  %~2
  198. if not "%~3"=="" echo  %~3
  199. if not "%~4"=="" echo  %~4
  200. echo ---------------------------------------------------------------
  201. goto :eof
Add Comment
Please, Sign In to add comment