Tuesday, August 24, 2021

Converting animated GIF to animated BPG

 The batch file below converts animated GIF to animated BPG. The main point to note is that when extracting frames from the animated GIF, transparency must be removed and the frames must have the same color type, otherwise BpgEnc will crash. Hence PNG24 must be specified to force removal of transparency and ensure the same color type.

@echo off

@Echo %~n0 converts animated GIF to BPG

md ~temp

if not '%1==' call :gif2bpg %*

if '%1==' For %%v in (*.gif) do if not exist "%%~nv.bpg" call :Gif2Bpg "%%v"

if '%1==' For %%v in (*.jpg *.png) do C:\PUB\bpg\bpgenc.exe "%%v" -o "%%~nv.bpg"

rd ~temp

goto :Eof


:Gif2Bpg

:: Coalesce and adjoin is needed to extract individual full frames from animated gif 

:: Extracted PNG must have same color type and without transparency otherwise BPGEnc will crash

Echo Using GraphicsMagick to extract PNG frames from %1 with same color depth, without transparency and coalescing previous frames

"C:\Program Files\GraphicsMagick-1.3.34-Q16\gm.exe" convert -coalesce +adjoin %1 PNG24:"~temp\%~n1_%%03d.png"


Echo Using GraphicsMagick to retrieve animation frame info from %1 and saving it to %~n1.anim

"C:\Program Files\GraphicsMagick-1.3.34-Q16\gm.exe" identify -format "%%T\n" %1 > "~temp\%~n1.anim"


Echo Using BpgEnc to convert extracted PNG frames to %~n1.bpg

bpgenc.exe -a "~temp\%~n1_%%3d.png" -fps 25 -loop 0 -delayfile "~temp\%~n1.anim" -o "%~n1.bpg"


del ~temp\%~n1*.*