Skip to content

Instantly share code, notes, and snippets.

@0b5vr
Last active December 6, 2024 22:58
Show Gist options
  • Save 0b5vr/a571cf2cbf872e376a75caccd6d2a169 to your computer and use it in GitHub Desktop.
Save 0b5vr/a571cf2cbf872e376a75caccd6d2a169 to your computer and use it in GitHub Desktop.
Batch: ffmpeg video trimmer optimized for Twitter
@echo off
setlocal
rem (c) 0b5vr, MIT License https://opensource.org/licenses/MIT
set FILE=""
set START=0
set TIME=139
set WIDTH=-1
set OUTPUT=twiv.mp4
set FADED=1
set AFADED=2
:params
if [%1] == [] goto process
if "%1" == "-h" goto help
if "%1" == "-i" set FILE=%2
if "%1" == "-s" set START=%2
if "%1" == "-t" set TIME=%2
if "%1" == "-w" set WIDTH=%2
if "%1" == "-o" set OUTPUT=%2
if "%1" == "--fade" set FADED=%2
if "%1" == "--afade" set AFADED=%2
shift
shift
goto params
:process
if %FILE% == "" goto help
set /a FADEOUTSTART=%TIME%-%FADED%
set /a AFADEOUTSTART=%TIME%-%AFADED%
set FILTERS=scale=%WIDTH%:-1,fade=t=out:st=%FADEOUTSTART%:d=%FADED%
set AFILTERS=afade=t=in:st=0:d=%AFADED%,afade=t=out:st=%AFADEOUTSTART%:d=%AFADED%
ffmpeg -ss %START% -i %FILE% -t %TIME% -c:v libx264 -pix_fmt yuv420p -vf "%FILTERS%" -af "%AFILTERS%" -b:a 192k -r 60 %OUTPUT%
goto end
:help
echo Usage:
echo twiv -i input.mp4
echo.
echo Options:
echo -h show this help
echo -i input file
echo -s start time in seconds
echo -t duration in seconds
echo -w width in pixels
echo -o output file
echo --fade fade out duration in seconds
echo --afade audio fade out duration in seconds
echo.
goto end
:end
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment