Skip to content

Instantly share code, notes, and snippets.

@Ravenna
Created October 14, 2013 14:55
Show Gist options
  • Select an option

  • Save Ravenna/6977001 to your computer and use it in GitHub Desktop.

Select an option

Save Ravenna/6977001 to your computer and use it in GitHub Desktop.
<?php
$GIF_SIZE_WIDTHxHEIGHT = "80x45";
$DURATION_SECS = 6;
$FIRST_FRAME_NUM = 41;
$FRAME_SKIP = 1;
$PREROLL_SECS = 2;
foreach (glob("*.{mp4}", GLOB_BRACE) as $named)
{
$src = substr($named,0,-4);
MP4ToAnimatedGIF($src, $GIF_SIZE_WIDTHxHEIGHT, $DURATION_SECS, $FIRST_FRAME_NUM, $FRAME_SKIP, $PREROLL_SECS);
}
function MP4ToAnimatedGIF ($mp4, $sizeXxY, $duration_seconds, $first_frame, $frame_skip, $preroll_seconds)
{
$TMP_DIRECTORY = "anim~";
mkdir($TMP_DIRECTORY);
@exec("ffmpeg -ss 0:00:$preroll_seconds -i $mp4.mp4 -t 0:0:".($duration_seconds*2)." -s $sizeXxY -f image2 $TMP_DIRECTORY/%03d.png");
$cmdline = "convert -coalesce -layers OptimizeTransparency +map ";
$last_frame = $duration_seconds * 29; // Assume 29.997 NTSC FPS
$frame_files_included = "";
while ($first_frame < $last_frame)
{
$frame_files_included .= $TMP_DIRECTORY."/".substr(sprintf('%03d', $first_frame),-3,3).".png ";
$first_frame += $frame_skip;
}
$cmdline .= $frame_files_included;
$cmdline .= "$mp4.gif";
@exec($cmdline);
rrmdir($TMP_DIRECTORY);
}
function rrmdir($dir)
{
foreach(glob($dir. "/*") as $file)
{
if(is_dir($file))
rrmdir($file);
else
unlink($file);
}
rmdir($dir);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment