How To Convert A Video File To GIF with PHP

How To Convert A Video File To GIF with PHP


Oct 30 2018, 19:43 in Web Development

First you will need to install the linux package ffmpeg

sudo apt-get install ffmpeg

In your PHP Project, install the php-ffmpeg package

composer require php-ffmpeg/php-ffmpeg

Assuming you have the full local path of the video to convert:

$videoPath = '/path/to/video.mp4';

// The gif duration will be as long as the video/
$ffprobe = FFProbe::create();
$duration = (int) $ffprobe->format($videoPath)->get('duration');

// The gif will have the same dimension. You can change that of course if needed.
$dimensions = $ffprobe->streams($videoPath)->videos()->first()->getDimensions();

$gifPath = '/path/to/gifvideo.gif';

// Transform
$ffmpeg = FFMpeg::create();
$ffmpegVideo = $ffmpeg->open($videoPath);
$ffmpegVideo->gif(TimeCode::fromSeconds(0), $dimensions, $duration)->save($gifPath);

That's it !! Easy right 😄


Share:
Like:

Disable AdBlock on this domain and offer me a cup of coffee :)