Advertisement
VP8M8

WebM Enhanced Multithreading Batch Script

Jul 20th, 2015
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.48 KB | None | 0 0
  1. @echo off
  2. REM Title: WebM Enhanced Multithreading Batch Script
  3. REM Author: VP8M8
  4. REM Version: v0.7.2
  5. REM Date: 7/20/2015
  6. REM How to use: Drag a video onto this batch file to start
  7.  
  8. REM Current Bugs:
  9. REM * Custom titles cannot have spaces
  10. REM * Time needs leading zeros for single digit numbers.
  11. REM To Do List in Priority Order:
  12. REM * Subtitles using the quick search hack
  13. REM * Show target bitrate vs actual bitrate stats
  14. REM * Force 854x480 instead of 853x480 for 480p 16:9 videos
  15. REM * Incorporate trimming into the Forced Multithreading option
  16. REM * Automatic output resolution scaling based on input resolution and time option
  17. REM * Auto high/low complexity video bitrate compensation calculation option
  18. REM * Optimize the Forced Multithreading option for n cores based on user input
  19.  
  20. REM ***** EDIT THE TWO LINES BELOW TO POINT TO THE LOCATION OF YOUR COPY OF FFMPEG AND FFPROBE *****
  21.  
  22. set ffmpeg=C:\Users\Anon\Documents\ffmpeg-20150517-git-2acc065-win64-static\bin\ffmpeg.exe
  23. set ffprobe=C:\Users\Anon\Documents\ffmpeg-20150517-git-2acc065-win64-static\bin\ffprobe.exe
  24.  
  25. REM ********* Test to see if user chose a video **********
  26.  
  27. if defined "%1" (
  28. goto :beginning
  29. ) else (
  30. echo Drag and drop a video onto this batch file to start. This window will close.&pause&exit
  31. )
  32.  
  33. :beginning
  34. REM ********* Hack to get duration ***************
  35.  
  36. for /f %%x in ('%ffprobe% %1 -loglevel quiet -show_format ^| findstr "="') do set %%x
  37.  
  38. REM ********* Hack to get date/time ***************
  39.  
  40. for /f %%x in ('wmic path win32_utctime get /format:list ^| findstr "="') do set %%x
  41. set today=%Year%-%Month%-%Day%
  42. REM The time %Hour%:%Minute%:%Second%
  43.  
  44. echo ^| ^| ^| ^| ^| ^| ^| \/ ^| ^| ___^| ^| ^|
  45. echo ^| ^| ^| ^| ___^| ^|__ ^| . . ^| ^| ^|__ _ __ ___ ___ __^| ^| ___ _ __
  46. echo ^| ^|/\^| ^|/ _ \ '_ \^| ^|\/^| ^| ^| __^| '_ \ / __/ _ \ / _` ^|/ _ \ '__^|
  47. echo \ /\ / __/ ^|_) ^| ^| ^| ^| ^| ^|__^| ^| ^| ^| ^(_^| (_) ^| (_^| ^| __/ ^|
  48. echo \/ \/ \___^|_.__/\_^| ^|_/ \____/_^| ^|_^|\___\___/ \__,_^|\___^|_^|
  49. echo Make sure you changed the two lines in the script!
  50. echo Press ENTER for the default values
  51. echo.
  52.  
  53. REM ********** Set title ************
  54.  
  55. set /p title="What should the title be? Custom titles cannot have spaces.(Default is input file name): "
  56. if [%title%]==[] @set title=%~n1
  57. echo The title is "%title%"
  58.  
  59. :audiobitrate
  60. REM ********** Audio bitrate *************
  61.  
  62. if [%bitrateError%]==[1] @echo Error! The video bitrate is negative because the audio bitrate was set too high for the file size. Please set a higher file size or a lower audio bitrate.
  63. set /a bitrateError=0
  64. echo.
  65.  
  66. set /a length = %duration%
  67.  
  68. set /p megabytesinput="How big should the WebM be in MB? (Default is 8): "
  69. set /a megabytes=%megabytesinput%
  70. if [%megabytes%]==[] @set /a megabytes=8
  71. echo The file size is %megabytes%MB
  72. echo.
  73.  
  74. set /p audioenable="Enable audio? Y/N (Default Y): "
  75. set /a audiobitrate=0
  76. if [%audioenable%]==[] @set audioOptions=-ac 2&echo Audio is enabled
  77. if [%audioenable%]==[y] @set audioOptions=-ac 2&echo Audio is enabled
  78. if [%audioenable%]==[Y] @set audioOptions=-ac 2&echo Audio is enabled
  79. if [%audioenable%]==[n] echo Audio is disabled&goto :videoBitrate
  80. if [%audioenable%]==[N] echo Audio is disabled&goto :videoBitrate
  81. echo.
  82.  
  83. set /p audiobitrateinput="What should the audio bitrate be in kbps? (Default is 64): "
  84. set /a audiobitrate=%audiobitrateinput%
  85. if [%audiobitrate%]==[0] @set audiobitrate=64
  86. echo The audio bitrate is %audiobitrate%kbps
  87.  
  88. :videoBitrate
  89. REM ********** Video Bitrate *******************
  90.  
  91. if %audiobitrate% EQU 0 @set audioOptions=-sn
  92. set /a calculatedbitrate=%megabytes% * 8 * 1024 * 1024 / 1000 / %length% - %audiobitrate%
  93.  
  94. echo The calculated video bitrate is %calculatedbitrate%kbps
  95. echo.
  96.  
  97. set /p videobitrateinput="What should the video bitrate be in kbps? (Default is auto calculated based on file size. *May be slightly bigger or smaller depending on input video and Quality setting*): "
  98. set /a videobitrate=%videobitrateinput%
  99. if [%videobitrate%]==[] @set /a videobitrate=%calculatedbitrate%
  100. echo The video bitrate is %videobitrate%
  101. echo.
  102.  
  103. REM # Catches if the auto calculated bitrate is negative.
  104. if %calculatedbitrate% LEQ 0 @set /a bitrateError=1
  105. if %bitrateError% EQU 1 goto :audiobitrate
  106.  
  107. REM ************** Set Scaling ***********************
  108.  
  109. set /p scaleheightinput="What should be the height? (Default is input height): "
  110. set /a scaleheight=%scaleheightinput%
  111. if [%scaleheight%]==[] @set /a scaleheight=-1
  112. set scale="scale=-1:%scaleheight%"
  113. echo The scaled height is %scaleheight%
  114. echo.
  115.  
  116. REM ************** Set Quality ***********************
  117.  
  118. set codec=libvpx-vp9
  119.  
  120. echo There are four quality settings. The higher the setting the better it will look but the longer it takes to convert.
  121. echo Low(1) : VP8 + Optimizations - Multithreaded
  122. echo Medium(2) : VP9 + Tweak - Multithreaded
  123. echo High(3) : VP9 + Optimizations - Single Threaded
  124. echo Ultra(4) : VP9 + More Optimizations - Single Threaded
  125. set /p qualityinput="What quality level should the WebM be? (Default is 2): "
  126. set /a quality=%qualityinput%
  127. if [%quality%]==[] @set options=-cpu-used 1 -frame-parallel 0 -threads 4 -pix_fmt yuv420p&@set qualitymode=Medium(2)
  128. if [%quality%]==[1] @set codec=libvpx&@set options= -cpu-used 1 -auto-alt-ref 1 -lag-in-frames 25 -sws_flags lanczos -g 9999 -threads 4 -pix_fmt yuv420p&@set qualitymode=Low(1)
  129. if [%quality%]==[2] @set options=-cpu-used 1 -frame-parallel 0 -threads 4 -pix_fmt yuv420p&@set qualitymode=Medium(2)
  130. if [%quality%]==[3] @set options=-cpu-used 1 -lag-in-frames 25 -auto-alt-ref 1 -frame-parallel 0 -tile-columns 0 -sws_flags lanczos -threads 1 -pix_fmt yuv420p&@set qualitymode=High(3)
  131. if [%quality%]==[4] @set options=-cpu-used 0 -lag-in-frames 25 -auto-alt-ref 1 -frame-parallel 0 -tile-columns 0 -sws_flags lanczos -threads 1 -pix_fmt yuv420p&@set qualitymode=Ultra(4)
  132. echo The quality setting is %qualitymode%
  133. echo.
  134.  
  135. REM ************** Set Experimental Multithreading **************
  136. echo Forced Multithreading (Experimental):
  137. echo This splits the video into 4 equal parts to encode/combine them to take advantage of at least 4 threads. This generally results in around the same file size with slightly lower quality but is typically over 2x faster on a multicore CPU. Only use with the High(3) or Ultra(4) quality settings. You will be unable to trim your video or enable subtitles if you enable this option. Do not touch the 4 extra windows that pop up.
  138. echo.
  139. set /p experimental="Use Forced Multithreading? Y/N (Default is N): "
  140. if [%experimental%]==[] goto :normalencoding
  141. if [%experimental%]==[n] goto :normalencoding
  142. if [%experimental%]==[N] goto :normalencoding
  143. if [%experimental%]==[y] goto :multithreaded
  144. if [%experimental%]==[Y] goto :multithreaded
  145.  
  146. :multithreaded
  147. REM [][][][][][][][][][][][][][][][][] MULTITHREADED ENCODING HERE [][][][][][][][][][][][][][][][][][]
  148.  
  149. REM ~~~~~~~~~~~~~~ Start Script Timer ~~~~~~~~~~~~~
  150.  
  151. echo.
  152. set starttimescript=%time%
  153.  
  154. REM Get encoding start time:
  155. for /F "tokens=1-4 delims=:.," %%a in ("%starttimescript%") do (
  156. set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
  157. )
  158.  
  159. REM ************** Set Fourths ***********************
  160.  
  161. set /a firstFourth = %length% / 4
  162. set /a secondFourth = %firstFourth% * 2
  163. set /a thirdFourth = %secondFourth% + %firstFourth%
  164.  
  165. REM ************ FIRST FOURTH ******************
  166.  
  167. (
  168. echo %ffmpeg% -y -i "%~1" -c:v %codec% -b:v %videobitrate%k -vf scale=-1:%scaleheight% -t %firstFourth% %options% -c:a libopus -b:a %audiobitrate%k %audioOptions% -sn -an -f webm -passlogfile ffmpeg2pass-1 -pass 1 NUL
  169. echo %ffmpeg% -y -i "%~1" -c:v %codec% -b:v %videobitrate%k -vf scale=-1:%scaleheight% -t %firstFourth% %options% -c:a libopus -b:a %audiobitrate%k %audioOptions% -sn -an -f webm -passlogfile ffmpeg2pass-1 -pass 2 "webm_part_1.webm"
  170. echo echo. ^> part1done.txt
  171. echo exit
  172. ) > part1.bat
  173.  
  174. start part1.bat
  175.  
  176. REM ************** SECOND FOURTH *****************
  177.  
  178. (
  179. echo %ffmpeg% -y -ss %firstFourth% -i "%~1" -c:v %codec% -b:v %videobitrate%k -vf scale=-1:%scaleheight% -t %firstFourth% %options% -c:a libopus -b:a %audiobitrate%k %audioOptions% -sn -an -f webm -passlogfile ffmpeg2pass-2 -pass 1 NUL
  180. echo %ffmpeg% -y -ss %firstFourth% -i "%~1" -c:v %codec% -b:v %videobitrate%k -vf scale=-1:%scaleheight% -t %firstFourth% %options% -c:a libopus -b:a %audiobitrate%k %audioOptions% -sn -an -f webm -passlogfile ffmpeg2pass-2 -pass 2 "webm_part_2.webm"
  181. echo echo. ^> part2done.txt
  182. echo exit
  183. ) > part2.bat
  184.  
  185. start part2.bat
  186.  
  187. REM *************** THIRD FOURTH *****************
  188.  
  189. (
  190. echo %ffmpeg% -y -ss %secondFourth% -i "%~1" -c:v %codec% -b:v %videobitrate%k -vf scale=-1:%scaleheight% -t %firstFourth% %options% -c:a libopus -b:a %audiobitrate%k %audioOptions% -sn -an -f webm -passlogfile ffmpeg2pass-3 -pass 1 NUL
  191. echo %ffmpeg% -y -ss %secondFourth% -i "%~1" -c:v %codec% -b:v %videobitrate%k -vf scale=-1:%scaleheight% -t %firstFourth% %options% -c:a libopus -b:a %audiobitrate%k %audioOptions% -sn -an -f webm -passlogfile ffmpeg2pass-3 -pass 2 "webm_part_3.webm"
  192. echo echo. ^> part3done.txt
  193. echo exit
  194. ) > part3.bat
  195.  
  196. start part3.bat
  197.  
  198. REM **************** FOURTH FOURTH *******************
  199.  
  200. (
  201. echo %ffmpeg% -y -ss %thirdFourth% -i "%~1" -c:v %codec% -b:v %videobitrate%k -vf scale=-1:%scaleheight% -to %duration% %options% -c:a libopus -b:a %audiobitrate%k -sn -an -f webm -passlogfile ffmpeg2pass-4 -pass 1 NUL
  202. echo %ffmpeg% -y -ss %thirdFourth% -i "%~1" -c:v %codec% -b:v %videobitrate%k -vf scale=-1:%scaleheight% -to %duration% %options% -c:a libopus -b:a %audiobitrate%k -sn -an -f webm -passlogfile ffmpeg2pass-4 -pass 2 "webm_part_4.webm"
  203. echo echo. ^> part4done.txt
  204. echo exit
  205. ) > part4.bat
  206.  
  207. start part4.bat
  208.  
  209. REM ****************** CHECK IF PARTS ARE DONE ********************
  210.  
  211. cls
  212. echo Forced Multithreading is enabled
  213. echo Waiting for the parts to finish...
  214.  
  215. :reCheck
  216. timeout /t 2 /nobreak > NUL
  217. goto :checkPart1
  218.  
  219. :checkPart1
  220. if exist "%~dp1part1done.txt" (
  221. goto :checkPart2
  222. ) else (
  223. goto :reCheck)
  224.  
  225. :checkPart2
  226. if exist "%~dp1part2done.txt" (
  227. goto :checkPart3
  228. ) else (
  229. goto :reCheck)
  230.  
  231. :checkPart3
  232. if exist "%~dp1part3done.txt" (
  233. goto :checkPart4
  234. ) else (
  235. goto :reCheck)
  236.  
  237. :checkPart4
  238. if exist "%~dp1part4done.txt" (
  239. goto :concatFile
  240. ) else (
  241. goto :reCheck)
  242.  
  243. REM ****************** MAKES CONCATINATION FILE ********************
  244.  
  245. :concatFile
  246. (
  247. echo file '%~dp1webm_part_1.webm'
  248. echo file '%~dp1webm_part_2.webm'
  249. echo file '%~dp1webm_part_3.webm'
  250. echo file '%~dp1webm_part_4.webm'
  251. ) > concatlist.txt
  252.  
  253. REM ****************** COMBINES PARTS ********************
  254.  
  255. %ffmpeg% -y -i "%~1" -c:a opus -b:a %audiobitrate%k -vn "%~dp1webm_audio.opus"
  256. %ffmpeg% -y -f concat -i concatlist.txt -i "%~dp1webm_audio.opus" -map 0:0 -map 1:0 -c copy -metadata creation_time="%today%" -metadata title="%title%" -f webm "%title%.webm"
  257.  
  258. cls
  259. echo FINISHED ENCODING!!!
  260.  
  261. DEL /Q /F "%~dp1ffmpeg2pass-1-0.log"
  262. DEL /Q /F "%~dp1ffmpeg2pass-2-0.log"
  263. DEL /Q /F "%~dp1ffmpeg2pass-3-0.log"
  264. DEL /Q /F "%~dp1ffmpeg2pass-4-0.log"
  265. DEL /Q /F "%~dp1part1done.txt"
  266. DEL /Q /F "%~dp1part2done.txt"
  267. DEL /Q /F "%~dp1part3done.txt"
  268. DEL /Q /F "%~dp1part4done.txt"
  269. DEL /Q /F "%~dp1part1.bat"
  270. DEL /Q /F "%~dp1part2.bat"
  271. DEL /Q /F "%~dp1part3.bat"
  272. DEL /Q /F "%~dp1part4.bat"
  273. DEL /Q /F "%~dp1concatlist.txt"
  274. DEL /Q /F "%~dp1webm_audio.opus"
  275. DEL /Q /F "%~dp1webm_part_1.webm"
  276. DEL /Q /F "%~dp1webm_part_2.webm"
  277. DEL /Q /F "%~dp1webm_part_3.webm"
  278. DEL /Q /F "%~dp1webm_part_4.webm"
  279.  
  280. echo Done cleaning up!
  281.  
  282. set endtimescript=%time%
  283.  
  284. REM Get encoding end time:
  285. for /F "tokens=1-4 delims=:.," %%a in ("%endtimescript%") do (
  286. set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
  287. )
  288.  
  289. REM ~~~~~~~~~~~~~~ End Script Timer ~~~~~~~~~~~~~
  290.  
  291. goto :END
  292.  
  293. :normalencoding
  294. REM [][][][][][][][][][][][][][][][][] NORMAL ENCODING HERE [][][][][][][][][][][][][][][][][][]
  295.  
  296. echo Experimental Multithreading is dissabled
  297. echo.
  298.  
  299. REM ************** Set Subtitles *******************
  300.  
  301. set /p insubs="Include Subtitles? Y/N (Default N): "
  302. set subsback=%1
  303. set subsback=%subsback:\=\\%
  304. set subsback=%subsback:'='\\\''%
  305. set subsback=%subsback::=\:%
  306. set subsback=%subsback:[=\[%
  307. set subsback=%subsback:]=\]%
  308. set subsback=%subsback:;=\;%
  309. set subsback=%subsback:,=\,%
  310.  
  311. REM if [%insubs%]==[y] @set subs=setpts=PTS+%starttime%/TB,subtitles=""'%subsback%'"",setpts=PTS-STARTPTS&@set /a subsinclude=1
  312. REM if [%insubs%]==[Y] @set subs=setpts=PTS+%starttime%/TB,subtitles=""'%subsback%'"",setpts=PTS-STARTPTS&@set /a subsinclude=1
  313. if [%insubs%]==[y] @set subs=subtitles=""'%subsback%'""&@set /a subsinclude=1
  314. if [%insubs%]==[Y] @set subs=subtitles=""'%subsback%'""&@set /a subsinclude=1
  315. if [%insubs%]==[n] @set /a subsinclude=0
  316. if [%insubs%]==[N] @set /a subsinclude=0
  317. if [%insubs%]==[] @set /a subsinclude=0&@set insubs=N
  318. echo Subtitles enabled: %insubs%
  319. echo.
  320.  
  321. if %subsinclude% EQU 1 (@set filter=%scale%,%subs%)
  322. if %subsinclude% EQU 0 (@set filter=%scale%)
  323.  
  324. REM ************** Set video trim *********************
  325.  
  326. set starttime=00:00:00.00
  327. set endtime=%duration%
  328. REM set starttimesubs=
  329.  
  330. set /p trim="Do you need to trim the video? Y/N (Default N): "
  331. if [%trim%]==[] goto :normal
  332. if [%trim%]==[n] goto :normal
  333. if [%trim%]==[N] goto :normal
  334. if [%trim%]==[y] goto :trim
  335. if [%trim%]==[Y] goto :trim
  336.  
  337. :trim
  338. set /p starttime="What should the start time be in HH:MM:SS.CC format? Ex: 00:01:48.00 (Default is 00:00:00.00): "
  339. if [%starttime%]==[] @set starttime=00:00:00.00
  340. echo The start time is %starttime%
  341. echo.
  342.  
  343. REM if %subsinclude% EQU 1 (@set starttimesubs=%starttime%&@set starttime=)
  344.  
  345. set /p endtime="What should the end time be in HH:MM:SS.CC format? Ex: 00:08:56.20 (Default is total length of input video): "
  346. if [%endtime%]==[] @set endtime=%duration%
  347. echo The end time is %endtime%
  348.  
  349. :normal
  350.  
  351. REM ~~~~~~~~~~~~~~~~~~ Start Script Timer ~~~~~~~~~~~~~~~~~~~
  352.  
  353. set starttimescript=%time%
  354.  
  355. REM Get encoding start time:
  356. for /F "tokens=1-4 delims=:.," %%a in ("%starttimescript%") do (
  357. set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
  358. )
  359.  
  360. %ffmpeg% -y -i "%~1" -ss %starttime% -to %endtime% -c:v %codec% -b:v %videobitrate%k -vf %filter% -sn %options% -c:a libopus -b:a %audiobitrate%k %audioOptions% -f webm -an -pass 1 NUL
  361. %ffmpeg% -y -i "%~1" -ss %starttime% -to %endtime% -c:v %codec% -b:v %videobitrate%k -vf %filter% -sn %options% -c:a libopus -b:a %audiobitrate%k %audioOptions% -f webm -metadata title="%title%" -metadata creation_time=%today% -pass 2 "%title%.webm"
  362.  
  363. cls
  364. echo FINISHED ENCODING!!!
  365. DEL /Q /F "%~dp1ffmpeg2pass-0.log"
  366. echo Done cleaning up!
  367.  
  368. set endtimescript=%time%
  369.  
  370. REM Get encoding end time:
  371. for /F "tokens=1-4 delims=:.," %%a in ("%endtimescript%") do (
  372. set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
  373. )
  374.  
  375. REM ~~~~~~~~~~~~~~ End Script Timer ~~~~~~~~~~~~~
  376.  
  377. :END
  378.  
  379. REM Get elapsed encoding time:
  380. set /A elapsed=%end%-%start%
  381.  
  382. REM Show elapsed time:
  383. set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100
  384. if %mm% lss 10 set mm=0%mm%
  385. if %ss% lss 10 set ss=0%ss%
  386. if %cc% lss 10 set cc=0%cc%
  387.  
  388. echo.
  389. echo Conversion stats:
  390. echo Target audio bitrate: %audiobitrate%k
  391. echo Target video bitrate: %videobitrate%k
  392. set /a targetbitrate=%audiobitrate% + %videobitrate%
  393. echo Target total bitrate: %targetbitrate%k
  394. echo Target file size: %megabytes%MB
  395. echo.
  396. echo Start time: %starttimescript%
  397. echo End time : %endtimescript%
  398. echo Elasped encoding time is %hh%:%mm%:%ss%.%cc%
  399. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement