Skip to content

Instantly share code, notes, and snippets.

@hexmoire
Created August 12, 2016 19:42
Show Gist options
  • Save hexmoire/f1cb472f8ff6101e73bead58cb66227d to your computer and use it in GitHub Desktop.
Save hexmoire/f1cb472f8ff6101e73bead58cb66227d to your computer and use it in GitHub Desktop.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ::
:: tumblrgif v3 ::
:: ::
:: converts video files to gif files that meet tumblr's size limit. ::
:: this is a windows batch file. tested on win 10. ::
:: ::
:: depends on: ::
:: ffmpeg ::
:: imagemagick ::
:: gifsicle ::
:: ::
:: create a source directory in the same directory as this file and put ::
:: video files to process in it. tested with h.264, should work with any ::
:: codec ffmpeg supports. this script should be in its own directory, not ::
:: sharing space with other work or original video files. it will ::
:: overwrite or delete files in subdirectories it has designated as work ::
:: spaces. ::
:: ::
:: pass ? argument for usage. ::
:: ::
:: logic governing color reduction is rather naive, feel free to improve ::
:: it. at some point i will encode a few gifs with varying attributes and ::
:: track filesize versus color count. ::
:: ::
:: the script is chatty. if you want to know what it's up to, just watch. ::
:: ::
:: make sure you have enough space for the files generated. if you're not ::
:: sure start small. batch files are goofy as hell and can be powerful ::
:: and dangerous. i made this in good faith but that doesn't make it safe. ::
:: if you are reading this it doesn't mean that you have exactly what i ::
:: made - trust your souces or don't use them. ::
:: ::
:: no warranty. make improvements. fuck it up. ::
:: ::
:: -hexmoire ::
:: ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
set scriptname=%0
if [%1] EQU [?] goto :usage
if [%3] NEQ [] goto :usage
:: setting names of directories to work in
set framedir=tumblrframes
set sourcedir=source
set destinationdir=gifs
:: default and non parameterized values, change these to easily affect behavior
:: default number of frames to pull from video
set defaultframecount=30
:: if colors drop below this many, script will print a failure message
set defaultmincolors=16
:: delay in gif frames, in 1/100 of second
set defaultgifframedelay=5
:: maximum size in bytes
set maxsize=2000000
echo ### %0
echo ### ensuring directory %sourcedir%\ exists
set filecount=0
if not exist "%sourcedir%" goto :nosource
for %%f in ("%sourcedir%\*") do (
set /A filecount = filecount + 1
goto :checkedsourcedir
)
:checkedsourcedir
if %filecount% EQU 0 goto :nosource
echo ### ensuring directory %framedir%\ exists
if not exist "%framedir%" mkdir "%framedir%"
echo ### ensuring directory %destinationdir%\ exists
if not exist "%destinationdir%" mkdir "%destinationdir%"
echo.
for %%f in ("%sourcedir%\*") do (
echo ### BEGIN %%f
call :makegif "%%f" %1 %2
echo ### END %%f
echo.
set /a filecount=filecount+1
)
echo ### cleaning up
del /Q "%framedir%"
echo.
call :reminder
goto :EOF
:makegif
call :filename %1
set /A mincolors=defaultmincolors
set /A framecount=defaultframecount
set /A gifframedelay=defaultgifframedelay
set hz=
if [%2] NEQ [] set framecount=%2
if [%3] NEQ [] set hz=-r %3
echo ### clearing previous work from %framedir%\
del /Q "%framedir%"
echo ### pulling frames from video to png with ffmpeg
ffmpeg -hide_banner -loglevel panic -i %1 -an -f image2 %hz% -vframes %framecount% "%framedir%/frame%%03d.png"
for %%f in ("%framedir%\*") do (
goto :framesextracted
)
echo ### ffmpeg was unable to extract frames from %1%
goto :leavemakegif
:framesextracted
echo ### resizing frames to tumblr resolution with imagemagick
magick mogrify -resize 540x540 "%framedir%/f*.png"
echo ###
set size=0
set colors=255
set gifname=%destinationdir%\%name%.gif
:gifloop
echo ### trying gif encode at %colors% colors
echo ### converting frames from png to gif with imagemagick
magick mogrify -format gif -colors %colors% "%framedir%/f*.png"
echo ### encoding to gif animation with gifsicle
gifsicle -w --loop -d%gifframedelay% "%framedir%/f*.gif" > "%gifname%"
call :filesize "%gifname%"
if %size% LSS %maxsize% goto :sizesuccess
echo ### file size is %size% bytes
echo ###
set /A sizeone=maxsize*3/2
set /A sizetwo=maxsize*5/4
if %size% GTR %sizeone% (
set /A colors=colors*maxsize/size-1
)
if %size% GTR %sizetwo% (
set /A colors=colors*maxsize/size-1
)
set /A colors=colors*maxsize/size-1
if %colors% LSS %mincolors% (
call :sizefail
goto :leavemakegif
)
goto :gifloop
:leavemakegif
exit /b 0
:filesize
set size=%~z1
exit /B 0
:filename
set name=%~n1
exit /B 0
:usage
echo.
echo usage:
echo %0 [framecount] [sample frames per second]
echo.
echo this script encodes a looping gif, shrinking file size by restricting color
echo count. frame delay of playback, file size limit, min color count, etc. can be
echo modified by editing %0
echo.
echo examples:
echo.
echo %0
echo encodes first %defaultframecount% frames from all sources
echo.
echo %0 60
echo encodes first 60 frames from all sources
echo.
echo %0 15 10
echo encodes 15 frames from all sources, starting at the first frame and sampling
echo 10 frames per second of video
echo.
echo %0 ?
echo prints this text
echo.
echo pressing CTRL+C ends this script during execution
echo.
echo IMPORTANT:
echo.
echo %0 attempts to operate on all files in
echo %sourcedir%\ regardless of extension
echo.
echo %0 will overwrite any gifs in %destinationdir%\ and
echo will delete all files in %framedir%\ while working
echo.
echo ffmpeg, gifsicle, and imagemagick must be visible to this script for it to work
echo this means their executables must either be in this directory or in the PATH.
echo (imagemagick puts itself in PATH when installed.)
goto :EOF
:nosource
echo.
echo first create a directory called %sourcedir%\ and put your source video files in it.
echo.
call :reminder
goto :EOF
:sizefail
echo ### Encoding Failed.
echo ### for file to be smaller than %maxsize% bytes
echo ### %colors% colors estimated, falling below minimum of %mincolors%
exit /b 0
:sizesuccess
echo ###
echo ### Encoding Succeeded!
echo ### %framecount% frames at %colors% colors
echo ### filesize of %size% bytes, less than upper bound of %maxsize% bytes
exit /b 0
:reminder
echo REMINDER:
echo.
echo %scriptname% ?
echo for usage notes
echo.
echo press CTRL+C to exit script during execution
echo.
echo gifs in %destinationdir%\ will be overwritten
echo all files in %framedir%\ will be deleted
exit /b 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment