Advertisement
Urik_Kane

nvenc_fix_shadowplay

Dec 8th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.67 KB | None | 0 0
  1. @echo off
  2. SetLocal EnableDelayedExpansion
  3. set title1=H.264 NVENC VBR
  4. title %title1%
  5.  
  6. if "%~1"=="" ( goto :MakeShortcut ) 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"
  10. set "ffprobe=ffprobe"
  11.  
  12. :: note: if you put ffmpeg.exe/ffprobe.exe and this script in the same folder, that would also work
  13.  
  14.  
  15. :: only these file extensions will be processed by the script
  16. set supported_formats=avi,mp4,mkv,mov,mts,m2ts,xmf,wmv
  17.  
  18. :: whether to play a chime sound after completion
  19. set playsound=true
  20.  
  21. :: the sound to play. you can change
  22. set "chime=C:\Windows\Media\tada.wav"
  23.  
  24. :: whether to open the output folder after completion
  25. set open_folder=true
  26.  
  27. :: whether to add suffix to output files. for example, set "suffix=_fixed"
  28. set suffix=
  29.  
  30. :: optional default subfolder to output to. for example, set "subfolder=converted"
  31. set subfolder=
  32.  
  33. :: load override settings from optional settings.ini located in the same folder as this script
  34. if exist "%~dp0settings.ini" ( for /f "tokens=* eol=;" %%L in ('type "%~dp0settings.ini"') do set "%%~L")
  35.  
  36. :CheckSubfolderVar
  37. if not "%subfolder%"=="" (
  38. set subfolder=%subfolder: =%
  39. for %%A in ("!subfolder!") do ( set "subfolder=%%~A" & goto :CheckFFMPEG)
  40. )
  41. set subfolder=
  42.  
  43. :CheckFFMPEG
  44. if not exist "%ffmpeg%" (
  45. if exist "%~dp0ffmpeg.exe" set "ffmpeg=%~dp0ffmpeg.exe" & goto :CheckFFPROBE
  46. ffmpeg>nul 2>&1
  47. if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFMPEG NOT FOUND / INSTALLED. EXITING." & echo  & pause & goto :eof
  48. )
  49.  
  50. :CheckFFPROBE
  51. if not exist "%ffprobe%" (
  52. if exist "%~dp0ffprobe.exe" set "ffprobe=%~dp0ffprobe.exe" & goto :CountFiles
  53. ffprobe>nul 2>&1
  54. if !errorlevel!==9009 CALL :PrintMessage "ERROR: FFPROBE NOT FOUND / INSTALLED. EXITING." & echo  & pause & goto :eof
  55. )
  56.  
  57. :CountFiles
  58. :: replace bad characters for batch
  59. set files=%*
  60. set files=%files:,=\comma\%
  61. set files=%files:(=\obr\%
  62. set files=%files:)=\cbr\%
  63.  
  64. :: count files
  65. set total=0
  66. for %%F in (%files%) do (
  67. if "%%~xF"=="" (
  68. CALL :CountFolder "%%~F"
  69. ) else (
  70. for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A set /a total=!total!+1)
  71. )
  72. )
  73. if %total%==0 echo  & CALL :PrintMessage "NO FILES TO PROCESS" "EXITING" & pause & goto :eof
  74. if %total% GTR 1 title %title1%^: %total% files
  75.  
  76. :EnterPath
  77. if "%subfolder%"=="" ( set "msg=output path = ") else ( set "msg=output path (default %subfolder%) ")
  78. set p=
  79. echo.
  80. set /p p="%msg%"
  81. if not "%p%"=="" (
  82. for %%F in ("!p!") do (
  83. set "outfolder=%%~F"
  84. goto :ConformPath
  85. )
  86. )
  87.  
  88. :ConformPath
  89. echo.
  90. if not "!outfolder!"=="" (
  91. if not "!outfolder:~-1!"=="\" (
  92. if "!outfolder:~-1!"=="/" set "outfolder=!outfolder:~0,-1!"
  93. set "outfolder=!outfolder!\"
  94. for %%A in ("%outfolder%") do (
  95. echo "%%~dA"| find /i "%~d1" > nul 2>&1
  96. if not !ErrorLevel!==0 ( set openfolder=true & goto :BatchProcess)
  97. )
  98. for %%A in ("%outfolder%") do (
  99. echo "%%~dpA"| find /i "%~dp1" > nul 2>&1
  100. if not !ErrorLevel!==0 ( set openfolder=true & goto :BatchProcess)
  101. )
  102. )
  103. )
  104.  
  105. :BatchProcess
  106. set current=
  107. for %%F in (%files%) do (
  108. set "file=%%~F"
  109. set "file=!file:\comma\=,!"
  110. set "file=!file:\cbr\=)!"
  111. set "file=!file:\obr\=(!"
  112. if "%%~xF"=="" (
  113. CALL :ProcessFolder "!file!"
  114. ) else (
  115. for %%A in (%supported_formats%) do ( if /i %%~xF==.%%A CALL :ProcessFile "!file!")
  116. )
  117. )
  118. title Finished
  119. if defined openfolder if %open_folder%==true explorer "%outfolder%"
  120. if %playsound%==true powershell -c (New-Object Media.SoundPlayer "%chime%").PlaySync();
  121. goto :eof
  122.  
  123. :: SUBROUTINES
  124. :CountFolder
  125. set "f=%~1"
  126. set "f=!f:\comma\=,!"
  127. set "f=!f:\cbr\=)!"
  128. set "f=!f:\obr\=(!"
  129. 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))
  130. goto :eof
  131.  
  132. :ProcessFolder
  133. for /f "tokens=*" %%F in ('dir "%~1" /s /b /a:-d') do (
  134. for %%A in (%supported_formats%) do (
  135. if /i %%~xF==.%%A (
  136. if not "%outfolder%"=="" ( set "foldertree=%%~dpF" & set "foldertree=!foldertree:%~dp1=!")
  137. CALL :ProcessFile "%%~F"
  138. )
  139. )
  140. )
  141. set foldertree=
  142. goto :eof
  143.  
  144. :: MAIN ROUTINE
  145. :ProcessFile
  146. if "!current!"=="" ( set current=1) else ( set /a current=!current!+1)
  147. if %total% GTR 1 set "ratio= !current!/!total!"
  148. title %title1%^:!ratio! "%~nx1"
  149. if not "%cd%\"=="%~dp1" cd /d "%~dp1"
  150.  
  151. :CheckBitrate
  152. set bitrate=
  153. for /f "tokens=*" %%L in ('""%ffprobe%" -hide_banner -show_streams -select_streams v -show_entries stream^=bit_rate "%~1""') do (
  154. echo "%%L"| find /i "bit_rate" > nul 2>&1
  155. if !ErrorLevel!==0 for /f "tokens=2 delims==" %%C in ("%%L") do set "bitrate=%%C"
  156. )
  157.  
  158. :CalcRates
  159. set /a bitrate=!bitrate! / 1000000
  160. set /a halfrate=!bitrate! / 2
  161. set /a maxrate=!halfrate! + !bitrate!
  162. set /a bufsize=!maxrate! * 2
  163. title %title1%^:!ratio! "%~nx1" Target bitrate^: !bitrate!Mb/s
  164.  
  165. :CheckFramerate
  166. if not "%customgop%"=="" goto :SetAudio
  167. set framerate=
  168. set gsize=
  169. set lookahead=
  170. for /f "tokens=*" %%L in ('""%ffprobe%" -hide_banner -show_streams -select_streams v -show_entries stream^=r_frame_rate^,avg_frame_rate "%~1""') do (
  171. echo "%%L"| find /i "r_frame_rate" > nul 2>&1
  172. if !ErrorLevel!==0 for /f "tokens=2 delims==" %%C in ("%%L") do ( set /a "framerate=%%C" & echo %%C)
  173. if "!framerate!"=="" (
  174. echo "%%L"| find /i "avg_frame_rate" > nul 2>&1
  175. if !ErrorLevel!==0 for /f "tokens=2 delims==" %%D in ("%%M") do ( set /a "framerate=%%D" & echo %%D)
  176. )
  177. )
  178. if not "!framerate!"=="" (
  179. set /a "gop=!framerate!/2"
  180. set gsize=-g !gop!
  181. set lookahead=-rc-lookahead !gop!
  182. ) else ( CALL :PrintMessage "WARNING^: COULD NOT IDENTIFY FRAMERATE." "GOP AND LOOKAHEAD WILL NOT BE DEFINED." & pause)
  183.  
  184.  
  185. :SetAudio
  186. for /f "tokens=*" %%L in ('""%ffprobe%" -hide_banner -show_streams -select_streams a -show_entries stream^=codec_name "%~1""') do (
  187. echo "%%L"| find /i "codec_name" >nul 2>&1
  188. if !ErrorLevel!==0 for /f "tokens=2 delims==" %%C in ("%%L") do set "audiocodec=%%C"
  189. )
  190. if "!audiocodec!"=="aac" ( set "audiosettings=-c:a copy") else ( set "audiosettings=-c:a aac -b:a 320k")
  191.  
  192. :Checkoutfolder
  193. if not "%outfolder%"=="" (
  194. set "outpath=%outfolder%!foldertree!"
  195. ) else (
  196. if "%subfolder%"=="" ( set "outpath=%~dp1") else ( set "outpath=%~dp1%subfolder%\")
  197. )
  198. if not exist "!outpath!" mkdir "!outpath!"
  199.  
  200. :Add_Postfix
  201. if exist "!outpath!%~n1%suffix%.mp4" (
  202. for /L %%N in (1,1,1000) do (
  203. if not exist "!outpath!%~n1%suffix%_%%N.mp4" ( set "postfix=_%%N" & goto :FFmpeg)
  204. )
  205. )
  206.  
  207. :FFmpeg
  208. "%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 !gsize! !lookahead! -rc vbr -b:v !bitrate!M -maxrate !maxrate!M -bufsize !bufsize!M !audiosettings! -movflags +faststart "!outpath!%~n1%suffix%!postfix!.mp4"
  209. goto :eof
  210.  
  211. :: UTILITIES
  212.  
  213. :PrintMessage
  214. echo.
  215. echo ---------------------------------------------------------------
  216. echo %~1
  217. if not "%~2"=="" echo %~2
  218. if not "%~3"=="" echo %~3
  219. if not "%~4"=="" echo %~4
  220. echo ---------------------------------------------------------------
  221. goto :eof
  222.  
  223. ::SETUP
  224.  
  225. :MakeShortcut
  226. title Generate Shortcut
  227. if exist "%ProgramFiles%\NVIDIA Corporation\NVIDIA GeForce Experience\NVIDIA GeForce Experience.exe" ( set icon=$s.IconLocation='%ProgramFiles%\NVIDIA Corporation\NVIDIA GeForce Experience\NVIDIA GeForce Experience.exe,0';) else ( set icon=$s.IconLocation='%windir%\system32\shell32.dll,115';)
  228. powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%appdata%\Microsoft\Windows\SendTo\Fix Shadowplay Color.lnk');$s.TargetPath='%~dpn0';%icon%$s.Save()"
  229. if %errorlevel%==0 CALL :PrintMessage "The shortcut has been created." "If you wish to rename or remove it, do it yourself at" "%appdata%\Microsoft\Windows\SendTo" "or just type sendto in explorer address bar to get there"
  230.  
  231.  
  232. :check_permissions
  233. title "Fix shadowplay" script^: Add/Remove context menu entry
  234. echo. & echo Checking permissions for adding/removing context menu entry in registry...
  235. net session >nul 2>&1
  236. if not %errorLevel% == 0 echo  & echo Error: For adding the context menu entry, you must run this script as administrator. & pause>nul & goto :FinishSetup
  237. echo.
  238.  
  239. :CheckRegistry
  240. reg query "HKCR\SystemFileAssociations\.mp4\Shell\fix_shadowplay">nul 2>&1
  241. if %errorlevel%==0 ( set isinstalled=true) else ( set isinstalled=false)
  242.  
  243. :Prompt
  244. title "Fix shadowplay" script^: Add/Remove context menu entry. Enter Y or N
  245. if %isinstalled%==true ( set "msg=Context menu entry found. Delete it" ) else ( set "msg=Install context menu entry")
  246. set proceed=
  247. set /p "proceed=%msg% (Y/N)?"
  248. if not "!proceed!"=="" (
  249. set "proceed=!proceed: =!"
  250. for /f "delims=yYnN" %%A in ('echo !proceed!') do goto :Prompt
  251. )
  252. if /i !proceed!==y ( if %isinstalled%==true ( goto :DeleteKey) else ( goto :AddKey))
  253. if /i !proceed!==n goto :FinishSetup
  254. goto :Prompt
  255.  
  256. :AddKey
  257. title "Fix shadowplay" script^: Adding context menu entry to registry
  258. if exist "%ProgramFiles%\NVIDIA Corporation\NVIDIA GeForce Experience\NVIDIA GeForce Experience.exe" ( set "icon=%ProgramFiles%\NVIDIA Corporation\NVIDIA GeForce Experience\NVIDIA GeForce Experience.exe" & set "icon2=0") else ( set "icon=%windir%\system32\shell32.dll" & set "icon2=115")
  259. reg add "HKCR\SystemFileAssociations\.mp4\Shell\fix_shadowplay" /f /v "" /t REG_SZ /d "Fix Shadowplay Color"
  260. reg add "HKCR\SystemFileAssociations\.mp4\Shell\fix_shadowplay" /f /v "Icon" /t REG_SZ /d "\"%icon%\",%icon2%"
  261. reg add "HKCR\SystemFileAssociations\.mp4\Shell\fix_shadowplay\command" /f /v "" /t REG_SZ /d "\"%~dpnx0\" \"%%1\""
  262. goto :FinishSetup
  263.  
  264. :DeleteKey
  265. title "Fix shadowplay" script^: Removing context menu entry from registry
  266. reg delete "HKCR\SystemFileAssociations\.mp4\Shell\fix_shadowplay" /f
  267. goto :FinishSetup
  268.  
  269. :FinishSetup
  270. title Done
  271. explorer "%appdata%\Microsoft\Windows\SendTo"
  272. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement