Skip to content

Instantly share code, notes, and snippets.

@bclymer
Last active April 15, 2023 05:33
Show Gist options
  • Save bclymer/3dfb77f76dba0e463508358ddd4de14d to your computer and use it in GitHub Desktop.
Save bclymer/3dfb77f76dba0e463508358ddd4de14d to your computer and use it in GitHub Desktop.
Automatically convert a video file into an 8MB file with decent quality to send via Discord.
@ECHO OFF
IF "%~1"=="" (
ECHO No file provided, please drag and drop an MP4 onto this file to run.
EXIT /B
)
IF %~z1 LSS 8388608 (
ECHO File less than 8MB. Exiting.
PAUSE
exit
)
where /q ffprobe
IF ERRORLEVEL 1 (
ECHO FFprobe is missing. Ensure it is installed and placed in your PATH. You can download the latest from here - https://www.ffmpeg.org/download.html
PAUSE
EXIT /B
)
where /q ffmpeg
IF ERRORLEVEL 1 (
ECHO FFmpeg is missing. Ensure it is installed and placed in your PATH. You can download the latest from here - https://www.ffmpeg.org/download.html
PAUSE
EXIT /B
)
ECHO Shrinking %1 to be 8MB
ffprobe -loglevel error -select_streams a -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 %1 > durationFile
SET /P duration= < durationFile
:: Batch can't do floats, so round up (it will round down by default)
SET /A finalDuration = duration + 1
DEL durationFile
ECHO Duration rounded up to %finalDuration%
:: Bits to Bytes
SET /A totalBitrate = 8388608 / %finalDuration% * 8
ECHO Final bitrate is going to be %totalBitrate%
:: Subtract audio bitrate
SET /A videoBitrate = totalBitrate - 80000
SET /A audioBitrate = 80000
IF %videoBitrate% LSS 1500000 (
ECHO Using veryslow preset due to long very duration video
SET /A preset = "veryslow"
) ELSE (
IF %videoBitrate% LSS 2000000 (
ECHO Using slow preset due to long duration video
SET /A preset = "slow"
) ELSE (
ECHO Using default preset due to short video
SET /A preset = "default"
)
)
ffmpeg -y -i %1 -c:v libx264 -b:v %videoBitrate% -pass 1 -an -preset %preset% -f mp4 NUL && ^
ffmpeg -i %1 -c:v libx264 -b:v %videoBitrate% -pass 2 -c:a aac -b:a %audioBitrate% -preset %preset% Sized.mp4
del ffmpeg2pass-0.log
del ffmpeg2pass-0.log.mbtree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment