BuccoBruce

VP9.bat

Nov 4th, 2021 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.32 KB | None | 0 0
  1. @ECHO OFF
  2. SETLOCAL EnableExtensions EnableDelayedExpansion
  3. CHCP 65001
  4. ::v0.01 Initial Release
  5. ::v0.02 A few minutes after that, two missing quotes made it shit itself. You can now drag/drop multiple files at once.
  6. ::v0.03 2021-11-04  VP9! Hallelujah!
  7. ::v0.0? 2022-07-06  I forgor
  8. ::v0.04 2023-01-16  Rudimentary constrained bitrate mode
  9. ::                  Now won't overwrite input WEBMs, lol
  10. ::                  Resample obnoxiously using libsoxr to 48k if needed, convert to 128k opus using libopus for any non-MP3/AAC/OGG/OPUS sources
  11. ::                  Yes, that means you need ffmpeg compiled with --enable-libopus and --enable-libsoxr
  12. ::                  But you already compiled it with both, and --disable-encoder=opus/--disable-decoder=opus, amongst many other such substitutions...
  13. ::                  Right?
  14. ::For batch converting any FFMPEG compatible inputs to a VP9 WEBM that can be posted on 4chinz.
  15. ::Done - Implement bit-rate calculation using FFProbe to determine input length and required bitrate to meet 3MB limit.
  16. ::Done - Check for audio in input, demux to original format in appropriate container per FFProbe without re-encoding, without requiring user to determine appropriate container. Automatically transcode to opus for any formats other than m4a/mp3/ogg/opus
  17. ::Done: Fix non-ASCII UTF-8 filename parsing, using "CHCP 65001" to change codepage.
  18. ::To-Do: Prompt user if they want a soundpost.
  19. ::To-Do: Call catbox.moe API using pwsh.exe Invoke-WebRequest, upload sound
  20. ::Note: Probably requires userhash/registered account.
  21. ::To-Do: /wsg/ prompt -> adjust to 6 MB, allow direct audio copy if input is Vorbis, else re-encode and include.
  22. ECHO nigger :D v0.04
  23. :AGAIN
  24. ECHO Calculating Duration and Bitrate for Input: "%~dpnx1" 
  25. ::Determines input duration using ffprobe, stores seconds value to duration variable.
  26. FOR /F %%I IN ('ffprobe.exe -v error -show_entries format^=duration -of default^=noprint_wrappers^=1:nokey^=1 "%~1"') do SET "DURATION=%%I"
  27. ::Determines required bitrate using [3 MiB * 1024 KiB/MiB * 8 kb/KiB] = 24576 kbit
  28. SET /A BITRATE = 24576 / DURATION
  29. SET /A MINBITS = BITRATE / 10
  30. SET /A MAXBITS = 11 * BITRATE / 10
  31. ECHO Duration is: %DURATION% seconds - %BITRATE% kbps required to meet 3 MB limit. Min:  %MINBITS% kbps - Max:  %MAXBITS% kbps
  32. ECHO Converting %~1 to %BITRATE% kbps 10-bit VP9, 1st Pass
  33. ffmpeg.exe -i "%~1" -c:v libvpx-vp9 -pix_fmt yuv420p10le -colorspace bt709 -color_primaries bt709 -color_trc bt709 -quality good -auto-alt-ref 1 -arnr-maxframes 12 -lag-in-frames 24 -tile-rows 0 -tile-columns 1 -tune-content film -enable-tpl 1 -row-mt 1 -qcomp 0.80 -keyint_min 24 -g 240 -pass 1 -b:v %BITRATE%k -minrate %MINBITS%k -maxrate %MAXBITS%k -f rawvideo -passlogfile "%TMP%/%~n1_2pass" -an -sn -y -hide_banner NUL
  34. ECHO 1st Pass Success
  35. ECHO Converting %~1 to 10-bit VP9, 2nd Pass
  36. ffmpeg.exe -i "%~1" -c:v libvpx-vp9 -pix_fmt yuv420p10le -colorspace bt709 -color_primaries bt709 -color_trc bt709 -quality good -auto-alt-ref 1 -arnr-maxframes 12 -lag-in-frames 24 -tile-rows 0 -tile-columns 1 -tune-content film -enable-tpl 1 -row-mt 1 -qcomp 0.80 -keyint_min 24 -g 240 -pass 2 -b:v %BITRATE%k -minrate %MINBITS%k -maxrate %MAXBITS%k -passlogfile "%TMP%/%~n1_2pass" -an -sn -hide_banner "%~pn1.out.webm"
  37. ECHO 2nd Pass Success
  38. ECHO Deleting 2-Pass Log File
  39. DEL "%TMP%\%~n1_2pass-0.log"
  40. ECHO Finished Converting %~1
  41. FOR /F "tokens=*" %%S IN ("%~pn1.out.webm") DO SET SIZE=%%~zS
  42. SET /A SIZEK=SIZE/1024
  43. ECHO 10-bit VP9 WEBM: %~dpn1.out.webm Size: %SIZE% bytes / %SIZEK% KiB
  44. SET MAX=3145728
  45. IF %SIZE% LSS %MAX% (ECHO %~n1.out.webm is under 3 MiB, libvpx obeyed. Please shitpost responsibly.) ELSE (ECHO %~n1.out.webm is over 3 MiB, fuck libvpx-vp9, fuck hiroshimoot, give us AV1 you wanker.)
  46. ECHO Probing Default Audio Track...
  47. FOR /F %%X IN ('ffprobe.exe -v error -select_streams a:0 -show_entries stream^=codec_name -of default^=noprint_wrappers^=1:nokey^=1 "%~1"') do SET "AF=%%X"
  48. SET AEXT=
  49. IF "%AF%" == "vorbis" SET "AEXT=.ogg"
  50. IF "%AF%" == "opus" SET "AEXT=.ogg"
  51. IF "%AF%" == "aac" SET "AEXT=.m4a"
  52. IF "%AF%" == "mp3" SET "AEXT=.mp3"
  53. IF [%AEXT%]==[] GOTO:TRANSCODEAUDIO
  54. ECHO Extracting %AF% audio to %~n1.audio%AEXT%
  55. ffmpeg.exe -i "%~1" -map 0:a -c:a copy -hide_banner "%~pn1.audio%AEXT%"
  56. ECHO %AF% audio extracted to: %~dpn1.audio%AEXT% Size: who cares?
  57. SHIFT
  58. IF "%~1" == "" GOTO:FINISH
  59. GOTO:AGAIN
  60. :TRANSCODEAUDIO
  61. ECHO  ;_; %AF% audio detected. Transcoding using libopus using obnoxious autismo settings.
  62. ffmpeg.exe -i "%~1" -map 0:a -af aresample=resampler=soxr:dither_method=low_shibata:precision=33:cutoff=0.95:cheby=1 -ar 48000 -b:a 128k -c:a libopus -hide_banner "%~pn1-opus.ogg"
  63. ECHO %AF% audio converted to: %~dpn1-opus.ogg Size: who cares?
  64. SHIFT
  65. IF "%~1" == "" GOTO:FINISH
  66. GOTO:AGAIN
  67. :FINISH
  68. ECHO  :) No additional files selected for conversion, please check the console for errors, if any.
  69. PAUSE
  70.  
Add Comment
Please, Sign In to add comment